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