1 |
586 |
jeremybenn |
/*
|
2 |
|
|
FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
|
3 |
|
|
|
4 |
|
|
***************************************************************************
|
5 |
|
|
* *
|
6 |
|
|
* If you are: *
|
7 |
|
|
* *
|
8 |
|
|
* + New to FreeRTOS, *
|
9 |
|
|
* + Wanting to learn FreeRTOS or multitasking in general quickly *
|
10 |
|
|
* + Looking for basic training, *
|
11 |
|
|
* + Wanting to improve your FreeRTOS skills and productivity *
|
12 |
|
|
* *
|
13 |
|
|
* then take a look at the FreeRTOS books - available as PDF or paperback *
|
14 |
|
|
* *
|
15 |
|
|
* "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
|
16 |
|
|
* http://www.FreeRTOS.org/Documentation *
|
17 |
|
|
* *
|
18 |
|
|
* A pdf reference manual is also available. Both are usually delivered *
|
19 |
|
|
* to your inbox within 20 minutes to two hours when purchased between 8am *
|
20 |
|
|
* and 8pm GMT (although please allow up to 24 hours in case of *
|
21 |
|
|
* exceptional circumstances). Thank you for your support! *
|
22 |
|
|
* *
|
23 |
|
|
***************************************************************************
|
24 |
|
|
|
25 |
|
|
This file is part of the FreeRTOS distribution.
|
26 |
|
|
|
27 |
|
|
FreeRTOS is free software; you can redistribute it and/or modify it under
|
28 |
|
|
the terms of the GNU General Public License (version 2) as published by the
|
29 |
|
|
Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
|
30 |
|
|
***NOTE*** The exception to the GPL is included to allow you to distribute
|
31 |
|
|
a combined work that includes FreeRTOS without being obliged to provide the
|
32 |
|
|
source code for proprietary components outside of the FreeRTOS kernel.
|
33 |
|
|
FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
|
34 |
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
35 |
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
36 |
|
|
more details. You should have received a copy of the GNU General Public
|
37 |
|
|
License and the FreeRTOS license exception along with FreeRTOS; if not it
|
38 |
|
|
can be viewed here: http://www.freertos.org/a00114.html and also obtained
|
39 |
|
|
by writing to Richard Barry, contact details for whom are available on the
|
40 |
|
|
FreeRTOS WEB site.
|
41 |
|
|
|
42 |
|
|
1 tab == 4 spaces!
|
43 |
|
|
|
44 |
|
|
http://www.FreeRTOS.org - Documentation, latest information, license and
|
45 |
|
|
contact details.
|
46 |
|
|
|
47 |
|
|
http://www.SafeRTOS.com - A version that is certified for use in safety
|
48 |
|
|
critical systems.
|
49 |
|
|
|
50 |
|
|
http://www.OpenRTOS.com - Commercial support, development, porting,
|
51 |
|
|
licensing and training services.
|
52 |
|
|
*/
|
53 |
|
|
|
54 |
|
|
|
55 |
|
|
/*
|
56 |
|
|
BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART
|
57 |
|
|
*/
|
58 |
|
|
|
59 |
|
|
/* Scheduler includes. */
|
60 |
|
|
#include "FreeRTOS.h"
|
61 |
|
|
#include "queue.h"
|
62 |
|
|
#include "task.h"
|
63 |
|
|
|
64 |
|
|
/* Demo application includes. */
|
65 |
|
|
#include "serial.h"
|
66 |
|
|
|
67 |
|
|
/* Library includes. */
|
68 |
|
|
#include "xparameters.h"
|
69 |
|
|
#include "xuartlite.h"
|
70 |
|
|
#include "xuartlite_l.h"
|
71 |
|
|
|
72 |
|
|
/*-----------------------------------------------------------*/
|
73 |
|
|
|
74 |
|
|
/* Queues used to hold received characters, and characters waiting to be
|
75 |
|
|
transmitted. */
|
76 |
|
|
static xQueueHandle xRxedChars;
|
77 |
|
|
static xQueueHandle xCharsForTx;
|
78 |
|
|
|
79 |
|
|
/* Structure that maintains information on the UART being used. */
|
80 |
|
|
static XUartLite xUART;
|
81 |
|
|
|
82 |
|
|
/*
|
83 |
|
|
* Sample UART interrupt handler. Note this is used to demonstrate the kernel
|
84 |
|
|
* features and test the port - it is not intended to represent an efficient
|
85 |
|
|
* implementation.
|
86 |
|
|
*/
|
87 |
|
|
static void vSerialISR( XUartLite *pxUART );
|
88 |
|
|
|
89 |
|
|
/*-----------------------------------------------------------*/
|
90 |
|
|
|
91 |
|
|
xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
|
92 |
|
|
{
|
93 |
|
|
/* NOTE: The baud rate used by this driver is determined by the hardware
|
94 |
|
|
parameterization of the UART Lite peripheral, and the baud value passed to
|
95 |
|
|
this function has no effect. */
|
96 |
|
|
( void ) ulWantedBaud;
|
97 |
|
|
|
98 |
|
|
/* Create the queues used to hold Rx and Tx characters. */
|
99 |
|
|
xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
|
100 |
|
|
xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
|
101 |
|
|
|
102 |
|
|
/* Only initialise the UART if the queues were created correctly. */
|
103 |
|
|
if( ( xRxedChars != NULL ) && ( xCharsForTx != NULL ) )
|
104 |
|
|
{
|
105 |
|
|
|
106 |
|
|
XUartLite_Initialize( &xUART, XPAR_RS232_UART_1_DEVICE_ID );
|
107 |
|
|
XUartLite_ResetFifos( &xUART );
|
108 |
|
|
XUartLite_DisableInterrupt( &xUART );
|
109 |
|
|
|
110 |
|
|
if( xPortInstallInterruptHandler( XPAR_XPS_INTC_0_RS232_UART_1_INTERRUPT_INTR, ( XInterruptHandler )vSerialISR, (void *)&xUART ) == pdPASS )
|
111 |
|
|
{
|
112 |
|
|
/* xPortInstallInterruptHandler() could fail if
|
113 |
|
|
vPortSetupInterruptController() has not been called prior to this
|
114 |
|
|
function. */
|
115 |
|
|
XUartLite_EnableInterrupt( &xUART );
|
116 |
|
|
}
|
117 |
|
|
}
|
118 |
|
|
|
119 |
|
|
/* There is only one port so the handle is not used. */
|
120 |
|
|
return ( xComPortHandle ) 0;
|
121 |
|
|
}
|
122 |
|
|
/*-----------------------------------------------------------*/
|
123 |
|
|
|
124 |
|
|
signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
|
125 |
|
|
{
|
126 |
|
|
/* The port handle is not required as this driver only supports one UART. */
|
127 |
|
|
( void ) pxPort;
|
128 |
|
|
|
129 |
|
|
/* Get the next character from the buffer. Return false if no characters
|
130 |
|
|
are available, or arrive before xBlockTime expires. */
|
131 |
|
|
if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
|
132 |
|
|
{
|
133 |
|
|
return pdTRUE;
|
134 |
|
|
}
|
135 |
|
|
else
|
136 |
|
|
{
|
137 |
|
|
return pdFALSE;
|
138 |
|
|
}
|
139 |
|
|
}
|
140 |
|
|
/*-----------------------------------------------------------*/
|
141 |
|
|
|
142 |
|
|
signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
|
143 |
|
|
{
|
144 |
|
|
portBASE_TYPE xReturn = pdTRUE;
|
145 |
|
|
|
146 |
|
|
/* Just to remove compiler warning. */
|
147 |
|
|
( void ) pxPort;
|
148 |
|
|
|
149 |
|
|
portENTER_CRITICAL();
|
150 |
|
|
{
|
151 |
|
|
/* If the UART FIFO is full we can block posting the new data on the
|
152 |
|
|
Tx queue. */
|
153 |
|
|
if( XUartLite_mIsTransmitFull( XPAR_RS232_UART_1_BASEADDR ) )
|
154 |
|
|
{
|
155 |
|
|
if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
|
156 |
|
|
{
|
157 |
|
|
xReturn = pdFAIL;
|
158 |
|
|
}
|
159 |
|
|
}
|
160 |
|
|
/* Otherwise, if there is data already in the queue we should add the
|
161 |
|
|
new data to the back of the queue to ensure the sequencing is
|
162 |
|
|
maintained. */
|
163 |
|
|
else if( uxQueueMessagesWaiting( xCharsForTx ) )
|
164 |
|
|
{
|
165 |
|
|
if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
|
166 |
|
|
{
|
167 |
|
|
xReturn = pdFAIL;
|
168 |
|
|
}
|
169 |
|
|
}
|
170 |
|
|
/* If the UART FIFO is not full and there is no data already in the
|
171 |
|
|
queue we can write directly to the FIFO without disrupting the
|
172 |
|
|
sequence. */
|
173 |
|
|
else
|
174 |
|
|
{
|
175 |
|
|
XIo_Out32( XPAR_RS232_UART_1_BASEADDR + XUL_TX_FIFO_OFFSET, cOutChar );
|
176 |
|
|
}
|
177 |
|
|
}
|
178 |
|
|
portEXIT_CRITICAL();
|
179 |
|
|
|
180 |
|
|
return xReturn;
|
181 |
|
|
}
|
182 |
|
|
/*-----------------------------------------------------------*/
|
183 |
|
|
|
184 |
|
|
void vSerialClose( xComPortHandle xPort )
|
185 |
|
|
{
|
186 |
|
|
/* Not supported as not required by the demo application. */
|
187 |
|
|
( void ) xPort;
|
188 |
|
|
}
|
189 |
|
|
/*-----------------------------------------------------------*/
|
190 |
|
|
|
191 |
|
|
static void vSerialISR( XUartLite *pxUART )
|
192 |
|
|
{
|
193 |
|
|
unsigned long ulISRStatus;
|
194 |
|
|
portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE, lDidSomething;
|
195 |
|
|
char cChar;
|
196 |
|
|
|
197 |
|
|
/* Just to remove compiler warning. */
|
198 |
|
|
( void ) pxUART;
|
199 |
|
|
|
200 |
|
|
do
|
201 |
|
|
{
|
202 |
|
|
lDidSomething = pdFALSE;
|
203 |
|
|
|
204 |
|
|
ulISRStatus = XIo_In32( XPAR_RS232_UART_1_BASEADDR + XUL_STATUS_REG_OFFSET );
|
205 |
|
|
|
206 |
|
|
if( ( ulISRStatus & XUL_SR_RX_FIFO_VALID_DATA ) != 0 )
|
207 |
|
|
{
|
208 |
|
|
/* A character is available - place it in the queue of received
|
209 |
|
|
characters. This might wake a task that was blocked waiting for
|
210 |
|
|
data. */
|
211 |
|
|
cChar = ( char ) XIo_In32( XPAR_RS232_UART_1_BASEADDR + XUL_RX_FIFO_OFFSET );
|
212 |
|
|
xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
|
213 |
|
|
lDidSomething = pdTRUE;
|
214 |
|
|
}
|
215 |
|
|
|
216 |
|
|
if( ( ulISRStatus & XUL_SR_TX_FIFO_EMPTY ) != 0 )
|
217 |
|
|
{
|
218 |
|
|
/* There is space in the FIFO - if there are any characters queue for
|
219 |
|
|
transmission they can be sent to the UART now. This might unblock a
|
220 |
|
|
task that was waiting for space to become available on the Tx queue. */
|
221 |
|
|
if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
|
222 |
|
|
{
|
223 |
|
|
XIo_Out32( XPAR_RS232_UART_1_BASEADDR + XUL_TX_FIFO_OFFSET, cChar );
|
224 |
|
|
lDidSomething = pdTRUE;
|
225 |
|
|
}
|
226 |
|
|
}
|
227 |
|
|
} while( lDidSomething == pdTRUE );
|
228 |
|
|
|
229 |
|
|
/* If we woke any tasks we may require a context switch. */
|
230 |
|
|
if( xHigherPriorityTaskWoken )
|
231 |
|
|
{
|
232 |
|
|
portYIELD_FROM_ISR();
|
233 |
|
|
}
|
234 |
|
|
}
|
235 |
|
|
|
236 |
|
|
|
237 |
|
|
|