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