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