2 FreeRTOS V8.2.3 - Copyright (C) 2015 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
72 * This is a mini co-routine demo for the Rowley CrossFire LM3S102 development
\r
73 * board. It makes use of the boards tri-colour LED and analogue input.
\r
75 * Four co-routines are created - an 'I2C' co-routine and three 'flash'
\r
78 * The I2C co-routine triggers an ADC conversion then blocks on a queue to
\r
79 * wait for the conversion result - which it receives on the queue directly
\r
80 * from the I2C interrupt service routine. The conversion result is then
\r
81 * scalled to a delay period. The I2C interrupt then wakes each of the
\r
82 * flash co-routines before itself delaying for the calculated period and
\r
83 * then repeating the whole process.
\r
85 * When woken by the I2C co-routine the flash co-routines each block for
\r
86 * a given period, illuminate an LED for a fixed period, then go back to
\r
87 * sleep to wait for the next cycle. The uxIndex parameter of the flash
\r
88 * co-routines is used to ensure that each flashes a different LED, and that
\r
89 * the delay periods are such that the LED's get flashed in sequence.
\r
93 /* Scheduler include files. */
\r
94 #include "FreeRTOS.h"
\r
97 #include "croutine.h"
\r
99 /* Demo application include files. */
\r
100 #include "partest.h"
\r
102 /* Library include files. */
\r
103 #include "DriverLib.h"
\r
105 /* States of the I2C master interface. */
\r
106 #define mainI2C_IDLE 0
\r
107 #define mainI2C_READ_1 1
\r
108 #define mainI2C_READ_2 2
\r
109 #define mainI2C_READ_DONE 3
\r
111 #define mainZERO_LENGTH 0
\r
113 /* Address of the A2D IC on the CrossFire board. */
\r
114 #define mainI2CAddress 0x4D
\r
116 /* The queue used to send data from the I2C ISR to the co-routine should never
\r
117 contain more than one item as the same co-routine is used to trigger the I2C
\r
119 #define mainQUEUE_LENGTH 1
\r
121 /* The CrossFire board contains a tri-colour LED. */
\r
122 #define mainNUM_LEDs 3
\r
124 /* The I2C co-routine has a higher priority than the flash co-routines. This
\r
125 is not really necessary as when the I2C co-routine is active the other
\r
126 co-routines are delaying. */
\r
127 #define mainI2c_CO_ROUTINE_PRIORITY 1
\r
130 /* The current state of the I2C master. */
\r
131 static volatile unsigned portBASE_TYPE uxState = mainI2C_IDLE;
\r
133 /* The delay period derived from the A2D value. */
\r
134 static volatile portBASE_TYPE uxDelay = 250;
\r
136 /* The queue used to communicate between the I2C interrupt and the I2C
\r
138 static QueueHandle_t xADCQueue;
\r
140 /* The queue used to synchronise the flash co-routines. */
\r
141 static QueueHandle_t xDelayQueue;
\r
144 * Sets up the PLL, I2C and GPIO used by the demo.
\r
146 static void prvSetupHardware( void );
\r
148 /* The co-routines as described at the top of the file. */
\r
149 static void vI2CCoRoutine( CoRoutineHandle_t xHandle, unsigned portBASE_TYPE uxIndex );
\r
150 static void vFlashCoRoutine( CoRoutineHandle_t xHandle, unsigned portBASE_TYPE uxIndex );
\r
152 /*-----------------------------------------------------------*/
\r
156 unsigned portBASE_TYPE uxCoRoutine;
\r
158 /* Setup all the hardware used by this demo. */
\r
159 prvSetupHardware();
\r
161 /* Create the queue used to communicate between the ISR and I2C co-routine.
\r
162 This can only ever contain one value. */
\r
163 xADCQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( TickType_t ) );
\r
165 /* Create the queue used to synchronise the flash co-routines. The queue
\r
166 is used to trigger three tasks, but is for synchronisation only and does
\r
167 not pass any data. It therefore has three position each of zero length. */
\r
168 xDelayQueue = xQueueCreate( mainNUM_LEDs, mainZERO_LENGTH );
\r
170 /* Create the co-routine that initiates the i2c. */
\r
171 xCoRoutineCreate( vI2CCoRoutine, mainI2c_CO_ROUTINE_PRIORITY, 0 );
\r
173 /* Create the flash co-routines. */
\r
174 for( uxCoRoutine = 0; uxCoRoutine < mainNUM_LEDs; uxCoRoutine++ )
\r
176 xCoRoutineCreate( vFlashCoRoutine, tskIDLE_PRIORITY, uxCoRoutine );
\r
179 /* Start the scheduler. From this point on the co-routines should
\r
181 vTaskStartScheduler();
\r
183 /* Should not get here unless we did not have enough memory to start the
\r
188 /*-----------------------------------------------------------*/
\r
190 static void prvSetupHardware( void )
\r
192 /* Setup the PLL. */
\r
193 SysCtlClockSet( SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_6MHZ );
\r
195 /* Enable the I2C used to read the pot. */
\r
196 SysCtlPeripheralEnable( SYSCTL_PERIPH_I2C );
\r
197 SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOB );
\r
198 GPIOPinTypeI2C( GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3 );
\r
200 /* Initialize the I2C master. */
\r
201 I2CMasterInit( I2C_MASTER_BASE, pdFALSE );
\r
203 /* Enable the I2C master interrupt. */
\r
204 I2CMasterIntEnable( I2C_MASTER_BASE );
\r
205 IntEnable( INT_I2C );
\r
207 /* Initialise the hardware used to talk to the LED's. */
\r
208 vParTestInitialise();
\r
210 /*-----------------------------------------------------------*/
\r
212 static void vI2CCoRoutine( CoRoutineHandle_t xHandle, unsigned portBASE_TYPE uxIndex )
\r
214 TickType_t xADCResult;
\r
215 static portBASE_TYPE xResult = 0, xMilliSecs, xLED;
\r
217 crSTART( xHandle );
\r
221 /* Start the I2C off to read the ADC. */
\r
222 uxState = mainI2C_READ_1;
\r
223 I2CMasterSlaveAddrSet( I2C_MASTER_BASE, mainI2CAddress, pdTRUE );
\r
224 I2CMasterControl( I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START );
\r
226 /* Wait to receive the conversion result. */
\r
227 crQUEUE_RECEIVE( xHandle, xADCQueue, &xADCResult, portMAX_DELAY, &xResult );
\r
229 /* Scale the result to give a useful range of values for a visual
\r
232 xMilliSecs = xADCResult / portTICK_PERIOD_MS;
\r
234 /* The delay is split between the four co-routines so they remain in
\r
236 uxDelay = xMilliSecs / ( mainNUM_LEDs + 1 );
\r
238 /* Trigger each of the flash co-routines. */
\r
239 for( xLED = 0; xLED < mainNUM_LEDs; xLED++ )
\r
241 crQUEUE_SEND( xHandle, xDelayQueue, &xLED, 0, &xResult );
\r
244 /* Wait for the full delay time then start again. This delay is long
\r
245 enough to ensure the flash co-routines have done their thing and gone
\r
247 crDELAY( xHandle, xMilliSecs );
\r
252 /*-----------------------------------------------------------*/
\r
254 static void vFlashCoRoutine( CoRoutineHandle_t xHandle, unsigned portBASE_TYPE uxIndex )
\r
256 portBASE_TYPE xResult, xNothing;
\r
258 crSTART( xHandle );
\r
262 /* Wait for start of next round. */
\r
263 crQUEUE_RECEIVE( xHandle, xDelayQueue, &xNothing, portMAX_DELAY, &xResult );
\r
265 /* Wait until it is this co-routines turn to flash. */
\r
266 crDELAY( xHandle, uxDelay * uxIndex );
\r
268 /* Turn on the LED for a fixed period. */
\r
269 vParTestSetLED( uxIndex, pdTRUE );
\r
270 crDELAY( xHandle, uxDelay );
\r
271 vParTestSetLED( uxIndex, pdFALSE );
\r
273 /* Go back and wait for the next round. */
\r
278 /*-----------------------------------------------------------*/
\r
280 void vI2C_ISR(void)
\r
282 static TickType_t xReading;
\r
284 /* Clear the interrupt. */
\r
285 I2CMasterIntClear( I2C_MASTER_BASE );
\r
287 /* Determine what to do based on the current uxState. */
\r
290 case mainI2C_IDLE: break;
\r
292 case mainI2C_READ_1: /* Read ADC result high byte. */
\r
293 xReading = I2CMasterDataGet( I2C_MASTER_BASE );
\r
296 /* Continue the burst read. */
\r
297 I2CMasterControl( I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT );
\r
298 uxState = mainI2C_READ_2;
\r
301 case mainI2C_READ_2: /* Read ADC result low byte. */
\r
302 xReading |= I2CMasterDataGet( I2C_MASTER_BASE );
\r
304 /* Finish the burst read. */
\r
305 I2CMasterControl( I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH );
\r
306 uxState = mainI2C_READ_DONE;
\r
309 case mainI2C_READ_DONE: /* Complete. */
\r
310 I2CMasterDataGet( I2C_MASTER_BASE );
\r
311 uxState = mainI2C_IDLE;
\r
313 /* Send the result to the co-routine. */
\r
314 crQUEUE_SEND_FROM_ISR( xADCQueue, &xReading, pdFALSE );
\r
318 /*-----------------------------------------------------------*/
\r
320 void vApplicationIdleHook( void )
\r
324 vCoRoutineSchedule();
\r