2 * FreeRTOS Kernel V10.1.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
30 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART
\r
33 /* Scheduler includes. */
\r
34 #include "FreeRTOS.h"
\r
38 /* Demo application includes. */
\r
41 /* Library includes. */
\r
42 #include "xparameters.h"
\r
43 #include "xuartlite.h"
\r
44 #include "xuartlite_l.h"
\r
46 /*-----------------------------------------------------------*/
\r
48 /* Queues used to hold received characters, and characters waiting to be
\r
50 static QueueHandle_t xRxedChars;
\r
51 static QueueHandle_t xCharsForTx;
\r
53 /* Structure that maintains information on the UART being used. */
\r
54 static XUartLite xUART;
\r
57 * Sample UART interrupt handler. Note this is used to demonstrate the kernel
\r
58 * features and test the port - it is not intended to represent an efficient
\r
61 static void vSerialISR( XUartLite *pxUART );
\r
63 /*-----------------------------------------------------------*/
\r
65 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
67 /* NOTE: The baud rate used by this driver is determined by the hardware
\r
68 parameterization of the UART Lite peripheral, and the baud value passed to
\r
69 this function has no effect. */
\r
70 ( void ) ulWantedBaud;
\r
72 /* Create the queues used to hold Rx and Tx characters. */
\r
73 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
74 xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
76 /* Only initialise the UART if the queues were created correctly. */
\r
77 if( ( xRxedChars != NULL ) && ( xCharsForTx != NULL ) )
\r
80 XUartLite_Initialize( &xUART, XPAR_RS232_UART_1_DEVICE_ID );
\r
81 XUartLite_ResetFifos( &xUART );
\r
82 XUartLite_DisableInterrupt( &xUART );
\r
84 if( xPortInstallInterruptHandler( XPAR_XPS_INTC_0_RS232_UART_1_INTERRUPT_INTR, ( XInterruptHandler )vSerialISR, (void *)&xUART ) == pdPASS )
\r
86 /* xPortInstallInterruptHandler() could fail if
\r
87 vPortSetupInterruptController() has not been called prior to this
\r
89 XUartLite_EnableInterrupt( &xUART );
\r
93 /* There is only one port so the handle is not used. */
\r
94 return ( xComPortHandle ) 0;
\r
96 /*-----------------------------------------------------------*/
\r
98 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
\r
100 /* The port handle is not required as this driver only supports one UART. */
\r
103 /* Get the next character from the buffer. Return false if no characters
\r
104 are available, or arrive before xBlockTime expires. */
\r
105 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
114 /*-----------------------------------------------------------*/
\r
116 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
\r
118 portBASE_TYPE xReturn = pdTRUE;
\r
120 /* Just to remove compiler warning. */
\r
123 portENTER_CRITICAL();
\r
125 /* If the UART FIFO is full we can block posting the new data on the
\r
127 if( XUartLite_mIsTransmitFull( XPAR_RS232_UART_1_BASEADDR ) )
\r
129 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
134 /* Otherwise, if there is data already in the queue we should add the
\r
135 new data to the back of the queue to ensure the sequencing is
\r
137 else if( uxQueueMessagesWaiting( xCharsForTx ) )
\r
139 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
144 /* If the UART FIFO is not full and there is no data already in the
\r
145 queue we can write directly to the FIFO without disrupting the
\r
149 XIo_Out32( XPAR_RS232_UART_1_BASEADDR + XUL_TX_FIFO_OFFSET, cOutChar );
\r
152 portEXIT_CRITICAL();
\r
156 /*-----------------------------------------------------------*/
\r
158 void vSerialClose( xComPortHandle xPort )
\r
160 /* Not supported as not required by the demo application. */
\r
163 /*-----------------------------------------------------------*/
\r
165 static void vSerialISR( XUartLite *pxUART )
\r
167 unsigned long ulISRStatus;
\r
168 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE, lDidSomething;
\r
171 /* Just to remove compiler warning. */
\r
176 lDidSomething = pdFALSE;
\r
178 ulISRStatus = XIo_In32( XPAR_RS232_UART_1_BASEADDR + XUL_STATUS_REG_OFFSET );
\r
180 if( ( ulISRStatus & XUL_SR_RX_FIFO_VALID_DATA ) != 0 )
\r
182 /* A character is available - place it in the queue of received
\r
183 characters. This might wake a task that was blocked waiting for
\r
185 cChar = ( char ) XIo_In32( XPAR_RS232_UART_1_BASEADDR + XUL_RX_FIFO_OFFSET );
\r
186 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
187 lDidSomething = pdTRUE;
\r
190 if( ( ulISRStatus & XUL_SR_TX_FIFO_EMPTY ) != 0 )
\r
192 /* There is space in the FIFO - if there are any characters queue for
\r
193 transmission they can be sent to the UART now. This might unblock a
\r
194 task that was waiting for space to become available on the Tx queue. */
\r
195 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
197 XIo_Out32( XPAR_RS232_UART_1_BASEADDR + XUL_TX_FIFO_OFFSET, cChar );
\r
198 lDidSomething = pdTRUE;
\r
201 } while( lDidSomething == pdTRUE );
\r
203 /* If we woke any tasks we may require a context switch. */
\r
204 if( xHigherPriorityTaskWoken )
\r
206 portYIELD_FROM_ISR();
\r