]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo/src/Full_Demo/serial.c
Update version number to 8.1.2 after moving the defaulting of configUSE_PORT_OPTIMISE...
[freertos] / FreeRTOS / Demo / CORTEX_A9_Zynq_ZC702 / RTOSDemo / src / Full_Demo / serial.c
1 /*\r
2  FreeRTOS V8.1.2 - 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     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS 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 = xSemaphoreCreateBinary();\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         ( void ) xStatus; /* Remove compiler warning if configASSERT() is not defined. */\r
154 \r
155         /* Misc. parameter configuration. */\r
156         XUartPs_SetBaudRate( &xUARTInstance, ulWantedBaud );\r
157         XUartPs_SetOperMode( &xUARTInstance, XUARTPS_OPER_MODE_NORMAL );\r
158 \r
159         /* Install the interrupt service routine that is defined within this\r
160         file. */\r
161         xStatus = XScuGic_Connect( &xInterruptController, XPAR_XUARTPS_1_INTR,  (Xil_ExceptionHandler) prvUART_Handler, (void *) &xUARTInstance );\r
162         configASSERT( xStatus == XST_SUCCESS );\r
163         ( void ) xStatus; /* Remove compiler warning if configASSERT() is not defined. */\r
164 \r
165         /* Ensure interrupts start clear. */\r
166         XUartPs_WriteReg( XPAR_PS7_UART_1_BASEADDR, XUARTPS_ISR_OFFSET, XUARTPS_IXR_MASK );\r
167 \r
168         /* Enable the UART interrupt within the GIC. */\r
169         XScuGic_Enable( &xInterruptController, XPAR_XUARTPS_1_INTR );\r
170 \r
171         /* Enable the interrupts of interest in the UART. */\r
172         XUartPs_SetInterruptMask( &xUARTInstance, XUARTPS_IXR_RXFULL | XUARTPS_IXR_RXOVR | XUARTPS_IXR_TOUT | XUARTPS_IXR_TXEMPTY );\r
173 \r
174         /* Set the receive timeout. */\r
175         XUartPs_SetRecvTimeout( &xUARTInstance, 8 );\r
176 \r
177         return ( xComPortHandle ) 0;\r
178 }\r
179 /*-----------------------------------------------------------*/\r
180 \r
181 BaseType_t xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
182 {\r
183 BaseType_t xReturn;\r
184 \r
185         /* Only a single port is supported. */\r
186         ( void ) pxPort;\r
187 \r
188         /* Obtain a received character from the queue - entering the Blocked state\r
189         (so not consuming any processing time) to wait for a character if one is not\r
190         already available. */\r
191         xReturn = xQueueReceive( xRxQueue, pcRxedChar, xBlockTime );\r
192         return xReturn;\r
193 }\r
194 /*-----------------------------------------------------------*/\r
195 \r
196 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
197 {\r
198 const TickType_t xMaxWait = 200UL / portTICK_PERIOD_MS;\r
199 \r
200         /* Only a single port is supported. */\r
201         ( void ) pxPort;\r
202 \r
203         /* Start the transmission.  The interrupt service routine will complete the\r
204         transmission if necessary. */\r
205         XUartPs_Send( &xUARTInstance, ( void * ) pcString, usStringLength );\r
206 \r
207         /* Wait until the string has been transmitted before exiting this function,\r
208         otherwise there is a risk the calling function will overwrite the string\r
209         pointed to by the pcString parameter while it is still being transmitted.\r
210         The calling task will wait in the Blocked state (so not consuming any\r
211         processing time) until the semaphore is available. */\r
212         xSemaphoreTake( xTxCompleteSemaphore, xMaxWait );\r
213 }\r
214 /*-----------------------------------------------------------*/\r
215 \r
216 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
217 {\r
218         /* Only a single port is supported. */\r
219         ( void ) pxPort;\r
220 \r
221         /* Send the character. */\r
222         XUartPs_Send( &xUARTInstance, ( void * ) &cOutChar, sizeof( cOutChar ) );\r
223 \r
224         /* Wait for the transmission to be complete so the semaphore is left in the\r
225         correct state for the next time vSerialPutString() is called. */\r
226         xSemaphoreTake( xTxCompleteSemaphore, xBlockTime );\r
227 \r
228         return pdPASS;\r
229 }\r
230 /*-----------------------------------------------------------*/\r
231 \r
232 void vSerialClose(xComPortHandle xPort)\r
233 {\r
234         /* Not supported as not required by the demo application. */\r
235         ( void ) xPort;\r
236 }\r
237 /*-----------------------------------------------------------*/\r
238 \r
239 void prvUART_Handler( void *pvNotUsed )\r
240 {\r
241 extern unsigned int XUartPs_SendBuffer( XUartPs *InstancePtr );\r
242 uint32_t ulActiveInterrupts, ulChannelStatusRegister;\r
243 BaseType_t xHigherPriorityTaskWoken = pdFALSE;\r
244 char cChar;\r
245 \r
246         configASSERT( pvNotUsed == &xUARTInstance );\r
247 \r
248         /* Remove compile warnings if configASSERT() is not defined. */\r
249         ( void ) pvNotUsed;\r
250 \r
251         /* Read the interrupt ID register to see which interrupt is active. */\r
252         ulActiveInterrupts = XUartPs_ReadReg(XPAR_PS7_UART_1_BASEADDR,  XUARTPS_IMR_OFFSET);\r
253         ulActiveInterrupts &= XUartPs_ReadReg(XPAR_PS7_UART_1_BASEADDR,  XUARTPS_ISR_OFFSET);\r
254 \r
255         /* Are any receive events of interest active? */\r
256         if( ( ulActiveInterrupts & serRECEIVE_INTERRUPT_MASK ) != 0 )\r
257         {\r
258                 /* Read the Channel Status Register to determine if there is any data in\r
259                 the RX FIFO. */\r
260                 ulChannelStatusRegister = XUartPs_ReadReg( XPAR_PS7_UART_1_BASEADDR, XUARTPS_SR_OFFSET );\r
261 \r
262                 /* Move data from the Rx FIFO to the Rx queue.  NOTE THE COMMENTS AT THE\r
263                 TOP OF THIS FILE ABOUT USING QUEUES FOR THIS PURPSOE. */\r
264                 while( ( ulChannelStatusRegister & XUARTPS_SR_RXEMPTY ) == 0 )\r
265                 {\r
266                         cChar = XUartPs_ReadReg( XPAR_PS7_UART_1_BASEADDR, XUARTPS_FIFO_OFFSET );\r
267 \r
268                         /* If writing to the queue unblocks a task, and the unblocked task\r
269                         has a priority above the currently running task (the task that this\r
270                         interrupt interrupted), then xHigherPriorityTaskWoken will be set\r
271                         to pdTRUE inside the xQueueSendFromISR() function.\r
272                         xHigherPriorityTaskWoken is then passed to portYIELD_FROM_ISR() at\r
273                         the end of this interrupt handler to request a context switch so the\r
274                         interrupt returns directly to the (higher priority) unblocked\r
275                         task. */\r
276                         xQueueSendFromISR( xRxQueue, &cChar, &xHigherPriorityTaskWoken );\r
277                         ulChannelStatusRegister = XUartPs_ReadReg( XPAR_PS7_UART_1_BASEADDR, XUARTPS_SR_OFFSET );\r
278                 }\r
279         }\r
280 \r
281         /* Are any transmit events of interest active? */\r
282         if( ( ulActiveInterrupts & serTRANSMIT_IINTERRUPT_MASK ) != 0 )\r
283         {\r
284                 if( xUARTInstance.SendBuffer.RemainingBytes == 0 )\r
285                 {\r
286                         /* Give back the semaphore to indicate that the tranmission is\r
287                         complete.  If giving the semaphore unblocks a task, and the\r
288                         unblocked task has a priority above the currently running task (the\r
289                         task that this interrupt interrupted), then xHigherPriorityTaskWoken\r
290                         will be set     to pdTRUE inside the xSemaphoreGiveFromISR() function.\r
291                         xHigherPriorityTaskWoken is then passed to portYIELD_FROM_ISR() at\r
292                         the end of this interrupt handler to request a context switch so the\r
293                         interrupt returns directly to the (higher priority) unblocked\r
294                         task. */\r
295                         xSemaphoreGiveFromISR( xTxCompleteSemaphore, &xHigherPriorityTaskWoken );\r
296 \r
297                         /* No more data to transmit. */\r
298                         XUartPs_WriteReg( XPAR_PS7_UART_1_BASEADDR, XUARTPS_IDR_OFFSET, XUARTPS_IXR_TXEMPTY );\r
299                 }\r
300                 else\r
301                 {\r
302                         /* More data to send. */\r
303                         XUartPs_SendBuffer( &xUARTInstance );\r
304                 }\r
305         }\r
306 \r
307         /* portYIELD_FROM_ISR() will request a context switch if executing this\r
308         interrupt handler caused a task to leave the blocked state, and the task\r
309         that left the blocked state has a higher priority than the currently running\r
310         task (the task this interrupt interrupted).  See the comment above the calls\r
311         to xSemaphoreGiveFromISR() and xQueueSendFromISR() within this function. */\r
312         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
313 \r
314         /* Clear the interrupt status. */\r
315         XUartPs_WriteReg( XPAR_PS7_UART_1_BASEADDR, XUARTPS_ISR_OFFSET, ulActiveInterrupts );\r
316 }\r
317 \r
318 \r
319 \r
320 \r
321 \r