]> begriffs open source - freertos/blob - Demo/CORTEX_LM3S102_Rowley/Demo3/main.c
Update to V4.4.0.
[freertos] / Demo / CORTEX_LM3S102_Rowley / Demo3 / main.c
1 /*\r
2         FreeRTOS.org V4.4.0 - Copyright (C) 2003-2007 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section \r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26         ***************************************************************************\r
27         See http://www.FreeRTOS.org for documentation, latest information, license \r
28         and contact details.  Please ensure to read the configuration and relevant \r
29         port sections of the online documentation.\r
30 \r
31         Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along\r
32         with commercial development and support options.\r
33         ***************************************************************************\r
34 */\r
35 \r
36 \r
37 /*\r
38  * This is a mini co-routine demo for the Rowley CrossFire LM3S102 development\r
39  * board.  It makes use of the boards tri-colour LED and analogue input.\r
40  *\r
41  * Four co-routines are created - an 'I2C' co-routine and three 'flash'\r
42  * co-routines.\r
43  *\r
44  * The I2C co-routine triggers an ADC conversion then blocks on a queue to \r
45  * wait for the conversion result - which it receives on the queue directly\r
46  * from the I2C interrupt service routine.  The conversion result is then\r
47  * scalled to a delay period.  The I2C interrupt then wakes each of the \r
48  * flash co-routines before itself delaying for the calculated period and\r
49  * then repeating the whole process.\r
50  *\r
51  * When woken by the I2C co-routine the flash co-routines each block for \r
52  * a given period, illuminate an LED for a fixed period, then go back to\r
53  * sleep to wait for the next cycle.  The uxIndex parameter of the flash\r
54  * co-routines is used to ensure that each flashes a different LED, and that\r
55  * the delay periods are such that the LED's get flashed in sequence.\r
56  */\r
57 \r
58 \r
59 /* Scheduler include files. */\r
60 #include "FreeRTOS.h"\r
61 #include "task.h"\r
62 #include "queue.h"\r
63 #include "croutine.h"\r
64 \r
65 /* Demo application include files. */\r
66 #include "partest.h"\r
67 \r
68 /* Library include files. */\r
69 #include "DriverLib.h"\r
70 \r
71 /* States of the I2C master interface. */\r
72 #define mainI2C_IDLE       0\r
73 #define mainI2C_READ_1     1\r
74 #define mainI2C_READ_2     2\r
75 #define mainI2C_READ_DONE  3\r
76 \r
77 #define mainZERO_LENGTH 0\r
78 \r
79 /* Address of the A2D IC on the CrossFire board. */\r
80 #define mainI2CAddress  0x4D\r
81 \r
82 /* The queue used to send data from the I2C ISR to the co-routine should never\r
83 contain more than one item as the same co-routine is used to trigger the I2C\r
84 activity. */\r
85 #define mainQUEUE_LENGTH 1\r
86 \r
87 /* The CrossFire board contains a tri-colour LED. */\r
88 #define mainNUM_LEDs    3\r
89 \r
90 /* The I2C co-routine has a higher priority than the flash co-routines.  This\r
91 is not really necessary as when the I2C co-routine is active the other \r
92 co-routines are delaying. */\r
93 #define mainI2c_CO_ROUTINE_PRIORITY 1\r
94 \r
95 \r
96 /* The current state of the I2C master. */\r
97 static volatile unsigned portBASE_TYPE uxState = mainI2C_IDLE;\r
98 \r
99 /* The delay period derived from the A2D value. */\r
100 static volatile portBASE_TYPE uxDelay = 250;\r
101 \r
102 /* The queue used to communicate between the I2C interrupt and the I2C \r
103 co-routine. */\r
104 static xQueueHandle xADCQueue;\r
105 \r
106 /* The queue used to synchronise the flash co-routines. */\r
107 static xQueueHandle xDelayQueue;\r
108 \r
109 /*\r
110  * Sets up the PLL, I2C and GPIO used by the demo.\r
111  */\r
112 static void prvSetupHardware( void );\r
113 \r
114 /* The co-routines as described at the top of the file. */\r
115 static void vI2CCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex );\r
116 static void vFlashCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex );\r
117 \r
118 /*-----------------------------------------------------------*/\r
119 \r
120 int main( void )\r
121 {\r
122 unsigned portBASE_TYPE uxCoRoutine;\r
123 \r
124         /* Setup all the hardware used by this demo. */\r
125         prvSetupHardware();\r
126 \r
127         /* Create the queue used to communicate between the ISR and I2C co-routine.\r
128         This can only ever contain one value. */\r
129         xADCQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( portTickType ) );\r
130 \r
131         /* Create the queue used to synchronise the flash co-routines.  The queue\r
132         is used to trigger three tasks, but is for synchronisation only and does\r
133         not pass any data.  It therefore has three position each of zero length. */\r
134         xDelayQueue = xQueueCreate( mainNUM_LEDs, mainZERO_LENGTH );\r
135 \r
136         /* Create the co-routine that initiates the i2c. */\r
137         xCoRoutineCreate( vI2CCoRoutine, mainI2c_CO_ROUTINE_PRIORITY, 0 );\r
138 \r
139         /* Create the flash co-routines. */\r
140         for( uxCoRoutine = 0; uxCoRoutine < mainNUM_LEDs; uxCoRoutine++ )\r
141         {\r
142                 xCoRoutineCreate( vFlashCoRoutine, tskIDLE_PRIORITY, uxCoRoutine );        \r
143         }\r
144 \r
145         /* Start the scheduler.  From this point on the co-routines should \r
146         execute. */\r
147         vTaskStartScheduler();\r
148 \r
149         /* Should not get here unless we did not have enough memory to start the\r
150         scheduler. */\r
151         for( ;; );\r
152         return 0;\r
153 }\r
154 /*-----------------------------------------------------------*/\r
155 \r
156 static void prvSetupHardware( void )\r
157 {\r
158         /* Setup the PLL. */\r
159         SysCtlClockSet( SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_6MHZ );\r
160 \r
161         /* Enable the I2C used to read the pot. */\r
162         SysCtlPeripheralEnable( SYSCTL_PERIPH_I2C );\r
163         SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOB );\r
164         GPIOPinTypeI2C( GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3 );\r
165 \r
166         /* Initialize the I2C master. */\r
167         I2CMasterInit( I2C_MASTER_BASE, pdFALSE );\r
168         \r
169         /* Enable the I2C master interrupt. */\r
170         I2CMasterIntEnable( I2C_MASTER_BASE );\r
171     IntEnable( INT_I2C );\r
172 \r
173         /* Initialise the hardware used to talk to the LED's. */\r
174         vParTestInitialise();\r
175 }\r
176 /*-----------------------------------------------------------*/\r
177 \r
178 static void vI2CCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )\r
179 {\r
180 portTickType xADCResult;\r
181 static portBASE_TYPE xResult = 0, xMilliSecs, xLED;\r
182 \r
183         crSTART( xHandle );\r
184 \r
185         for( ;; )\r
186         {\r
187                 /* Start the I2C off to read the ADC. */\r
188                 uxState = mainI2C_READ_1;\r
189                 I2CMasterSlaveAddrSet( I2C_MASTER_BASE, mainI2CAddress, pdTRUE );               \r
190                 I2CMasterControl( I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START );\r
191 \r
192                 /* Wait to receive the conversion result. */\r
193                 crQUEUE_RECEIVE( xHandle, xADCQueue, &xADCResult, portMAX_DELAY, &xResult );\r
194 \r
195                 /* Scale the result to give a useful range of values for a visual \r
196                 demo. */\r
197                 xADCResult >>= 2;\r
198                 xMilliSecs = xADCResult / portTICK_RATE_MS;\r
199 \r
200                 /* The delay is split between the four co-routines so they remain in\r
201                 synch. */\r
202                 uxDelay = xMilliSecs / ( mainNUM_LEDs + 1 );\r
203 \r
204                 /* Trigger each of the flash co-routines. */\r
205                 for( xLED = 0; xLED < mainNUM_LEDs; xLED++ )\r
206                 {\r
207                         crQUEUE_SEND( xHandle, xDelayQueue, &xLED, 0, &xResult );\r
208                 }\r
209 \r
210                 /* Wait for the full delay time then start again.  This delay is long \r
211                 enough to ensure the flash co-routines have done their thing and gone\r
212                 back to sleep. */\r
213                 crDELAY( xHandle, xMilliSecs );\r
214         }\r
215 \r
216         crEND();\r
217 }\r
218 /*-----------------------------------------------------------*/\r
219 \r
220 static void vFlashCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )\r
221 {\r
222 portBASE_TYPE xResult, xNothing;\r
223 \r
224         crSTART( xHandle );\r
225 \r
226         for( ;; )\r
227         {\r
228                 /* Wait for start of next round. */\r
229                 crQUEUE_RECEIVE( xHandle, xDelayQueue, &xNothing, portMAX_DELAY, &xResult );\r
230 \r
231                 /* Wait until it is this co-routines turn to flash. */\r
232                 crDELAY( xHandle, uxDelay * uxIndex );\r
233 \r
234                 /* Turn on the LED for a fixed period. */\r
235                 vParTestSetLED( uxIndex, pdTRUE );\r
236                 crDELAY( xHandle, uxDelay );\r
237                 vParTestSetLED( uxIndex, pdFALSE );\r
238 \r
239                 /* Go back and wait for the next round. */\r
240         }\r
241 \r
242         crEND();\r
243 }\r
244 /*-----------------------------------------------------------*/\r
245 \r
246 void vI2C_ISR(void)\r
247 {\r
248 static portTickType xReading;\r
249 \r
250         /* Clear the interrupt. */\r
251         I2CMasterIntClear( I2C_MASTER_BASE );\r
252 \r
253         /* Determine what to do based on the current uxState. */\r
254         switch (uxState)\r
255         {\r
256                 case mainI2C_IDLE:              break;\r
257         \r
258                 case mainI2C_READ_1:    /* Read ADC result high byte. */\r
259                                                                 xReading = I2CMasterDataGet( I2C_MASTER_BASE );\r
260                                                                 xReading <<= 8;\r
261                 \r
262                                                                 /* Continue the burst read. */\r
263                                                                 I2CMasterControl( I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT );\r
264                                                                 uxState = mainI2C_READ_2;\r
265                                                                 break;\r
266         \r
267                 case mainI2C_READ_2:    /* Read ADC result low byte. */\r
268                                                                 xReading |= I2CMasterDataGet( I2C_MASTER_BASE );                                                                \r
269                         \r
270                                                                 /* Finish the burst read. */\r
271                                                                 I2CMasterControl( I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH );\r
272                                                                 uxState = mainI2C_READ_DONE;\r
273                                                                 break;\r
274                         \r
275                 case mainI2C_READ_DONE: /* Complete. */\r
276                                                                 I2CMasterDataGet( I2C_MASTER_BASE );\r
277                                                                 uxState = mainI2C_IDLE;\r
278 \r
279                                                                 /* Send the result to the co-routine. */\r
280                                 crQUEUE_SEND_FROM_ISR( xADCQueue, &xReading, pdFALSE );\r
281                                                                 break;\r
282         }\r
283 }\r
284 /*-----------------------------------------------------------*/\r
285 \r
286 void vApplicationIdleHook( void )\r
287 {\r
288         for( ;; )\r
289         {\r
290                 vCoRoutineSchedule();\r
291         }\r
292 }\r
293 \r