]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_CY8C5588_PSoC_Creator_GCC/FreeRTOS_Demo.cydsn/Serial.c
Add additional critical section to the default tickless implementations.
[freertos] / FreeRTOS / Demo / CORTEX_CY8C5588_PSoC_Creator_GCC / FreeRTOS_Demo.cydsn / Serial.c
1 /*\r
2     FreeRTOS V7.5.2 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 #include <device.h>\r
66 #include "FreeRTOS.h"\r
67 #include "queue.h"\r
68 #include "task.h"\r
69 #include "serial.h"\r
70 /*---------------------------------------------------------------------------*/\r
71 \r
72 #define serialSTRING_DELAY_TICKS                ( portMAX_DELAY )\r
73 /*---------------------------------------------------------------------------*/\r
74 \r
75 CY_ISR_PROTO( vUartRxISR );\r
76 CY_ISR_PROTO( vUartTxISR );\r
77 /*---------------------------------------------------------------------------*/\r
78 \r
79 static xQueueHandle xSerialTxQueue = NULL;\r
80 static xQueueHandle xSerialRxQueue = NULL;\r
81 /*---------------------------------------------------------------------------*/\r
82 \r
83 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
84 {\r
85         /* Configure Rx. */\r
86         xSerialRxQueue = xQueueCreate( uxQueueLength, sizeof( signed char ) );  \r
87         isr_UART1_RX_BYTE_RECEIVED_ClearPending();\r
88         isr_UART1_RX_BYTE_RECEIVED_StartEx(vUartRxISR);\r
89 \r
90         /* Configure Tx */\r
91         xSerialTxQueue = xQueueCreate( uxQueueLength, sizeof( signed char ) );\r
92         isr_UART1_TX_BYTE_COMPLETE_ClearPending() ;\r
93         isr_UART1_TX_BYTE_COMPLETE_StartEx(vUartTxISR);\r
94 \r
95         /* Clear the interrupt modes for the Tx for the time being. */\r
96         UART_1_SetTxInterruptMode( 0 );\r
97 \r
98         /* Both configured successfully. */\r
99         return ( xComPortHandle )( xSerialTxQueue && xSerialRxQueue );\r
100 }\r
101 /*---------------------------------------------------------------------------*/\r
102 \r
103 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
104 {\r
105 unsigned short usIndex = 0;\r
106 \r
107         for( usIndex = 0; usIndex < usStringLength; usIndex++ )\r
108         {\r
109                 /* Check for pre-mature end of line. */\r
110                 if( '\0' == pcString[ usIndex ] )\r
111                 {\r
112                         break;\r
113                 }\r
114                 \r
115                 /* Send out, one character at a time. */\r
116                 if( pdTRUE != xSerialPutChar( NULL, pcString[ usIndex ], serialSTRING_DELAY_TICKS ) )\r
117                 {\r
118                         /* Failed to send, this will be picked up in the receive comtest task. */\r
119                 }\r
120         }\r
121 }\r
122 /*---------------------------------------------------------------------------*/\r
123 \r
124 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )\r
125 {\r
126 portBASE_TYPE xReturn = pdFALSE;\r
127 \r
128         if( pdTRUE == xQueueReceive( xSerialRxQueue, pcRxedChar, xBlockTime ) )\r
129         {\r
130                 /* Picked up a character. */\r
131                 xReturn = pdTRUE;\r
132         }\r
133         return xReturn;\r
134 }\r
135 /*---------------------------------------------------------------------------*/\r
136 \r
137 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )\r
138 {\r
139 portBASE_TYPE xReturn = pdFALSE;\r
140 \r
141         /* The ISR is processing characters is so just add to the end of the queue. */\r
142         if( pdTRUE == xQueueSend( xSerialTxQueue, &cOutChar, xBlockTime ) )\r
143         {       \r
144                 xReturn = pdTRUE;\r
145         }\r
146         else\r
147         {\r
148                 /* The queue is probably full. */\r
149                 xReturn = pdFALSE;\r
150         }\r
151 \r
152         /* Make sure that the interrupt will fire in the case where:\r
153             Currently sending so the Tx Complete will fire.\r
154             Not sending so the Empty will fire. */\r
155         taskENTER_CRITICAL();\r
156                 UART_1_SetTxInterruptMode( UART_1_TX_STS_COMPLETE | UART_1_TX_STS_FIFO_EMPTY );\r
157         taskEXIT_CRITICAL();\r
158         \r
159         return xReturn;\r
160 }\r
161 /*---------------------------------------------------------------------------*/\r
162 \r
163 CY_ISR(vUartRxISR)\r
164 {\r
165 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
166 volatile unsigned char ucStatus = 0;\r
167 signed char cInChar = 0;\r
168 unsigned long ulMask = 0;\r
169 \r
170         /* Read the status to acknowledge. */\r
171         ucStatus = UART_1_ReadRxStatus();\r
172 \r
173         /* Only interested in a character being received. */\r
174         if( 0 != ( ucStatus & UART_1_RX_STS_FIFO_NOTEMPTY ) )\r
175         {\r
176                 /* Get the character. */\r
177                 cInChar = UART_1_GetChar();\r
178                 \r
179                 /* Mask off the other RTOS interrupts to interact with the queue. */\r
180                 ulMask = portSET_INTERRUPT_MASK_FROM_ISR();\r
181                 {\r
182                         /* Try to deliver the character. */\r
183                         if( pdTRUE != xQueueSendFromISR( xSerialRxQueue, &cInChar, &xHigherPriorityTaskWoken ) )\r
184                         {\r
185                                 /* Run out of space. */\r
186                         }\r
187                 }\r
188                 portCLEAR_INTERRUPT_MASK_FROM_ISR( ulMask );\r
189         }\r
190 \r
191         /* If we delivered the character then a context switch might be required.\r
192         xHigherPriorityTaskWoken was set to pdFALSE on interrupt entry.  If calling \r
193         xQueueSendFromISR() caused a task to unblock, and the unblocked task has\r
194         a priority equal to or higher than the currently running task (the task this\r
195         ISR interrupted), then xHigherPriorityTaskWoken will have been set to pdTRUE and\r
196         portEND_SWITCHING_ISR() will request a context switch to the newly unblocked\r
197         task. */\r
198         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
199 }\r
200 /*---------------------------------------------------------------------------*/\r
201 \r
202 CY_ISR(vUartTxISR)\r
203 {\r
204 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
205 volatile unsigned char ucStatus = 0;\r
206 signed char cOutChar = 0;\r
207 unsigned long ulMask = 0;\r
208 \r
209         /* Read the status to acknowledge. */\r
210         ucStatus = UART_1_ReadTxStatus();\r
211         \r
212         /* Check to see whether this is a genuine interrupt. */\r
213         if( ( 0 != ( ucStatus & UART_1_TX_STS_COMPLETE ) ) || ( 0 != ( ucStatus & UART_1_TX_STS_FIFO_EMPTY ) ) )\r
214         {       \r
215                 /* Mask off the other RTOS interrupts to interact with the queue. */\r
216                 ulMask = portSET_INTERRUPT_MASK_FROM_ISR();\r
217                 {\r
218                         if( pdTRUE == xQueueReceiveFromISR( xSerialTxQueue, &cOutChar, &xHigherPriorityTaskWoken ) )\r
219                         {\r
220                                 /* Send the next character. */\r
221                                 UART_1_PutChar( cOutChar );                     \r
222 \r
223                                 /* If we are firing, then the only interrupt we are interested in\r
224                                 is the Complete. The application code will add the Empty interrupt\r
225                                 when there is something else to be done. */\r
226                                 UART_1_SetTxInterruptMode( UART_1_TX_STS_COMPLETE );\r
227                         }\r
228                         else\r
229                         {\r
230                                 /* There is no work left so disable the interrupt until the application \r
231                                 puts more into the queue. */\r
232                                 UART_1_SetTxInterruptMode( 0 );\r
233                         }\r
234                 }\r
235                 portCLEAR_INTERRUPT_MASK_FROM_ISR( ulMask );\r
236         }\r
237 \r
238         /* If we delivered the character then a context switch might be required.\r
239         xHigherPriorityTaskWoken was set to pdFALSE on interrupt entry.  If calling \r
240         xQueueSendFromISR() caused a task to unblock, and the unblocked task has\r
241         a priority equal to or higher than the currently running task (the task this\r
242         ISR interrupted), then xHigherPriorityTaskWoken will have been set to pdTRUE and\r
243         portEND_SWITCHING_ISR() will request a context switch to the newly unblocked\r
244         task. */\r
245         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
246 }\r
247 /*---------------------------------------------------------------------------*/\r