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