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