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