2 FreeRTOS.org V4.4.0 - Copyright (C) 2003-2007 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
27 See http://www.FreeRTOS.org for documentation, latest information, license
\r
28 and contact details. Please ensure to read the configuration and relevant
\r
29 port sections of the online documentation.
\r
31 Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along
\r
32 with commercial development and support options.
\r
33 ***************************************************************************
\r
38 * This is a mini co-routine demo for the Rowley CrossFire LM3S102 development
\r
39 * board. It makes use of the boards tri-colour LED and analogue input.
\r
41 * Four co-routines are created - an 'I2C' co-routine and three 'flash'
\r
44 * The I2C co-routine triggers an ADC conversion then blocks on a queue to
\r
45 * wait for the conversion result - which it receives on the queue directly
\r
46 * from the I2C interrupt service routine. The conversion result is then
\r
47 * scalled to a delay period. The I2C interrupt then wakes each of the
\r
48 * flash co-routines before itself delaying for the calculated period and
\r
49 * then repeating the whole process.
\r
51 * When woken by the I2C co-routine the flash co-routines each block for
\r
52 * a given period, illuminate an LED for a fixed period, then go back to
\r
53 * sleep to wait for the next cycle. The uxIndex parameter of the flash
\r
54 * co-routines is used to ensure that each flashes a different LED, and that
\r
55 * the delay periods are such that the LED's get flashed in sequence.
\r
59 /* Scheduler include files. */
\r
60 #include "FreeRTOS.h"
\r
63 #include "croutine.h"
\r
65 /* Demo application include files. */
\r
66 #include "partest.h"
\r
68 /* Library include files. */
\r
69 #include "DriverLib.h"
\r
71 /* States of the I2C master interface. */
\r
72 #define mainI2C_IDLE 0
\r
73 #define mainI2C_READ_1 1
\r
74 #define mainI2C_READ_2 2
\r
75 #define mainI2C_READ_DONE 3
\r
77 #define mainZERO_LENGTH 0
\r
79 /* Address of the A2D IC on the CrossFire board. */
\r
80 #define mainI2CAddress 0x4D
\r
82 /* The queue used to send data from the I2C ISR to the co-routine should never
\r
83 contain more than one item as the same co-routine is used to trigger the I2C
\r
85 #define mainQUEUE_LENGTH 1
\r
87 /* The CrossFire board contains a tri-colour LED. */
\r
88 #define mainNUM_LEDs 3
\r
90 /* The I2C co-routine has a higher priority than the flash co-routines. This
\r
91 is not really necessary as when the I2C co-routine is active the other
\r
92 co-routines are delaying. */
\r
93 #define mainI2c_CO_ROUTINE_PRIORITY 1
\r
96 /* The current state of the I2C master. */
\r
97 static volatile unsigned portBASE_TYPE uxState = mainI2C_IDLE;
\r
99 /* The delay period derived from the A2D value. */
\r
100 static volatile portBASE_TYPE uxDelay = 250;
\r
102 /* The queue used to communicate between the I2C interrupt and the I2C
\r
104 static xQueueHandle xADCQueue;
\r
106 /* The queue used to synchronise the flash co-routines. */
\r
107 static xQueueHandle xDelayQueue;
\r
110 * Sets up the PLL, I2C and GPIO used by the demo.
\r
112 static void prvSetupHardware( void );
\r
114 /* The co-routines as described at the top of the file. */
\r
115 static void vI2CCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex );
\r
116 static void vFlashCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex );
\r
118 /*-----------------------------------------------------------*/
\r
122 unsigned portBASE_TYPE uxCoRoutine;
\r
124 /* Setup all the hardware used by this demo. */
\r
125 prvSetupHardware();
\r
127 /* Create the queue used to communicate between the ISR and I2C co-routine.
\r
128 This can only ever contain one value. */
\r
129 xADCQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( portTickType ) );
\r
131 /* Create the queue used to synchronise the flash co-routines. The queue
\r
132 is used to trigger three tasks, but is for synchronisation only and does
\r
133 not pass any data. It therefore has three position each of zero length. */
\r
134 xDelayQueue = xQueueCreate( mainNUM_LEDs, mainZERO_LENGTH );
\r
136 /* Create the co-routine that initiates the i2c. */
\r
137 xCoRoutineCreate( vI2CCoRoutine, mainI2c_CO_ROUTINE_PRIORITY, 0 );
\r
139 /* Create the flash co-routines. */
\r
140 for( uxCoRoutine = 0; uxCoRoutine < mainNUM_LEDs; uxCoRoutine++ )
\r
142 xCoRoutineCreate( vFlashCoRoutine, tskIDLE_PRIORITY, uxCoRoutine );
\r
145 /* Start the scheduler. From this point on the co-routines should
\r
147 vTaskStartScheduler();
\r
149 /* Should not get here unless we did not have enough memory to start the
\r
154 /*-----------------------------------------------------------*/
\r
156 static void prvSetupHardware( void )
\r
158 /* Setup the PLL. */
\r
159 SysCtlClockSet( SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_6MHZ );
\r
161 /* Enable the I2C used to read the pot. */
\r
162 SysCtlPeripheralEnable( SYSCTL_PERIPH_I2C );
\r
163 SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOB );
\r
164 GPIOPinTypeI2C( GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3 );
\r
166 /* Initialize the I2C master. */
\r
167 I2CMasterInit( I2C_MASTER_BASE, pdFALSE );
\r
169 /* Enable the I2C master interrupt. */
\r
170 I2CMasterIntEnable( I2C_MASTER_BASE );
\r
171 IntEnable( INT_I2C );
\r
173 /* Initialise the hardware used to talk to the LED's. */
\r
174 vParTestInitialise();
\r
176 /*-----------------------------------------------------------*/
\r
178 static void vI2CCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
180 portTickType xADCResult;
\r
181 static portBASE_TYPE xResult = 0, xMilliSecs, xLED;
\r
183 crSTART( xHandle );
\r
187 /* Start the I2C off to read the ADC. */
\r
188 uxState = mainI2C_READ_1;
\r
189 I2CMasterSlaveAddrSet( I2C_MASTER_BASE, mainI2CAddress, pdTRUE );
\r
190 I2CMasterControl( I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START );
\r
192 /* Wait to receive the conversion result. */
\r
193 crQUEUE_RECEIVE( xHandle, xADCQueue, &xADCResult, portMAX_DELAY, &xResult );
\r
195 /* Scale the result to give a useful range of values for a visual
\r
198 xMilliSecs = xADCResult / portTICK_RATE_MS;
\r
200 /* The delay is split between the four co-routines so they remain in
\r
202 uxDelay = xMilliSecs / ( mainNUM_LEDs + 1 );
\r
204 /* Trigger each of the flash co-routines. */
\r
205 for( xLED = 0; xLED < mainNUM_LEDs; xLED++ )
\r
207 crQUEUE_SEND( xHandle, xDelayQueue, &xLED, 0, &xResult );
\r
210 /* Wait for the full delay time then start again. This delay is long
\r
211 enough to ensure the flash co-routines have done their thing and gone
\r
213 crDELAY( xHandle, xMilliSecs );
\r
218 /*-----------------------------------------------------------*/
\r
220 static void vFlashCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
222 portBASE_TYPE xResult, xNothing;
\r
224 crSTART( xHandle );
\r
228 /* Wait for start of next round. */
\r
229 crQUEUE_RECEIVE( xHandle, xDelayQueue, &xNothing, portMAX_DELAY, &xResult );
\r
231 /* Wait until it is this co-routines turn to flash. */
\r
232 crDELAY( xHandle, uxDelay * uxIndex );
\r
234 /* Turn on the LED for a fixed period. */
\r
235 vParTestSetLED( uxIndex, pdTRUE );
\r
236 crDELAY( xHandle, uxDelay );
\r
237 vParTestSetLED( uxIndex, pdFALSE );
\r
239 /* Go back and wait for the next round. */
\r
244 /*-----------------------------------------------------------*/
\r
246 void vI2C_ISR(void)
\r
248 static portTickType xReading;
\r
250 /* Clear the interrupt. */
\r
251 I2CMasterIntClear( I2C_MASTER_BASE );
\r
253 /* Determine what to do based on the current uxState. */
\r
256 case mainI2C_IDLE: break;
\r
258 case mainI2C_READ_1: /* Read ADC result high byte. */
\r
259 xReading = I2CMasterDataGet( I2C_MASTER_BASE );
\r
262 /* Continue the burst read. */
\r
263 I2CMasterControl( I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT );
\r
264 uxState = mainI2C_READ_2;
\r
267 case mainI2C_READ_2: /* Read ADC result low byte. */
\r
268 xReading |= I2CMasterDataGet( I2C_MASTER_BASE );
\r
270 /* Finish the burst read. */
\r
271 I2CMasterControl( I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH );
\r
272 uxState = mainI2C_READ_DONE;
\r
275 case mainI2C_READ_DONE: /* Complete. */
\r
276 I2CMasterDataGet( I2C_MASTER_BASE );
\r
277 uxState = mainI2C_IDLE;
\r
279 /* Send the result to the co-routine. */
\r
280 crQUEUE_SEND_FROM_ISR( xADCQueue, &xReading, pdFALSE );
\r
284 /*-----------------------------------------------------------*/
\r
286 void vApplicationIdleHook( void )
\r
290 vCoRoutineSchedule();
\r