]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_LM3S102_Rowley/Demo3/main.c
72360595e7d0be4d98794ef677c80c20b5a8b287
[freertos] / Demo / CORTEX_LM3S102_Rowley / Demo3 / main.c
1 /*\r
2     FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\r
8     *    + New to FreeRTOS,                                                   *\r
9     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *\r
10     *    + Looking for basic training,                                        *\r
11     *    + Wanting to improve your FreeRTOS skills and productivity           *\r
12     *                                                                         *\r
13     * then take a look at the FreeRTOS eBook                                  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\r
18     * A pdf reference manual is also available.  Both are usually delivered   *\r
19     * to your inbox within 20 minutes to two hours when purchased between 8am *\r
20     * and 8pm GMT (although please allow up to 24 hours in case of            *\r
21     * exceptional circumstances).  Thank you for your support!                *\r
22     *                                                                         *\r
23     ***************************************************************************\r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     ***NOTE*** The exception to the GPL is included to allow you to distribute\r
31     a combined work that includes FreeRTOS without being obliged to provide the\r
32     source code for proprietary components outside of the FreeRTOS kernel.\r
33     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
34     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
35     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public \r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it \r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained \r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 \r
55 /*\r
56  * This is a mini co-routine demo for the Rowley CrossFire LM3S102 development\r
57  * board.  It makes use of the boards tri-colour LED and analogue input.\r
58  *\r
59  * Four co-routines are created - an 'I2C' co-routine and three 'flash'\r
60  * co-routines.\r
61  *\r
62  * The I2C co-routine triggers an ADC conversion then blocks on a queue to \r
63  * wait for the conversion result - which it receives on the queue directly\r
64  * from the I2C interrupt service routine.  The conversion result is then\r
65  * scalled to a delay period.  The I2C interrupt then wakes each of the \r
66  * flash co-routines before itself delaying for the calculated period and\r
67  * then repeating the whole process.\r
68  *\r
69  * When woken by the I2C co-routine the flash co-routines each block for \r
70  * a given period, illuminate an LED for a fixed period, then go back to\r
71  * sleep to wait for the next cycle.  The uxIndex parameter of the flash\r
72  * co-routines is used to ensure that each flashes a different LED, and that\r
73  * the delay periods are such that the LED's get flashed in sequence.\r
74  */\r
75 \r
76 \r
77 /* Scheduler include files. */\r
78 #include "FreeRTOS.h"\r
79 #include "task.h"\r
80 #include "queue.h"\r
81 #include "croutine.h"\r
82 \r
83 /* Demo application include files. */\r
84 #include "partest.h"\r
85 \r
86 /* Library include files. */\r
87 #include "DriverLib.h"\r
88 \r
89 /* States of the I2C master interface. */\r
90 #define mainI2C_IDLE       0\r
91 #define mainI2C_READ_1     1\r
92 #define mainI2C_READ_2     2\r
93 #define mainI2C_READ_DONE  3\r
94 \r
95 #define mainZERO_LENGTH 0\r
96 \r
97 /* Address of the A2D IC on the CrossFire board. */\r
98 #define mainI2CAddress  0x4D\r
99 \r
100 /* The queue used to send data from the I2C ISR to the co-routine should never\r
101 contain more than one item as the same co-routine is used to trigger the I2C\r
102 activity. */\r
103 #define mainQUEUE_LENGTH 1\r
104 \r
105 /* The CrossFire board contains a tri-colour LED. */\r
106 #define mainNUM_LEDs    3\r
107 \r
108 /* The I2C co-routine has a higher priority than the flash co-routines.  This\r
109 is not really necessary as when the I2C co-routine is active the other \r
110 co-routines are delaying. */\r
111 #define mainI2c_CO_ROUTINE_PRIORITY 1\r
112 \r
113 \r
114 /* The current state of the I2C master. */\r
115 static volatile unsigned portBASE_TYPE uxState = mainI2C_IDLE;\r
116 \r
117 /* The delay period derived from the A2D value. */\r
118 static volatile portBASE_TYPE uxDelay = 250;\r
119 \r
120 /* The queue used to communicate between the I2C interrupt and the I2C \r
121 co-routine. */\r
122 static xQueueHandle xADCQueue;\r
123 \r
124 /* The queue used to synchronise the flash co-routines. */\r
125 static xQueueHandle xDelayQueue;\r
126 \r
127 /*\r
128  * Sets up the PLL, I2C and GPIO used by the demo.\r
129  */\r
130 static void prvSetupHardware( void );\r
131 \r
132 /* The co-routines as described at the top of the file. */\r
133 static void vI2CCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex );\r
134 static void vFlashCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex );\r
135 \r
136 /*-----------------------------------------------------------*/\r
137 \r
138 int main( void )\r
139 {\r
140 unsigned portBASE_TYPE uxCoRoutine;\r
141 \r
142         /* Setup all the hardware used by this demo. */\r
143         prvSetupHardware();\r
144 \r
145         /* Create the queue used to communicate between the ISR and I2C co-routine.\r
146         This can only ever contain one value. */\r
147         xADCQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( portTickType ) );\r
148 \r
149         /* Create the queue used to synchronise the flash co-routines.  The queue\r
150         is used to trigger three tasks, but is for synchronisation only and does\r
151         not pass any data.  It therefore has three position each of zero length. */\r
152         xDelayQueue = xQueueCreate( mainNUM_LEDs, mainZERO_LENGTH );\r
153 \r
154         /* Create the co-routine that initiates the i2c. */\r
155         xCoRoutineCreate( vI2CCoRoutine, mainI2c_CO_ROUTINE_PRIORITY, 0 );\r
156 \r
157         /* Create the flash co-routines. */\r
158         for( uxCoRoutine = 0; uxCoRoutine < mainNUM_LEDs; uxCoRoutine++ )\r
159         {\r
160                 xCoRoutineCreate( vFlashCoRoutine, tskIDLE_PRIORITY, uxCoRoutine );        \r
161         }\r
162 \r
163         /* Start the scheduler.  From this point on the co-routines should \r
164         execute. */\r
165         vTaskStartScheduler();\r
166 \r
167         /* Should not get here unless we did not have enough memory to start the\r
168         scheduler. */\r
169         for( ;; );\r
170         return 0;\r
171 }\r
172 /*-----------------------------------------------------------*/\r
173 \r
174 static void prvSetupHardware( void )\r
175 {\r
176         /* Setup the PLL. */\r
177         SysCtlClockSet( SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_6MHZ );\r
178 \r
179         /* Enable the I2C used to read the pot. */\r
180         SysCtlPeripheralEnable( SYSCTL_PERIPH_I2C );\r
181         SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOB );\r
182         GPIOPinTypeI2C( GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3 );\r
183 \r
184         /* Initialize the I2C master. */\r
185         I2CMasterInit( I2C_MASTER_BASE, pdFALSE );\r
186         \r
187         /* Enable the I2C master interrupt. */\r
188         I2CMasterIntEnable( I2C_MASTER_BASE );\r
189     IntEnable( INT_I2C );\r
190 \r
191         /* Initialise the hardware used to talk to the LED's. */\r
192         vParTestInitialise();\r
193 }\r
194 /*-----------------------------------------------------------*/\r
195 \r
196 static void vI2CCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )\r
197 {\r
198 portTickType xADCResult;\r
199 static portBASE_TYPE xResult = 0, xMilliSecs, xLED;\r
200 \r
201         crSTART( xHandle );\r
202 \r
203         for( ;; )\r
204         {\r
205                 /* Start the I2C off to read the ADC. */\r
206                 uxState = mainI2C_READ_1;\r
207                 I2CMasterSlaveAddrSet( I2C_MASTER_BASE, mainI2CAddress, pdTRUE );               \r
208                 I2CMasterControl( I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START );\r
209 \r
210                 /* Wait to receive the conversion result. */\r
211                 crQUEUE_RECEIVE( xHandle, xADCQueue, &xADCResult, portMAX_DELAY, &xResult );\r
212 \r
213                 /* Scale the result to give a useful range of values for a visual \r
214                 demo. */\r
215                 xADCResult >>= 2;\r
216                 xMilliSecs = xADCResult / portTICK_RATE_MS;\r
217 \r
218                 /* The delay is split between the four co-routines so they remain in\r
219                 synch. */\r
220                 uxDelay = xMilliSecs / ( mainNUM_LEDs + 1 );\r
221 \r
222                 /* Trigger each of the flash co-routines. */\r
223                 for( xLED = 0; xLED < mainNUM_LEDs; xLED++ )\r
224                 {\r
225                         crQUEUE_SEND( xHandle, xDelayQueue, &xLED, 0, &xResult );\r
226                 }\r
227 \r
228                 /* Wait for the full delay time then start again.  This delay is long \r
229                 enough to ensure the flash co-routines have done their thing and gone\r
230                 back to sleep. */\r
231                 crDELAY( xHandle, xMilliSecs );\r
232         }\r
233 \r
234         crEND();\r
235 }\r
236 /*-----------------------------------------------------------*/\r
237 \r
238 static void vFlashCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )\r
239 {\r
240 portBASE_TYPE xResult, xNothing;\r
241 \r
242         crSTART( xHandle );\r
243 \r
244         for( ;; )\r
245         {\r
246                 /* Wait for start of next round. */\r
247                 crQUEUE_RECEIVE( xHandle, xDelayQueue, &xNothing, portMAX_DELAY, &xResult );\r
248 \r
249                 /* Wait until it is this co-routines turn to flash. */\r
250                 crDELAY( xHandle, uxDelay * uxIndex );\r
251 \r
252                 /* Turn on the LED for a fixed period. */\r
253                 vParTestSetLED( uxIndex, pdTRUE );\r
254                 crDELAY( xHandle, uxDelay );\r
255                 vParTestSetLED( uxIndex, pdFALSE );\r
256 \r
257                 /* Go back and wait for the next round. */\r
258         }\r
259 \r
260         crEND();\r
261 }\r
262 /*-----------------------------------------------------------*/\r
263 \r
264 void vI2C_ISR(void)\r
265 {\r
266 static portTickType xReading;\r
267 \r
268         /* Clear the interrupt. */\r
269         I2CMasterIntClear( I2C_MASTER_BASE );\r
270 \r
271         /* Determine what to do based on the current uxState. */\r
272         switch (uxState)\r
273         {\r
274                 case mainI2C_IDLE:              break;\r
275         \r
276                 case mainI2C_READ_1:    /* Read ADC result high byte. */\r
277                                                                 xReading = I2CMasterDataGet( I2C_MASTER_BASE );\r
278                                                                 xReading <<= 8;\r
279                 \r
280                                                                 /* Continue the burst read. */\r
281                                                                 I2CMasterControl( I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT );\r
282                                                                 uxState = mainI2C_READ_2;\r
283                                                                 break;\r
284         \r
285                 case mainI2C_READ_2:    /* Read ADC result low byte. */\r
286                                                                 xReading |= I2CMasterDataGet( I2C_MASTER_BASE );                                                                \r
287                         \r
288                                                                 /* Finish the burst read. */\r
289                                                                 I2CMasterControl( I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH );\r
290                                                                 uxState = mainI2C_READ_DONE;\r
291                                                                 break;\r
292                         \r
293                 case mainI2C_READ_DONE: /* Complete. */\r
294                                                                 I2CMasterDataGet( I2C_MASTER_BASE );\r
295                                                                 uxState = mainI2C_IDLE;\r
296 \r
297                                                                 /* Send the result to the co-routine. */\r
298                                 crQUEUE_SEND_FROM_ISR( xADCQueue, &xReading, pdFALSE );\r
299                                                                 break;\r
300         }\r
301 }\r
302 /*-----------------------------------------------------------*/\r
303 \r
304 void vApplicationIdleHook( void )\r
305 {\r
306         for( ;; )\r
307         {\r
308                 vCoRoutineSchedule();\r
309         }\r
310 }\r
311 \r