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