2 FreeRTOS V7.5.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
67 * This is a mini co-routine demo for the Rowley CrossFire LM3S102 development
\r
68 * board. It makes use of the boards tri-colour LED and analogue input.
\r
70 * Four co-routines are created - an 'I2C' co-routine and three 'flash'
\r
73 * The I2C co-routine triggers an ADC conversion then blocks on a queue to
\r
74 * wait for the conversion result - which it receives on the queue directly
\r
75 * from the I2C interrupt service routine. The conversion result is then
\r
76 * scalled to a delay period. The I2C interrupt then wakes each of the
\r
77 * flash co-routines before itself delaying for the calculated period and
\r
78 * then repeating the whole process.
\r
80 * When woken by the I2C co-routine the flash co-routines each block for
\r
81 * a given period, illuminate an LED for a fixed period, then go back to
\r
82 * sleep to wait for the next cycle. The uxIndex parameter of the flash
\r
83 * co-routines is used to ensure that each flashes a different LED, and that
\r
84 * the delay periods are such that the LED's get flashed in sequence.
\r
88 /* Scheduler include files. */
\r
89 #include "FreeRTOS.h"
\r
92 #include "croutine.h"
\r
94 /* Demo application include files. */
\r
95 #include "partest.h"
\r
97 /* Library include files. */
\r
98 #include "DriverLib.h"
\r
100 /* States of the I2C master interface. */
\r
101 #define mainI2C_IDLE 0
\r
102 #define mainI2C_READ_1 1
\r
103 #define mainI2C_READ_2 2
\r
104 #define mainI2C_READ_DONE 3
\r
106 #define mainZERO_LENGTH 0
\r
108 /* Address of the A2D IC on the CrossFire board. */
\r
109 #define mainI2CAddress 0x4D
\r
111 /* The queue used to send data from the I2C ISR to the co-routine should never
\r
112 contain more than one item as the same co-routine is used to trigger the I2C
\r
114 #define mainQUEUE_LENGTH 1
\r
116 /* The CrossFire board contains a tri-colour LED. */
\r
117 #define mainNUM_LEDs 3
\r
119 /* The I2C co-routine has a higher priority than the flash co-routines. This
\r
120 is not really necessary as when the I2C co-routine is active the other
\r
121 co-routines are delaying. */
\r
122 #define mainI2c_CO_ROUTINE_PRIORITY 1
\r
125 /* The current state of the I2C master. */
\r
126 static volatile unsigned portBASE_TYPE uxState = mainI2C_IDLE;
\r
128 /* The delay period derived from the A2D value. */
\r
129 static volatile portBASE_TYPE uxDelay = 250;
\r
131 /* The queue used to communicate between the I2C interrupt and the I2C
\r
133 static xQueueHandle xADCQueue;
\r
135 /* The queue used to synchronise the flash co-routines. */
\r
136 static xQueueHandle xDelayQueue;
\r
139 * Sets up the PLL, I2C and GPIO used by the demo.
\r
141 static void prvSetupHardware( void );
\r
143 /* The co-routines as described at the top of the file. */
\r
144 static void vI2CCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex );
\r
145 static void vFlashCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex );
\r
147 /*-----------------------------------------------------------*/
\r
151 unsigned portBASE_TYPE uxCoRoutine;
\r
153 /* Setup all the hardware used by this demo. */
\r
154 prvSetupHardware();
\r
156 /* Create the queue used to communicate between the ISR and I2C co-routine.
\r
157 This can only ever contain one value. */
\r
158 xADCQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( portTickType ) );
\r
160 /* Create the queue used to synchronise the flash co-routines. The queue
\r
161 is used to trigger three tasks, but is for synchronisation only and does
\r
162 not pass any data. It therefore has three position each of zero length. */
\r
163 xDelayQueue = xQueueCreate( mainNUM_LEDs, mainZERO_LENGTH );
\r
165 /* Create the co-routine that initiates the i2c. */
\r
166 xCoRoutineCreate( vI2CCoRoutine, mainI2c_CO_ROUTINE_PRIORITY, 0 );
\r
168 /* Create the flash co-routines. */
\r
169 for( uxCoRoutine = 0; uxCoRoutine < mainNUM_LEDs; uxCoRoutine++ )
\r
171 xCoRoutineCreate( vFlashCoRoutine, tskIDLE_PRIORITY, uxCoRoutine );
\r
174 /* Start the scheduler. From this point on the co-routines should
\r
176 vTaskStartScheduler();
\r
178 /* Should not get here unless we did not have enough memory to start the
\r
183 /*-----------------------------------------------------------*/
\r
185 static void prvSetupHardware( void )
\r
187 /* Setup the PLL. */
\r
188 SysCtlClockSet( SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_6MHZ );
\r
190 /* Enable the I2C used to read the pot. */
\r
191 SysCtlPeripheralEnable( SYSCTL_PERIPH_I2C );
\r
192 SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOB );
\r
193 GPIOPinTypeI2C( GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3 );
\r
195 /* Initialize the I2C master. */
\r
196 I2CMasterInit( I2C_MASTER_BASE, pdFALSE );
\r
198 /* Enable the I2C master interrupt. */
\r
199 I2CMasterIntEnable( I2C_MASTER_BASE );
\r
200 IntEnable( INT_I2C );
\r
202 /* Initialise the hardware used to talk to the LED's. */
\r
203 vParTestInitialise();
\r
205 /*-----------------------------------------------------------*/
\r
207 static void vI2CCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
209 portTickType xADCResult;
\r
210 static portBASE_TYPE xResult = 0, xMilliSecs, xLED;
\r
212 crSTART( xHandle );
\r
216 /* Start the I2C off to read the ADC. */
\r
217 uxState = mainI2C_READ_1;
\r
218 I2CMasterSlaveAddrSet( I2C_MASTER_BASE, mainI2CAddress, pdTRUE );
\r
219 I2CMasterControl( I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START );
\r
221 /* Wait to receive the conversion result. */
\r
222 crQUEUE_RECEIVE( xHandle, xADCQueue, &xADCResult, portMAX_DELAY, &xResult );
\r
224 /* Scale the result to give a useful range of values for a visual
\r
227 xMilliSecs = xADCResult / portTICK_RATE_MS;
\r
229 /* The delay is split between the four co-routines so they remain in
\r
231 uxDelay = xMilliSecs / ( mainNUM_LEDs + 1 );
\r
233 /* Trigger each of the flash co-routines. */
\r
234 for( xLED = 0; xLED < mainNUM_LEDs; xLED++ )
\r
236 crQUEUE_SEND( xHandle, xDelayQueue, &xLED, 0, &xResult );
\r
239 /* Wait for the full delay time then start again. This delay is long
\r
240 enough to ensure the flash co-routines have done their thing and gone
\r
242 crDELAY( xHandle, xMilliSecs );
\r
247 /*-----------------------------------------------------------*/
\r
249 static void vFlashCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
251 portBASE_TYPE xResult, xNothing;
\r
253 crSTART( xHandle );
\r
257 /* Wait for start of next round. */
\r
258 crQUEUE_RECEIVE( xHandle, xDelayQueue, &xNothing, portMAX_DELAY, &xResult );
\r
260 /* Wait until it is this co-routines turn to flash. */
\r
261 crDELAY( xHandle, uxDelay * uxIndex );
\r
263 /* Turn on the LED for a fixed period. */
\r
264 vParTestSetLED( uxIndex, pdTRUE );
\r
265 crDELAY( xHandle, uxDelay );
\r
266 vParTestSetLED( uxIndex, pdFALSE );
\r
268 /* Go back and wait for the next round. */
\r
273 /*-----------------------------------------------------------*/
\r
275 void vI2C_ISR(void)
\r
277 static portTickType xReading;
\r
279 /* Clear the interrupt. */
\r
280 I2CMasterIntClear( I2C_MASTER_BASE );
\r
282 /* Determine what to do based on the current uxState. */
\r
285 case mainI2C_IDLE: break;
\r
287 case mainI2C_READ_1: /* Read ADC result high byte. */
\r
288 xReading = I2CMasterDataGet( I2C_MASTER_BASE );
\r
291 /* Continue the burst read. */
\r
292 I2CMasterControl( I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT );
\r
293 uxState = mainI2C_READ_2;
\r
296 case mainI2C_READ_2: /* Read ADC result low byte. */
\r
297 xReading |= I2CMasterDataGet( I2C_MASTER_BASE );
\r
299 /* Finish the burst read. */
\r
300 I2CMasterControl( I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH );
\r
301 uxState = mainI2C_READ_DONE;
\r
304 case mainI2C_READ_DONE: /* Complete. */
\r
305 I2CMasterDataGet( I2C_MASTER_BASE );
\r
306 uxState = mainI2C_IDLE;
\r
308 /* Send the result to the co-routine. */
\r
309 crQUEUE_SEND_FROM_ISR( xADCQueue, &xReading, pdFALSE );
\r
313 /*-----------------------------------------------------------*/
\r
315 void vApplicationIdleHook( void )
\r
319 vCoRoutineSchedule();
\r