2 FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.
\r
4 ***************************************************************************
\r
8 * + New to FreeRTOS, *
\r
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
\r
10 * + Looking for basic training, *
\r
11 * + Wanting to improve your FreeRTOS skills and productivity *
\r
13 * then take a look at the FreeRTOS eBook *
\r
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
\r
16 * http://www.FreeRTOS.org/Documentation *
\r
18 * A pdf reference manual is also available. Both are usually delivered *
\r
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
\r
20 * and 8pm GMT (although please allow up to 24 hours in case of *
\r
21 * exceptional circumstances). Thank you for your support! *
\r
23 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 ***NOTE*** The exception to the GPL is included to allow you to distribute
\r
31 a combined work that includes FreeRTOS without being obliged to provide the
\r
32 source code for proprietary components outside of the FreeRTOS kernel.
\r
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
35 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
56 * This is a mini co-routine demo for the Rowley CrossFire LM3S102 development
\r
57 * board. It makes use of the boards tri-colour LED and analogue input.
\r
59 * Four co-routines are created - an 'I2C' co-routine and three 'flash'
\r
62 * The I2C co-routine triggers an ADC conversion then blocks on a queue to
\r
63 * wait for the conversion result - which it receives on the queue directly
\r
64 * from the I2C interrupt service routine. The conversion result is then
\r
65 * scalled to a delay period. The I2C interrupt then wakes each of the
\r
66 * flash co-routines before itself delaying for the calculated period and
\r
67 * then repeating the whole process.
\r
69 * When woken by the I2C co-routine the flash co-routines each block for
\r
70 * a given period, illuminate an LED for a fixed period, then go back to
\r
71 * sleep to wait for the next cycle. The uxIndex parameter of the flash
\r
72 * co-routines is used to ensure that each flashes a different LED, and that
\r
73 * the delay periods are such that the LED's get flashed in sequence.
\r
77 /* Scheduler include files. */
\r
78 #include "FreeRTOS.h"
\r
81 #include "croutine.h"
\r
83 /* Demo application include files. */
\r
84 #include "partest.h"
\r
86 /* Library include files. */
\r
87 #include "DriverLib.h"
\r
89 /* States of the I2C master interface. */
\r
90 #define mainI2C_IDLE 0
\r
91 #define mainI2C_READ_1 1
\r
92 #define mainI2C_READ_2 2
\r
93 #define mainI2C_READ_DONE 3
\r
95 #define mainZERO_LENGTH 0
\r
97 /* Address of the A2D IC on the CrossFire board. */
\r
98 #define mainI2CAddress 0x4D
\r
100 /* The queue used to send data from the I2C ISR to the co-routine should never
\r
101 contain more than one item as the same co-routine is used to trigger the I2C
\r
103 #define mainQUEUE_LENGTH 1
\r
105 /* The CrossFire board contains a tri-colour LED. */
\r
106 #define mainNUM_LEDs 3
\r
108 /* The I2C co-routine has a higher priority than the flash co-routines. This
\r
109 is not really necessary as when the I2C co-routine is active the other
\r
110 co-routines are delaying. */
\r
111 #define mainI2c_CO_ROUTINE_PRIORITY 1
\r
114 /* The current state of the I2C master. */
\r
115 static volatile unsigned portBASE_TYPE uxState = mainI2C_IDLE;
\r
117 /* The delay period derived from the A2D value. */
\r
118 static volatile portBASE_TYPE uxDelay = 250;
\r
120 /* The queue used to communicate between the I2C interrupt and the I2C
\r
122 static xQueueHandle xADCQueue;
\r
124 /* The queue used to synchronise the flash co-routines. */
\r
125 static xQueueHandle xDelayQueue;
\r
128 * Sets up the PLL, I2C and GPIO used by the demo.
\r
130 static void prvSetupHardware( void );
\r
132 /* The co-routines as described at the top of the file. */
\r
133 static void vI2CCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex );
\r
134 static void vFlashCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex );
\r
136 /*-----------------------------------------------------------*/
\r
140 unsigned portBASE_TYPE uxCoRoutine;
\r
142 /* Setup all the hardware used by this demo. */
\r
143 prvSetupHardware();
\r
145 /* Create the queue used to communicate between the ISR and I2C co-routine.
\r
146 This can only ever contain one value. */
\r
147 xADCQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( portTickType ) );
\r
149 /* Create the queue used to synchronise the flash co-routines. The queue
\r
150 is used to trigger three tasks, but is for synchronisation only and does
\r
151 not pass any data. It therefore has three position each of zero length. */
\r
152 xDelayQueue = xQueueCreate( mainNUM_LEDs, mainZERO_LENGTH );
\r
154 /* Create the co-routine that initiates the i2c. */
\r
155 xCoRoutineCreate( vI2CCoRoutine, mainI2c_CO_ROUTINE_PRIORITY, 0 );
\r
157 /* Create the flash co-routines. */
\r
158 for( uxCoRoutine = 0; uxCoRoutine < mainNUM_LEDs; uxCoRoutine++ )
\r
160 xCoRoutineCreate( vFlashCoRoutine, tskIDLE_PRIORITY, uxCoRoutine );
\r
163 /* Start the scheduler. From this point on the co-routines should
\r
165 vTaskStartScheduler();
\r
167 /* Should not get here unless we did not have enough memory to start the
\r
172 /*-----------------------------------------------------------*/
\r
174 static void prvSetupHardware( void )
\r
176 /* Setup the PLL. */
\r
177 SysCtlClockSet( SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_6MHZ );
\r
179 /* Enable the I2C used to read the pot. */
\r
180 SysCtlPeripheralEnable( SYSCTL_PERIPH_I2C );
\r
181 SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOB );
\r
182 GPIOPinTypeI2C( GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3 );
\r
184 /* Initialize the I2C master. */
\r
185 I2CMasterInit( I2C_MASTER_BASE, pdFALSE );
\r
187 /* Enable the I2C master interrupt. */
\r
188 I2CMasterIntEnable( I2C_MASTER_BASE );
\r
189 IntEnable( INT_I2C );
\r
191 /* Initialise the hardware used to talk to the LED's. */
\r
192 vParTestInitialise();
\r
194 /*-----------------------------------------------------------*/
\r
196 static void vI2CCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
198 portTickType xADCResult;
\r
199 static portBASE_TYPE xResult = 0, xMilliSecs, xLED;
\r
201 crSTART( xHandle );
\r
205 /* Start the I2C off to read the ADC. */
\r
206 uxState = mainI2C_READ_1;
\r
207 I2CMasterSlaveAddrSet( I2C_MASTER_BASE, mainI2CAddress, pdTRUE );
\r
208 I2CMasterControl( I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START );
\r
210 /* Wait to receive the conversion result. */
\r
211 crQUEUE_RECEIVE( xHandle, xADCQueue, &xADCResult, portMAX_DELAY, &xResult );
\r
213 /* Scale the result to give a useful range of values for a visual
\r
216 xMilliSecs = xADCResult / portTICK_RATE_MS;
\r
218 /* The delay is split between the four co-routines so they remain in
\r
220 uxDelay = xMilliSecs / ( mainNUM_LEDs + 1 );
\r
222 /* Trigger each of the flash co-routines. */
\r
223 for( xLED = 0; xLED < mainNUM_LEDs; xLED++ )
\r
225 crQUEUE_SEND( xHandle, xDelayQueue, &xLED, 0, &xResult );
\r
228 /* Wait for the full delay time then start again. This delay is long
\r
229 enough to ensure the flash co-routines have done their thing and gone
\r
231 crDELAY( xHandle, xMilliSecs );
\r
236 /*-----------------------------------------------------------*/
\r
238 static void vFlashCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
240 portBASE_TYPE xResult, xNothing;
\r
242 crSTART( xHandle );
\r
246 /* Wait for start of next round. */
\r
247 crQUEUE_RECEIVE( xHandle, xDelayQueue, &xNothing, portMAX_DELAY, &xResult );
\r
249 /* Wait until it is this co-routines turn to flash. */
\r
250 crDELAY( xHandle, uxDelay * uxIndex );
\r
252 /* Turn on the LED for a fixed period. */
\r
253 vParTestSetLED( uxIndex, pdTRUE );
\r
254 crDELAY( xHandle, uxDelay );
\r
255 vParTestSetLED( uxIndex, pdFALSE );
\r
257 /* Go back and wait for the next round. */
\r
262 /*-----------------------------------------------------------*/
\r
264 void vI2C_ISR(void)
\r
266 static portTickType xReading;
\r
268 /* Clear the interrupt. */
\r
269 I2CMasterIntClear( I2C_MASTER_BASE );
\r
271 /* Determine what to do based on the current uxState. */
\r
274 case mainI2C_IDLE: break;
\r
276 case mainI2C_READ_1: /* Read ADC result high byte. */
\r
277 xReading = I2CMasterDataGet( I2C_MASTER_BASE );
\r
280 /* Continue the burst read. */
\r
281 I2CMasterControl( I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT );
\r
282 uxState = mainI2C_READ_2;
\r
285 case mainI2C_READ_2: /* Read ADC result low byte. */
\r
286 xReading |= I2CMasterDataGet( I2C_MASTER_BASE );
\r
288 /* Finish the burst read. */
\r
289 I2CMasterControl( I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH );
\r
290 uxState = mainI2C_READ_DONE;
\r
293 case mainI2C_READ_DONE: /* Complete. */
\r
294 I2CMasterDataGet( I2C_MASTER_BASE );
\r
295 uxState = mainI2C_IDLE;
\r
297 /* Send the result to the co-routine. */
\r
298 crQUEUE_SEND_FROM_ISR( xADCQueue, &xReading, pdFALSE );
\r
302 /*-----------------------------------------------------------*/
\r
304 void vApplicationIdleHook( void )
\r
308 vCoRoutineSchedule();
\r