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