1 |
581 |
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 |
|
|
* This is a mini co-routine demo for the Rowley CrossFire LM3S102 development
|
57 |
|
|
* board. It makes use of the boards tri-colour LED and analogue input.
|
58 |
|
|
*
|
59 |
|
|
* Four co-routines are created - an 'I2C' co-routine and three 'flash'
|
60 |
|
|
* co-routines.
|
61 |
|
|
*
|
62 |
|
|
* The I2C co-routine triggers an ADC conversion then blocks on a queue to
|
63 |
|
|
* wait for the conversion result - which it receives on the queue directly
|
64 |
|
|
* from the I2C interrupt service routine. The conversion result is then
|
65 |
|
|
* scalled to a delay period. The I2C interrupt then wakes each of the
|
66 |
|
|
* flash co-routines before itself delaying for the calculated period and
|
67 |
|
|
* then repeating the whole process.
|
68 |
|
|
*
|
69 |
|
|
* When woken by the I2C co-routine the flash co-routines each block for
|
70 |
|
|
* a given period, illuminate an LED for a fixed period, then go back to
|
71 |
|
|
* sleep to wait for the next cycle. The uxIndex parameter of the flash
|
72 |
|
|
* co-routines is used to ensure that each flashes a different LED, and that
|
73 |
|
|
* the delay periods are such that the LED's get flashed in sequence.
|
74 |
|
|
*/
|
75 |
|
|
|
76 |
|
|
|
77 |
|
|
/* Scheduler include files. */
|
78 |
|
|
#include "FreeRTOS.h"
|
79 |
|
|
#include "task.h"
|
80 |
|
|
#include "queue.h"
|
81 |
|
|
#include "croutine.h"
|
82 |
|
|
|
83 |
|
|
/* Demo application include files. */
|
84 |
|
|
#include "partest.h"
|
85 |
|
|
|
86 |
|
|
/* Library include files. */
|
87 |
|
|
#include "DriverLib.h"
|
88 |
|
|
|
89 |
|
|
/* States of the I2C master interface. */
|
90 |
|
|
#define mainI2C_IDLE 0
|
91 |
|
|
#define mainI2C_READ_1 1
|
92 |
|
|
#define mainI2C_READ_2 2
|
93 |
|
|
#define mainI2C_READ_DONE 3
|
94 |
|
|
|
95 |
|
|
#define mainZERO_LENGTH 0
|
96 |
|
|
|
97 |
|
|
/* Address of the A2D IC on the CrossFire board. */
|
98 |
|
|
#define mainI2CAddress 0x4D
|
99 |
|
|
|
100 |
|
|
/* The queue used to send data from the I2C ISR to the co-routine should never
|
101 |
|
|
contain more than one item as the same co-routine is used to trigger the I2C
|
102 |
|
|
activity. */
|
103 |
|
|
#define mainQUEUE_LENGTH 1
|
104 |
|
|
|
105 |
|
|
/* The CrossFire board contains a tri-colour LED. */
|
106 |
|
|
#define mainNUM_LEDs 3
|
107 |
|
|
|
108 |
|
|
/* The I2C co-routine has a higher priority than the flash co-routines. This
|
109 |
|
|
is not really necessary as when the I2C co-routine is active the other
|
110 |
|
|
co-routines are delaying. */
|
111 |
|
|
#define mainI2c_CO_ROUTINE_PRIORITY 1
|
112 |
|
|
|
113 |
|
|
|
114 |
|
|
/* The current state of the I2C master. */
|
115 |
|
|
static volatile unsigned portBASE_TYPE uxState = mainI2C_IDLE;
|
116 |
|
|
|
117 |
|
|
/* The delay period derived from the A2D value. */
|
118 |
|
|
static volatile portBASE_TYPE uxDelay = 250;
|
119 |
|
|
|
120 |
|
|
/* The queue used to communicate between the I2C interrupt and the I2C
|
121 |
|
|
co-routine. */
|
122 |
|
|
static xQueueHandle xADCQueue;
|
123 |
|
|
|
124 |
|
|
/* The queue used to synchronise the flash co-routines. */
|
125 |
|
|
static xQueueHandle xDelayQueue;
|
126 |
|
|
|
127 |
|
|
/*
|
128 |
|
|
* Sets up the PLL, I2C and GPIO used by the demo.
|
129 |
|
|
*/
|
130 |
|
|
static void prvSetupHardware( void );
|
131 |
|
|
|
132 |
|
|
/* The co-routines as described at the top of the file. */
|
133 |
|
|
static void vI2CCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex );
|
134 |
|
|
static void vFlashCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex );
|
135 |
|
|
|
136 |
|
|
/*-----------------------------------------------------------*/
|
137 |
|
|
|
138 |
|
|
int main( void )
|
139 |
|
|
{
|
140 |
|
|
unsigned portBASE_TYPE uxCoRoutine;
|
141 |
|
|
|
142 |
|
|
/* Setup all the hardware used by this demo. */
|
143 |
|
|
prvSetupHardware();
|
144 |
|
|
|
145 |
|
|
/* Create the queue used to communicate between the ISR and I2C co-routine.
|
146 |
|
|
This can only ever contain one value. */
|
147 |
|
|
xADCQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( portTickType ) );
|
148 |
|
|
|
149 |
|
|
/* Create the queue used to synchronise the flash co-routines. The queue
|
150 |
|
|
is used to trigger three tasks, but is for synchronisation only and does
|
151 |
|
|
not pass any data. It therefore has three position each of zero length. */
|
152 |
|
|
xDelayQueue = xQueueCreate( mainNUM_LEDs, mainZERO_LENGTH );
|
153 |
|
|
|
154 |
|
|
/* Create the co-routine that initiates the i2c. */
|
155 |
|
|
xCoRoutineCreate( vI2CCoRoutine, mainI2c_CO_ROUTINE_PRIORITY, 0 );
|
156 |
|
|
|
157 |
|
|
/* Create the flash co-routines. */
|
158 |
|
|
for( uxCoRoutine = 0; uxCoRoutine < mainNUM_LEDs; uxCoRoutine++ )
|
159 |
|
|
{
|
160 |
|
|
xCoRoutineCreate( vFlashCoRoutine, tskIDLE_PRIORITY, uxCoRoutine );
|
161 |
|
|
}
|
162 |
|
|
|
163 |
|
|
/* Start the scheduler. From this point on the co-routines should
|
164 |
|
|
execute. */
|
165 |
|
|
vTaskStartScheduler();
|
166 |
|
|
|
167 |
|
|
/* Should not get here unless we did not have enough memory to start the
|
168 |
|
|
scheduler. */
|
169 |
|
|
for( ;; );
|
170 |
|
|
return 0;
|
171 |
|
|
}
|
172 |
|
|
/*-----------------------------------------------------------*/
|
173 |
|
|
|
174 |
|
|
static void prvSetupHardware( void )
|
175 |
|
|
{
|
176 |
|
|
/* Setup the PLL. */
|
177 |
|
|
SysCtlClockSet( SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_6MHZ );
|
178 |
|
|
|
179 |
|
|
/* Enable the I2C used to read the pot. */
|
180 |
|
|
SysCtlPeripheralEnable( SYSCTL_PERIPH_I2C );
|
181 |
|
|
SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOB );
|
182 |
|
|
GPIOPinTypeI2C( GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3 );
|
183 |
|
|
|
184 |
|
|
/* Initialize the I2C master. */
|
185 |
|
|
I2CMasterInit( I2C_MASTER_BASE, pdFALSE );
|
186 |
|
|
|
187 |
|
|
/* Enable the I2C master interrupt. */
|
188 |
|
|
I2CMasterIntEnable( I2C_MASTER_BASE );
|
189 |
|
|
IntEnable( INT_I2C );
|
190 |
|
|
|
191 |
|
|
/* Initialise the hardware used to talk to the LED's. */
|
192 |
|
|
vParTestInitialise();
|
193 |
|
|
}
|
194 |
|
|
/*-----------------------------------------------------------*/
|
195 |
|
|
|
196 |
|
|
static void vI2CCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
|
197 |
|
|
{
|
198 |
|
|
portTickType xADCResult;
|
199 |
|
|
static portBASE_TYPE xResult = 0, xMilliSecs, xLED;
|
200 |
|
|
|
201 |
|
|
crSTART( xHandle );
|
202 |
|
|
|
203 |
|
|
for( ;; )
|
204 |
|
|
{
|
205 |
|
|
/* Start the I2C off to read the ADC. */
|
206 |
|
|
uxState = mainI2C_READ_1;
|
207 |
|
|
I2CMasterSlaveAddrSet( I2C_MASTER_BASE, mainI2CAddress, pdTRUE );
|
208 |
|
|
I2CMasterControl( I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START );
|
209 |
|
|
|
210 |
|
|
/* Wait to receive the conversion result. */
|
211 |
|
|
crQUEUE_RECEIVE( xHandle, xADCQueue, &xADCResult, portMAX_DELAY, &xResult );
|
212 |
|
|
|
213 |
|
|
/* Scale the result to give a useful range of values for a visual
|
214 |
|
|
demo. */
|
215 |
|
|
xADCResult >>= 2;
|
216 |
|
|
xMilliSecs = xADCResult / portTICK_RATE_MS;
|
217 |
|
|
|
218 |
|
|
/* The delay is split between the four co-routines so they remain in
|
219 |
|
|
synch. */
|
220 |
|
|
uxDelay = xMilliSecs / ( mainNUM_LEDs + 1 );
|
221 |
|
|
|
222 |
|
|
/* Trigger each of the flash co-routines. */
|
223 |
|
|
for( xLED = 0; xLED < mainNUM_LEDs; xLED++ )
|
224 |
|
|
{
|
225 |
|
|
crQUEUE_SEND( xHandle, xDelayQueue, &xLED, 0, &xResult );
|
226 |
|
|
}
|
227 |
|
|
|
228 |
|
|
/* Wait for the full delay time then start again. This delay is long
|
229 |
|
|
enough to ensure the flash co-routines have done their thing and gone
|
230 |
|
|
back to sleep. */
|
231 |
|
|
crDELAY( xHandle, xMilliSecs );
|
232 |
|
|
}
|
233 |
|
|
|
234 |
|
|
crEND();
|
235 |
|
|
}
|
236 |
|
|
/*-----------------------------------------------------------*/
|
237 |
|
|
|
238 |
|
|
static void vFlashCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
|
239 |
|
|
{
|
240 |
|
|
portBASE_TYPE xResult, xNothing;
|
241 |
|
|
|
242 |
|
|
crSTART( xHandle );
|
243 |
|
|
|
244 |
|
|
for( ;; )
|
245 |
|
|
{
|
246 |
|
|
/* Wait for start of next round. */
|
247 |
|
|
crQUEUE_RECEIVE( xHandle, xDelayQueue, &xNothing, portMAX_DELAY, &xResult );
|
248 |
|
|
|
249 |
|
|
/* Wait until it is this co-routines turn to flash. */
|
250 |
|
|
crDELAY( xHandle, uxDelay * uxIndex );
|
251 |
|
|
|
252 |
|
|
/* Turn on the LED for a fixed period. */
|
253 |
|
|
vParTestSetLED( uxIndex, pdTRUE );
|
254 |
|
|
crDELAY( xHandle, uxDelay );
|
255 |
|
|
vParTestSetLED( uxIndex, pdFALSE );
|
256 |
|
|
|
257 |
|
|
/* Go back and wait for the next round. */
|
258 |
|
|
}
|
259 |
|
|
|
260 |
|
|
crEND();
|
261 |
|
|
}
|
262 |
|
|
/*-----------------------------------------------------------*/
|
263 |
|
|
|
264 |
|
|
void vI2C_ISR(void)
|
265 |
|
|
{
|
266 |
|
|
static portTickType xReading;
|
267 |
|
|
|
268 |
|
|
/* Clear the interrupt. */
|
269 |
|
|
I2CMasterIntClear( I2C_MASTER_BASE );
|
270 |
|
|
|
271 |
|
|
/* Determine what to do based on the current uxState. */
|
272 |
|
|
switch (uxState)
|
273 |
|
|
{
|
274 |
|
|
case mainI2C_IDLE: break;
|
275 |
|
|
|
276 |
|
|
case mainI2C_READ_1: /* Read ADC result high byte. */
|
277 |
|
|
xReading = I2CMasterDataGet( I2C_MASTER_BASE );
|
278 |
|
|
xReading <<= 8;
|
279 |
|
|
|
280 |
|
|
/* Continue the burst read. */
|
281 |
|
|
I2CMasterControl( I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT );
|
282 |
|
|
uxState = mainI2C_READ_2;
|
283 |
|
|
break;
|
284 |
|
|
|
285 |
|
|
case mainI2C_READ_2: /* Read ADC result low byte. */
|
286 |
|
|
xReading |= I2CMasterDataGet( I2C_MASTER_BASE );
|
287 |
|
|
|
288 |
|
|
/* Finish the burst read. */
|
289 |
|
|
I2CMasterControl( I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH );
|
290 |
|
|
uxState = mainI2C_READ_DONE;
|
291 |
|
|
break;
|
292 |
|
|
|
293 |
|
|
case mainI2C_READ_DONE: /* Complete. */
|
294 |
|
|
I2CMasterDataGet( I2C_MASTER_BASE );
|
295 |
|
|
uxState = mainI2C_IDLE;
|
296 |
|
|
|
297 |
|
|
/* Send the result to the co-routine. */
|
298 |
|
|
crQUEUE_SEND_FROM_ISR( xADCQueue, &xReading, pdFALSE );
|
299 |
|
|
break;
|
300 |
|
|
}
|
301 |
|
|
}
|
302 |
|
|
/*-----------------------------------------------------------*/
|
303 |
|
|
|
304 |
|
|
void vApplicationIdleHook( void )
|
305 |
|
|
{
|
306 |
|
|
for( ;; )
|
307 |
|
|
{
|
308 |
|
|
vCoRoutineSchedule();
|
309 |
|
|
}
|
310 |
|
|
}
|
311 |
|
|
|