]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo/src/Full_Demo/serial.c
5624e45d0210c7fb21d2312f4c765a8464866348
[freertos] / FreeRTOS / Demo / CORTEX_A9_Zynq_ZC702 / RTOSDemo / src / Full_Demo / serial.c
1 /*\r
2  * FreeRTOS Kernel V10.2.1\r
3  * Copyright (C) 2019 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 /*\r
29         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER.\r
30 \r
31         Note1:  This driver is used specifically to provide an interface to the\r
32         FreeRTOS+CLI command interpreter.  It is *not* intended to be a generic\r
33         serial port driver.  Nor is it intended to be used as an example of an\r
34         efficient implementation.  In particular, a queue is used to buffer\r
35         received characters, which is fine in this case as key presses arrive\r
36         slowly, but a DMA and/or RAM buffer should be used in place of the queue in\r
37         applications that expect higher throughput.\r
38 \r
39         Note2:  This driver does not attempt to handle UART errors.\r
40 */\r
41 \r
42 /* Scheduler includes. */\r
43 #include "FreeRTOS.h"\r
44 #include "task.h"\r
45 #include "queue.h"\r
46 #include "semphr.h"\r
47 \r
48 /* Demo application includes. */\r
49 #include "serial.h"\r
50 \r
51 /* Xilinx includes. */\r
52 #include "xuartps.h"\r
53 #include "xscugic.h"\r
54 #include "xil_exception.h"\r
55 \r
56 /* The UART interrupts of interest when receiving. */\r
57 #define serRECEIVE_INTERRUPT_MASK       ( XUARTPS_IXR_RXOVR | XUARTPS_IXR_RXFULL | XUARTPS_IXR_TOUT )\r
58 \r
59 /* The UART interrupts of interest when transmitting. */\r
60 #define serTRANSMIT_IINTERRUPT_MASK ( XUARTPS_IXR_TXEMPTY )\r
61 \r
62 /*-----------------------------------------------------------*/\r
63 \r
64 /* The UART being used. */\r
65 static XUartPs xUARTInstance;\r
66 \r
67 /* The interrupt controller, which is configred by the hardware setup routines\r
68 defined in main(). */\r
69 extern XScuGic xInterruptController;\r
70 \r
71 /* The queue into which received key presses are placed.  NOTE THE COMMENTS AT\r
72 THE TOP OF THIS FILE REGARDING THE USE OF QUEUES FOR THIS PURPOSE. */\r
73 static QueueHandle_t xRxQueue = NULL;\r
74 \r
75 /* The semaphore used to indicate the end of a transmission. */\r
76 static SemaphoreHandle_t xTxCompleteSemaphore = NULL;\r
77 \r
78 /*-----------------------------------------------------------*/\r
79 \r
80 /*\r
81  * The UART interrupt handler is defined in this file to provide more control,\r
82  * but still uses parts of the Xilinx provided driver.\r
83  */\r
84 void prvUART_Handler( void *pvNotUsed );\r
85 \r
86 /*-----------------------------------------------------------*/\r
87 \r
88 /*\r
89  * See the serial2.h header file.\r
90  */\r
91 xComPortHandle xSerialPortInitMinimal( uint32_t ulWantedBaud, UBaseType_t uxQueueLength )\r
92 {\r
93 BaseType_t xStatus;\r
94 XUartPs_Config *pxConfig;\r
95 \r
96         /* Create the queue used to hold received characters.  NOTE THE COMMENTS AT\r
97         THE TOP OF THIS FILE REGARDING THE USE OF QUEUES FOR THIS PURPSOE. */\r
98         xRxQueue = xQueueCreate( uxQueueLength, sizeof( char ) );\r
99         configASSERT( xRxQueue );\r
100 \r
101         /* Create the semaphore used to signal the end of a transmission, then take\r
102         the semaphore so it is in the correct state the first time\r
103         xSerialSendString() is called.  A block time of zero is used when taking\r
104         the semaphore as it is guaranteed to be available (it was just created). */\r
105         xTxCompleteSemaphore = xSemaphoreCreateBinary();\r
106         configASSERT( xTxCompleteSemaphore );\r
107         xSemaphoreTake( xTxCompleteSemaphore, 0 );\r
108 \r
109         /* Look up the UART configuration then initialise the dirver. */\r
110         pxConfig = XUartPs_LookupConfig( XPAR_XUARTPS_0_DEVICE_ID );\r
111 \r
112         /* Initialise the driver. */\r
113         xStatus = XUartPs_CfgInitialize( &xUARTInstance, pxConfig, XPAR_PS7_UART_1_BASEADDR );\r
114         configASSERT( xStatus == XST_SUCCESS );\r
115         ( void ) xStatus; /* Remove compiler warning if configASSERT() is not defined. */\r
116 \r
117         /* Misc. parameter configuration. */\r
118         XUartPs_SetBaudRate( &xUARTInstance, ulWantedBaud );\r
119         XUartPs_SetOperMode( &xUARTInstance, XUARTPS_OPER_MODE_NORMAL );\r
120 \r
121         /* Install the interrupt service routine that is defined within this\r
122         file. */\r
123         xStatus = XScuGic_Connect( &xInterruptController, XPAR_XUARTPS_1_INTR,  (Xil_ExceptionHandler) prvUART_Handler, (void *) &xUARTInstance );\r
124         configASSERT( xStatus == XST_SUCCESS );\r
125         ( void ) xStatus; /* Remove compiler warning if configASSERT() is not defined. */\r
126 \r
127         /* Ensure interrupts start clear. */\r
128         XUartPs_WriteReg( XPAR_PS7_UART_1_BASEADDR, XUARTPS_ISR_OFFSET, XUARTPS_IXR_MASK );\r
129 \r
130         /* Enable the UART interrupt within the GIC. */\r
131         XScuGic_Enable( &xInterruptController, XPAR_XUARTPS_1_INTR );\r
132 \r
133         /* Enable the interrupts of interest in the UART. */\r
134         XUartPs_SetInterruptMask( &xUARTInstance, XUARTPS_IXR_RXFULL | XUARTPS_IXR_RXOVR | XUARTPS_IXR_TOUT | XUARTPS_IXR_TXEMPTY );\r
135 \r
136         /* Set the receive timeout. */\r
137         XUartPs_SetRecvTimeout( &xUARTInstance, 8 );\r
138 \r
139         return ( xComPortHandle ) 0;\r
140 }\r
141 /*-----------------------------------------------------------*/\r
142 \r
143 BaseType_t xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
144 {\r
145 BaseType_t xReturn;\r
146 \r
147         /* Only a single port is supported. */\r
148         ( void ) pxPort;\r
149 \r
150         /* Obtain a received character from the queue - entering the Blocked state\r
151         (so not consuming any processing time) to wait for a character if one is not\r
152         already available. */\r
153         xReturn = xQueueReceive( xRxQueue, pcRxedChar, xBlockTime );\r
154         return xReturn;\r
155 }\r
156 /*-----------------------------------------------------------*/\r
157 \r
158 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
159 {\r
160 const TickType_t xMaxWait = 200UL / portTICK_PERIOD_MS;\r
161 \r
162         /* Only a single port is supported. */\r
163         ( void ) pxPort;\r
164 \r
165         /* Start the transmission.  The interrupt service routine will complete the\r
166         transmission if necessary. */\r
167         XUartPs_Send( &xUARTInstance, ( void * ) pcString, usStringLength );\r
168 \r
169         /* Wait until the string has been transmitted before exiting this function,\r
170         otherwise there is a risk the calling function will overwrite the string\r
171         pointed to by the pcString parameter while it is still being transmitted.\r
172         The calling task will wait in the Blocked state (so not consuming any\r
173         processing time) until the semaphore is available. */\r
174         xSemaphoreTake( xTxCompleteSemaphore, xMaxWait );\r
175 }\r
176 /*-----------------------------------------------------------*/\r
177 \r
178 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
179 {\r
180         /* Only a single port is supported. */\r
181         ( void ) pxPort;\r
182 \r
183         /* Send the character. */\r
184         XUartPs_Send( &xUARTInstance, ( void * ) &cOutChar, sizeof( cOutChar ) );\r
185 \r
186         /* Wait for the transmission to be complete so the semaphore is left in the\r
187         correct state for the next time vSerialPutString() is called. */\r
188         xSemaphoreTake( xTxCompleteSemaphore, xBlockTime );\r
189 \r
190         return pdPASS;\r
191 }\r
192 /*-----------------------------------------------------------*/\r
193 \r
194 void vSerialClose(xComPortHandle xPort)\r
195 {\r
196         /* Not supported as not required by the demo application. */\r
197         ( void ) xPort;\r
198 }\r
199 /*-----------------------------------------------------------*/\r
200 \r
201 void prvUART_Handler( void *pvNotUsed )\r
202 {\r
203 extern unsigned int XUartPs_SendBuffer( XUartPs *InstancePtr );\r
204 uint32_t ulActiveInterrupts, ulChannelStatusRegister;\r
205 BaseType_t xHigherPriorityTaskWoken = pdFALSE;\r
206 char cChar;\r
207 \r
208         configASSERT( pvNotUsed == &xUARTInstance );\r
209 \r
210         /* Remove compile warnings if configASSERT() is not defined. */\r
211         ( void ) pvNotUsed;\r
212 \r
213         /* Read the interrupt ID register to see which interrupt is active. */\r
214         ulActiveInterrupts = XUartPs_ReadReg(XPAR_PS7_UART_1_BASEADDR,  XUARTPS_IMR_OFFSET);\r
215         ulActiveInterrupts &= XUartPs_ReadReg(XPAR_PS7_UART_1_BASEADDR,  XUARTPS_ISR_OFFSET);\r
216 \r
217         /* Are any receive events of interest active? */\r
218         if( ( ulActiveInterrupts & serRECEIVE_INTERRUPT_MASK ) != 0 )\r
219         {\r
220                 /* Read the Channel Status Register to determine if there is any data in\r
221                 the RX FIFO. */\r
222                 ulChannelStatusRegister = XUartPs_ReadReg( XPAR_PS7_UART_1_BASEADDR, XUARTPS_SR_OFFSET );\r
223 \r
224                 /* Move data from the Rx FIFO to the Rx queue.  NOTE THE COMMENTS AT THE\r
225                 TOP OF THIS FILE ABOUT USING QUEUES FOR THIS PURPSOE. */\r
226                 while( ( ulChannelStatusRegister & XUARTPS_SR_RXEMPTY ) == 0 )\r
227                 {\r
228                         cChar = XUartPs_ReadReg( XPAR_PS7_UART_1_BASEADDR, XUARTPS_FIFO_OFFSET );\r
229 \r
230                         /* If writing to the queue unblocks a task, and the unblocked task\r
231                         has a priority above the currently running task (the task that this\r
232                         interrupt interrupted), then xHigherPriorityTaskWoken will be set\r
233                         to pdTRUE inside the xQueueSendFromISR() function.\r
234                         xHigherPriorityTaskWoken is then passed to portYIELD_FROM_ISR() at\r
235                         the end of this interrupt handler to request a context switch so the\r
236                         interrupt returns directly to the (higher priority) unblocked\r
237                         task. */\r
238                         xQueueSendFromISR( xRxQueue, &cChar, &xHigherPriorityTaskWoken );\r
239                         ulChannelStatusRegister = XUartPs_ReadReg( XPAR_PS7_UART_1_BASEADDR, XUARTPS_SR_OFFSET );\r
240                 }\r
241         }\r
242 \r
243         /* Are any transmit events of interest active? */\r
244         if( ( ulActiveInterrupts & serTRANSMIT_IINTERRUPT_MASK ) != 0 )\r
245         {\r
246                 if( xUARTInstance.SendBuffer.RemainingBytes == 0 )\r
247                 {\r
248                         /* Give back the semaphore to indicate that the tranmission is\r
249                         complete.  If giving the semaphore unblocks a task, and the\r
250                         unblocked task has a priority above the currently running task (the\r
251                         task that this interrupt interrupted), then xHigherPriorityTaskWoken\r
252                         will be set     to pdTRUE inside the xSemaphoreGiveFromISR() function.\r
253                         xHigherPriorityTaskWoken is then passed to portYIELD_FROM_ISR() at\r
254                         the end of this interrupt handler to request a context switch so the\r
255                         interrupt returns directly to the (higher priority) unblocked\r
256                         task. */\r
257                         xSemaphoreGiveFromISR( xTxCompleteSemaphore, &xHigherPriorityTaskWoken );\r
258 \r
259                         /* No more data to transmit. */\r
260                         XUartPs_WriteReg( XPAR_PS7_UART_1_BASEADDR, XUARTPS_IDR_OFFSET, XUARTPS_IXR_TXEMPTY );\r
261                 }\r
262                 else\r
263                 {\r
264                         /* More data to send. */\r
265                         XUartPs_SendBuffer( &xUARTInstance );\r
266                 }\r
267         }\r
268 \r
269         /* portYIELD_FROM_ISR() will request a context switch if executing this\r
270         interrupt handler caused a task to leave the blocked state, and the task\r
271         that left the blocked state has a higher priority than the currently running\r
272         task (the task this interrupt interrupted).  See the comment above the calls\r
273         to xSemaphoreGiveFromISR() and xQueueSendFromISR() within this function. */\r
274         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
275 \r
276         /* Clear the interrupt status. */\r
277         XUartPs_WriteReg( XPAR_PS7_UART_1_BASEADDR, XUARTPS_ISR_OFFSET, ulActiveInterrupts );\r
278 }\r
279 \r
280 \r
281 \r
282 \r
283 \r