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 UART1.
|
56 |
|
|
*/
|
57 |
|
|
|
58 |
|
|
/* Library includes. */
|
59 |
|
|
#include "91x_lib.h"
|
60 |
|
|
|
61 |
|
|
/* Scheduler includes. */
|
62 |
|
|
#include "FreeRTOS.h"
|
63 |
|
|
#include "queue.h"
|
64 |
|
|
#include "semphr.h"
|
65 |
|
|
|
66 |
|
|
/* Demo application includes. */
|
67 |
|
|
#include "serial.h"
|
68 |
|
|
/*-----------------------------------------------------------*/
|
69 |
|
|
|
70 |
|
|
/* Misc defines. */
|
71 |
|
|
#define serINVALID_QUEUE ( ( xQueueHandle ) 0 )
|
72 |
|
|
#define serNO_BLOCK ( ( portTickType ) 0 )
|
73 |
|
|
#define serTX_BLOCK_TIME ( 40 / portTICK_RATE_MS )
|
74 |
|
|
|
75 |
|
|
/* Interrupt and status bit definitions. */
|
76 |
|
|
#define mainTXRIS 0x20
|
77 |
|
|
#define mainRXRIS 0x50
|
78 |
|
|
#define serTX_FIFO_FULL 0x20
|
79 |
|
|
#define serCLEAR_ALL_INTERRUPTS 0x3ff
|
80 |
|
|
/*-----------------------------------------------------------*/
|
81 |
|
|
|
82 |
|
|
/* The queue used to hold received characters. */
|
83 |
|
|
static xQueueHandle xRxedChars;
|
84 |
|
|
|
85 |
|
|
/* The semaphore used to wake a task waiting for space to become available
|
86 |
|
|
in the FIFO. */
|
87 |
|
|
static xSemaphoreHandle xTxFIFOSemaphore;
|
88 |
|
|
|
89 |
|
|
/*-----------------------------------------------------------*/
|
90 |
|
|
|
91 |
|
|
/* UART interrupt handler. */
|
92 |
|
|
void UART1_IRQHandler( void );
|
93 |
|
|
|
94 |
|
|
/* The interrupt service routine - called from the assembly entry point. */
|
95 |
|
|
__arm void UART1_IRQHandler( void );
|
96 |
|
|
|
97 |
|
|
/*-----------------------------------------------------------*/
|
98 |
|
|
|
99 |
|
|
/* Flag to indicate whether or not a task is blocked waiting for space on
|
100 |
|
|
the FIFO. */
|
101 |
|
|
static long lTaskWaiting = pdFALSE;
|
102 |
|
|
|
103 |
|
|
/*
|
104 |
|
|
* See the serial2.h header file.
|
105 |
|
|
*/
|
106 |
|
|
xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
|
107 |
|
|
{
|
108 |
|
|
xComPortHandle xReturn;
|
109 |
|
|
UART_InitTypeDef xUART1_Init;
|
110 |
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
111 |
|
|
|
112 |
|
|
/* Create the queues used to hold Rx characters. */
|
113 |
|
|
xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
|
114 |
|
|
|
115 |
|
|
/* Create the semaphore used to wake a task waiting for space to become
|
116 |
|
|
available in the FIFO. */
|
117 |
|
|
vSemaphoreCreateBinary( xTxFIFOSemaphore );
|
118 |
|
|
|
119 |
|
|
/* If the queue/semaphore was created correctly then setup the serial port
|
120 |
|
|
hardware. */
|
121 |
|
|
if( ( xRxedChars != serINVALID_QUEUE ) && ( xTxFIFOSemaphore != serINVALID_QUEUE ) )
|
122 |
|
|
{
|
123 |
|
|
/* Pre take the semaphore so a task will block if it tries to access
|
124 |
|
|
it. */
|
125 |
|
|
xSemaphoreTake( xTxFIFOSemaphore, 0 );
|
126 |
|
|
|
127 |
|
|
/* Configure the UART. */
|
128 |
|
|
xUART1_Init.UART_WordLength = UART_WordLength_8D;
|
129 |
|
|
xUART1_Init.UART_StopBits = UART_StopBits_1;
|
130 |
|
|
xUART1_Init.UART_Parity = UART_Parity_No;
|
131 |
|
|
xUART1_Init.UART_BaudRate = ulWantedBaud;
|
132 |
|
|
xUART1_Init.UART_HardwareFlowControl = UART_HardwareFlowControl_None;
|
133 |
|
|
xUART1_Init.UART_Mode = UART_Mode_Tx_Rx;
|
134 |
|
|
xUART1_Init.UART_FIFO = UART_FIFO_Enable;
|
135 |
|
|
|
136 |
|
|
/* Enable the UART1 Clock */
|
137 |
|
|
SCU_APBPeriphClockConfig( __UART1, ENABLE );
|
138 |
|
|
|
139 |
|
|
/* Enable the GPIO3 Clock */
|
140 |
|
|
SCU_APBPeriphClockConfig( __GPIO3, ENABLE );
|
141 |
|
|
|
142 |
|
|
/* Configure UART1_Rx pin GPIO3.2 */
|
143 |
|
|
GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
|
144 |
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
|
145 |
|
|
GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
|
146 |
|
|
GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable;
|
147 |
|
|
GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1 ;
|
148 |
|
|
GPIO_Init( GPIO3, &GPIO_InitStructure );
|
149 |
|
|
|
150 |
|
|
/* Configure UART1_Tx pin GPIO3.3 */
|
151 |
|
|
GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
|
152 |
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
|
153 |
|
|
GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
|
154 |
|
|
GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable;
|
155 |
|
|
GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2 ;
|
156 |
|
|
GPIO_Init( GPIO3, &GPIO_InitStructure );
|
157 |
|
|
|
158 |
|
|
|
159 |
|
|
portENTER_CRITICAL();
|
160 |
|
|
{
|
161 |
|
|
/* Configure the UART itself. */
|
162 |
|
|
UART_DeInit( UART1 );
|
163 |
|
|
UART_Init( UART1, &xUART1_Init );
|
164 |
|
|
UART_ITConfig( UART1, UART_IT_Receive | UART_IT_Transmit, ENABLE );
|
165 |
|
|
UART1->ICR = serCLEAR_ALL_INTERRUPTS;
|
166 |
|
|
UART_LoopBackConfig( UART1, DISABLE );
|
167 |
|
|
UART_IrDACmd( IrDA1, DISABLE );
|
168 |
|
|
|
169 |
|
|
/* Configure the VIC for the UART interrupts. */
|
170 |
|
|
VIC_Config( UART1_ITLine, VIC_IRQ, 9 );
|
171 |
|
|
VIC_ITCmd( UART1_ITLine, ENABLE );
|
172 |
|
|
|
173 |
|
|
UART_Cmd( UART1, ENABLE );
|
174 |
|
|
lTaskWaiting = pdFALSE;
|
175 |
|
|
}
|
176 |
|
|
portEXIT_CRITICAL();
|
177 |
|
|
}
|
178 |
|
|
else
|
179 |
|
|
{
|
180 |
|
|
xReturn = ( xComPortHandle ) 0;
|
181 |
|
|
}
|
182 |
|
|
|
183 |
|
|
/* This demo file only supports a single port but we have to return
|
184 |
|
|
something to comply with the standard demo header file. */
|
185 |
|
|
return xReturn;
|
186 |
|
|
}
|
187 |
|
|
/*-----------------------------------------------------------*/
|
188 |
|
|
|
189 |
|
|
signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
|
190 |
|
|
{
|
191 |
|
|
/* The port handle is not required as this driver only supports one port. */
|
192 |
|
|
( void ) pxPort;
|
193 |
|
|
|
194 |
|
|
/* Get the next character from the buffer. Return false if no characters
|
195 |
|
|
are available, or arrive before xBlockTime expires. */
|
196 |
|
|
if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
|
197 |
|
|
{
|
198 |
|
|
return pdTRUE;
|
199 |
|
|
}
|
200 |
|
|
else
|
201 |
|
|
{
|
202 |
|
|
return pdFALSE;
|
203 |
|
|
}
|
204 |
|
|
}
|
205 |
|
|
/*-----------------------------------------------------------*/
|
206 |
|
|
|
207 |
|
|
void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
|
208 |
|
|
{
|
209 |
|
|
signed char *pxNext;
|
210 |
|
|
|
211 |
|
|
/* A couple of parameters that this port does not use. */
|
212 |
|
|
( void ) usStringLength;
|
213 |
|
|
( void ) pxPort;
|
214 |
|
|
|
215 |
|
|
/* NOTE: This implementation does not handle the queue being full as no
|
216 |
|
|
block time is used! */
|
217 |
|
|
|
218 |
|
|
/* The port handle is not required as this driver only supports UART1. */
|
219 |
|
|
( void ) pxPort;
|
220 |
|
|
|
221 |
|
|
/* Send each character in the string, one at a time. */
|
222 |
|
|
pxNext = ( signed char * ) pcString;
|
223 |
|
|
while( *pxNext )
|
224 |
|
|
{
|
225 |
|
|
xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
|
226 |
|
|
pxNext++;
|
227 |
|
|
}
|
228 |
|
|
}
|
229 |
|
|
/*-----------------------------------------------------------*/
|
230 |
|
|
|
231 |
|
|
signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
|
232 |
|
|
{
|
233 |
|
|
portBASE_TYPE xReturn;
|
234 |
|
|
|
235 |
|
|
portENTER_CRITICAL();
|
236 |
|
|
{
|
237 |
|
|
/* Can we write to the FIFO? */
|
238 |
|
|
if( UART1->FR & serTX_FIFO_FULL )
|
239 |
|
|
{
|
240 |
|
|
/* Wait for the interrupt letting us know there is space on the
|
241 |
|
|
FIFO. It is ok to block in a critical section, interrupts will be
|
242 |
|
|
enabled for other tasks once we force a switch. */
|
243 |
|
|
lTaskWaiting = pdTRUE;
|
244 |
|
|
|
245 |
|
|
/* Just to be a bit different this driver uses a semaphore to
|
246 |
|
|
block the sending task when the FIFO is full. The standard COMTest
|
247 |
|
|
task assumes a queue of adequate length exists so does not use
|
248 |
|
|
a block time. For this demo the block time is therefore hard
|
249 |
|
|
coded. */
|
250 |
|
|
xReturn = xSemaphoreTake( xTxFIFOSemaphore, serTX_BLOCK_TIME );
|
251 |
|
|
if( xReturn )
|
252 |
|
|
{
|
253 |
|
|
UART1->DR = cOutChar;
|
254 |
|
|
}
|
255 |
|
|
}
|
256 |
|
|
else
|
257 |
|
|
{
|
258 |
|
|
UART1->DR = cOutChar;
|
259 |
|
|
xReturn = pdPASS;
|
260 |
|
|
}
|
261 |
|
|
}
|
262 |
|
|
portEXIT_CRITICAL();
|
263 |
|
|
|
264 |
|
|
return xReturn;
|
265 |
|
|
}
|
266 |
|
|
/*-----------------------------------------------------------*/
|
267 |
|
|
|
268 |
|
|
void vSerialClose( xComPortHandle xPort )
|
269 |
|
|
{
|
270 |
|
|
/* Not supported as not required by the demo application. */
|
271 |
|
|
}
|
272 |
|
|
/*-----------------------------------------------------------*/
|
273 |
|
|
|
274 |
|
|
void UART1_IRQHandler( void )
|
275 |
|
|
{
|
276 |
|
|
signed char cChar;
|
277 |
|
|
portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
|
278 |
|
|
|
279 |
|
|
while( UART1->RIS & mainRXRIS )
|
280 |
|
|
{
|
281 |
|
|
/* The interrupt was caused by a character being received. Grab the
|
282 |
|
|
character from the DR and place it in the queue of received
|
283 |
|
|
characters. */
|
284 |
|
|
cChar = UART1->DR;
|
285 |
|
|
xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
|
286 |
|
|
}
|
287 |
|
|
|
288 |
|
|
if( UART1->RIS & mainTXRIS )
|
289 |
|
|
{
|
290 |
|
|
if( lTaskWaiting == pdTRUE )
|
291 |
|
|
{
|
292 |
|
|
/* This interrupt was caused by space becoming available on the Tx
|
293 |
|
|
FIFO, wake any task that is waiting to post (if any). */
|
294 |
|
|
xSemaphoreGiveFromISR( xTxFIFOSemaphore, &xHigherPriorityTaskWoken );
|
295 |
|
|
lTaskWaiting = pdFALSE;
|
296 |
|
|
}
|
297 |
|
|
|
298 |
|
|
UART1->ICR = mainTXRIS;
|
299 |
|
|
}
|
300 |
|
|
|
301 |
|
|
/* If a task was woken by either a character being received or a character
|
302 |
|
|
being transmitted then we may need to switch to another task. */
|
303 |
|
|
portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
|
304 |
|
|
}
|
305 |
|
|
|
306 |
|
|
|
307 |
|
|
|
308 |
|
|
|
309 |
|
|
|
310 |
|
|
|