2 FreeRTOS V8.0.1 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\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
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
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\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
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
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
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\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
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
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
67 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER.
\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
77 Note2: This driver does not attempt to handle UART errors.
\r
80 /* Scheduler includes. */
\r
81 #include "FreeRTOS.h"
\r
86 /* Demo application includes. */
\r
89 /* Xilinx includes. */
\r
90 #include "xuartps.h"
\r
91 #include "xscugic.h"
\r
92 #include "xil_exception.h"
\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
97 /* The UART interrupts of interest when transmitting. */
\r
98 #define serTRANSMIT_IINTERRUPT_MASK ( XUARTPS_IXR_TXEMPTY )
\r
100 /*-----------------------------------------------------------*/
\r
102 /* The UART being used. */
\r
103 static XUartPs xUARTInstance;
\r
105 /* The interrupt controller, which is configred by the hardware setup routines
\r
106 defined in main(). */
\r
107 extern XScuGic xInterruptController;
\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
113 /* The semaphore used to indicate the end of a transmission. */
\r
114 static SemaphoreHandle_t xTxCompleteSemaphore = NULL;
\r
116 /*-----------------------------------------------------------*/
\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
122 void prvUART_Handler( void *pvNotUsed );
\r
124 /*-----------------------------------------------------------*/
\r
127 * See the serial2.h header file.
\r
129 xComPortHandle xSerialPortInitMinimal( uint32_t ulWantedBaud, UBaseType_t uxQueueLength )
\r
131 BaseType_t xStatus;
\r
132 XUartPs_Config *pxConfig;
\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
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
147 /* Look up the UART configuration then initialise the dirver. */
\r
148 pxConfig = XUartPs_LookupConfig( XPAR_XUARTPS_0_DEVICE_ID );
\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
155 /* Misc. parameter configuration. */
\r
156 XUartPs_SetBaudRate( &xUARTInstance, ulWantedBaud );
\r
157 XUartPs_SetOperMode( &xUARTInstance, XUARTPS_OPER_MODE_NORMAL );
\r
159 /* Install the interrupt service routine that is defined within this
\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
165 /* Ensure interrupts start clear. */
\r
166 XUartPs_WriteReg( XPAR_PS7_UART_1_BASEADDR, XUARTPS_ISR_OFFSET, XUARTPS_IXR_MASK );
\r
168 /* Enable the UART interrupt within the GIC. */
\r
169 XScuGic_Enable( &xInterruptController, XPAR_XUARTPS_1_INTR );
\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
174 /* Set the receive timeout. */
\r
175 XUartPs_SetRecvTimeout( &xUARTInstance, 8 );
\r
177 return ( xComPortHandle ) 0;
\r
179 /*-----------------------------------------------------------*/
\r
181 BaseType_t xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
\r
183 BaseType_t xReturn;
\r
185 /* Only a single port is supported. */
\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
194 /*-----------------------------------------------------------*/
\r
196 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
198 const TickType_t xMaxWait = 200UL / portTICK_PERIOD_MS;
\r
200 /* Only a single port is supported. */
\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
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 mutex is available. */
\r
212 xSemaphoreTake( xTxCompleteSemaphore, xMaxWait );
\r
214 /*-----------------------------------------------------------*/
\r
216 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
\r
218 /* Only a single port is supported. */
\r
221 /* Send the character. */
\r
222 XUartPs_Send( &xUARTInstance, ( void * ) &cOutChar, sizeof( cOutChar ) );
\r
224 /* Wait for the transmission to be complete so the mutex is left in the
\r
225 correct state for the next time vSerialPutString() is called. */
\r
226 xSemaphoreTake( xTxCompleteSemaphore, xBlockTime );
\r
230 /*-----------------------------------------------------------*/
\r
232 void vSerialClose(xComPortHandle xPort)
\r
234 /* Not supported as not required by the demo application. */
\r
237 /*-----------------------------------------------------------*/
\r
239 void prvUART_Handler( void *pvNotUsed )
\r
241 extern unsigned int XUartPs_SendBuffer( XUartPs *InstancePtr );
\r
242 uint32_t ulActiveInterrupts, ulChannelStatusRegister;
\r
243 BaseType_t xHigherPriorityTaskWoken = pdFALSE;
\r
246 configASSERT( pvNotUsed == &xUARTInstance );
\r
248 /* Remove compile warnings if configASSERT() is not defined. */
\r
249 ( void ) pvNotUsed;
\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
255 /* Are any receive events of interest active? */
\r
256 if( ( ulActiveInterrupts & serRECEIVE_INTERRUPT_MASK ) != 0 )
\r
258 /* Read the Channel Status Register to determine if there is any data in
\r
260 ulChannelStatusRegister = XUartPs_ReadReg( XPAR_PS7_UART_1_BASEADDR, XUARTPS_SR_OFFSET );
\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
266 cChar = XUartPs_ReadReg( XPAR_PS7_UART_1_BASEADDR, XUARTPS_FIFO_OFFSET );
\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
276 xQueueSendFromISR( xRxQueue, &cChar, &xHigherPriorityTaskWoken );
\r
277 ulChannelStatusRegister = XUartPs_ReadReg( XPAR_PS7_UART_1_BASEADDR, XUARTPS_SR_OFFSET );
\r
281 /* Are any transmit events of interest active? */
\r
282 if( ( ulActiveInterrupts & serTRANSMIT_IINTERRUPT_MASK ) != 0 )
\r
284 if( xUARTInstance.SendBuffer.RemainingBytes == 0 )
\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
295 xSemaphoreGiveFromISR( xTxCompleteSemaphore, &xHigherPriorityTaskWoken );
\r
297 /* No more data to transmit. */
\r
298 XUartPs_WriteReg( XPAR_PS7_UART_1_BASEADDR, XUARTPS_IDR_OFFSET, XUARTPS_IXR_TXEMPTY );
\r
302 /* More data to send. */
\r
303 XUartPs_SendBuffer( &xUARTInstance );
\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
314 /* Clear the interrupt status. */
\r
315 XUartPs_WriteReg( XPAR_PS7_UART_1_BASEADDR, XUARTPS_ISR_OFFSET, ulActiveInterrupts );
\r