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