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