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