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