2 FreeRTOS.org V5.4.0 - Copyright (C) 2003-2009 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify it
\r
7 under the terms of the GNU General Public License (version 2) as published
\r
8 by the Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS.org without being obliged to provide
\r
11 the source code for any proprietary components. Alternative commercial
\r
12 license and support terms are also available upon request. See the
\r
13 licensing section of http://www.FreeRTOS.org for full details.
\r
15 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
20 You should have received a copy of the GNU General Public License along
\r
21 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
22 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
25 ***************************************************************************
\r
27 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
29 * This is a concise, step by step, 'hands on' guide that describes both *
\r
30 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
31 * explains numerous examples that are written using the FreeRTOS API. *
\r
32 * Full source code for all the examples is provided in an accompanying *
\r
35 ***************************************************************************
\r
39 Please ensure to read the configuration and relevant port sections of the
\r
40 online documentation.
\r
42 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
45 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
48 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
49 licensing and training services.
\r
54 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART
\r
57 /* Scheduler includes. */
\r
58 #include "FreeRTOS.h"
\r
62 /* Demo application includes. */
\r
65 /* Library includes. */
\r
66 #include "xparameters.h"
\r
67 #include "xuartlite.h"
\r
68 #include "xuartlite_l.h"
\r
70 /*-----------------------------------------------------------*/
\r
72 /* Queues used to hold received characters, and characters waiting to be
\r
74 static xQueueHandle xRxedChars;
\r
75 static xQueueHandle xCharsForTx;
\r
77 /* Structure that maintains information on the UART being used. */
\r
78 static XUartLite xUART;
\r
81 * Sample UART interrupt handler. Note this is used to demonstrate the kernel
\r
82 * features and test the port - it is not intended to represent an efficient
\r
85 static void vSerialISR( XUartLite *pxUART );
\r
87 /*-----------------------------------------------------------*/
\r
89 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
91 /* NOTE: The baud rate used by this driver is determined by the hardware
\r
92 parameterization of the UART Lite peripheral, and the baud value passed to
\r
93 this function has no effect. */
\r
94 ( void ) ulWantedBaud;
\r
96 /* Create the queues used to hold Rx and Tx characters. */
\r
97 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
98 xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
100 /* Only initialise the UART if the queues were created correctly. */
\r
101 if( ( xRxedChars != NULL ) && ( xCharsForTx != NULL ) )
\r
104 XUartLite_Initialize( &xUART, XPAR_RS232_UART_1_DEVICE_ID );
\r
105 XUartLite_ResetFifos( &xUART );
\r
106 XUartLite_DisableInterrupt( &xUART );
\r
108 if( xPortInstallInterruptHandler( XPAR_XPS_INTC_0_RS232_UART_1_INTERRUPT_INTR, ( XInterruptHandler )vSerialISR, (void *)&xUART ) == pdPASS )
\r
110 /* xPortInstallInterruptHandler() could fail if
\r
111 vPortSetupInterruptController() has not been called prior to this
\r
113 XUartLite_EnableInterrupt( &xUART );
\r
117 /* There is only one port so the handle is not used. */
\r
118 return ( xComPortHandle ) 0;
\r
120 /*-----------------------------------------------------------*/
\r
122 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )
\r
124 /* The port handle is not required as this driver only supports one UART. */
\r
127 /* Get the next character from the buffer. Return false if no characters
\r
128 are available, or arrive before xBlockTime expires. */
\r
129 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
138 /*-----------------------------------------------------------*/
\r
140 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )
\r
142 portBASE_TYPE xReturn = pdTRUE;
\r
144 /* Just to remove compiler warning. */
\r
147 portENTER_CRITICAL();
\r
149 /* If the UART FIFO is full we can block posting the new data on the
\r
151 if( XUartLite_mIsTransmitFull( XPAR_RS232_UART_1_BASEADDR ) )
\r
153 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
158 /* Otherwise, if there is data already in the queue we should add the
\r
159 new data to the back of the queue to ensure the sequencing is
\r
161 else if( uxQueueMessagesWaiting( xCharsForTx ) )
\r
163 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
168 /* If the UART FIFO is not full and there is no data already in the
\r
169 queue we can write directly to the FIFO without disrupting the
\r
173 XIo_Out32( XPAR_RS232_UART_1_BASEADDR + XUL_TX_FIFO_OFFSET, cOutChar );
\r
176 portEXIT_CRITICAL();
\r
180 /*-----------------------------------------------------------*/
\r
182 void vSerialClose( xComPortHandle xPort )
\r
184 /* Not supported as not required by the demo application. */
\r
187 /*-----------------------------------------------------------*/
\r
189 static void vSerialISR( XUartLite *pxUART )
\r
191 unsigned portLONG ulISRStatus;
\r
192 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE, lDidSomething;
\r
195 /* Just to remove compiler warning. */
\r
200 lDidSomething = pdFALSE;
\r
202 ulISRStatus = XIo_In32( XPAR_RS232_UART_1_BASEADDR + XUL_STATUS_REG_OFFSET );
\r
204 if( ( ulISRStatus & XUL_SR_RX_FIFO_VALID_DATA ) != 0 )
\r
206 /* A character is available - place it in the queue of received
\r
207 characters. This might wake a task that was blocked waiting for
\r
209 cChar = ( portCHAR ) XIo_In32( XPAR_RS232_UART_1_BASEADDR + XUL_RX_FIFO_OFFSET );
\r
210 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
211 lDidSomething = pdTRUE;
\r
214 if( ( ulISRStatus & XUL_SR_TX_FIFO_EMPTY ) != 0 )
\r
216 /* There is space in the FIFO - if there are any characters queue for
\r
217 transmission they can be sent to the UART now. This might unblock a
\r
218 task that was waiting for space to become available on the Tx queue. */
\r
219 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
221 XIo_Out32( XPAR_RS232_UART_1_BASEADDR + XUL_TX_FIFO_OFFSET, cChar );
\r
222 lDidSomething = pdTRUE;
\r
225 } while( lDidSomething == pdTRUE );
\r
227 /* If we woke any tasks we may require a context switch. */
\r
228 if( xHigherPriorityTaskWoken )
\r
230 portYIELD_FROM_ISR();
\r