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