2 FreeRTOS V7.2.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\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 modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or 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 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
69 * This is a mini co-routine demo for the Rowley CrossFire LM3S102 development
\r
70 * board. It makes use of the boards tri-colour LED and analogue input.
\r
72 * Four co-routines are created - an 'I2C' co-routine and three 'flash'
\r
75 * The I2C co-routine triggers an ADC conversion then blocks on a queue to
\r
76 * wait for the conversion result - which it receives on the queue directly
\r
77 * from the I2C interrupt service routine. The conversion result is then
\r
78 * scalled to a delay period. The I2C interrupt then wakes each of the
\r
79 * flash co-routines before itself delaying for the calculated period and
\r
80 * then repeating the whole process.
\r
82 * When woken by the I2C co-routine the flash co-routines each block for
\r
83 * a given period, illuminate an LED for a fixed period, then go back to
\r
84 * sleep to wait for the next cycle. The uxIndex parameter of the flash
\r
85 * co-routines is used to ensure that each flashes a different LED, and that
\r
86 * the delay periods are such that the LED's get flashed in sequence.
\r
90 /* Scheduler include files. */
\r
91 #include "FreeRTOS.h"
\r
94 #include "croutine.h"
\r
96 /* Demo application include files. */
\r
97 #include "partest.h"
\r
99 /* Library include files. */
\r
100 #include "DriverLib.h"
\r
102 /* States of the I2C master interface. */
\r
103 #define mainI2C_IDLE 0
\r
104 #define mainI2C_READ_1 1
\r
105 #define mainI2C_READ_2 2
\r
106 #define mainI2C_READ_DONE 3
\r
108 #define mainZERO_LENGTH 0
\r
110 /* Address of the A2D IC on the CrossFire board. */
\r
111 #define mainI2CAddress 0x4D
\r
113 /* The queue used to send data from the I2C ISR to the co-routine should never
\r
114 contain more than one item as the same co-routine is used to trigger the I2C
\r
116 #define mainQUEUE_LENGTH 1
\r
118 /* The CrossFire board contains a tri-colour LED. */
\r
119 #define mainNUM_LEDs 3
\r
121 /* The I2C co-routine has a higher priority than the flash co-routines. This
\r
122 is not really necessary as when the I2C co-routine is active the other
\r
123 co-routines are delaying. */
\r
124 #define mainI2c_CO_ROUTINE_PRIORITY 1
\r
127 /* The current state of the I2C master. */
\r
128 static volatile unsigned portBASE_TYPE uxState = mainI2C_IDLE;
\r
130 /* The delay period derived from the A2D value. */
\r
131 static volatile portBASE_TYPE uxDelay = 250;
\r
133 /* The queue used to communicate between the I2C interrupt and the I2C
\r
135 static xQueueHandle xADCQueue;
\r
137 /* The queue used to synchronise the flash co-routines. */
\r
138 static xQueueHandle xDelayQueue;
\r
141 * Sets up the PLL, I2C and GPIO used by the demo.
\r
143 static void prvSetupHardware( void );
\r
145 /* The co-routines as described at the top of the file. */
\r
146 static void vI2CCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex );
\r
147 static void vFlashCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex );
\r
149 /*-----------------------------------------------------------*/
\r
153 unsigned portBASE_TYPE uxCoRoutine;
\r
155 /* Setup all the hardware used by this demo. */
\r
156 prvSetupHardware();
\r
158 /* Create the queue used to communicate between the ISR and I2C co-routine.
\r
159 This can only ever contain one value. */
\r
160 xADCQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( portTickType ) );
\r
162 /* Create the queue used to synchronise the flash co-routines. The queue
\r
163 is used to trigger three tasks, but is for synchronisation only and does
\r
164 not pass any data. It therefore has three position each of zero length. */
\r
165 xDelayQueue = xQueueCreate( mainNUM_LEDs, mainZERO_LENGTH );
\r
167 /* Create the co-routine that initiates the i2c. */
\r
168 xCoRoutineCreate( vI2CCoRoutine, mainI2c_CO_ROUTINE_PRIORITY, 0 );
\r
170 /* Create the flash co-routines. */
\r
171 for( uxCoRoutine = 0; uxCoRoutine < mainNUM_LEDs; uxCoRoutine++ )
\r
173 xCoRoutineCreate( vFlashCoRoutine, tskIDLE_PRIORITY, uxCoRoutine );
\r
176 /* Start the scheduler. From this point on the co-routines should
\r
178 vTaskStartScheduler();
\r
180 /* Should not get here unless we did not have enough memory to start the
\r
185 /*-----------------------------------------------------------*/
\r
187 static void prvSetupHardware( void )
\r
189 /* Setup the PLL. */
\r
190 SysCtlClockSet( SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_6MHZ );
\r
192 /* Enable the I2C used to read the pot. */
\r
193 SysCtlPeripheralEnable( SYSCTL_PERIPH_I2C );
\r
194 SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOB );
\r
195 GPIOPinTypeI2C( GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3 );
\r
197 /* Initialize the I2C master. */
\r
198 I2CMasterInit( I2C_MASTER_BASE, pdFALSE );
\r
200 /* Enable the I2C master interrupt. */
\r
201 I2CMasterIntEnable( I2C_MASTER_BASE );
\r
202 IntEnable( INT_I2C );
\r
204 /* Initialise the hardware used to talk to the LED's. */
\r
205 vParTestInitialise();
\r
207 /*-----------------------------------------------------------*/
\r
209 static void vI2CCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
211 portTickType xADCResult;
\r
212 static portBASE_TYPE xResult = 0, xMilliSecs, xLED;
\r
214 crSTART( xHandle );
\r
218 /* Start the I2C off to read the ADC. */
\r
219 uxState = mainI2C_READ_1;
\r
220 I2CMasterSlaveAddrSet( I2C_MASTER_BASE, mainI2CAddress, pdTRUE );
\r
221 I2CMasterControl( I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START );
\r
223 /* Wait to receive the conversion result. */
\r
224 crQUEUE_RECEIVE( xHandle, xADCQueue, &xADCResult, portMAX_DELAY, &xResult );
\r
226 /* Scale the result to give a useful range of values for a visual
\r
229 xMilliSecs = xADCResult / portTICK_RATE_MS;
\r
231 /* The delay is split between the four co-routines so they remain in
\r
233 uxDelay = xMilliSecs / ( mainNUM_LEDs + 1 );
\r
235 /* Trigger each of the flash co-routines. */
\r
236 for( xLED = 0; xLED < mainNUM_LEDs; xLED++ )
\r
238 crQUEUE_SEND( xHandle, xDelayQueue, &xLED, 0, &xResult );
\r
241 /* Wait for the full delay time then start again. This delay is long
\r
242 enough to ensure the flash co-routines have done their thing and gone
\r
244 crDELAY( xHandle, xMilliSecs );
\r
249 /*-----------------------------------------------------------*/
\r
251 static void vFlashCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
253 portBASE_TYPE xResult, xNothing;
\r
255 crSTART( xHandle );
\r
259 /* Wait for start of next round. */
\r
260 crQUEUE_RECEIVE( xHandle, xDelayQueue, &xNothing, portMAX_DELAY, &xResult );
\r
262 /* Wait until it is this co-routines turn to flash. */
\r
263 crDELAY( xHandle, uxDelay * uxIndex );
\r
265 /* Turn on the LED for a fixed period. */
\r
266 vParTestSetLED( uxIndex, pdTRUE );
\r
267 crDELAY( xHandle, uxDelay );
\r
268 vParTestSetLED( uxIndex, pdFALSE );
\r
270 /* Go back and wait for the next round. */
\r
275 /*-----------------------------------------------------------*/
\r
277 void vI2C_ISR(void)
\r
279 static portTickType xReading;
\r
281 /* Clear the interrupt. */
\r
282 I2CMasterIntClear( I2C_MASTER_BASE );
\r
284 /* Determine what to do based on the current uxState. */
\r
287 case mainI2C_IDLE: break;
\r
289 case mainI2C_READ_1: /* Read ADC result high byte. */
\r
290 xReading = I2CMasterDataGet( I2C_MASTER_BASE );
\r
293 /* Continue the burst read. */
\r
294 I2CMasterControl( I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT );
\r
295 uxState = mainI2C_READ_2;
\r
298 case mainI2C_READ_2: /* Read ADC result low byte. */
\r
299 xReading |= I2CMasterDataGet( I2C_MASTER_BASE );
\r
301 /* Finish the burst read. */
\r
302 I2CMasterControl( I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH );
\r
303 uxState = mainI2C_READ_DONE;
\r
306 case mainI2C_READ_DONE: /* Complete. */
\r
307 I2CMasterDataGet( I2C_MASTER_BASE );
\r
308 uxState = mainI2C_IDLE;
\r
310 /* Send the result to the co-routine. */
\r
311 crQUEUE_SEND_FROM_ISR( xADCQueue, &xReading, pdFALSE );
\r
315 /*-----------------------------------------------------------*/
\r
317 void vApplicationIdleHook( void )
\r
321 vCoRoutineSchedule();
\r