1 |
584 |
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 |
|
|
/* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER.
|
55 |
|
|
*
|
56 |
|
|
* This file only supports UART 0
|
57 |
|
|
*/
|
58 |
|
|
|
59 |
|
|
/* Standard includes. */
|
60 |
|
|
#include <stdlib.h>
|
61 |
|
|
|
62 |
|
|
/* Scheduler includes. */
|
63 |
|
|
#include "FreeRTOS.h"
|
64 |
|
|
#include "queue.h"
|
65 |
|
|
#include "task.h"
|
66 |
|
|
|
67 |
|
|
/* Demo application includes. */
|
68 |
|
|
#include "serial.h"
|
69 |
|
|
|
70 |
|
|
/* The queue used to hold received characters. */
|
71 |
|
|
static xQueueHandle xRxedChars;
|
72 |
|
|
|
73 |
|
|
/* The queue used to hold characters waiting transmission. */
|
74 |
|
|
static xQueueHandle xCharsForTx;
|
75 |
|
|
|
76 |
|
|
static volatile portSHORT sTHREEmpty;
|
77 |
|
|
|
78 |
|
|
static volatile portSHORT queueFail = pdFALSE;
|
79 |
|
|
|
80 |
|
|
/*-----------------------------------------------------------*/
|
81 |
|
|
xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
|
82 |
|
|
{
|
83 |
|
|
/* Initialise the hardware. */
|
84 |
|
|
portENTER_CRITICAL();
|
85 |
|
|
{
|
86 |
|
|
/* Create the queues used by the com test task. */
|
87 |
|
|
xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof(signed portCHAR) );
|
88 |
|
|
xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof(signed portCHAR) );
|
89 |
|
|
|
90 |
|
|
if( xRxedChars == 0 )
|
91 |
|
|
{
|
92 |
|
|
queueFail = pdTRUE;
|
93 |
|
|
}
|
94 |
|
|
|
95 |
|
|
if( xCharsForTx == 0 )
|
96 |
|
|
{
|
97 |
|
|
queueFail = pdTRUE;
|
98 |
|
|
}
|
99 |
|
|
|
100 |
|
|
/* Initialize UART asynchronous mode */
|
101 |
|
|
BGR0 = configCLKP1_CLOCK_HZ / ulWantedBaud;
|
102 |
|
|
|
103 |
|
|
SCR0 = 0x17; /* 8N1 */
|
104 |
|
|
SMR0 = 0x0d; /* enable SOT3, Reset, normal mode */
|
105 |
|
|
SSR0 = 0x02; /* LSB first, enable receive interrupts */
|
106 |
|
|
|
107 |
|
|
PIER08_IE2 = 1; /* enable input */
|
108 |
|
|
DDR08_D2 = 0; /* switch P08_2 to input */
|
109 |
|
|
DDR08_D3 = 1; /* switch P08_3 to output */
|
110 |
|
|
}
|
111 |
|
|
portEXIT_CRITICAL();
|
112 |
|
|
|
113 |
|
|
/* Unlike other ports, this serial code does not allow for more than one
|
114 |
|
|
com port. We therefore don't return a pointer to a port structure and can
|
115 |
|
|
instead just return NULL. */
|
116 |
|
|
return NULL;
|
117 |
|
|
}
|
118 |
|
|
/*-----------------------------------------------------------*/
|
119 |
|
|
|
120 |
|
|
signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )
|
121 |
|
|
{
|
122 |
|
|
/* Get the next character from the buffer. Return false if no characters
|
123 |
|
|
are available, or arrive before xBlockTime expires. */
|
124 |
|
|
if( xQueueReceive(xRxedChars, pcRxedChar, xBlockTime) )
|
125 |
|
|
{
|
126 |
|
|
return pdTRUE;
|
127 |
|
|
}
|
128 |
|
|
else
|
129 |
|
|
{
|
130 |
|
|
return pdFALSE;
|
131 |
|
|
}
|
132 |
|
|
}
|
133 |
|
|
/*-----------------------------------------------------------*/
|
134 |
|
|
|
135 |
|
|
signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )
|
136 |
|
|
{
|
137 |
|
|
signed portBASE_TYPE xReturn;
|
138 |
|
|
|
139 |
|
|
/* Transmit a character. */
|
140 |
|
|
portENTER_CRITICAL();
|
141 |
|
|
{
|
142 |
|
|
if( sTHREEmpty == pdTRUE )
|
143 |
|
|
{
|
144 |
|
|
/* If sTHREEmpty is true then the UART Tx ISR has indicated that
|
145 |
|
|
there are no characters queued to be transmitted - so we can
|
146 |
|
|
write the character directly to the shift Tx register. */
|
147 |
|
|
sTHREEmpty = pdFALSE;
|
148 |
|
|
TDR0 = cOutChar;
|
149 |
|
|
xReturn = pdPASS;
|
150 |
|
|
}
|
151 |
|
|
else
|
152 |
|
|
{
|
153 |
|
|
/* sTHREEmpty is false, so there are still characters waiting to be
|
154 |
|
|
transmitted. We have to queue this character so it gets
|
155 |
|
|
transmitted in turn. */
|
156 |
|
|
|
157 |
|
|
/* Return false if after the block time there is no room on the Tx
|
158 |
|
|
queue. It is ok to block inside a critical section as each task
|
159 |
|
|
maintains it's own critical section status. */
|
160 |
|
|
if( xQueueSend(xCharsForTx, &cOutChar, xBlockTime) == pdTRUE )
|
161 |
|
|
{
|
162 |
|
|
xReturn = pdPASS;
|
163 |
|
|
}
|
164 |
|
|
else
|
165 |
|
|
{
|
166 |
|
|
xReturn = pdFAIL;
|
167 |
|
|
}
|
168 |
|
|
}
|
169 |
|
|
|
170 |
|
|
if( pdPASS == xReturn )
|
171 |
|
|
{
|
172 |
|
|
/* Turn on the Tx interrupt so the ISR will remove the character from the
|
173 |
|
|
queue and send it. This does not need to be in a critical section as
|
174 |
|
|
if the interrupt has already removed the character the next interrupt
|
175 |
|
|
will simply turn off the Tx interrupt again. */
|
176 |
|
|
SSR0_TIE = 1;
|
177 |
|
|
}
|
178 |
|
|
}
|
179 |
|
|
portEXIT_CRITICAL();
|
180 |
|
|
|
181 |
|
|
return pdPASS;
|
182 |
|
|
}
|
183 |
|
|
/*-----------------------------------------------------------*/
|
184 |
|
|
|
185 |
|
|
/*
|
186 |
|
|
* UART RX interrupt service routine.
|
187 |
|
|
*/
|
188 |
|
|
__interrupt void UART0_RxISR( void )
|
189 |
|
|
{
|
190 |
|
|
volatile signed portCHAR cChar;
|
191 |
|
|
portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
|
192 |
|
|
|
193 |
|
|
/* Get the character from the UART and post it on the queue of Rxed
|
194 |
|
|
characters. */
|
195 |
|
|
cChar = RDR0;
|
196 |
|
|
|
197 |
|
|
xQueueSendFromISR( xRxedChars, ( const void *const ) &cChar, &xHigherPriorityTaskWoken );
|
198 |
|
|
|
199 |
|
|
if( xHigherPriorityTaskWoken )
|
200 |
|
|
{
|
201 |
|
|
/*If the post causes a task to wake force a context switch
|
202 |
|
|
as the woken task may have a higher priority than the task we have
|
203 |
|
|
interrupted. */
|
204 |
|
|
portYIELD_FROM_ISR();
|
205 |
|
|
}
|
206 |
|
|
}
|
207 |
|
|
/*-----------------------------------------------------------*/
|
208 |
|
|
|
209 |
|
|
/*
|
210 |
|
|
* UART Tx interrupt service routine.
|
211 |
|
|
*/
|
212 |
|
|
__interrupt void UART0_TxISR( void )
|
213 |
|
|
{
|
214 |
|
|
signed portCHAR cChar;
|
215 |
|
|
signed portBASE_TYPE xTaskWoken = pdFALSE;
|
216 |
|
|
|
217 |
|
|
/* The previous character has been transmitted. See if there are any
|
218 |
|
|
further characters waiting transmission. */
|
219 |
|
|
if( xQueueReceiveFromISR(xCharsForTx, &cChar, &xTaskWoken) == pdTRUE )
|
220 |
|
|
{
|
221 |
|
|
/* There was another character queued - transmit it now. */
|
222 |
|
|
TDR0 = cChar;
|
223 |
|
|
}
|
224 |
|
|
else
|
225 |
|
|
{
|
226 |
|
|
/* There were no other characters to transmit. */
|
227 |
|
|
sTHREEmpty = pdTRUE;
|
228 |
|
|
|
229 |
|
|
/* Disable transmit interrupts */
|
230 |
|
|
SSR0_TIE = 0;
|
231 |
|
|
}
|
232 |
|
|
}
|