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