]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo/src/serial.c
Update Zynq serial.c to be interrupt driven.
[freertos] / FreeRTOS / Demo / CORTEX_A9_Zynq_ZC702 / RTOSDemo / src / serial.c
1 /*\r
2  FreeRTOS V8.0.0:rc2 - Copyright (C) 2014 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  ***************************************************************************\r
8  *                                                                       *\r
9  *    FreeRTOS provides completely free yet professionally developed,    *\r
10  *    robust, strictly quality controlled, supported, and cross          *\r
11  *    platform software that has become a de facto standard.             *\r
12  *                                                                       *\r
13  *    Help yourself get started quickly and support the FreeRTOS         *\r
14  *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15  *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16  *                                                                       *\r
17  *    Thank you!                                                         *\r
18  *                                                                       *\r
19  ***************************************************************************\r
20 \r
21  This file is part of the FreeRTOS distribution.\r
22 \r
23  FreeRTOS is free software; you can redistribute it and/or modify it under\r
24  the terms of the GNU General Public License (version 2) as published by the\r
25  Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27  >>! NOTE: The modification to the GPL is included to allow you to distribute\r
28  >>! a combined work that includes FreeRTOS without being obliged to provide\r
29  >>! the source code for proprietary components outside of the FreeRTOS\r
30  >>! kernel.\r
31 \r
32  FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34  FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35  link: http://www.freertos.org/a00114.html\r
36 \r
37  1 tab == 4 spaces!\r
38 \r
39  ***************************************************************************\r
40  *                                                                       *\r
41  *    Having a problem?  Start by reading the FAQ "My application does   *\r
42  *    not run, what could be wrong?"                                     *\r
43  *                                                                       *\r
44  *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45  *                                                                       *\r
46  ***************************************************************************\r
47 \r
48  http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49  license and Real Time Engineers Ltd. contact details.\r
50 \r
51  http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52  including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53  compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55  http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56  Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57  licenses offer ticketed support, indemnification and middleware.\r
58 \r
59  http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60  engineered and independently SIL3 certified version for use in safety and\r
61  mission critical applications that require provable dependability.\r
62 \r
63  1 tab == 4 spaces!\r
64  */\r
65 \r
66 /*\r
67         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER.\r
68 \r
69         Note1:  This driver is used specifically to provide an interface to the\r
70         FreeRTOS+CLI command interpreter.  It is *not* intended to be a generic\r
71         serial port driver.  Nor is it intended to be used as an example of an\r
72         efficient implementation.  In particular, a queue is used to buffer\r
73         received characters, which is fine in this case as key presses arrive\r
74         slowly, but a DMA and/or RAM buffer should be used in place of the queue in\r
75         applications that expect higher throughput.\r
76 \r
77         Note2:  This driver does not attempt to handle UART errors.\r
78 */\r
79 \r
80 /* Scheduler includes. */\r
81 #include "FreeRTOS.h"\r
82 #include "task.h"\r
83 #include "queue.h"\r
84 #include "semphr.h"\r
85 \r
86 /* Demo application includes. */\r
87 #include "serial.h"\r
88 \r
89 /* Xilinx includes. */\r
90 #include "xuartps.h"\r
91 #include "xscugic.h"\r
92 #include "xil_exception.h"\r
93 \r
94 /* The UART interrupts of interest when receiving. */\r
95 #define serRECEIVE_INTERRUPT_MASK       ( XUARTPS_IXR_RXOVR | XUARTPS_IXR_RXFULL | XUARTPS_IXR_TOUT )\r
96 \r
97 /* The UART interrupts of interest when transmitting. */\r
98 #define serTRANSMIT_IINTERRUPT_MASK ( XUARTPS_IXR_TXEMPTY )\r
99 \r
100 /*-----------------------------------------------------------*/\r
101 \r
102 /* The UART being used. */\r
103 static XUartPs xUARTInstance;\r
104 \r
105 /* The interrupt controller, which is configred by the hardware setup routines\r
106 defined in main(). */\r
107 extern XScuGic xInterruptController;\r
108 \r
109 /* The queue into which received key presses are placed.  NOTE THE COMMENTS AT\r
110 THE TOP OF THIS FILE REGARDING THE USE OF QUEUES FOR THIS PURPOSE. */\r
111 static QueueHandle_t xRxQueue = NULL;\r
112 \r
113 /* The semaphore used to indicate the end of a transmission. */\r
114 static SemaphoreHandle_t xTxCompleteSemaphore = NULL;\r
115 \r
116 /*-----------------------------------------------------------*/\r
117 \r
118 /*\r
119  * The UART interrupt handler is defined in this file to provide more control,\r
120  * but still uses parts of the Xilinx provided driver.\r
121  */\r
122 void prvUART_Handler( void *pvNotUsed );\r
123 \r
124 /*-----------------------------------------------------------*/\r
125 \r
126 /*\r
127  * See the serial2.h header file.\r
128  */\r
129 xComPortHandle xSerialPortInitMinimal( uint32_t ulWantedBaud, UBaseType_t uxQueueLength )\r
130 {\r
131 BaseType_t xStatus;\r
132 XUartPs_Config *pxConfig;\r
133 \r
134         /* Create the queue used to hold received characters.  NOTE THE COMMENTS AT\r
135         THE TOP OF THIS FILE REGARDING THE QUEUE OF QUEUES FOR THIS PURPSOE. */\r
136         xRxQueue = xQueueCreate( uxQueueLength, sizeof( char ) );\r
137         configASSERT( xRxQueue );\r
138 \r
139         /* Create the semaphore used to signal the end of a transmission, then take\r
140         the semaphore so it is in the correct state the first time\r
141         xSerialSendString() is called.  A block time of zero is used when taking\r
142         the semaphore as it is guaranteed to be available (it was just created). */\r
143         xTxCompleteSemaphore = xSemaphoreCreateMutex();\r
144         configASSERT( xTxCompleteSemaphore );\r
145         xSemaphoreTake( xTxCompleteSemaphore, 0 );\r
146 \r
147         /* Look up the UART configuration then initialise the dirver. */\r
148         pxConfig = XUartPs_LookupConfig( XPAR_XUARTPS_0_DEVICE_ID );\r
149 \r
150         /* Initialise the driver. */\r
151         xStatus = XUartPs_CfgInitialize( &xUARTInstance, pxConfig, XPAR_PS7_UART_1_BASEADDR );\r
152         configASSERT( xStatus == XST_SUCCESS );\r
153 \r
154         /* Misc. parameter configuration. */\r
155         XUartPs_SetBaudRate( &xUARTInstance, ulWantedBaud );\r
156         XUartPs_SetOperMode( &xUARTInstance, XUARTPS_OPER_MODE_NORMAL );\r
157 \r
158         /* Install the interrupt service routine that is defined within this\r
159         file. */\r
160         xStatus = XScuGic_Connect( &xInterruptController, XPAR_XUARTPS_1_INTR,  (Xil_ExceptionHandler) prvUART_Handler, (void *) &xUARTInstance );\r
161         configASSERT( xStatus == XST_SUCCESS );\r
162 \r
163         /* Ensure interrupts start clear. */\r
164         XUartPs_WriteReg( XPAR_PS7_UART_1_BASEADDR, XUARTPS_ISR_OFFSET, XUARTPS_IXR_MASK );\r
165 \r
166         /* Enable the UART interrupt within the GIC. */\r
167         XScuGic_Enable( &xInterruptController, XPAR_XUARTPS_1_INTR );\r
168 \r
169         /* Enable the interrupts of interest in the UART. */\r
170         XUartPs_SetInterruptMask( &xUARTInstance, XUARTPS_IXR_RXFULL | XUARTPS_IXR_RXOVR | XUARTPS_IXR_TOUT | XUARTPS_IXR_TXEMPTY );\r
171 \r
172         /* Set the receive timeout. */\r
173         XUartPs_SetRecvTimeout( &xUARTInstance, 8 );\r
174 \r
175         return ( xComPortHandle ) 0;\r
176 }\r
177 /*-----------------------------------------------------------*/\r
178 \r
179 BaseType_t xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )\r
180 {\r
181 BaseType_t xReturn;\r
182 \r
183         /* Only a single port is supported. */\r
184         ( void ) pxPort;\r
185 \r
186         /* Obtain a received character from the queue - entering the Blocked state\r
187         (so not consuming any processing time) to wait for a character if one is not\r
188         already available. */\r
189         xReturn = xQueueReceive( xRxQueue, pcRxedChar, xBlockTime );\r
190         return xReturn;\r
191 }\r
192 /*-----------------------------------------------------------*/\r
193 \r
194 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
195 {\r
196 const TickType_t xMaxWait = 200UL / portTICK_PERIOD_MS;\r
197 \r
198         /* Only a single port is supported. */\r
199         ( void ) pxPort;\r
200 \r
201         /* Start the transmission.  The interrupt service routine will complete the\r
202         transmission if necessary. */\r
203         XUartPs_Send( &xUARTInstance, ( void * ) pcString, usStringLength );\r
204 \r
205         /* Wait until the string has been transmitted before exiting this function,\r
206         otherwise there is a risk the calling function will overwrite the string\r
207         pointed to by the pcString parameter while it is still being transmitted.\r
208         The calling task will wait in the Blocked state (so not consuming any\r
209         processing time) until the mutex is available. */\r
210         xSemaphoreTake( xTxCompleteSemaphore, xMaxWait );\r
211 }\r
212 /*-----------------------------------------------------------*/\r
213 \r
214 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )\r
215 {\r
216         /* Only a single port is supported. */\r
217         ( void ) pxPort;\r
218 \r
219         /* Send the character. */\r
220         XUartPs_Send( &xUARTInstance, ( void * ) &cOutChar, sizeof( cOutChar ) );\r
221 \r
222         /* Wait for the transmission to be complete so the mutex is left in the\r
223         correct state for the next time vSerialPutString() is called. */\r
224         xSemaphoreTake( xTxCompleteSemaphore, xBlockTime );\r
225 \r
226         return pdPASS;\r
227 }\r
228 /*-----------------------------------------------------------*/\r
229 \r
230 void vSerialClose(xComPortHandle xPort)\r
231 {\r
232         /* Not supported as not required by the demo application. */\r
233         ( void ) xPort;\r
234 }\r
235 /*-----------------------------------------------------------*/\r
236 \r
237 void prvUART_Handler( void *pvNotUsed )\r
238 {\r
239 extern unsigned int XUartPs_SendBuffer( XUartPs *InstancePtr );\r
240 uint32_t ulActiveInterrupts, ulChannelStatusRegister;\r
241 BaseType_t xHigherPriorityTaskWoken = pdFALSE;\r
242 char cChar;\r
243 \r
244         configASSERT( pvNotUsed == &xUARTInstance );\r
245 \r
246         /* Read the interrupt ID register to see which interrupt is active. */\r
247         ulActiveInterrupts = XUartPs_ReadReg(XPAR_PS7_UART_1_BASEADDR,  XUARTPS_IMR_OFFSET);\r
248         ulActiveInterrupts &= XUartPs_ReadReg(XPAR_PS7_UART_1_BASEADDR,  XUARTPS_ISR_OFFSET);\r
249 \r
250         /* Are any receive events of interest active? */\r
251         if( ( ulActiveInterrupts & serRECEIVE_INTERRUPT_MASK ) != 0 )\r
252         {\r
253                 /* Read the Channel Status Register to determine if there is any data in\r
254                 the RX FIFO. */\r
255                 ulChannelStatusRegister = XUartPs_ReadReg( XPAR_PS7_UART_1_BASEADDR, XUARTPS_SR_OFFSET );\r
256 \r
257                 /* Move data from the Rx FIFO to the Rx queue.  NOTE THE COMMENTS AT THE\r
258                 TOP OF THIS FILE ABOUT USING QUEUES FOR THIS PURPSOE. */\r
259                 while( ( ulChannelStatusRegister & XUARTPS_SR_RXEMPTY ) == 0 )\r
260                 {\r
261                         cChar = XUartPs_ReadReg( XPAR_PS7_UART_1_BASEADDR, XUARTPS_FIFO_OFFSET );\r
262 \r
263                         /* If writing to the queue unblocks a task, and the unblocked task\r
264                         has a priority above the currently running task (the task that this\r
265                         interrupt interrupted), then xHigherPriorityTaskWoken will be set\r
266                         to pdTRUE inside the xQueueSendFromISR() function.\r
267                         xHigherPriorityTaskWoken is then passed to portYIELD_FROM_ISR() at\r
268                         the end of this interrupt handler to request a context switch so the\r
269                         interrupt returns directly to the (higher priority) unblocked\r
270                         task. */\r
271                         xQueueSendFromISR( xRxQueue, &cChar, &xHigherPriorityTaskWoken );\r
272                         ulChannelStatusRegister = XUartPs_ReadReg( XPAR_PS7_UART_1_BASEADDR, XUARTPS_SR_OFFSET );\r
273                 }\r
274         }\r
275 \r
276         /* Are any transmit events of interest active? */\r
277         if( ( ulActiveInterrupts & serTRANSMIT_IINTERRUPT_MASK ) != 0 )\r
278         {\r
279                 if( xUARTInstance.SendBuffer.RemainingBytes == 0 )\r
280                 {\r
281                         /* Give back the semaphore to indicate that the tranmission is\r
282                         complete.  If giving the semaphore unblocks a task, and the\r
283                         unblocked task has a priority above the currently running task (the\r
284                         task that this interrupt interrupted), then xHigherPriorityTaskWoken\r
285                         will be set     to pdTRUE inside the xSemaphoreGiveFromISR() function.\r
286                         xHigherPriorityTaskWoken is then passed to portYIELD_FROM_ISR() at\r
287                         the end of this interrupt handler to request a context switch so the\r
288                         interrupt returns directly to the (higher priority) unblocked\r
289                         task. */\r
290                         xSemaphoreGiveFromISR( xTxCompleteSemaphore, &xHigherPriorityTaskWoken );\r
291 \r
292                         /* No more data to transmit. */\r
293                         XUartPs_WriteReg( XPAR_PS7_UART_1_BASEADDR, XUARTPS_IDR_OFFSET, XUARTPS_IXR_TXEMPTY );\r
294                 }\r
295                 else\r
296                 {\r
297                         /* More data to send. */\r
298                         XUartPs_SendBuffer( &xUARTInstance );\r
299                 }\r
300         }\r
301 \r
302         /* portYIELD_FROM_ISR() will request a context switch if executing this\r
303         interrupt handler caused a task to leave the blocked state, and the task\r
304         that left the blocked state has a higher priority than the currently running\r
305         task (the task this interrupt interrupted).  See the comment above the calls\r
306         to xSemaphoreGiveFromISR() and xQueueSendFromISR() within this function. */\r
307         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
308 \r
309         /* Clear the interrupt status. */\r
310         XUartPs_WriteReg( XPAR_PS7_UART_1_BASEADDR, XUARTPS_ISR_OFFSET, ulActiveInterrupts );\r
311 }\r
312 \r
313 \r
314 \r
315 \r
316 \r