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