]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_LM3S316_IAR/main.c
Update to V4.5.0 files and directory structure.
[freertos] / Demo / CORTEX_LM3S316_IAR / main.c
1 /*\r
2         FreeRTOS.org V4.5.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  * This demo application creates eight co-routines and four tasks (five \r
38  * including the idle task).  The co-routines execute as part of the idle task \r
39  * hook.  The application is limited in size to allow its compilation using\r
40  * the KickStart version of the IAR compiler.\r
41  *\r
42  * Six of the created co-routines are the standard 'co-routine flash' \r
43  * co-routines contained within the Demo/Common/Minimal/crflash.c file and \r
44  * documented on the FreeRTOS.org WEB site.  \r
45  *\r
46  * The 'LCD Task' waits on a message queue for messages informing it what and\r
47  * where to display text.  This is the only task that accesses the LCD\r
48  * so mutual exclusion is guaranteed.\r
49  *\r
50  * The 'LCD Message Task' periodically sends strings to the LCD Task using\r
51  * the message queue.  The strings are rotated to form a short message and\r
52  * are written to the top row of the LCD.\r
53  *\r
54  * The 'ADC Co-routine' periodically reads the ADC input that is connected to\r
55  * the light sensor, forms a short message from the value, and then sends this\r
56  * message to the LCD Task using the same message queue.  The ADC readings are\r
57  * displayed on the bottom row of the LCD.  \r
58  *\r
59  * The eighth co-routine and final task control the transmission and reception\r
60  * of a string to UART 0.  The co-routine periodically sends the first \r
61  * character of the string to the UART, with the UART's TxEnd interrupt being\r
62  * used to transmit the remaining characters.  The UART's RxEnd interrupt \r
63  * receives the characters and places them on a queue to be processed by the \r
64  * 'COMs Rx' task.  An error is latched should an unexpected character be \r
65  * received, or any character be received out of sequence.  \r
66  *\r
67  * A loopback connector is required to ensure that each character transmitted \r
68  * on the UART is also received on the same UART.  For test purposes the UART\r
69  * FIFO's are not utalised in order to maximise the interrupt overhead.  Also\r
70  * a pseudo random interval is used between the start of each transmission in \r
71  * order that the resultant interrupts are more randomly distributed and \r
72  * therefore more likely to highlight any problems.\r
73  *\r
74  * The flash co-routines control LED's zero to four.  LED five is toggled each\r
75  * time the string is transmitted on the UART.  LED six is toggled each time\r
76  * the string is CORRECTLY received on the UART.  LED seven is latched on \r
77  * should an error be detected in any task or co-routine.\r
78  *\r
79  * In addition the idle task makes repetitive calls to \r
80  * vSetAndCheckRegisters().  This simply loads the general purpose registers \r
81  * with a known value, then checks each register to ensure the held value is \r
82  * still correct.  As a low priority task this checking routine is likely to \r
83  * get repeatedly swapped in and out.  A register being found to contain an \r
84  * incorrect value is therefore indicative of an error in the task switching \r
85  * mechanism.\r
86  *\r
87  */\r
88 \r
89 /* standard include files. */\r
90 #include <stdio.h>\r
91 \r
92 /* Scheduler include files. */\r
93 #include "FreeRTOS.h"\r
94 #include "task.h"\r
95 #include "queue.h"\r
96 #include "croutine.h"\r
97 \r
98 /* Demo application include files. */\r
99 #include "partest.h"\r
100 #include "crflash.h"\r
101 #include "commstest.h"\r
102 \r
103 /* Library include files. */\r
104 #include "DriverLib.h"\r
105 \r
106 /* The time to delay between writing each character to the LCD. */\r
107 #define mainCHAR_WRITE_DELAY            ( 2 / portTICK_RATE_MS )\r
108 \r
109 /* The time to delay between writing each string to the LCD. */\r
110 #define mainSTRING_WRITE_DELAY          ( 400 / portTICK_RATE_MS )\r
111 \r
112 #define mainADC_DELAY                           ( 200 / portTICK_RATE_MS )\r
113 \r
114 /* The number of flash co-routines to create. */\r
115 #define mainNUM_FLASH_CO_ROUTINES       ( 5 )\r
116 \r
117 /* The length of the queue used to send messages to the LCD task. */\r
118 #define mainLCD_QUEUE_LEN                       ( 3 )\r
119 \r
120 /* The priority of the co-routine used to initiate the transmission of the \r
121 string on UART 0. */\r
122 #define mainTX_CO_ROUTINE_PRIORITY      ( 1 )\r
123 #define mainADC_CO_ROUTINE_PRIORITY     ( 2 )\r
124 \r
125 /* Only one of each co-routine is created so its index is not important. */\r
126 #define mainTX_CO_ROUTINE_INDEX         ( 0 )\r
127 #define mainADC_CO_ROUTINE_INDEX        ( 0 )\r
128 \r
129 /* The task priorities. */\r
130 #define mainLCD_TASK_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
131 #define mainMSG_TASK_PRIORITY           ( mainLCD_TASK_PRIORITY - 1 )\r
132 #define mainCOMMS_RX_TASK_PRIORITY      ( tskIDLE_PRIORITY + 1 )\r
133 \r
134 /* The LCD had two rows. */\r
135 #define mainTOP_ROW             0\r
136 #define mainBOTTOM_ROW  1\r
137 \r
138 /* Dimension for the buffer into which the ADC value string is written. */\r
139 #define mainMAX_ADC_STRING_LEN  20\r
140 \r
141 /* The LED that is lit should an error be detected in any of the tasks or\r
142 co-routines. */\r
143 #define mainFAIL_LED                    ( 7 )\r
144 \r
145 /*-----------------------------------------------------------*/\r
146 \r
147 /*\r
148  * The task that displays text on the LCD.\r
149  */\r
150 static void prvLCDTask( void * pvParameters );\r
151 \r
152 /*\r
153  * The task that sends messages to be displayed on the top row of the LCD.\r
154  */\r
155 static void prvLCDMessageTask( void * pvParameters );\r
156 \r
157 /*\r
158  * The co-routine that reads the ADC and sends messages for display on the\r
159  * bottom row of the LCD.\r
160  */\r
161 static void prvADCCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex );\r
162 \r
163 /*\r
164  * Function to simply set a known value into the general purpose registers\r
165  * then read them back to ensure they remain set correctly.  An incorrect value\r
166  * being indicative of an error in the task switching mechanism.\r
167  */\r
168 extern void vSetAndCheckRegisters( void );\r
169 \r
170 /*\r
171  * Latch the LED that indicates that an error has occurred. \r
172  */\r
173 void vSetErrorLED( void );\r
174 \r
175 /*\r
176  * Thread safe write to the PDC.\r
177  */\r
178 static void prvPDCWrite( portCHAR cAddress, portCHAR cData );\r
179 \r
180 /*\r
181  * Sets up the hardware used by the demo.\r
182  */\r
183 static void prvSetupHardware( void );\r
184 \r
185 \r
186 /*-----------------------------------------------------------*/\r
187 \r
188 /* The structure that is passed on the LCD message queue. */\r
189 typedef struct\r
190 {\r
191         portCHAR **ppcMessageToDisplay; /*<< Points to a char* pointing to the message to display. */\r
192         portBASE_TYPE xRow;                             /*<< The row on which the message should be displayed. */\r
193 } xLCDMessage;\r
194 \r
195 /* Error flag set to pdFAIL if an error is encountered in the tasks/co-routines\r
196 defined within this file. */\r
197 unsigned portBASE_TYPE uxErrorStatus = pdPASS;\r
198 \r
199 /* The queue used to transmit messages to the LCD task. */\r
200 static xQueueHandle xLCDQueue;\r
201 \r
202 /*-----------------------------------------------------------*/\r
203 \r
204 /*\r
205  * Setup the hardware, create the tasks/co-routines, then start the scheduler.\r
206  */\r
207 void main( void )\r
208 {\r
209         /* Create the queue used by tasks wanting to write to the LCD. */\r
210         xLCDQueue = xQueueCreate( mainLCD_QUEUE_LEN, sizeof( xLCDMessage ) );\r
211 \r
212         /* Setup the ports used by the demo and the clock. */\r
213         prvSetupHardware();\r
214 \r
215         /* Create the co-routines that flash the LED's. */\r
216         vStartFlashCoRoutines( mainNUM_FLASH_CO_ROUTINES );\r
217 \r
218         /* Create the co-routine that initiates the transmission of characters\r
219         on the UART and the task that receives them, as described at the top of\r
220         this file. */\r
221         xCoRoutineCreate( vSerialTxCoRoutine, mainTX_CO_ROUTINE_PRIORITY, mainTX_CO_ROUTINE_INDEX );\r
222         xTaskCreate( vCommsRxTask, "CMS", configMINIMAL_STACK_SIZE, NULL, mainCOMMS_RX_TASK_PRIORITY, NULL );\r
223 \r
224         /* Create the task that waits for messages to display on the LCD, plus the\r
225         task and co-routine that send messages for display (as described at the top\r
226         of this file. */\r
227         xTaskCreate( prvLCDTask, "LCD", configMINIMAL_STACK_SIZE, ( void * ) &xLCDQueue, mainLCD_TASK_PRIORITY, NULL );\r
228         xTaskCreate( prvLCDMessageTask, "MSG", configMINIMAL_STACK_SIZE, ( void * ) &xLCDQueue, mainMSG_TASK_PRIORITY, NULL );\r
229         xCoRoutineCreate( prvADCCoRoutine, mainADC_CO_ROUTINE_PRIORITY, mainADC_CO_ROUTINE_INDEX );\r
230 \r
231         /* Start the scheduler running the tasks and co-routines just created. */\r
232         vTaskStartScheduler();\r
233 \r
234         /* Should not get here unless we did not have enough memory to start the\r
235         scheduler. */\r
236         for( ;; );\r
237 }\r
238 /*-----------------------------------------------------------*/\r
239 \r
240 static void prvLCDMessageTask( void * pvParameters )\r
241 {\r
242 /* The strings that are written to the LCD. */\r
243 portCHAR *pcStringsToDisplay[] = {                                                                              \r
244                                                                         "IAR             ",\r
245                                                                         "Stellaris       ",\r
246                                                                         "Demo            ",\r
247                                                                         "www.FreeRTOS.org",\r
248                                                                         ""\r
249                                                                 };\r
250 \r
251 xQueueHandle *pxLCDQueue;       \r
252 xLCDMessage xMessageToSend;\r
253 portBASE_TYPE xIndex = 0;\r
254 \r
255         /* To test the parameter passing mechanism, the queue on which messages are\r
256         posted is passed in as a parameter even though it is available as a file\r
257         scope variable anyway. */\r
258         pxLCDQueue = ( xQueueHandle * ) pvParameters;\r
259 \r
260         for( ;; )\r
261         {\r
262                 /* Wait until it is time to move onto the next string. */\r
263                 vTaskDelay( mainSTRING_WRITE_DELAY );           \r
264                 \r
265                 /* Create the message object to send to the LCD task. */\r
266                 xMessageToSend.ppcMessageToDisplay = &pcStringsToDisplay[ xIndex ];\r
267                 xMessageToSend.xRow = mainTOP_ROW;\r
268                 \r
269                 /* Post the message to be displayed. */\r
270                 if( !xQueueSend( *pxLCDQueue, ( void * ) &xMessageToSend, 0 ) )\r
271                 {\r
272                         uxErrorStatus = pdFAIL;\r
273                 }\r
274                 \r
275                 /* Move onto the next message, wrapping when necessary. */\r
276                 xIndex++;               \r
277                 if( *( pcStringsToDisplay[ xIndex ] ) == 0x00 )\r
278                 {\r
279                         xIndex = 0;\r
280 \r
281                         /* Delay longer before going back to the start of the messages. */\r
282                         vTaskDelay( mainSTRING_WRITE_DELAY * 2 );\r
283                 }\r
284         }\r
285 }\r
286 /*-----------------------------------------------------------*/\r
287 \r
288 void prvLCDTask( void * pvParameters )\r
289 {\r
290 unsigned portBASE_TYPE uxIndex;\r
291 xQueueHandle *pxLCDQueue;\r
292 xLCDMessage xReceivedMessage;\r
293 portCHAR *pcString;\r
294 const unsigned portCHAR ucCFGData[] = { \r
295                                                                                         0x30,   /* Set data bus to 8-bits. */\r
296                                                                                         0x30,\r
297                                                                                         0x30,\r
298                                                                                         0x3C,   /* Number of lines/font. */\r
299                                                                                         0x08,   /* Display off. */\r
300                                                                                         0x01,   /* Display clear. */\r
301                                                                                         0x06,   /* Entry mode [cursor dir][shift]. */\r
302                                                                                         0x0C    /* Display on [display on][curson on][blinking on]. */\r
303                                                                           };  \r
304 \r
305         /* To test the parameter passing mechanism, the queue on which messages are\r
306         received is passed in as a parameter even though it is available as a file\r
307         scope variable anyway. */\r
308         pxLCDQueue = ( xQueueHandle * ) pvParameters;\r
309 \r
310         /* Configure the LCD. */\r
311         uxIndex = 0;\r
312         while( uxIndex < sizeof( ucCFGData ) )\r
313         {\r
314                 prvPDCWrite( PDC_LCD_CSR, ucCFGData[ uxIndex ] );\r
315                 uxIndex++;\r
316                 vTaskDelay( mainCHAR_WRITE_DELAY );\r
317         }\r
318 \r
319         /* Turn the LCD Backlight on. */\r
320         prvPDCWrite( PDC_CSR, 0x01 );\r
321 \r
322         /* Clear display. */\r
323         vTaskDelay( mainCHAR_WRITE_DELAY );\r
324         prvPDCWrite( PDC_LCD_CSR, LCD_CLEAR ); \r
325 \r
326         uxIndex = 0;\r
327         for( ;; )    \r
328         {\r
329                 /* Wait for a message to arrive. */\r
330                 if( xQueueReceive( *pxLCDQueue, &xReceivedMessage, portMAX_DELAY ) )\r
331                 {\r
332                         /* Which row does the received message say to write to? */\r
333                         PDCLCDSetPos( 0, xReceivedMessage.xRow );\r
334 \r
335                         /* Where is the string we are going to display? */\r
336                         pcString = *xReceivedMessage.ppcMessageToDisplay;\r
337                         \r
338                         while( *pcString )\r
339                         {\r
340                                 /* Don't write out the string too quickly as LCD's are usually \r
341                                 pretty slow devices. */\r
342                                 vTaskDelay( mainCHAR_WRITE_DELAY );\r
343                                 prvPDCWrite( PDC_LCD_RAM, *pcString );\r
344                                 pcString++;\r
345                         }               \r
346                 }\r
347         }\r
348 }\r
349 /*-----------------------------------------------------------*/\r
350 \r
351 static void prvADCCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )\r
352 {\r
353 static unsigned portLONG ulADCValue;\r
354 static portCHAR cMessageBuffer[ mainMAX_ADC_STRING_LEN ];\r
355 static portCHAR *pcMessage;\r
356 static xLCDMessage xMessageToSend;\r
357 \r
358         /* Co-routines MUST start with a call to crSTART(). */\r
359         crSTART( xHandle );\r
360         \r
361         for( ;; )\r
362         {\r
363                 /* Start an ADC conversion. */\r
364                 ADCProcessorTrigger( ADC_BASE, 0 );\r
365                 \r
366                 /* Simply delay - when we unblock the result should be available */     \r
367                 crDELAY( xHandle, mainADC_DELAY );\r
368                 \r
369                 /* Get the ADC result. */\r
370                 ADCSequenceDataGet( ADC_BASE, 0, &ulADCValue );\r
371 \r
372                 /* Create a string with the result. */          \r
373                 sprintf( cMessageBuffer, "ADC = %d   ", ulADCValue );\r
374                 pcMessage = cMessageBuffer;\r
375 \r
376                 /* Configure the message we are going to send for display. */\r
377                 xMessageToSend.ppcMessageToDisplay = ( portCHAR** ) &pcMessage;\r
378                 xMessageToSend.xRow = mainBOTTOM_ROW;\r
379                 \r
380                 /* Send the string to the LCD task for display.  We are sending\r
381                 on a task queue so do not have the option to block. */\r
382                 if( !xQueueSend( xLCDQueue, ( void * ) &xMessageToSend, 0 ) )\r
383                 {\r
384                         uxErrorStatus = pdFAIL;\r
385                 }               \r
386         }\r
387         \r
388         /* Co-routines MUST end with a call to crEND(). */\r
389         crEND();\r
390 }\r
391 /*-----------------------------------------------------------*/\r
392 \r
393 static void prvSetupHardware( void )\r
394 {\r
395         /* Setup the PLL. */\r
396         SysCtlClockSet( SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_6MHZ );\r
397         \r
398         /* Initialise the hardware used to talk to the LCD, LED's and UART. */\r
399         PDCInit();\r
400         vParTestInitialise();\r
401         vSerialInit();\r
402 \r
403         /* The ADC is used to read the light sensor. */\r
404         SysCtlPeripheralEnable( SYSCTL_PERIPH_ADC );\r
405     ADCSequenceConfigure( ADC_BASE, 3, ADC_TRIGGER_PROCESSOR, 0);\r
406     ADCSequenceStepConfigure( ADC_BASE, 0, 0, ADC_CTL_CH0 | ADC_CTL_END );\r
407     ADCSequenceEnable( ADC_BASE, 0 );\r
408 \r
409 }\r
410 /*-----------------------------------------------------------*/\r
411 \r
412 static void prvPDCWrite( portCHAR cAddress, portCHAR cData )\r
413 {\r
414         vTaskSuspendAll();\r
415         {\r
416                 PDCWrite( cAddress, cData );\r
417         }\r
418         xTaskResumeAll();\r
419 }\r
420 /*-----------------------------------------------------------*/\r
421 \r
422 void vSetErrorLED( void )\r
423 {\r
424         vParTestSetLED( mainFAIL_LED, pdTRUE );\r
425 }\r
426 /*-----------------------------------------------------------*/\r
427 \r
428 void vApplicationIdleHook( void )\r
429 {\r
430         /* The co-routines are executed in the idle task using the idle task \r
431         hook. */\r
432         for( ;; )\r
433         {\r
434                 /* Schedule the co-routines. */\r
435                 vCoRoutineSchedule();\r
436 \r
437                 /* Run the register check function between each co-routine. */\r
438                 vSetAndCheckRegisters();\r
439                 \r
440                 /* See if the comms task and co-routine has found any errors. */\r
441                 if( uxGetCommsStatus() != pdPASS )\r
442                 {\r
443                         vParTestSetLED( mainFAIL_LED, pdTRUE );\r
444                 }\r
445         }\r
446 }\r
447 /*-----------------------------------------------------------*/\r