2 FreeRTOS V8.0.0:rc2 - 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 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
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
154 /* Misc. parameter configuration. */
\r
155 XUartPs_SetBaudRate( &xUARTInstance, ulWantedBaud );
\r
156 XUartPs_SetOperMode( &xUARTInstance, XUARTPS_OPER_MODE_NORMAL );
\r
158 /* Install the interrupt service routine that is defined within this
\r
160 xStatus = XScuGic_Connect( &xInterruptController, XPAR_XUARTPS_1_INTR, (Xil_ExceptionHandler) prvUART_Handler, (void *) &xUARTInstance );
\r
161 configASSERT( xStatus == XST_SUCCESS );
\r
163 /* Ensure interrupts start clear. */
\r
164 XUartPs_WriteReg( XPAR_PS7_UART_1_BASEADDR, XUARTPS_ISR_OFFSET, XUARTPS_IXR_MASK );
\r
166 /* Enable the UART interrupt within the GIC. */
\r
167 XScuGic_Enable( &xInterruptController, XPAR_XUARTPS_1_INTR );
\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
172 /* Set the receive timeout. */
\r
173 XUartPs_SetRecvTimeout( &xUARTInstance, 8 );
\r
175 return ( xComPortHandle ) 0;
\r
177 /*-----------------------------------------------------------*/
\r
179 BaseType_t xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
\r
181 BaseType_t xReturn;
\r
183 /* Only a single port is supported. */
\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
192 /*-----------------------------------------------------------*/
\r
194 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
196 const TickType_t xMaxWait = 200UL / portTICK_PERIOD_MS;
\r
198 /* Only a single port is supported. */
\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
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
212 /*-----------------------------------------------------------*/
\r
214 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
\r
216 /* Only a single port is supported. */
\r
219 /* Send the character. */
\r
220 XUartPs_Send( &xUARTInstance, ( void * ) &cOutChar, sizeof( cOutChar ) );
\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
228 /*-----------------------------------------------------------*/
\r
230 void vSerialClose(xComPortHandle xPort)
\r
232 /* Not supported as not required by the demo application. */
\r
235 /*-----------------------------------------------------------*/
\r
237 void prvUART_Handler( void *pvNotUsed )
\r
239 extern unsigned int XUartPs_SendBuffer( XUartPs *InstancePtr );
\r
240 uint32_t ulActiveInterrupts, ulChannelStatusRegister;
\r
241 BaseType_t xHigherPriorityTaskWoken = pdFALSE;
\r
244 configASSERT( pvNotUsed == &xUARTInstance );
\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
250 /* Are any receive events of interest active? */
\r
251 if( ( ulActiveInterrupts & serRECEIVE_INTERRUPT_MASK ) != 0 )
\r
253 /* Read the Channel Status Register to determine if there is any data in
\r
255 ulChannelStatusRegister = XUartPs_ReadReg( XPAR_PS7_UART_1_BASEADDR, XUARTPS_SR_OFFSET );
\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
261 cChar = XUartPs_ReadReg( XPAR_PS7_UART_1_BASEADDR, XUARTPS_FIFO_OFFSET );
\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
271 xQueueSendFromISR( xRxQueue, &cChar, &xHigherPriorityTaskWoken );
\r
272 ulChannelStatusRegister = XUartPs_ReadReg( XPAR_PS7_UART_1_BASEADDR, XUARTPS_SR_OFFSET );
\r
276 /* Are any transmit events of interest active? */
\r
277 if( ( ulActiveInterrupts & serTRANSMIT_IINTERRUPT_MASK ) != 0 )
\r
279 if( xUARTInstance.SendBuffer.RemainingBytes == 0 )
\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
290 xSemaphoreGiveFromISR( xTxCompleteSemaphore, &xHigherPriorityTaskWoken );
\r
292 /* No more data to transmit. */
\r
293 XUartPs_WriteReg( XPAR_PS7_UART_1_BASEADDR, XUARTPS_IDR_OFFSET, XUARTPS_IXR_TXEMPTY );
\r
297 /* More data to send. */
\r
298 XUartPs_SendBuffer( &xUARTInstance );
\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
309 /* Clear the interrupt status. */
\r
310 XUartPs_WriteReg( XPAR_PS7_UART_1_BASEADDR, XUARTPS_ISR_OFFSET, ulActiveInterrupts );
\r