2 FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
69 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART
\r
72 /* Scheduler includes. */
\r
73 #include "FreeRTOS.h"
\r
77 /* Demo application includes. */
\r
80 /* Library includes. */
\r
81 #include "xparameters.h"
\r
82 #include "xuartlite.h"
\r
83 #include "xuartlite_l.h"
\r
85 /*-----------------------------------------------------------*/
\r
87 /* Queues used to hold received characters, and characters waiting to be
\r
89 static xQueueHandle xRxedChars;
\r
90 static xQueueHandle xCharsForTx;
\r
92 /* Structure that maintains information on the UART being used. */
\r
93 static XUartLite xUART;
\r
96 * Sample UART interrupt handler. Note this is used to demonstrate the kernel
\r
97 * features and test the port - it is not intended to represent an efficient
\r
100 static void vSerialISR( XUartLite *pxUART );
\r
102 /*-----------------------------------------------------------*/
\r
104 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
106 /* NOTE: The baud rate used by this driver is determined by the hardware
\r
107 parameterization of the UART Lite peripheral, and the baud value passed to
\r
108 this function has no effect. */
\r
109 ( void ) ulWantedBaud;
\r
111 /* Create the queues used to hold Rx and Tx characters. */
\r
112 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
113 xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
115 /* Only initialise the UART if the queues were created correctly. */
\r
116 if( ( xRxedChars != NULL ) && ( xCharsForTx != NULL ) )
\r
119 XUartLite_Initialize( &xUART, XPAR_RS232_UART_DEVICE_ID );
\r
120 XUartLite_ResetFifos( &xUART );
\r
121 XUartLite_DisableInterrupt( &xUART );
\r
123 if( xPortInstallInterruptHandler( XPAR_XPS_INTC_0_RS232_UART_INTERRUPT_INTR, ( XInterruptHandler )vSerialISR, (void *)&xUART ) == pdPASS )
\r
125 /* xPortInstallInterruptHandler() could fail if
\r
126 vPortSetupInterruptController() has not been called prior to this
\r
128 XUartLite_EnableInterrupt( &xUART );
\r
132 /* There is only one port so the handle is not used. */
\r
133 return ( xComPortHandle ) 0;
\r
135 /*-----------------------------------------------------------*/
\r
137 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )
\r
139 /* The port handle is not required as this driver only supports one UART. */
\r
142 /* Get the next character from the buffer. Return false if no characters
\r
143 are available, or arrive before xBlockTime expires. */
\r
144 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
153 /*-----------------------------------------------------------*/
\r
155 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )
\r
157 portBASE_TYPE xReturn = pdTRUE;
\r
159 /* Just to remove compiler warning. */
\r
162 portENTER_CRITICAL();
\r
164 /* If the UART FIFO is full we can block posting the new data on the
\r
166 if( XUartLite_mIsTransmitFull( XPAR_RS232_UART_BASEADDR ) )
\r
168 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
173 /* Otherwise, if there is data already in the queue we should add the
\r
174 new data to the back of the queue to ensure the sequencing is
\r
176 else if( uxQueueMessagesWaiting( xCharsForTx ) )
\r
178 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
183 /* If the UART FIFO is not full and there is no data already in the
\r
184 queue we can write directly to the FIFO without disrupting the
\r
188 XIo_Out32( XPAR_RS232_UART_BASEADDR + XUL_TX_FIFO_OFFSET, cOutChar );
\r
191 portEXIT_CRITICAL();
\r
195 /*-----------------------------------------------------------*/
\r
197 void vSerialClose( xComPortHandle xPort )
\r
199 /* Not supported as not required by the demo application. */
\r
202 /*-----------------------------------------------------------*/
\r
204 static void vSerialISR( XUartLite *pxUART )
\r
206 unsigned portLONG ulISRStatus;
\r
207 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE, lDidSomething;
\r
210 /* Just to remove compiler warning. */
\r
215 lDidSomething = pdFALSE;
\r
217 ulISRStatus = XIo_In32( XPAR_RS232_UART_BASEADDR + XUL_STATUS_REG_OFFSET );
\r
219 if( ( ulISRStatus & XUL_SR_RX_FIFO_VALID_DATA ) != 0 )
\r
221 /* A character is available - place it in the queue of received
\r
222 characters. This might wake a task that was blocked waiting for
\r
224 cChar = ( portCHAR ) XIo_In32( XPAR_RS232_UART_BASEADDR + XUL_RX_FIFO_OFFSET );
\r
225 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
226 lDidSomething = pdTRUE;
\r
229 if( ( ulISRStatus & XUL_SR_TX_FIFO_EMPTY ) != 0 )
\r
231 /* There is space in the FIFO - if there are any characters queue for
\r
232 transmission they can be sent to the UART now. This might unblock a
\r
233 task that was waiting for space to become available on the Tx queue. */
\r
234 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
236 XIo_Out32( XPAR_RS232_UART_BASEADDR + XUL_TX_FIFO_OFFSET, cChar );
\r
237 lDidSomething = pdTRUE;
\r
240 } while( lDidSomething == pdTRUE );
\r
242 /* If we woke any tasks we may require a context switch. */
\r
243 if( xHigherPriorityTaskWoken )
\r
245 portYIELD_FROM_ISR();
\r