2 FreeRTOS V8.2.0rc1 - Copyright (C) 2014 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 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
14 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
15 >>! obliged to provide the source code for proprietary components !<<
\r
16 >>! outside of the FreeRTOS kernel. !<<
\r
18 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
19 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
20 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
21 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * Having a problem? Start by reading the FAQ "My application does *
\r
28 * not run, what could be wrong?". Have you defined configASSERT()? *
\r
30 * http://www.FreeRTOS.org/FAQHelp.html *
\r
32 ***************************************************************************
\r
34 ***************************************************************************
\r
36 * FreeRTOS provides completely free yet professionally developed, *
\r
37 * robust, strictly quality controlled, supported, and cross *
\r
38 * platform software that is more than just the market leader, it *
\r
39 * is the industry's de facto standard. *
\r
41 * Help yourself get started quickly while simultaneously helping *
\r
42 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
43 * tutorial book, reference manual, or both: *
\r
44 * http://www.FreeRTOS.org/Documentation *
\r
46 ***************************************************************************
\r
48 ***************************************************************************
\r
50 * Investing in training allows your team to be as productive as *
\r
51 * possible as early as possible, lowering your overall development *
\r
52 * cost, and enabling you to bring a more robust product to market *
\r
53 * earlier than would otherwise be possible. Richard Barry is both *
\r
54 * the architect and key author of FreeRTOS, and so also the world's *
\r
55 * leading authority on what is the world's most popular real time *
\r
56 * kernel for deeply embedded MCU designs. Obtaining your training *
\r
57 * from Richard ensures your team will gain directly from his in-depth *
\r
58 * product knowledge and years of usage experience. Contact Real Time *
\r
59 * Engineers Ltd to enquire about the FreeRTOS Masterclass, presented *
\r
60 * by Richard Barry: http://www.FreeRTOS.org/contact
\r
62 ***************************************************************************
\r
64 ***************************************************************************
\r
66 * You are receiving this top quality software for free. Please play *
\r
67 * fair and reciprocate by reporting any suspected issues and *
\r
68 * participating in the community forum: *
\r
69 * http://www.FreeRTOS.org/support *
\r
73 ***************************************************************************
\r
75 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
76 license and Real Time Engineers Ltd. contact details.
\r
78 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
79 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
80 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
82 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
83 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
85 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
86 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
87 licenses offer ticketed support, indemnification and commercial middleware.
\r
89 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
90 engineered and independently SIL3 certified version for use in safety and
\r
91 mission critical applications that require provable dependability.
\r
98 * This is a mini co-routine demo for the Rowley CrossFire LM3S102 development
\r
99 * board. It makes use of the boards tri-colour LED and analogue input.
\r
101 * Four co-routines are created - an 'I2C' co-routine and three 'flash'
\r
104 * The I2C co-routine triggers an ADC conversion then blocks on a queue to
\r
105 * wait for the conversion result - which it receives on the queue directly
\r
106 * from the I2C interrupt service routine. The conversion result is then
\r
107 * scalled to a delay period. The I2C interrupt then wakes each of the
\r
108 * flash co-routines before itself delaying for the calculated period and
\r
109 * then repeating the whole process.
\r
111 * When woken by the I2C co-routine the flash co-routines each block for
\r
112 * a given period, illuminate an LED for a fixed period, then go back to
\r
113 * sleep to wait for the next cycle. The uxIndex parameter of the flash
\r
114 * co-routines is used to ensure that each flashes a different LED, and that
\r
115 * the delay periods are such that the LED's get flashed in sequence.
\r
119 /* Scheduler include files. */
\r
120 #include "FreeRTOS.h"
\r
123 #include "croutine.h"
\r
125 /* Demo application include files. */
\r
126 #include "partest.h"
\r
128 /* Library include files. */
\r
129 #include "DriverLib.h"
\r
131 /* States of the I2C master interface. */
\r
132 #define mainI2C_IDLE 0
\r
133 #define mainI2C_READ_1 1
\r
134 #define mainI2C_READ_2 2
\r
135 #define mainI2C_READ_DONE 3
\r
137 #define mainZERO_LENGTH 0
\r
139 /* Address of the A2D IC on the CrossFire board. */
\r
140 #define mainI2CAddress 0x4D
\r
142 /* The queue used to send data from the I2C ISR to the co-routine should never
\r
143 contain more than one item as the same co-routine is used to trigger the I2C
\r
145 #define mainQUEUE_LENGTH 1
\r
147 /* The CrossFire board contains a tri-colour LED. */
\r
148 #define mainNUM_LEDs 3
\r
150 /* The I2C co-routine has a higher priority than the flash co-routines. This
\r
151 is not really necessary as when the I2C co-routine is active the other
\r
152 co-routines are delaying. */
\r
153 #define mainI2c_CO_ROUTINE_PRIORITY 1
\r
156 /* The current state of the I2C master. */
\r
157 static volatile unsigned portBASE_TYPE uxState = mainI2C_IDLE;
\r
159 /* The delay period derived from the A2D value. */
\r
160 static volatile portBASE_TYPE uxDelay = 250;
\r
162 /* The queue used to communicate between the I2C interrupt and the I2C
\r
164 static QueueHandle_t xADCQueue;
\r
166 /* The queue used to synchronise the flash co-routines. */
\r
167 static QueueHandle_t xDelayQueue;
\r
170 * Sets up the PLL, I2C and GPIO used by the demo.
\r
172 static void prvSetupHardware( void );
\r
174 /* The co-routines as described at the top of the file. */
\r
175 static void vI2CCoRoutine( CoRoutineHandle_t xHandle, unsigned portBASE_TYPE uxIndex );
\r
176 static void vFlashCoRoutine( CoRoutineHandle_t xHandle, unsigned portBASE_TYPE uxIndex );
\r
178 /*-----------------------------------------------------------*/
\r
182 unsigned portBASE_TYPE uxCoRoutine;
\r
184 /* Setup all the hardware used by this demo. */
\r
185 prvSetupHardware();
\r
187 /* Create the queue used to communicate between the ISR and I2C co-routine.
\r
188 This can only ever contain one value. */
\r
189 xADCQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( TickType_t ) );
\r
191 /* Create the queue used to synchronise the flash co-routines. The queue
\r
192 is used to trigger three tasks, but is for synchronisation only and does
\r
193 not pass any data. It therefore has three position each of zero length. */
\r
194 xDelayQueue = xQueueCreate( mainNUM_LEDs, mainZERO_LENGTH );
\r
196 /* Create the co-routine that initiates the i2c. */
\r
197 xCoRoutineCreate( vI2CCoRoutine, mainI2c_CO_ROUTINE_PRIORITY, 0 );
\r
199 /* Create the flash co-routines. */
\r
200 for( uxCoRoutine = 0; uxCoRoutine < mainNUM_LEDs; uxCoRoutine++ )
\r
202 xCoRoutineCreate( vFlashCoRoutine, tskIDLE_PRIORITY, uxCoRoutine );
\r
205 /* Start the scheduler. From this point on the co-routines should
\r
207 vTaskStartScheduler();
\r
209 /* Should not get here unless we did not have enough memory to start the
\r
214 /*-----------------------------------------------------------*/
\r
216 static void prvSetupHardware( void )
\r
218 /* Setup the PLL. */
\r
219 SysCtlClockSet( SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_6MHZ );
\r
221 /* Enable the I2C used to read the pot. */
\r
222 SysCtlPeripheralEnable( SYSCTL_PERIPH_I2C );
\r
223 SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOB );
\r
224 GPIOPinTypeI2C( GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3 );
\r
226 /* Initialize the I2C master. */
\r
227 I2CMasterInit( I2C_MASTER_BASE, pdFALSE );
\r
229 /* Enable the I2C master interrupt. */
\r
230 I2CMasterIntEnable( I2C_MASTER_BASE );
\r
231 IntEnable( INT_I2C );
\r
233 /* Initialise the hardware used to talk to the LED's. */
\r
234 vParTestInitialise();
\r
236 /*-----------------------------------------------------------*/
\r
238 static void vI2CCoRoutine( CoRoutineHandle_t xHandle, unsigned portBASE_TYPE uxIndex )
\r
240 TickType_t xADCResult;
\r
241 static portBASE_TYPE xResult = 0, xMilliSecs, xLED;
\r
243 crSTART( xHandle );
\r
247 /* Start the I2C off to read the ADC. */
\r
248 uxState = mainI2C_READ_1;
\r
249 I2CMasterSlaveAddrSet( I2C_MASTER_BASE, mainI2CAddress, pdTRUE );
\r
250 I2CMasterControl( I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START );
\r
252 /* Wait to receive the conversion result. */
\r
253 crQUEUE_RECEIVE( xHandle, xADCQueue, &xADCResult, portMAX_DELAY, &xResult );
\r
255 /* Scale the result to give a useful range of values for a visual
\r
258 xMilliSecs = xADCResult / portTICK_PERIOD_MS;
\r
260 /* The delay is split between the four co-routines so they remain in
\r
262 uxDelay = xMilliSecs / ( mainNUM_LEDs + 1 );
\r
264 /* Trigger each of the flash co-routines. */
\r
265 for( xLED = 0; xLED < mainNUM_LEDs; xLED++ )
\r
267 crQUEUE_SEND( xHandle, xDelayQueue, &xLED, 0, &xResult );
\r
270 /* Wait for the full delay time then start again. This delay is long
\r
271 enough to ensure the flash co-routines have done their thing and gone
\r
273 crDELAY( xHandle, xMilliSecs );
\r
278 /*-----------------------------------------------------------*/
\r
280 static void vFlashCoRoutine( CoRoutineHandle_t xHandle, unsigned portBASE_TYPE uxIndex )
\r
282 portBASE_TYPE xResult, xNothing;
\r
284 crSTART( xHandle );
\r
288 /* Wait for start of next round. */
\r
289 crQUEUE_RECEIVE( xHandle, xDelayQueue, &xNothing, portMAX_DELAY, &xResult );
\r
291 /* Wait until it is this co-routines turn to flash. */
\r
292 crDELAY( xHandle, uxDelay * uxIndex );
\r
294 /* Turn on the LED for a fixed period. */
\r
295 vParTestSetLED( uxIndex, pdTRUE );
\r
296 crDELAY( xHandle, uxDelay );
\r
297 vParTestSetLED( uxIndex, pdFALSE );
\r
299 /* Go back and wait for the next round. */
\r
304 /*-----------------------------------------------------------*/
\r
306 void vI2C_ISR(void)
\r
308 static TickType_t xReading;
\r
310 /* Clear the interrupt. */
\r
311 I2CMasterIntClear( I2C_MASTER_BASE );
\r
313 /* Determine what to do based on the current uxState. */
\r
316 case mainI2C_IDLE: break;
\r
318 case mainI2C_READ_1: /* Read ADC result high byte. */
\r
319 xReading = I2CMasterDataGet( I2C_MASTER_BASE );
\r
322 /* Continue the burst read. */
\r
323 I2CMasterControl( I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT );
\r
324 uxState = mainI2C_READ_2;
\r
327 case mainI2C_READ_2: /* Read ADC result low byte. */
\r
328 xReading |= I2CMasterDataGet( I2C_MASTER_BASE );
\r
330 /* Finish the burst read. */
\r
331 I2CMasterControl( I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH );
\r
332 uxState = mainI2C_READ_DONE;
\r
335 case mainI2C_READ_DONE: /* Complete. */
\r
336 I2CMasterDataGet( I2C_MASTER_BASE );
\r
337 uxState = mainI2C_IDLE;
\r
339 /* Send the result to the co-routine. */
\r
340 crQUEUE_SEND_FROM_ISR( xADCQueue, &xReading, pdFALSE );
\r
344 /*-----------------------------------------------------------*/
\r
346 void vApplicationIdleHook( void )
\r
350 vCoRoutineSchedule();
\r