1 |
577 |
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 |
|
|
BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0.
|
56 |
|
|
*/
|
57 |
|
|
|
58 |
|
|
/* Library includes. */
|
59 |
|
|
#include "uart.h"
|
60 |
|
|
#include "gpio.h"
|
61 |
|
|
#include "eic.h"
|
62 |
|
|
|
63 |
|
|
/* Scheduler includes. */
|
64 |
|
|
#include "FreeRTOS.h"
|
65 |
|
|
#include "queue.h"
|
66 |
|
|
|
67 |
|
|
/* Demo application includes. */
|
68 |
|
|
#include "serial.h"
|
69 |
|
|
|
70 |
|
|
#define UART0_Rx_Pin ( 0x0001<< 8 )
|
71 |
|
|
#define UART0_Tx_Pin ( 0x0001<< 9 )
|
72 |
|
|
|
73 |
|
|
#define serINVALID_QUEUE ( ( xQueueHandle ) 0 )
|
74 |
|
|
#define serNO_BLOCK ( ( portTickType ) 0 )
|
75 |
|
|
|
76 |
|
|
/* Macros to turn on and off the Tx empty interrupt. */
|
77 |
|
|
#define serINTERRUPT_ON() UART0->IER |= UART_TxHalfEmpty
|
78 |
|
|
#define serINTERRUPT_OFF() UART0->IER &= ~UART_TxHalfEmpty
|
79 |
|
|
|
80 |
|
|
/*-----------------------------------------------------------*/
|
81 |
|
|
|
82 |
|
|
/* Queues used to hold received characters, and characters waiting to be
|
83 |
|
|
transmitted. */
|
84 |
|
|
static xQueueHandle xRxedChars;
|
85 |
|
|
static xQueueHandle xCharsForTx;
|
86 |
|
|
|
87 |
|
|
/*-----------------------------------------------------------*/
|
88 |
|
|
|
89 |
|
|
/* Interrupt entry point written in the assembler file serialISR.s79. */
|
90 |
|
|
extern void vSerialISREntry( void );
|
91 |
|
|
|
92 |
|
|
/* The interrupt service routine - called from the assembly entry point. */
|
93 |
|
|
__arm void vSerialISR( void );
|
94 |
|
|
|
95 |
|
|
/*-----------------------------------------------------------*/
|
96 |
|
|
|
97 |
|
|
/*
|
98 |
|
|
* See the serial2.h header file.
|
99 |
|
|
*/
|
100 |
|
|
xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
|
101 |
|
|
{
|
102 |
|
|
xComPortHandle xReturn;
|
103 |
|
|
|
104 |
|
|
/* Create the queues used to hold Rx and Tx characters. */
|
105 |
|
|
xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
|
106 |
|
|
xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
|
107 |
|
|
|
108 |
|
|
/* If the queues were created correctly then setup the serial port
|
109 |
|
|
hardware. */
|
110 |
|
|
if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )
|
111 |
|
|
{
|
112 |
|
|
portENTER_CRITICAL();
|
113 |
|
|
{
|
114 |
|
|
/* Setup the UART port pins. */
|
115 |
|
|
GPIO_Config( GPIO0, UART0_Tx_Pin, GPIO_AF_PP );
|
116 |
|
|
GPIO_Config( GPIO0, UART0_Rx_Pin, GPIO_IN_TRI_CMOS );
|
117 |
|
|
|
118 |
|
|
/* Configure the UART. */
|
119 |
|
|
UART_OnOffConfig( UART0, ENABLE );
|
120 |
|
|
UART_FifoConfig( UART0, DISABLE );
|
121 |
|
|
UART_FifoReset( UART0, UART_RxFIFO );
|
122 |
|
|
UART_FifoReset( UART0, UART_TxFIFO );
|
123 |
|
|
UART_LoopBackConfig(UART0, DISABLE );
|
124 |
|
|
UART_Config( UART0, ulWantedBaud, UART_NO_PARITY, UART_1_StopBits, UARTM_8D );
|
125 |
|
|
UART_RxConfig( UART0, ENABLE );
|
126 |
|
|
|
127 |
|
|
/* Configure the IEC for the UART interrupts. */
|
128 |
|
|
EIC_IRQChannelPriorityConfig( UART0_IRQChannel, 1 );
|
129 |
|
|
EIC_IRQChannelConfig( UART0_IRQChannel, ENABLE );
|
130 |
|
|
EIC_IRQConfig( ENABLE );
|
131 |
|
|
UART_ItConfig( UART0, UART_RxBufFull, ENABLE );
|
132 |
|
|
}
|
133 |
|
|
portEXIT_CRITICAL();
|
134 |
|
|
}
|
135 |
|
|
else
|
136 |
|
|
{
|
137 |
|
|
xReturn = ( xComPortHandle ) 0;
|
138 |
|
|
}
|
139 |
|
|
|
140 |
|
|
/* This demo file only supports a single port but we have to return
|
141 |
|
|
something to comply with the standard demo header file. */
|
142 |
|
|
return xReturn;
|
143 |
|
|
}
|
144 |
|
|
/*-----------------------------------------------------------*/
|
145 |
|
|
|
146 |
|
|
signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
|
147 |
|
|
{
|
148 |
|
|
/* The port handle is not required as this driver only supports one port. */
|
149 |
|
|
( void ) pxPort;
|
150 |
|
|
|
151 |
|
|
/* Get the next character from the buffer. Return false if no characters
|
152 |
|
|
are available, or arrive before xBlockTime expires. */
|
153 |
|
|
if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
|
154 |
|
|
{
|
155 |
|
|
return pdTRUE;
|
156 |
|
|
}
|
157 |
|
|
else
|
158 |
|
|
{
|
159 |
|
|
return pdFALSE;
|
160 |
|
|
}
|
161 |
|
|
}
|
162 |
|
|
/*-----------------------------------------------------------*/
|
163 |
|
|
|
164 |
|
|
void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
|
165 |
|
|
{
|
166 |
|
|
signed char *pxNext;
|
167 |
|
|
|
168 |
|
|
/* A couple of parameters that this port does not use. */
|
169 |
|
|
( void ) usStringLength;
|
170 |
|
|
( void ) pxPort;
|
171 |
|
|
|
172 |
|
|
/* NOTE: This implementation does not handle the queue being full as no
|
173 |
|
|
block time is used! */
|
174 |
|
|
|
175 |
|
|
/* The port handle is not required as this driver only supports UART0. */
|
176 |
|
|
( void ) pxPort;
|
177 |
|
|
|
178 |
|
|
/* Send each character in the string, one at a time. */
|
179 |
|
|
pxNext = ( signed char * ) pcString;
|
180 |
|
|
while( *pxNext )
|
181 |
|
|
{
|
182 |
|
|
xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
|
183 |
|
|
pxNext++;
|
184 |
|
|
}
|
185 |
|
|
}
|
186 |
|
|
/*-----------------------------------------------------------*/
|
187 |
|
|
|
188 |
|
|
signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
|
189 |
|
|
{
|
190 |
|
|
/* Place the character in the queue of characters to be transmitted. */
|
191 |
|
|
if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
|
192 |
|
|
{
|
193 |
|
|
return pdFAIL;
|
194 |
|
|
}
|
195 |
|
|
|
196 |
|
|
/* Turn on the Tx interrupt so the ISR will remove the character from the
|
197 |
|
|
queue and send it. This does not need to be in a critical section as
|
198 |
|
|
if the interrupt has already removed the character the next interrupt
|
199 |
|
|
will simply turn off the Tx interrupt again. */
|
200 |
|
|
serINTERRUPT_ON();
|
201 |
|
|
|
202 |
|
|
return pdPASS;
|
203 |
|
|
}
|
204 |
|
|
/*-----------------------------------------------------------*/
|
205 |
|
|
|
206 |
|
|
void vSerialClose( xComPortHandle xPort )
|
207 |
|
|
{
|
208 |
|
|
/* Not supported as not required by the demo application. */
|
209 |
|
|
}
|
210 |
|
|
/*-----------------------------------------------------------*/
|
211 |
|
|
|
212 |
|
|
/* Serial port ISR. This can cause a context switch so is not defined as a
|
213 |
|
|
standard ISR using the __irq keyword. Instead a wrapper function is defined
|
214 |
|
|
within serialISR.s79 which in turn calls this function. See the port
|
215 |
|
|
documentation on the FreeRTOS.org website for more information. */
|
216 |
|
|
__arm void vSerialISR( void )
|
217 |
|
|
{
|
218 |
|
|
unsigned short usStatus;
|
219 |
|
|
signed char cChar;
|
220 |
|
|
portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
|
221 |
|
|
|
222 |
|
|
/* What caused the interrupt? */
|
223 |
|
|
usStatus = UART_FlagStatus( UART0 );
|
224 |
|
|
|
225 |
|
|
if( usStatus & UART_TxHalfEmpty )
|
226 |
|
|
{
|
227 |
|
|
/* The interrupt was caused by the THR becoming empty. Are there any
|
228 |
|
|
more characters to transmit? */
|
229 |
|
|
if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
|
230 |
|
|
{
|
231 |
|
|
/* A character was retrieved from the queue so can be sent to the
|
232 |
|
|
THR now. */
|
233 |
|
|
UART0->TxBUFR = cChar;
|
234 |
|
|
}
|
235 |
|
|
else
|
236 |
|
|
{
|
237 |
|
|
/* Queue empty, nothing to send so turn off the Tx interrupt. */
|
238 |
|
|
serINTERRUPT_OFF();
|
239 |
|
|
}
|
240 |
|
|
}
|
241 |
|
|
|
242 |
|
|
if( usStatus & UART_RxBufFull )
|
243 |
|
|
{
|
244 |
|
|
/* The interrupt was caused by a character being received. Grab the
|
245 |
|
|
character from the RHR and place it in the queue of received
|
246 |
|
|
characters. */
|
247 |
|
|
cChar = UART0->RxBUFR;
|
248 |
|
|
xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
|
249 |
|
|
}
|
250 |
|
|
|
251 |
|
|
/* If a task was woken by either a character being received or a character
|
252 |
|
|
being transmitted then we may need to switch to another task. */
|
253 |
|
|
portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
|
254 |
|
|
|
255 |
|
|
/* End the interrupt in the EIC. */
|
256 |
|
|
portCLEAR_EIC();
|
257 |
|
|
}
|
258 |
|
|
|
259 |
|
|
|
260 |
|
|
|
261 |
|
|
|
262 |
|
|
|
263 |
|
|
|