2 FreeRTOS V8.0.0:rc1 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
68 * This is a mini co-routine demo for the Rowley CrossFire LM3S102 development
\r
69 * board. It makes use of the boards tri-colour LED and analogue input.
\r
71 * Four co-routines are created - an 'I2C' co-routine and three 'flash'
\r
74 * The I2C co-routine triggers an ADC conversion then blocks on a queue to
\r
75 * wait for the conversion result - which it receives on the queue directly
\r
76 * from the I2C interrupt service routine. The conversion result is then
\r
77 * scalled to a delay period. The I2C interrupt then wakes each of the
\r
78 * flash co-routines before itself delaying for the calculated period and
\r
79 * then repeating the whole process.
\r
81 * When woken by the I2C co-routine the flash co-routines each block for
\r
82 * a given period, illuminate an LED for a fixed period, then go back to
\r
83 * sleep to wait for the next cycle. The uxIndex parameter of the flash
\r
84 * co-routines is used to ensure that each flashes a different LED, and that
\r
85 * the delay periods are such that the LED's get flashed in sequence.
\r
89 /* Scheduler include files. */
\r
90 #include "FreeRTOS.h"
\r
93 #include "croutine.h"
\r
95 /* Demo application include files. */
\r
96 #include "partest.h"
\r
98 /* Library include files. */
\r
99 #include "DriverLib.h"
\r
101 /* States of the I2C master interface. */
\r
102 #define mainI2C_IDLE 0
\r
103 #define mainI2C_READ_1 1
\r
104 #define mainI2C_READ_2 2
\r
105 #define mainI2C_READ_DONE 3
\r
107 #define mainZERO_LENGTH 0
\r
109 /* Address of the A2D IC on the CrossFire board. */
\r
110 #define mainI2CAddress 0x4D
\r
112 /* The queue used to send data from the I2C ISR to the co-routine should never
\r
113 contain more than one item as the same co-routine is used to trigger the I2C
\r
115 #define mainQUEUE_LENGTH 1
\r
117 /* The CrossFire board contains a tri-colour LED. */
\r
118 #define mainNUM_LEDs 3
\r
120 /* The I2C co-routine has a higher priority than the flash co-routines. This
\r
121 is not really necessary as when the I2C co-routine is active the other
\r
122 co-routines are delaying. */
\r
123 #define mainI2c_CO_ROUTINE_PRIORITY 1
\r
126 /* The current state of the I2C master. */
\r
127 static volatile unsigned portBASE_TYPE uxState = mainI2C_IDLE;
\r
129 /* The delay period derived from the A2D value. */
\r
130 static volatile portBASE_TYPE uxDelay = 250;
\r
132 /* The queue used to communicate between the I2C interrupt and the I2C
\r
134 static xQueueHandle xADCQueue;
\r
136 /* The queue used to synchronise the flash co-routines. */
\r
137 static xQueueHandle xDelayQueue;
\r
140 * Sets up the PLL, I2C and GPIO used by the demo.
\r
142 static void prvSetupHardware( void );
\r
144 /* The co-routines as described at the top of the file. */
\r
145 static void vI2CCoRoutine( CoRoutineHandle_t xHandle, unsigned portBASE_TYPE uxIndex );
\r
146 static void vFlashCoRoutine( CoRoutineHandle_t xHandle, unsigned portBASE_TYPE uxIndex );
\r
148 /*-----------------------------------------------------------*/
\r
152 unsigned portBASE_TYPE uxCoRoutine;
\r
154 /* Setup all the hardware used by this demo. */
\r
155 prvSetupHardware();
\r
157 /* Create the queue used to communicate between the ISR and I2C co-routine.
\r
158 This can only ever contain one value. */
\r
159 xADCQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( portTickType ) );
\r
161 /* Create the queue used to synchronise the flash co-routines. The queue
\r
162 is used to trigger three tasks, but is for synchronisation only and does
\r
163 not pass any data. It therefore has three position each of zero length. */
\r
164 xDelayQueue = xQueueCreate( mainNUM_LEDs, mainZERO_LENGTH );
\r
166 /* Create the co-routine that initiates the i2c. */
\r
167 xCoRoutineCreate( vI2CCoRoutine, mainI2c_CO_ROUTINE_PRIORITY, 0 );
\r
169 /* Create the flash co-routines. */
\r
170 for( uxCoRoutine = 0; uxCoRoutine < mainNUM_LEDs; uxCoRoutine++ )
\r
172 xCoRoutineCreate( vFlashCoRoutine, tskIDLE_PRIORITY, uxCoRoutine );
\r
175 /* Start the scheduler. From this point on the co-routines should
\r
177 vTaskStartScheduler();
\r
179 /* Should not get here unless we did not have enough memory to start the
\r
184 /*-----------------------------------------------------------*/
\r
186 static void prvSetupHardware( void )
\r
188 /* Setup the PLL. */
\r
189 SysCtlClockSet( SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_6MHZ );
\r
191 /* Enable the I2C used to read the pot. */
\r
192 SysCtlPeripheralEnable( SYSCTL_PERIPH_I2C );
\r
193 SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOB );
\r
194 GPIOPinTypeI2C( GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3 );
\r
196 /* Initialize the I2C master. */
\r
197 I2CMasterInit( I2C_MASTER_BASE, pdFALSE );
\r
199 /* Enable the I2C master interrupt. */
\r
200 I2CMasterIntEnable( I2C_MASTER_BASE );
\r
201 IntEnable( INT_I2C );
\r
203 /* Initialise the hardware used to talk to the LED's. */
\r
204 vParTestInitialise();
\r
206 /*-----------------------------------------------------------*/
\r
208 static void vI2CCoRoutine( CoRoutineHandle_t xHandle, unsigned portBASE_TYPE uxIndex )
\r
210 portTickType xADCResult;
\r
211 static portBASE_TYPE xResult = 0, xMilliSecs, xLED;
\r
213 crSTART( xHandle );
\r
217 /* Start the I2C off to read the ADC. */
\r
218 uxState = mainI2C_READ_1;
\r
219 I2CMasterSlaveAddrSet( I2C_MASTER_BASE, mainI2CAddress, pdTRUE );
\r
220 I2CMasterControl( I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START );
\r
222 /* Wait to receive the conversion result. */
\r
223 crQUEUE_RECEIVE( xHandle, xADCQueue, &xADCResult, portMAX_DELAY, &xResult );
\r
225 /* Scale the result to give a useful range of values for a visual
\r
228 xMilliSecs = xADCResult / portTICK_RATE_MS;
\r
230 /* The delay is split between the four co-routines so they remain in
\r
232 uxDelay = xMilliSecs / ( mainNUM_LEDs + 1 );
\r
234 /* Trigger each of the flash co-routines. */
\r
235 for( xLED = 0; xLED < mainNUM_LEDs; xLED++ )
\r
237 crQUEUE_SEND( xHandle, xDelayQueue, &xLED, 0, &xResult );
\r
240 /* Wait for the full delay time then start again. This delay is long
\r
241 enough to ensure the flash co-routines have done their thing and gone
\r
243 crDELAY( xHandle, xMilliSecs );
\r
248 /*-----------------------------------------------------------*/
\r
250 static void vFlashCoRoutine( CoRoutineHandle_t xHandle, unsigned portBASE_TYPE uxIndex )
\r
252 portBASE_TYPE xResult, xNothing;
\r
254 crSTART( xHandle );
\r
258 /* Wait for start of next round. */
\r
259 crQUEUE_RECEIVE( xHandle, xDelayQueue, &xNothing, portMAX_DELAY, &xResult );
\r
261 /* Wait until it is this co-routines turn to flash. */
\r
262 crDELAY( xHandle, uxDelay * uxIndex );
\r
264 /* Turn on the LED for a fixed period. */
\r
265 vParTestSetLED( uxIndex, pdTRUE );
\r
266 crDELAY( xHandle, uxDelay );
\r
267 vParTestSetLED( uxIndex, pdFALSE );
\r
269 /* Go back and wait for the next round. */
\r
274 /*-----------------------------------------------------------*/
\r
276 void vI2C_ISR(void)
\r
278 static portTickType xReading;
\r
280 /* Clear the interrupt. */
\r
281 I2CMasterIntClear( I2C_MASTER_BASE );
\r
283 /* Determine what to do based on the current uxState. */
\r
286 case mainI2C_IDLE: break;
\r
288 case mainI2C_READ_1: /* Read ADC result high byte. */
\r
289 xReading = I2CMasterDataGet( I2C_MASTER_BASE );
\r
292 /* Continue the burst read. */
\r
293 I2CMasterControl( I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT );
\r
294 uxState = mainI2C_READ_2;
\r
297 case mainI2C_READ_2: /* Read ADC result low byte. */
\r
298 xReading |= I2CMasterDataGet( I2C_MASTER_BASE );
\r
300 /* Finish the burst read. */
\r
301 I2CMasterControl( I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH );
\r
302 uxState = mainI2C_READ_DONE;
\r
305 case mainI2C_READ_DONE: /* Complete. */
\r
306 I2CMasterDataGet( I2C_MASTER_BASE );
\r
307 uxState = mainI2C_IDLE;
\r
309 /* Send the result to the co-routine. */
\r
310 crQUEUE_SEND_FROM_ISR( xADCQueue, &xReading, pdFALSE );
\r
314 /*-----------------------------------------------------------*/
\r
316 void vApplicationIdleHook( void )
\r
320 vCoRoutineSchedule();
\r