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