2 FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
32 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
33 distribute a combined work that includes FreeRTOS without being obliged to
\r
34 provide the source code for proprietary components outside of the FreeRTOS
\r
35 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
36 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
37 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
38 more details. You should have received a copy of the GNU General Public
\r
39 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
40 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
41 by writing to Richard Barry, contact details for whom are available on the
\r
46 ***************************************************************************
\r
48 * Having a problem? Start by reading the FAQ "My application does *
\r
49 * not run, what could be wrong?" *
\r
51 * http://www.FreeRTOS.org/FAQHelp.html *
\r
53 ***************************************************************************
\r
56 http://www.FreeRTOS.org - Documentation, training, latest versions, license
\r
57 and contact details.
\r
59 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
60 including FreeRTOS+Trace - an indispensable productivity tool.
\r
62 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
63 the code with commercial support, indemnification, and middleware, under
\r
64 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
65 provide a safety engineered and independently SIL3 certified version under
\r
66 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
71 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART
\r
74 /* Scheduler includes. */
\r
75 #include "FreeRTOS.h"
\r
79 /* Demo application includes. */
\r
82 /* Microblaze driver includes. */
\r
83 #include "xuartlite_l.h"
\r
84 #include "xintc_l.h"
\r
86 /*-----------------------------------------------------------*/
\r
88 /* Queues used to hold received characters, and characters waiting to be
\r
90 static xQueueHandle xRxedChars;
\r
91 static xQueueHandle xCharsForTx;
\r
93 /*-----------------------------------------------------------*/
\r
95 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
97 unsigned long ulControlReg, ulMask;
\r
99 /* NOTE: The baud rate used by this driver is determined by the hardware
\r
100 parameterization of the UART Lite peripheral, and the baud value passed to
\r
101 this function has no effect. */
\r
103 /* Create the queues used to hold Rx and Tx characters. */
\r
104 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
105 xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
107 if( ( xRxedChars ) && ( xCharsForTx ) )
\r
109 /* Disable the interrupt. */
\r
110 XUartLite_mDisableIntr( XPAR_RS232_UART_BASEADDR );
\r
112 /* Flush the fifos. */
\r
113 ulControlReg = XIo_In32( XPAR_RS232_UART_BASEADDR + XUL_STATUS_REG_OFFSET );
\r
114 XIo_Out32( XPAR_RS232_UART_BASEADDR + XUL_CONTROL_REG_OFFSET, ulControlReg | XUL_CR_FIFO_TX_RESET | XUL_CR_FIFO_RX_RESET );
\r
116 /* Enable the interrupt again. The interrupt controller has not yet been
\r
117 initialised so there is no chance of receiving an interrupt until the
\r
118 scheduler has been started. */
\r
119 XUartLite_mEnableIntr( XPAR_RS232_UART_BASEADDR );
\r
121 /* Enable the interrupt in the interrupt controller while maintaining
\r
122 all the other bit settings. */
\r
123 ulMask = XIntc_In32( ( XPAR_OPB_INTC_0_BASEADDR + XIN_IER_OFFSET ) );
\r
124 ulMask |= XPAR_RS232_UART_INTERRUPT_MASK;
\r
125 XIntc_Out32( ( XPAR_OPB_INTC_0_BASEADDR + XIN_IER_OFFSET ), ( ulMask ) );
\r
126 XIntc_mAckIntr( XPAR_INTC_SINGLE_BASEADDR, 2 );
\r
129 return ( xComPortHandle ) 0;
\r
131 /*-----------------------------------------------------------*/
\r
133 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
\r
135 /* The port handle is not required as this driver only supports one UART. */
\r
138 /* Get the next character from the buffer. Return false if no characters
\r
139 are available, or arrive before xBlockTime expires. */
\r
140 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
149 /*-----------------------------------------------------------*/
\r
151 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
\r
153 portBASE_TYPE xReturn = pdTRUE;
\r
155 portENTER_CRITICAL();
\r
157 /* If the UART FIFO is full we can block posting the new data on the
\r
159 if( XUartLite_mIsTransmitFull( XPAR_RS232_UART_BASEADDR ) )
\r
161 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
166 /* Otherwise, if there is data already in the queue we should add the
\r
167 new data to the back of the queue to ensure the sequencing is
\r
169 else if( uxQueueMessagesWaiting( xCharsForTx ) )
\r
171 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
176 /* If the UART FIFO is not full and there is no data already in the
\r
177 queue we can write directly to the FIFO without disrupting the
\r
181 XIo_Out32( XPAR_RS232_UART_BASEADDR + XUL_TX_FIFO_OFFSET, cOutChar );
\r
184 portEXIT_CRITICAL();
\r
188 /*-----------------------------------------------------------*/
\r
190 void vSerialClose( xComPortHandle xPort )
\r
192 /* Not supported as not required by the demo application. */
\r
195 /*-----------------------------------------------------------*/
\r
197 void vSerialISR( void *pvBaseAddress )
\r
199 unsigned long ulISRStatus;
\r
200 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
203 /* Determine the cause of the interrupt. */
\r
204 ulISRStatus = XIo_In32( XPAR_RS232_UART_BASEADDR + XUL_STATUS_REG_OFFSET );
\r
206 if( ( ulISRStatus & ( XUL_SR_RX_FIFO_FULL | 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_BASEADDR + XUL_RX_FIFO_OFFSET );
\r
212 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
215 if( ( ulISRStatus & XUL_SR_TX_FIFO_EMPTY ) != 0 )
\r
217 /* There is space in the FIFO - if there are any characters queue for
\r
218 transmission they can be send to the UART now. This might unblock a
\r
219 task that was waiting for space to become available on the Tx queue. */
\r
220 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
222 XIo_Out32( XPAR_RS232_UART_BASEADDR + XUL_TX_FIFO_OFFSET, cChar );
\r
226 /* If we woke any tasks we may require a context switch. */
\r
227 if( xHigherPriorityTaskWoken )
\r
229 portYIELD_FROM_ISR();
\r