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