]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_CY8C5588_PSoC_Creator_GCC/FreeRTOS_Demo.cydsn/Serial.c
Update license information text files for the CLI, TCP and UDP products to be correct...
[freertos] / FreeRTOS / Demo / CORTEX_CY8C5588_PSoC_Creator_GCC / FreeRTOS_Demo.cydsn / Serial.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 #include <device.h>\r
30 #include "FreeRTOS.h"\r
31 #include "queue.h"\r
32 #include "task.h"\r
33 #include "serial.h"\r
34 /*---------------------------------------------------------------------------*/\r
35 \r
36 #define serialSTRING_DELAY_TICKS                ( portMAX_DELAY )\r
37 /*---------------------------------------------------------------------------*/\r
38 \r
39 CY_ISR_PROTO( vUartRxISR );\r
40 CY_ISR_PROTO( vUartTxISR );\r
41 /*---------------------------------------------------------------------------*/\r
42 \r
43 static QueueHandle_t xSerialTxQueue = NULL;\r
44 static QueueHandle_t xSerialRxQueue = NULL;\r
45 /*---------------------------------------------------------------------------*/\r
46 \r
47 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
48 {\r
49         /* Configure Rx. */\r
50         xSerialRxQueue = xQueueCreate( uxQueueLength, sizeof( signed char ) );  \r
51         isr_UART1_RX_BYTE_RECEIVED_ClearPending();\r
52         isr_UART1_RX_BYTE_RECEIVED_StartEx(vUartRxISR);\r
53 \r
54         /* Configure Tx */\r
55         xSerialTxQueue = xQueueCreate( uxQueueLength, sizeof( signed char ) );\r
56         isr_UART1_TX_BYTE_COMPLETE_ClearPending() ;\r
57         isr_UART1_TX_BYTE_COMPLETE_StartEx(vUartTxISR);\r
58 \r
59         /* Clear the interrupt modes for the Tx for the time being. */\r
60         UART_1_SetTxInterruptMode( 0 );\r
61 \r
62         /* Both configured successfully. */\r
63         return ( xComPortHandle )( xSerialTxQueue && xSerialRxQueue );\r
64 }\r
65 /*---------------------------------------------------------------------------*/\r
66 \r
67 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
68 {\r
69 unsigned short usIndex = 0;\r
70 \r
71         for( usIndex = 0; usIndex < usStringLength; usIndex++ )\r
72         {\r
73                 /* Check for pre-mature end of line. */\r
74                 if( '\0' == pcString[ usIndex ] )\r
75                 {\r
76                         break;\r
77                 }\r
78                 \r
79                 /* Send out, one character at a time. */\r
80                 if( pdTRUE != xSerialPutChar( NULL, pcString[ usIndex ], serialSTRING_DELAY_TICKS ) )\r
81                 {\r
82                         /* Failed to send, this will be picked up in the receive comtest task. */\r
83                 }\r
84         }\r
85 }\r
86 /*---------------------------------------------------------------------------*/\r
87 \r
88 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
89 {\r
90 portBASE_TYPE xReturn = pdFALSE;\r
91 \r
92         if( pdTRUE == xQueueReceive( xSerialRxQueue, pcRxedChar, xBlockTime ) )\r
93         {\r
94                 /* Picked up a character. */\r
95                 xReturn = pdTRUE;\r
96         }\r
97         return xReturn;\r
98 }\r
99 /*---------------------------------------------------------------------------*/\r
100 \r
101 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
102 {\r
103 portBASE_TYPE xReturn = pdFALSE;\r
104 \r
105         /* The ISR is processing characters is so just add to the end of the queue. */\r
106         if( pdTRUE == xQueueSend( xSerialTxQueue, &cOutChar, xBlockTime ) )\r
107         {       \r
108                 xReturn = pdTRUE;\r
109         }\r
110         else\r
111         {\r
112                 /* The queue is probably full. */\r
113                 xReturn = pdFALSE;\r
114         }\r
115 \r
116         /* Make sure that the interrupt will fire in the case where:\r
117             Currently sending so the Tx Complete will fire.\r
118             Not sending so the Empty will fire. */\r
119         taskENTER_CRITICAL();\r
120                 UART_1_SetTxInterruptMode( UART_1_TX_STS_COMPLETE | UART_1_TX_STS_FIFO_EMPTY );\r
121         taskEXIT_CRITICAL();\r
122         \r
123         return xReturn;\r
124 }\r
125 /*---------------------------------------------------------------------------*/\r
126 \r
127 CY_ISR(vUartRxISR)\r
128 {\r
129 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
130 volatile unsigned char ucStatus = 0;\r
131 signed char cInChar = 0;\r
132 unsigned long ulMask = 0;\r
133 \r
134         /* Read the status to acknowledge. */\r
135         ucStatus = UART_1_ReadRxStatus();\r
136 \r
137         /* Only interested in a character being received. */\r
138         if( 0 != ( ucStatus & UART_1_RX_STS_FIFO_NOTEMPTY ) )\r
139         {\r
140                 /* Get the character. */\r
141                 cInChar = UART_1_GetChar();\r
142                 \r
143                 /* Mask off the other RTOS interrupts to interact with the queue. */\r
144                 ulMask = portSET_INTERRUPT_MASK_FROM_ISR();\r
145                 {\r
146                         /* Try to deliver the character. */\r
147                         if( pdTRUE != xQueueSendFromISR( xSerialRxQueue, &cInChar, &xHigherPriorityTaskWoken ) )\r
148                         {\r
149                                 /* Run out of space. */\r
150                         }\r
151                 }\r
152                 portCLEAR_INTERRUPT_MASK_FROM_ISR( ulMask );\r
153         }\r
154 \r
155         /* If we delivered the character then a context switch might be required.\r
156         xHigherPriorityTaskWoken was set to pdFALSE on interrupt entry.  If calling \r
157         xQueueSendFromISR() caused a task to unblock, and the unblocked task has\r
158         a priority equal to or higher than the currently running task (the task this\r
159         ISR interrupted), then xHigherPriorityTaskWoken will have been set to pdTRUE and\r
160         portEND_SWITCHING_ISR() will request a context switch to the newly unblocked\r
161         task. */\r
162         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
163 }\r
164 /*---------------------------------------------------------------------------*/\r
165 \r
166 CY_ISR(vUartTxISR)\r
167 {\r
168 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
169 volatile unsigned char ucStatus = 0;\r
170 signed char cOutChar = 0;\r
171 unsigned long ulMask = 0;\r
172 \r
173         /* Read the status to acknowledge. */\r
174         ucStatus = UART_1_ReadTxStatus();\r
175         \r
176         /* Check to see whether this is a genuine interrupt. */\r
177         if( ( 0 != ( ucStatus & UART_1_TX_STS_COMPLETE ) ) || ( 0 != ( ucStatus & UART_1_TX_STS_FIFO_EMPTY ) ) )\r
178         {       \r
179                 /* Mask off the other RTOS interrupts to interact with the queue. */\r
180                 ulMask = portSET_INTERRUPT_MASK_FROM_ISR();\r
181                 {\r
182                         if( pdTRUE == xQueueReceiveFromISR( xSerialTxQueue, &cOutChar, &xHigherPriorityTaskWoken ) )\r
183                         {\r
184                                 /* Send the next character. */\r
185                                 UART_1_PutChar( cOutChar );                     \r
186 \r
187                                 /* If we are firing, then the only interrupt we are interested in\r
188                                 is the Complete. The application code will add the Empty interrupt\r
189                                 when there is something else to be done. */\r
190                                 UART_1_SetTxInterruptMode( UART_1_TX_STS_COMPLETE );\r
191                         }\r
192                         else\r
193                         {\r
194                                 /* There is no work left so disable the interrupt until the application \r
195                                 puts more into the queue. */\r
196                                 UART_1_SetTxInterruptMode( 0 );\r
197                         }\r
198                 }\r
199                 portCLEAR_INTERRUPT_MASK_FROM_ISR( ulMask );\r
200         }\r
201 \r
202         /* If we delivered the character then a context switch might be required.\r
203         xHigherPriorityTaskWoken was set to pdFALSE on interrupt entry.  If calling \r
204         xQueueSendFromISR() caused a task to unblock, and the unblocked task has\r
205         a priority equal to or higher than the currently running task (the task this\r
206         ISR interrupted), then xHigherPriorityTaskWoken will have been set to pdTRUE and\r
207         portEND_SWITCHING_ISR() will request a context switch to the newly unblocked\r
208         task. */\r
209         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
210 }\r
211 /*---------------------------------------------------------------------------*/\r