]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_LM3S102_Rowley/Demo3/main.c
Prepare for V7.4.0 release.
[freertos] / FreeRTOS / Demo / CORTEX_LM3S102_Rowley / Demo3 / main.c
1 /*\r
2     FreeRTOS V7.4.0 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \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
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\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
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \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
32 \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
36     kernel.\r
37 \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 itcan 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
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions, \r
60     license and Real Time Engineers Ltd. contact details.\r
61 \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
65 \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
69     \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
73 */\r
74 \r
75 \r
76 /*\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
79  *\r
80  * Four co-routines are created - an 'I2C' co-routine and three 'flash'\r
81  * co-routines.\r
82  *\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
89  *\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
95  */\r
96 \r
97 \r
98 /* Scheduler include files. */\r
99 #include "FreeRTOS.h"\r
100 #include "task.h"\r
101 #include "queue.h"\r
102 #include "croutine.h"\r
103 \r
104 /* Demo application include files. */\r
105 #include "partest.h"\r
106 \r
107 /* Library include files. */\r
108 #include "DriverLib.h"\r
109 \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
115 \r
116 #define mainZERO_LENGTH 0\r
117 \r
118 /* Address of the A2D IC on the CrossFire board. */\r
119 #define mainI2CAddress  0x4D\r
120 \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
123 activity. */\r
124 #define mainQUEUE_LENGTH 1\r
125 \r
126 /* The CrossFire board contains a tri-colour LED. */\r
127 #define mainNUM_LEDs    3\r
128 \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
133 \r
134 \r
135 /* The current state of the I2C master. */\r
136 static volatile unsigned portBASE_TYPE uxState = mainI2C_IDLE;\r
137 \r
138 /* The delay period derived from the A2D value. */\r
139 static volatile portBASE_TYPE uxDelay = 250;\r
140 \r
141 /* The queue used to communicate between the I2C interrupt and the I2C \r
142 co-routine. */\r
143 static xQueueHandle xADCQueue;\r
144 \r
145 /* The queue used to synchronise the flash co-routines. */\r
146 static xQueueHandle xDelayQueue;\r
147 \r
148 /*\r
149  * Sets up the PLL, I2C and GPIO used by the demo.\r
150  */\r
151 static void prvSetupHardware( void );\r
152 \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
156 \r
157 /*-----------------------------------------------------------*/\r
158 \r
159 int main( void )\r
160 {\r
161 unsigned portBASE_TYPE uxCoRoutine;\r
162 \r
163         /* Setup all the hardware used by this demo. */\r
164         prvSetupHardware();\r
165 \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
169 \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
174 \r
175         /* Create the co-routine that initiates the i2c. */\r
176         xCoRoutineCreate( vI2CCoRoutine, mainI2c_CO_ROUTINE_PRIORITY, 0 );\r
177 \r
178         /* Create the flash co-routines. */\r
179         for( uxCoRoutine = 0; uxCoRoutine < mainNUM_LEDs; uxCoRoutine++ )\r
180         {\r
181                 xCoRoutineCreate( vFlashCoRoutine, tskIDLE_PRIORITY, uxCoRoutine );        \r
182         }\r
183 \r
184         /* Start the scheduler.  From this point on the co-routines should \r
185         execute. */\r
186         vTaskStartScheduler();\r
187 \r
188         /* Should not get here unless we did not have enough memory to start the\r
189         scheduler. */\r
190         for( ;; );\r
191         return 0;\r
192 }\r
193 /*-----------------------------------------------------------*/\r
194 \r
195 static void prvSetupHardware( void )\r
196 {\r
197         /* Setup the PLL. */\r
198         SysCtlClockSet( SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_6MHZ );\r
199 \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
204 \r
205         /* Initialize the I2C master. */\r
206         I2CMasterInit( I2C_MASTER_BASE, pdFALSE );\r
207         \r
208         /* Enable the I2C master interrupt. */\r
209         I2CMasterIntEnable( I2C_MASTER_BASE );\r
210     IntEnable( INT_I2C );\r
211 \r
212         /* Initialise the hardware used to talk to the LED's. */\r
213         vParTestInitialise();\r
214 }\r
215 /*-----------------------------------------------------------*/\r
216 \r
217 static void vI2CCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )\r
218 {\r
219 portTickType xADCResult;\r
220 static portBASE_TYPE xResult = 0, xMilliSecs, xLED;\r
221 \r
222         crSTART( xHandle );\r
223 \r
224         for( ;; )\r
225         {\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
230 \r
231                 /* Wait to receive the conversion result. */\r
232                 crQUEUE_RECEIVE( xHandle, xADCQueue, &xADCResult, portMAX_DELAY, &xResult );\r
233 \r
234                 /* Scale the result to give a useful range of values for a visual \r
235                 demo. */\r
236                 xADCResult >>= 2;\r
237                 xMilliSecs = xADCResult / portTICK_RATE_MS;\r
238 \r
239                 /* The delay is split between the four co-routines so they remain in\r
240                 synch. */\r
241                 uxDelay = xMilliSecs / ( mainNUM_LEDs + 1 );\r
242 \r
243                 /* Trigger each of the flash co-routines. */\r
244                 for( xLED = 0; xLED < mainNUM_LEDs; xLED++ )\r
245                 {\r
246                         crQUEUE_SEND( xHandle, xDelayQueue, &xLED, 0, &xResult );\r
247                 }\r
248 \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
251                 back to sleep. */\r
252                 crDELAY( xHandle, xMilliSecs );\r
253         }\r
254 \r
255         crEND();\r
256 }\r
257 /*-----------------------------------------------------------*/\r
258 \r
259 static void vFlashCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )\r
260 {\r
261 portBASE_TYPE xResult, xNothing;\r
262 \r
263         crSTART( xHandle );\r
264 \r
265         for( ;; )\r
266         {\r
267                 /* Wait for start of next round. */\r
268                 crQUEUE_RECEIVE( xHandle, xDelayQueue, &xNothing, portMAX_DELAY, &xResult );\r
269 \r
270                 /* Wait until it is this co-routines turn to flash. */\r
271                 crDELAY( xHandle, uxDelay * uxIndex );\r
272 \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
277 \r
278                 /* Go back and wait for the next round. */\r
279         }\r
280 \r
281         crEND();\r
282 }\r
283 /*-----------------------------------------------------------*/\r
284 \r
285 void vI2C_ISR(void)\r
286 {\r
287 static portTickType xReading;\r
288 \r
289         /* Clear the interrupt. */\r
290         I2CMasterIntClear( I2C_MASTER_BASE );\r
291 \r
292         /* Determine what to do based on the current uxState. */\r
293         switch (uxState)\r
294         {\r
295                 case mainI2C_IDLE:              break;\r
296         \r
297                 case mainI2C_READ_1:    /* Read ADC result high byte. */\r
298                                                                 xReading = I2CMasterDataGet( I2C_MASTER_BASE );\r
299                                                                 xReading <<= 8;\r
300                 \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
304                                                                 break;\r
305         \r
306                 case mainI2C_READ_2:    /* Read ADC result low byte. */\r
307                                                                 xReading |= I2CMasterDataGet( I2C_MASTER_BASE );                                                                \r
308                         \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
312                                                                 break;\r
313                         \r
314                 case mainI2C_READ_DONE: /* Complete. */\r
315                                                                 I2CMasterDataGet( I2C_MASTER_BASE );\r
316                                                                 uxState = mainI2C_IDLE;\r
317 \r
318                                                                 /* Send the result to the co-routine. */\r
319                                 crQUEUE_SEND_FROM_ISR( xADCQueue, &xReading, pdFALSE );\r
320                                                                 break;\r
321         }\r
322 }\r
323 /*-----------------------------------------------------------*/\r
324 \r
325 void vApplicationIdleHook( void )\r
326 {\r
327         for( ;; )\r
328         {\r
329                 vCoRoutineSchedule();\r
330         }\r
331 }\r
332 \r