2 FreeRTOS V8.2.0 - Copyright (C) 2015 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
72 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART
\r
75 /* Scheduler includes. */
\r
76 #include "FreeRTOS.h"
\r
80 /* Demo application includes. */
\r
83 /* Library includes. */
\r
84 #include "xparameters.h"
\r
85 #include "xuartlite.h"
\r
86 #include "xuartlite_l.h"
\r
88 /*-----------------------------------------------------------*/
\r
90 /* Queues used to hold received characters, and characters waiting to be
\r
92 static QueueHandle_t xRxedChars;
\r
93 static QueueHandle_t xCharsForTx;
\r
95 /* Structure that maintains information on the UART being used. */
\r
96 static XUartLite xUART;
\r
99 * Sample UART interrupt handler. Note this is used to demonstrate the kernel
\r
100 * features and test the port - it is not intended to represent an efficient
\r
103 static void vSerialISR( XUartLite *pxUART );
\r
105 /*-----------------------------------------------------------*/
\r
107 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
109 /* NOTE: The baud rate used by this driver is determined by the hardware
\r
110 parameterization of the UART Lite peripheral, and the baud value passed to
\r
111 this function has no effect. */
\r
112 ( void ) ulWantedBaud;
\r
114 /* Create the queues used to hold Rx and Tx characters. */
\r
115 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
116 xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
118 /* Only initialise the UART if the queues were created correctly. */
\r
119 if( ( xRxedChars != NULL ) && ( xCharsForTx != NULL ) )
\r
122 XUartLite_Initialize( &xUART, XPAR_RS232_UART_1_DEVICE_ID );
\r
123 XUartLite_ResetFifos( &xUART );
\r
124 XUartLite_DisableInterrupt( &xUART );
\r
126 if( xPortInstallInterruptHandler( XPAR_XPS_INTC_0_RS232_UART_1_INTERRUPT_INTR, ( XInterruptHandler )vSerialISR, (void *)&xUART ) == pdPASS )
\r
128 /* xPortInstallInterruptHandler() could fail if
\r
129 vPortSetupInterruptController() has not been called prior to this
\r
131 XUartLite_EnableInterrupt( &xUART );
\r
135 /* There is only one port so the handle is not used. */
\r
136 return ( xComPortHandle ) 0;
\r
138 /*-----------------------------------------------------------*/
\r
140 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
\r
142 /* The port handle is not required as this driver only supports one UART. */
\r
145 /* Get the next character from the buffer. Return false if no characters
\r
146 are available, or arrive before xBlockTime expires. */
\r
147 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
156 /*-----------------------------------------------------------*/
\r
158 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
\r
160 portBASE_TYPE xReturn = pdTRUE;
\r
162 /* Just to remove compiler warning. */
\r
165 portENTER_CRITICAL();
\r
167 /* If the UART FIFO is full we can block posting the new data on the
\r
169 if( XUartLite_mIsTransmitFull( XPAR_RS232_UART_1_BASEADDR ) )
\r
171 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
176 /* Otherwise, if there is data already in the queue we should add the
\r
177 new data to the back of the queue to ensure the sequencing is
\r
179 else if( uxQueueMessagesWaiting( xCharsForTx ) )
\r
181 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
186 /* If the UART FIFO is not full and there is no data already in the
\r
187 queue we can write directly to the FIFO without disrupting the
\r
191 XIo_Out32( XPAR_RS232_UART_1_BASEADDR + XUL_TX_FIFO_OFFSET, cOutChar );
\r
194 portEXIT_CRITICAL();
\r
198 /*-----------------------------------------------------------*/
\r
200 void vSerialClose( xComPortHandle xPort )
\r
202 /* Not supported as not required by the demo application. */
\r
205 /*-----------------------------------------------------------*/
\r
207 static void vSerialISR( XUartLite *pxUART )
\r
209 unsigned long ulISRStatus;
\r
210 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE, lDidSomething;
\r
213 /* Just to remove compiler warning. */
\r
218 lDidSomething = pdFALSE;
\r
220 ulISRStatus = XIo_In32( XPAR_RS232_UART_1_BASEADDR + XUL_STATUS_REG_OFFSET );
\r
222 if( ( ulISRStatus & XUL_SR_RX_FIFO_VALID_DATA ) != 0 )
\r
224 /* A character is available - place it in the queue of received
\r
225 characters. This might wake a task that was blocked waiting for
\r
227 cChar = ( char ) XIo_In32( XPAR_RS232_UART_1_BASEADDR + XUL_RX_FIFO_OFFSET );
\r
228 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
229 lDidSomething = pdTRUE;
\r
232 if( ( ulISRStatus & XUL_SR_TX_FIFO_EMPTY ) != 0 )
\r
234 /* There is space in the FIFO - if there are any characters queue for
\r
235 transmission they can be sent to the UART now. This might unblock a
\r
236 task that was waiting for space to become available on the Tx queue. */
\r
237 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
239 XIo_Out32( XPAR_RS232_UART_1_BASEADDR + XUL_TX_FIFO_OFFSET, cChar );
\r
240 lDidSomething = pdTRUE;
\r
243 } while( lDidSomething == pdTRUE );
\r
245 /* If we woke any tasks we may require a context switch. */
\r
246 if( xHigherPriorityTaskWoken )
\r
248 portYIELD_FROM_ISR();
\r