]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_CY8C5588_PSoC_Creator_RVDS/FreeRTOS_Demo.cydsn/Serial.c
Start to re-arrange files to include FreeRTOS+ in main download.
[freertos] / Demo / CORTEX_CY8C5588_PSoC_Creator_RVDS / FreeRTOS_Demo.cydsn / Serial.c
1 /*\r
2     FreeRTOS V7.1.1 - Copyright (C) 2012 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     ***************************************************************************\r
45      *                                                                       *\r
46      *    Having a problem?  Start by reading the FAQ "My application does   *\r
47      *    not run, what could be wrong?                                      *\r
48      *                                                                       *\r
49      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
50      *                                                                       *\r
51     ***************************************************************************\r
52 \r
53     \r
54     http://www.FreeRTOS.org - Documentation, training, latest information, \r
55     license and contact details.\r
56     \r
57     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
58     including FreeRTOS+Trace - an indispensable productivity tool.\r
59 \r
60     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
61     the code with commercial support, indemnification, and middleware, under \r
62     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
63     provide a safety engineered and independently SIL3 certified version under \r
64     the SafeRTOS brand: http://www.SafeRTOS.com.\r
65 */\r
66 \r
67 #include <device.h>\r
68 #include "FreeRTOS.h"\r
69 #include "queue.h"\r
70 #include "task.h"\r
71 #include "serial.h"\r
72 /*---------------------------------------------------------------------------*/\r
73 \r
74 #define serialSTRING_DELAY_TICKS                ( portMAX_DELAY )\r
75 /*---------------------------------------------------------------------------*/\r
76 \r
77 CY_ISR_PROTO( vUartRxISR );\r
78 CY_ISR_PROTO( vUartTxISR );\r
79 /*---------------------------------------------------------------------------*/\r
80 \r
81 static xQueueHandle xSerialTxQueue = NULL;\r
82 static xQueueHandle xSerialRxQueue = NULL;\r
83 /*---------------------------------------------------------------------------*/\r
84 \r
85 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
86 {\r
87         /* Configure Rx. */\r
88         xSerialRxQueue = xQueueCreate( uxQueueLength, sizeof( signed char ) );  \r
89         isr_UART1_RX_BYTE_RECEIVED_ClearPending();\r
90         isr_UART1_RX_BYTE_RECEIVED_StartEx(vUartRxISR);\r
91 \r
92         /* Configure Tx */\r
93         xSerialTxQueue = xQueueCreate( uxQueueLength, sizeof( signed char ) );\r
94         isr_UART1_TX_BYTE_COMPLETE_ClearPending() ;\r
95         isr_UART1_TX_BYTE_COMPLETE_StartEx(vUartTxISR);\r
96 \r
97         /* Clear the interrupt modes for the Tx for the time being. */\r
98         UART_1_SetTxInterruptMode( 0 );\r
99 \r
100         /* Both configured successfully. */\r
101         return ( xComPortHandle )( xSerialTxQueue && xSerialRxQueue );\r
102 }\r
103 /*---------------------------------------------------------------------------*/\r
104 \r
105 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
106 {\r
107 unsigned short usIndex = 0;\r
108 \r
109         for( usIndex = 0; usIndex < usStringLength; usIndex++ )\r
110         {\r
111                 /* Check for pre-mature end of line. */\r
112                 if( '\0' == pcString[ usIndex ] )\r
113                 {\r
114                         break;\r
115                 }\r
116                 \r
117                 /* Send out, one character at a time. */\r
118                 if( pdTRUE != xSerialPutChar( NULL, pcString[ usIndex ], serialSTRING_DELAY_TICKS ) )\r
119                 {\r
120                         /* Failed to send, this will be picked up in the receive comtest task. */\r
121                 }\r
122         }\r
123 }\r
124 /*---------------------------------------------------------------------------*/\r
125 \r
126 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )\r
127 {\r
128 portBASE_TYPE xReturn = pdFALSE;\r
129 \r
130         if( pdTRUE == xQueueReceive( xSerialRxQueue, pcRxedChar, xBlockTime ) )\r
131         {\r
132                 /* Picked up a character. */\r
133                 xReturn = pdTRUE;\r
134         }\r
135         return xReturn;\r
136 }\r
137 /*---------------------------------------------------------------------------*/\r
138 \r
139 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )\r
140 {\r
141 portBASE_TYPE xReturn = pdFALSE;\r
142 \r
143         /* The ISR is processing characters is so just add to the end of the queue. */\r
144         if( pdTRUE == xQueueSend( xSerialTxQueue, &cOutChar, xBlockTime ) )\r
145         {       \r
146                 xReturn = pdTRUE;\r
147         }\r
148         else\r
149         {\r
150                 /* The queue is probably full. */\r
151                 xReturn = pdFALSE;\r
152         }\r
153 \r
154         /* Make sure that the interrupt will fire in the case where:\r
155             Currently sending so the Tx Complete will fire.\r
156             Not sending so the Empty will fire. */\r
157         taskENTER_CRITICAL();\r
158                 UART_1_SetTxInterruptMode( UART_1_TX_STS_COMPLETE | UART_1_TX_STS_FIFO_EMPTY );\r
159         taskEXIT_CRITICAL();\r
160         \r
161         return xReturn;\r
162 }\r
163 /*---------------------------------------------------------------------------*/\r
164 \r
165 CY_ISR(vUartRxISR)\r
166 {\r
167 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
168 volatile unsigned char ucStatus = 0;\r
169 signed char cInChar = 0;\r
170 unsigned long ulMask = 0;\r
171 \r
172         /* Read the status to acknowledge. */\r
173         ucStatus = UART_1_ReadRxStatus();\r
174 \r
175         /* Only interested in a character being received. */\r
176         if( 0 != ( ucStatus & UART_1_RX_STS_FIFO_NOTEMPTY ) )\r
177         {\r
178                 /* Get the character. */\r
179                 cInChar = UART_1_GetChar();\r
180                 \r
181                 /* Mask off the other RTOS interrupts to interact with the queue. */\r
182                 ulMask = portSET_INTERRUPT_MASK_FROM_ISR();\r
183                 {\r
184                         /* Try to deliver the character. */\r
185                         if( pdTRUE != xQueueSendFromISR( xSerialRxQueue, &cInChar, &xHigherPriorityTaskWoken ) )\r
186                         {\r
187                                 /* Run out of space. */\r
188                         }\r
189                 }\r
190                 portCLEAR_INTERRUPT_MASK_FROM_ISR( ulMask );\r
191         }\r
192 \r
193         /* If we delivered the character then a context switch might be required.\r
194         xHigherPriorityTaskWoken was set to pdFALSE on interrupt entry.  If calling \r
195         xQueueSendFromISR() caused a task to unblock, and the unblocked task has\r
196         a priority equal to or higher than the currently running task (the task this\r
197         ISR interrupted), then xHigherPriorityTaskWoken will have been set to pdTRUE and\r
198         portEND_SWITCHING_ISR() will request a context switch to the newly unblocked\r
199         task. */\r
200         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
201 }\r
202 /*---------------------------------------------------------------------------*/\r
203 \r
204 CY_ISR(vUartTxISR)\r
205 {\r
206 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
207 volatile unsigned char ucStatus = 0;\r
208 signed char cOutChar = 0;\r
209 unsigned long ulMask = 0;\r
210 \r
211         /* Read the status to acknowledge. */\r
212         ucStatus = UART_1_ReadTxStatus();\r
213         \r
214         /* Check to see whether this is a genuine interrupt. */\r
215         if( ( 0 != ( ucStatus & UART_1_TX_STS_COMPLETE ) ) || ( 0 != ( ucStatus & UART_1_TX_STS_FIFO_EMPTY ) ) )\r
216         {       \r
217                 /* Mask off the other RTOS interrupts to interact with the queue. */\r
218                 ulMask = portSET_INTERRUPT_MASK_FROM_ISR();\r
219                 {\r
220                         if( pdTRUE == xQueueReceiveFromISR( xSerialTxQueue, &cOutChar, &xHigherPriorityTaskWoken ) )\r
221                         {\r
222                                 /* Send the next character. */\r
223                                 UART_1_PutChar( cOutChar );                     \r
224 \r
225                                 /* If we are firing, then the only interrupt we are interested in\r
226                                 is the Complete. The application code will add the Empty interrupt\r
227                                 when there is something else to be done. */\r
228                                 UART_1_SetTxInterruptMode( UART_1_TX_STS_COMPLETE );\r
229                         }\r
230                         else\r
231                         {\r
232                                 /* There is no work left so disable the interrupt until the application \r
233                                 puts more into the queue. */\r
234                                 UART_1_SetTxInterruptMode( 0 );\r
235                         }\r
236                 }\r
237                 portCLEAR_INTERRUPT_MASK_FROM_ISR( ulMask );\r
238         }\r
239 \r
240         /* If we delivered the character then a context switch might be required.\r
241         xHigherPriorityTaskWoken was set to pdFALSE on interrupt entry.  If calling \r
242         xQueueSendFromISR() caused a task to unblock, and the unblocked task has\r
243         a priority equal to or higher than the currently running task (the task this\r
244         ISR interrupted), then xHigherPriorityTaskWoken will have been set to pdTRUE and\r
245         portEND_SWITCHING_ISR() will request a context switch to the newly unblocked\r
246         task. */\r
247         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
248 }\r
249 /*---------------------------------------------------------------------------*/\r