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