2 FreeRTOS V7.4.1 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not it can be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
77 * This is a mini co-routine demo for the Rowley CrossFire LM3S102 development
\r
78 * board. It makes use of the boards tri-colour LED and analogue input.
\r
80 * Four co-routines are created - an 'I2C' co-routine and three 'flash'
\r
83 * The I2C co-routine triggers an ADC conversion then blocks on a queue to
\r
84 * wait for the conversion result - which it receives on the queue directly
\r
85 * from the I2C interrupt service routine. The conversion result is then
\r
86 * scalled to a delay period. The I2C interrupt then wakes each of the
\r
87 * flash co-routines before itself delaying for the calculated period and
\r
88 * then repeating the whole process.
\r
90 * When woken by the I2C co-routine the flash co-routines each block for
\r
91 * a given period, illuminate an LED for a fixed period, then go back to
\r
92 * sleep to wait for the next cycle. The uxIndex parameter of the flash
\r
93 * co-routines is used to ensure that each flashes a different LED, and that
\r
94 * the delay periods are such that the LED's get flashed in sequence.
\r
98 /* Scheduler include files. */
\r
99 #include "FreeRTOS.h"
\r
102 #include "croutine.h"
\r
104 /* Demo application include files. */
\r
105 #include "partest.h"
\r
107 /* Library include files. */
\r
108 #include "DriverLib.h"
\r
110 /* States of the I2C master interface. */
\r
111 #define mainI2C_IDLE 0
\r
112 #define mainI2C_READ_1 1
\r
113 #define mainI2C_READ_2 2
\r
114 #define mainI2C_READ_DONE 3
\r
116 #define mainZERO_LENGTH 0
\r
118 /* Address of the A2D IC on the CrossFire board. */
\r
119 #define mainI2CAddress 0x4D
\r
121 /* The queue used to send data from the I2C ISR to the co-routine should never
\r
122 contain more than one item as the same co-routine is used to trigger the I2C
\r
124 #define mainQUEUE_LENGTH 1
\r
126 /* The CrossFire board contains a tri-colour LED. */
\r
127 #define mainNUM_LEDs 3
\r
129 /* The I2C co-routine has a higher priority than the flash co-routines. This
\r
130 is not really necessary as when the I2C co-routine is active the other
\r
131 co-routines are delaying. */
\r
132 #define mainI2c_CO_ROUTINE_PRIORITY 1
\r
135 /* The current state of the I2C master. */
\r
136 static volatile unsigned portBASE_TYPE uxState = mainI2C_IDLE;
\r
138 /* The delay period derived from the A2D value. */
\r
139 static volatile portBASE_TYPE uxDelay = 250;
\r
141 /* The queue used to communicate between the I2C interrupt and the I2C
\r
143 static xQueueHandle xADCQueue;
\r
145 /* The queue used to synchronise the flash co-routines. */
\r
146 static xQueueHandle xDelayQueue;
\r
149 * Sets up the PLL, I2C and GPIO used by the demo.
\r
151 static void prvSetupHardware( void );
\r
153 /* The co-routines as described at the top of the file. */
\r
154 static void vI2CCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex );
\r
155 static void vFlashCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex );
\r
157 /*-----------------------------------------------------------*/
\r
161 unsigned portBASE_TYPE uxCoRoutine;
\r
163 /* Setup all the hardware used by this demo. */
\r
164 prvSetupHardware();
\r
166 /* Create the queue used to communicate between the ISR and I2C co-routine.
\r
167 This can only ever contain one value. */
\r
168 xADCQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( portTickType ) );
\r
170 /* Create the queue used to synchronise the flash co-routines. The queue
\r
171 is used to trigger three tasks, but is for synchronisation only and does
\r
172 not pass any data. It therefore has three position each of zero length. */
\r
173 xDelayQueue = xQueueCreate( mainNUM_LEDs, mainZERO_LENGTH );
\r
175 /* Create the co-routine that initiates the i2c. */
\r
176 xCoRoutineCreate( vI2CCoRoutine, mainI2c_CO_ROUTINE_PRIORITY, 0 );
\r
178 /* Create the flash co-routines. */
\r
179 for( uxCoRoutine = 0; uxCoRoutine < mainNUM_LEDs; uxCoRoutine++ )
\r
181 xCoRoutineCreate( vFlashCoRoutine, tskIDLE_PRIORITY, uxCoRoutine );
\r
184 /* Start the scheduler. From this point on the co-routines should
\r
186 vTaskStartScheduler();
\r
188 /* Should not get here unless we did not have enough memory to start the
\r
193 /*-----------------------------------------------------------*/
\r
195 static void prvSetupHardware( void )
\r
197 /* Setup the PLL. */
\r
198 SysCtlClockSet( SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_6MHZ );
\r
200 /* Enable the I2C used to read the pot. */
\r
201 SysCtlPeripheralEnable( SYSCTL_PERIPH_I2C );
\r
202 SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOB );
\r
203 GPIOPinTypeI2C( GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3 );
\r
205 /* Initialize the I2C master. */
\r
206 I2CMasterInit( I2C_MASTER_BASE, pdFALSE );
\r
208 /* Enable the I2C master interrupt. */
\r
209 I2CMasterIntEnable( I2C_MASTER_BASE );
\r
210 IntEnable( INT_I2C );
\r
212 /* Initialise the hardware used to talk to the LED's. */
\r
213 vParTestInitialise();
\r
215 /*-----------------------------------------------------------*/
\r
217 static void vI2CCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
219 portTickType xADCResult;
\r
220 static portBASE_TYPE xResult = 0, xMilliSecs, xLED;
\r
222 crSTART( xHandle );
\r
226 /* Start the I2C off to read the ADC. */
\r
227 uxState = mainI2C_READ_1;
\r
228 I2CMasterSlaveAddrSet( I2C_MASTER_BASE, mainI2CAddress, pdTRUE );
\r
229 I2CMasterControl( I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START );
\r
231 /* Wait to receive the conversion result. */
\r
232 crQUEUE_RECEIVE( xHandle, xADCQueue, &xADCResult, portMAX_DELAY, &xResult );
\r
234 /* Scale the result to give a useful range of values for a visual
\r
237 xMilliSecs = xADCResult / portTICK_RATE_MS;
\r
239 /* The delay is split between the four co-routines so they remain in
\r
241 uxDelay = xMilliSecs / ( mainNUM_LEDs + 1 );
\r
243 /* Trigger each of the flash co-routines. */
\r
244 for( xLED = 0; xLED < mainNUM_LEDs; xLED++ )
\r
246 crQUEUE_SEND( xHandle, xDelayQueue, &xLED, 0, &xResult );
\r
249 /* Wait for the full delay time then start again. This delay is long
\r
250 enough to ensure the flash co-routines have done their thing and gone
\r
252 crDELAY( xHandle, xMilliSecs );
\r
257 /*-----------------------------------------------------------*/
\r
259 static void vFlashCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
261 portBASE_TYPE xResult, xNothing;
\r
263 crSTART( xHandle );
\r
267 /* Wait for start of next round. */
\r
268 crQUEUE_RECEIVE( xHandle, xDelayQueue, &xNothing, portMAX_DELAY, &xResult );
\r
270 /* Wait until it is this co-routines turn to flash. */
\r
271 crDELAY( xHandle, uxDelay * uxIndex );
\r
273 /* Turn on the LED for a fixed period. */
\r
274 vParTestSetLED( uxIndex, pdTRUE );
\r
275 crDELAY( xHandle, uxDelay );
\r
276 vParTestSetLED( uxIndex, pdFALSE );
\r
278 /* Go back and wait for the next round. */
\r
283 /*-----------------------------------------------------------*/
\r
285 void vI2C_ISR(void)
\r
287 static portTickType xReading;
\r
289 /* Clear the interrupt. */
\r
290 I2CMasterIntClear( I2C_MASTER_BASE );
\r
292 /* Determine what to do based on the current uxState. */
\r
295 case mainI2C_IDLE: break;
\r
297 case mainI2C_READ_1: /* Read ADC result high byte. */
\r
298 xReading = I2CMasterDataGet( I2C_MASTER_BASE );
\r
301 /* Continue the burst read. */
\r
302 I2CMasterControl( I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT );
\r
303 uxState = mainI2C_READ_2;
\r
306 case mainI2C_READ_2: /* Read ADC result low byte. */
\r
307 xReading |= I2CMasterDataGet( I2C_MASTER_BASE );
\r
309 /* Finish the burst read. */
\r
310 I2CMasterControl( I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH );
\r
311 uxState = mainI2C_READ_DONE;
\r
314 case mainI2C_READ_DONE: /* Complete. */
\r
315 I2CMasterDataGet( I2C_MASTER_BASE );
\r
316 uxState = mainI2C_IDLE;
\r
318 /* Send the result to the co-routine. */
\r
319 crQUEUE_SEND_FROM_ISR( xADCQueue, &xReading, pdFALSE );
\r
323 /*-----------------------------------------------------------*/
\r
325 void vApplicationIdleHook( void )
\r
329 vCoRoutineSchedule();
\r