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