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 |
|
|
/* Standard includes. */
|
59 |
|
|
#include <stdlib.h>
|
60 |
|
|
|
61 |
|
|
/* Scheduler includes. */
|
62 |
|
|
#include "FreeRTOS.h"
|
63 |
|
|
#include "queue.h"
|
64 |
|
|
|
65 |
|
|
/* Demo application includes. */
|
66 |
|
|
#include "serial.h"
|
67 |
|
|
|
68 |
|
|
/*-----------------------------------------------------------*/
|
69 |
|
|
|
70 |
|
|
/* Location of the COM0 registers. */
|
71 |
|
|
#define serCOM0 ( ( AT91PS_USART ) AT91C_BASE_US0 )
|
72 |
|
|
|
73 |
|
|
/* Interrupt control macros. */
|
74 |
|
|
#define serINTERRUPT_LEVEL ( 5 )
|
75 |
|
|
#define vInterruptOn() AT91F_US_EnableIt( serCOM0, AT91C_US_TXRDY | AT91C_US_RXRDY )
|
76 |
|
|
#define vInterruptOff() AT91F_US_DisableIt( serCOM0, AT91C_US_TXRDY )
|
77 |
|
|
|
78 |
|
|
/* Misc constants. */
|
79 |
|
|
#define serINVALID_QUEUE ( ( xQueueHandle ) 0 )
|
80 |
|
|
#define serHANDLE ( ( xComPortHandle ) 1 )
|
81 |
|
|
#define serNO_BLOCK ( ( portTickType ) 0 )
|
82 |
|
|
#define serNO_TIMEGUARD ( ( unsigned long ) 0 )
|
83 |
|
|
#define serNO_PERIPHERAL_B_SETUP ( ( unsigned long ) 0 )
|
84 |
|
|
|
85 |
|
|
|
86 |
|
|
/* Queues used to hold received characters, and characters waiting to be
|
87 |
|
|
transmitted. */
|
88 |
|
|
static xQueueHandle xRxedChars;
|
89 |
|
|
static xQueueHandle xCharsForTx;
|
90 |
|
|
|
91 |
|
|
/*-----------------------------------------------------------*/
|
92 |
|
|
|
93 |
|
|
/* Interrupt entry point written in the assembler file serialISR.s79. */
|
94 |
|
|
extern void vSerialISREntry( void );
|
95 |
|
|
|
96 |
|
|
/* The interrupt service routine - called from the assembly entry point. */
|
97 |
|
|
__arm void vSerialISR( void );
|
98 |
|
|
|
99 |
|
|
/*-----------------------------------------------------------*/
|
100 |
|
|
|
101 |
|
|
/*
|
102 |
|
|
* See the serial2.h header file.
|
103 |
|
|
*/
|
104 |
|
|
xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
|
105 |
|
|
{
|
106 |
|
|
xComPortHandle xReturn = serHANDLE;
|
107 |
|
|
extern void ( vUART_ISR )( void );
|
108 |
|
|
|
109 |
|
|
/* Create the queues used to hold Rx and Tx characters. */
|
110 |
|
|
xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
|
111 |
|
|
xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
|
112 |
|
|
|
113 |
|
|
/* If the queues were created correctly then setup the serial port
|
114 |
|
|
hardware. */
|
115 |
|
|
if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )
|
116 |
|
|
{
|
117 |
|
|
portENTER_CRITICAL();
|
118 |
|
|
{
|
119 |
|
|
/* Enable the USART clock. */
|
120 |
|
|
AT91F_PMC_EnablePeriphClock( AT91C_BASE_PMC, 1 << AT91C_ID_US0 );
|
121 |
|
|
|
122 |
|
|
AT91F_PIO_CfgPeriph( AT91C_BASE_PIOA, ( ( unsigned long ) AT91C_PA5_RXD0 ) | ( ( unsigned long ) AT91C_PA6_TXD0 ), serNO_PERIPHERAL_B_SETUP );
|
123 |
|
|
|
124 |
|
|
/* Set the required protocol. */
|
125 |
|
|
AT91F_US_Configure( serCOM0, configCPU_CLOCK_HZ, AT91C_US_ASYNC_MODE, ulWantedBaud, serNO_TIMEGUARD );
|
126 |
|
|
|
127 |
|
|
/* Enable Rx and Tx. */
|
128 |
|
|
serCOM0->US_CR = AT91C_US_RXEN | AT91C_US_TXEN;
|
129 |
|
|
|
130 |
|
|
/* Enable the Rx interrupts. The Tx interrupts are not enabled
|
131 |
|
|
until there are characters to be transmitted. */
|
132 |
|
|
AT91F_US_EnableIt( serCOM0, AT91C_US_RXRDY );
|
133 |
|
|
|
134 |
|
|
/* Enable the interrupts in the AIC. */
|
135 |
|
|
AT91F_AIC_ConfigureIt( AT91C_BASE_AIC, AT91C_ID_US0, serINTERRUPT_LEVEL, AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE, ( void (*)( void ) ) vSerialISREntry );
|
136 |
|
|
AT91F_AIC_EnableIt( AT91C_BASE_AIC, AT91C_ID_US0 );
|
137 |
|
|
}
|
138 |
|
|
portEXIT_CRITICAL();
|
139 |
|
|
}
|
140 |
|
|
else
|
141 |
|
|
{
|
142 |
|
|
xReturn = ( xComPortHandle ) 0;
|
143 |
|
|
}
|
144 |
|
|
|
145 |
|
|
/* This demo file only supports a single port but we have to return
|
146 |
|
|
something to comply with the standard demo header file. */
|
147 |
|
|
return xReturn;
|
148 |
|
|
}
|
149 |
|
|
/*-----------------------------------------------------------*/
|
150 |
|
|
|
151 |
|
|
signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
|
152 |
|
|
{
|
153 |
|
|
/* The port handle is not required as this driver only supports one port. */
|
154 |
|
|
( void ) pxPort;
|
155 |
|
|
|
156 |
|
|
/* Get the next character from the buffer. Return false if no characters
|
157 |
|
|
are available, or arrive before xBlockTime expires. */
|
158 |
|
|
if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
|
159 |
|
|
{
|
160 |
|
|
return pdTRUE;
|
161 |
|
|
}
|
162 |
|
|
else
|
163 |
|
|
{
|
164 |
|
|
return pdFALSE;
|
165 |
|
|
}
|
166 |
|
|
}
|
167 |
|
|
/*-----------------------------------------------------------*/
|
168 |
|
|
|
169 |
|
|
void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
|
170 |
|
|
{
|
171 |
|
|
signed char *pxNext;
|
172 |
|
|
|
173 |
|
|
/* A couple of parameters that this port does not use. */
|
174 |
|
|
( void ) usStringLength;
|
175 |
|
|
( void ) pxPort;
|
176 |
|
|
|
177 |
|
|
/* NOTE: This implementation does not handle the queue being full as no
|
178 |
|
|
block time is used! */
|
179 |
|
|
|
180 |
|
|
/* The port handle is not required as this driver only supports UART0. */
|
181 |
|
|
( void ) pxPort;
|
182 |
|
|
|
183 |
|
|
/* Send each character in the string, one at a time. */
|
184 |
|
|
pxNext = ( signed char * ) pcString;
|
185 |
|
|
while( *pxNext )
|
186 |
|
|
{
|
187 |
|
|
xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
|
188 |
|
|
pxNext++;
|
189 |
|
|
}
|
190 |
|
|
}
|
191 |
|
|
/*-----------------------------------------------------------*/
|
192 |
|
|
|
193 |
|
|
signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
|
194 |
|
|
{
|
195 |
|
|
/* Place the character in the queue of characters to be transmitted. */
|
196 |
|
|
if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
|
197 |
|
|
{
|
198 |
|
|
return pdFAIL;
|
199 |
|
|
}
|
200 |
|
|
|
201 |
|
|
/* Turn on the Tx interrupt so the ISR will remove the character from the
|
202 |
|
|
queue and send it. This does not need to be in a critical section as
|
203 |
|
|
if the interrupt has already removed the character the next interrupt
|
204 |
|
|
will simply turn off the Tx interrupt again. */
|
205 |
|
|
vInterruptOn();
|
206 |
|
|
|
207 |
|
|
return pdPASS;
|
208 |
|
|
}
|
209 |
|
|
/*-----------------------------------------------------------*/
|
210 |
|
|
|
211 |
|
|
void vSerialClose( xComPortHandle xPort )
|
212 |
|
|
{
|
213 |
|
|
/* Not supported as not required by the demo application. */
|
214 |
|
|
}
|
215 |
|
|
/*-----------------------------------------------------------*/
|
216 |
|
|
|
217 |
|
|
/* Serial port ISR. This can cause a context switch so is not defined as a
|
218 |
|
|
standard ISR using the __irq keyword. Instead a wrapper function is defined
|
219 |
|
|
within serialISR.s79 which in turn calls this function. See the port
|
220 |
|
|
documentation on the FreeRTOS.org website for more information. */
|
221 |
|
|
__arm void vSerialISR( void )
|
222 |
|
|
{
|
223 |
|
|
unsigned long ulStatus;
|
224 |
|
|
signed char cChar;
|
225 |
|
|
portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
|
226 |
|
|
|
227 |
|
|
/* What caused the interrupt? */
|
228 |
|
|
ulStatus = serCOM0->US_CSR &= serCOM0->US_IMR;
|
229 |
|
|
|
230 |
|
|
if( ulStatus & AT91C_US_TXRDY )
|
231 |
|
|
{
|
232 |
|
|
/* The interrupt was caused by the THR becoming empty. Are there any
|
233 |
|
|
more characters to transmit? */
|
234 |
|
|
if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
|
235 |
|
|
{
|
236 |
|
|
/* A character was retrieved from the queue so can be sent to the
|
237 |
|
|
THR now. */
|
238 |
|
|
serCOM0->US_THR = cChar;
|
239 |
|
|
}
|
240 |
|
|
else
|
241 |
|
|
{
|
242 |
|
|
/* Queue empty, nothing to send so turn off the Tx interrupt. */
|
243 |
|
|
vInterruptOff();
|
244 |
|
|
}
|
245 |
|
|
}
|
246 |
|
|
|
247 |
|
|
if( ulStatus & AT91C_US_RXRDY )
|
248 |
|
|
{
|
249 |
|
|
/* The interrupt was caused by a character being received. Grab the
|
250 |
|
|
character from the RHR and place it in the queue or received
|
251 |
|
|
characters. */
|
252 |
|
|
cChar = serCOM0->US_RHR;
|
253 |
|
|
xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
|
254 |
|
|
}
|
255 |
|
|
|
256 |
|
|
/* If a task was woken by either a character being received or a character
|
257 |
|
|
being transmitted then we may need to switch to another task. */
|
258 |
|
|
portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
|
259 |
|
|
|
260 |
|
|
/* End the interrupt in the AIC. */
|
261 |
|
|
AT91C_BASE_AIC->AIC_EOICR = 0;
|
262 |
|
|
}
|
263 |
|
|
|
264 |
|
|
|
265 |
|
|
|
266 |
|
|
|
267 |
|
|
|