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