2 FreeRTOS V7.0.0 - Copyright (C) 2011 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 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
56 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART
\r
59 /* Scheduler includes. */
\r
60 #include "FreeRTOS.h"
\r
64 /* Demo application includes. */
\r
67 /* Library includes. */
\r
68 #include "xparameters.h"
\r
69 #include "xuartlite.h"
\r
70 #include "xuartlite_l.h"
\r
72 /*-----------------------------------------------------------*/
\r
74 /* Queues used to hold received characters, and characters waiting to be
\r
76 static xQueueHandle xRxedChars;
\r
77 static xQueueHandle xCharsForTx;
\r
79 /* Structure that maintains information on the UART being used. */
\r
80 static XUartLite xUART;
\r
83 * Sample UART interrupt handler. Note this is used to demonstrate the kernel
\r
84 * features and test the port - it is not intended to represent an efficient
\r
87 static void vSerialISR( XUartLite *pxUART );
\r
89 /*-----------------------------------------------------------*/
\r
91 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
93 /* NOTE: The baud rate used by this driver is determined by the hardware
\r
94 parameterization of the UART Lite peripheral, and the baud value passed to
\r
95 this function has no effect. */
\r
96 ( void ) ulWantedBaud;
\r
98 /* Create the queues used to hold Rx and Tx characters. */
\r
99 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
100 xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
102 /* Only initialise the UART if the queues were created correctly. */
\r
103 if( ( xRxedChars != NULL ) && ( xCharsForTx != NULL ) )
\r
106 XUartLite_Initialize( &xUART, XPAR_RS232_UART_1_DEVICE_ID );
\r
107 XUartLite_ResetFifos( &xUART );
\r
108 XUartLite_DisableInterrupt( &xUART );
\r
110 if( xPortInstallInterruptHandler( XPAR_XPS_INTC_0_RS232_UART_1_INTERRUPT_INTR, ( XInterruptHandler )vSerialISR, (void *)&xUART ) == pdPASS )
\r
112 /* xPortInstallInterruptHandler() could fail if
\r
113 vPortSetupInterruptController() has not been called prior to this
\r
115 XUartLite_EnableInterrupt( &xUART );
\r
119 /* There is only one port so the handle is not used. */
\r
120 return ( xComPortHandle ) 0;
\r
122 /*-----------------------------------------------------------*/
\r
124 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
\r
126 /* The port handle is not required as this driver only supports one UART. */
\r
129 /* Get the next character from the buffer. Return false if no characters
\r
130 are available, or arrive before xBlockTime expires. */
\r
131 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
140 /*-----------------------------------------------------------*/
\r
142 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
\r
144 portBASE_TYPE xReturn = pdTRUE;
\r
146 /* Just to remove compiler warning. */
\r
149 portENTER_CRITICAL();
\r
151 /* If the UART FIFO is full we can block posting the new data on the
\r
153 if( XUartLite_mIsTransmitFull( XPAR_RS232_UART_1_BASEADDR ) )
\r
155 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
160 /* Otherwise, if there is data already in the queue we should add the
\r
161 new data to the back of the queue to ensure the sequencing is
\r
163 else if( uxQueueMessagesWaiting( xCharsForTx ) )
\r
165 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
170 /* If the UART FIFO is not full and there is no data already in the
\r
171 queue we can write directly to the FIFO without disrupting the
\r
175 XIo_Out32( XPAR_RS232_UART_1_BASEADDR + XUL_TX_FIFO_OFFSET, cOutChar );
\r
178 portEXIT_CRITICAL();
\r
182 /*-----------------------------------------------------------*/
\r
184 void vSerialClose( xComPortHandle xPort )
\r
186 /* Not supported as not required by the demo application. */
\r
189 /*-----------------------------------------------------------*/
\r
191 static void vSerialISR( XUartLite *pxUART )
\r
193 unsigned long ulISRStatus;
\r
194 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE, lDidSomething;
\r
197 /* Just to remove compiler warning. */
\r
202 lDidSomething = pdFALSE;
\r
204 ulISRStatus = XIo_In32( XPAR_RS232_UART_1_BASEADDR + XUL_STATUS_REG_OFFSET );
\r
206 if( ( ulISRStatus & XUL_SR_RX_FIFO_VALID_DATA ) != 0 )
\r
208 /* A character is available - place it in the queue of received
\r
209 characters. This might wake a task that was blocked waiting for
\r
211 cChar = ( char ) XIo_In32( XPAR_RS232_UART_1_BASEADDR + XUL_RX_FIFO_OFFSET );
\r
212 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
213 lDidSomething = pdTRUE;
\r
216 if( ( ulISRStatus & XUL_SR_TX_FIFO_EMPTY ) != 0 )
\r
218 /* There is space in the FIFO - if there are any characters queue for
\r
219 transmission they can be sent to the UART now. This might unblock a
\r
220 task that was waiting for space to become available on the Tx queue. */
\r
221 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
223 XIo_Out32( XPAR_RS232_UART_1_BASEADDR + XUL_TX_FIFO_OFFSET, cChar );
\r
224 lDidSomething = pdTRUE;
\r
227 } while( lDidSomething == pdTRUE );
\r
229 /* If we woke any tasks we may require a context switch. */
\r
230 if( xHigherPriorityTaskWoken )
\r
232 portYIELD_FROM_ISR();
\r