]> git.sur5r.net Git - freertos/blob - Demo/ARM9_STR91X_IAR/main.c
Update to V6.1.1
[freertos] / Demo / ARM9_STR91X_IAR / main.c
1 /*\r
2     FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\r
8     *    + New to FreeRTOS,                                                   *\r
9     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *\r
10     *    + Looking for basic training,                                        *\r
11     *    + Wanting to improve your FreeRTOS skills and productivity           *\r
12     *                                                                         *\r
13     * then take a look at the FreeRTOS books - available as PDF or paperback  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\r
18     * A pdf reference manual is also available.  Both are usually delivered   *\r
19     * to your inbox within 20 minutes to two hours when purchased between 8am *\r
20     * and 8pm GMT (although please allow up to 24 hours in case of            *\r
21     * exceptional circumstances).  Thank you for your support!                *\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 exception to the GPL is included to allow you to distribute\r
31     a combined work that includes FreeRTOS without being obliged to provide the\r
32     source code for proprietary components outside of the FreeRTOS kernel.\r
33     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
34     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
35     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     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /*\r
55         NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.\r
56         The processor MUST be in supervisor mode when vTaskStartScheduler is\r
57         called.  The demo applications included in the FreeRTOS.org download switch\r
58         to supervisor mode prior to main being called.  If you are not using one of\r
59         these demo application projects then ensure Supervisor mode is used.\r
60 */\r
61 \r
62 /*\r
63  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
64  * documentation provides more details of the demo application tasks.\r
65  *\r
66  * A few tasks are created that are not part of the standard demo.  These are\r
67  * the 'LCD' task, the 'LCD Message' task, a WEB server task and the 'Check'\r
68  * task.\r
69  *\r
70  * The LCD task is the only task that accesses the LCD directly, so mutual\r
71  * exclusion is ensured.  Any task wishing to display text sends the LCD task\r
72  * a message containing a pointer to the string that should be displayed.\r
73  * The LCD task itself just blocks on a queue waiting for such a message to\r
74  * arrive - processing each in turn.\r
75  *\r
76  * The LCD Message task does nothing other than periodically send messages to\r
77  * the LCD task.  The messages originating from the LCD Message task are\r
78  * displayed on the top row of the LCD.\r
79  *\r
80  * The Check task only executes every three seconds but has the highest\r
81  * priority so is guaranteed to get processor time.  Its main function is to\r
82  * check that all the other tasks are still operational. Most tasks maintain\r
83  * a unique count that is incremented each time the task successfully completes\r
84  * a cycle of its function.  Should any error occur within such a task the\r
85  * count is permanently halted.  The check task sets a bit in an error status\r
86  * flag should it find any counter variable at a value that indicates an\r
87  * error has occurred.  The error flag value is converted to a string and sent\r
88  * to the LCD task for display on the bottom row on the LCD.\r
89  */\r
90 \r
91 /* Standard includes. */\r
92 #include <stdio.h>\r
93 \r
94 /* Library includes. */\r
95 #include "91x_lib.h"\r
96 \r
97 /* Scheduler includes. */\r
98 #include "FreeRTOS.h"\r
99 #include "task.h"\r
100 #include "queue.h"\r
101 \r
102 /* Demo application includes. */\r
103 #include "lcd.h"\r
104 #include "flash.h"\r
105 #include "integer.h"\r
106 #include "PollQ.h"\r
107 #include "BlockQ.h"\r
108 #include "semtest.h"\r
109 #include "dynamic.h"\r
110 #include "partest.h"\r
111 #include "flop.h"\r
112 #include "comtest2.h"\r
113 #include "serial.h"\r
114 #include "GenQTest.h"\r
115 #include "QPeek.h"\r
116 \r
117 #ifdef STACK_LWIP\r
118         #include "BasicWEB.h"\r
119         #include "sys.h"\r
120 #endif\r
121 \r
122 /* Priorities for the demo application tasks. */\r
123 #define mainLED_TASK_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
124 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
125 #define mainCHECK_TASK_PRIORITY         ( tskIDLE_PRIORITY + 4 )\r
126 #define mainSEM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
127 #define mainBLOCK_Q_PRIORITY            ( tskIDLE_PRIORITY + 2 )\r
128 #define mainCOM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 3 )\r
129 #define mainLCD_TASK_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
130 #define mainMSG_TASK_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
131 #define mainGENERIC_QUEUE_PRIORITY      ( tskIDLE_PRIORITY )\r
132 \r
133 /* Delays used by the various tasks defined in this file. */\r
134 #define mainCHECK_PERIOD                        ( ( portTickType ) 3000 / portTICK_RATE_MS  )\r
135 #define mainSTRING_WRITE_DELAY          ( 500 / portTICK_RATE_MS )\r
136 #define mainLCD_DELAY                           ( 20 / portTICK_RATE_MS )\r
137 \r
138 /* Constants for the ComTest tasks. */\r
139 #define mainCOM_TEST_BAUD_RATE          ( ( unsigned long ) 115200 )\r
140 #define mainCOM_TEST_LED                        ( 3 )\r
141 \r
142 /* The maximum number of messages that can be pending to be written to the LCD. */\r
143 #define mainLCD_QUEUE_LEN                       ( 6 )\r
144 \r
145 /* Dimension the buffer used to write the error flag string. */\r
146 #define mainMAX_FLAG_STRING_LEN         ( 32 )\r
147 \r
148 /* The structure that is passed on the LCD message queue. */\r
149 typedef struct\r
150 {\r
151         char **ppcMessageToDisplay; /*<< Points to a char* pointing to the message to display. */\r
152         portBASE_TYPE xRow;                             /*<< The row on which the message should be displayed. */\r
153 } xLCDMessage;\r
154 /*-----------------------------------------------------------*/\r
155 \r
156 /*\r
157  * The task that executes at the highest priority and calls\r
158  * prvCheckOtherTasksAreStillRunning().  See the description at the top\r
159  * of the file.\r
160  */\r
161 static void vErrorChecks( void *pvParameters );\r
162 \r
163 /*\r
164  * Configure the processor clock and ports.\r
165  */\r
166 static void prvSetupHardware( void );\r
167 \r
168 /*\r
169  * Checks that all the demo application tasks are still executing without error\r
170  * - as described at the top of the file.  Called by vErrorChecks().\r
171  */\r
172 static void prvCheckOtherTasksAreStillRunning( void );\r
173 \r
174 #ifdef STACK_UIP\r
175         /*\r
176          * The WEB server task prototype.  The task is created in this file but defined\r
177          * elsewhere.  STACK_UIP is defined when the uIP stack is used in preference\r
178          * to the lwIP stack.\r
179          */\r
180         extern void vuIP_Task(void *pvParameters);\r
181 #endif\r
182 \r
183 /*\r
184  * The task that displays text on the LCD.\r
185  */\r
186 static void prvLCDTask( void * pvParameters );\r
187 \r
188 /*\r
189  * The task that sends messages to be displayed on the top row of the LCD.\r
190  */\r
191 static void prvLCDMessageTask( void * pvParameters );\r
192 \r
193 /*-----------------------------------------------------------*/\r
194 \r
195 /* The queue used to pass messages to the LCD task. */\r
196 static xQueueHandle xLCDQueue;\r
197 \r
198 /* Error status flag. */\r
199 static unsigned long ulErrorFlags = 0;\r
200 \r
201 /*-----------------------------------------------------------*/\r
202 \r
203 /*\r
204  * Starts all the other tasks, then starts the scheduler.\r
205  */\r
206 void main( void )\r
207 {\r
208         #ifdef DEBUG\r
209                 debug();\r
210         #endif\r
211         \r
212         /* Setup any hardware that has not already been configured by the low\r
213         level init routines. */\r
214         prvSetupHardware();\r
215         /* Create the queue used to send data to the LCD task. */\r
216         xLCDQueue = xQueueCreate( mainLCD_QUEUE_LEN, sizeof( xLCDMessage ) );\r
217         \r
218         /* Start all the standard demo application tasks. */\r
219         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
220         vStartLEDFlashTasks( mainLED_TASK_PRIORITY );\r
221         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
222         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
223         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
224         vStartDynamicPriorityTasks();\r
225         vStartMathTasks( tskIDLE_PRIORITY );\r
226         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
227         vStartGenericQueueTasks( mainGENERIC_QUEUE_PRIORITY );\r
228         vStartQueuePeekTasks(); \r
229 \r
230         /* Start the tasks which are defined in this file. */\r
231         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
232         xTaskCreate( prvLCDTask, "LCD", configMINIMAL_STACK_SIZE, ( void * ) &xLCDQueue, mainLCD_TASK_PRIORITY, NULL );\r
233         xTaskCreate( prvLCDMessageTask, "MSG", configMINIMAL_STACK_SIZE, ( void * ) &xLCDQueue, mainMSG_TASK_PRIORITY, NULL );\r
234 \r
235         /* Start either the uIP TCP/IP stack or the lwIP TCP/IP stack. */\r
236         #ifdef STACK_UIP\r
237                 /* Finally, create the WEB server task. */\r
238                 xTaskCreate( vuIP_Task, "uIP", configMINIMAL_STACK_SIZE * 3, NULL, mainCHECK_TASK_PRIORITY - 1, NULL );\r
239         #endif\r
240 \r
241         #ifdef STACK_LWIP       \r
242                 /* Create the lwIP task.  This uses the lwIP RTOS abstraction layer.*/\r
243                 vlwIPInit();\r
244                 sys_set_state(  ( signed char * ) "httpd", lwipBASIC_SERVER_STACK_SIZE );\r
245                 sys_thread_new( vBasicWEBServer, ( void * ) NULL, basicwebWEBSERVER_PRIORITY );\r
246                 sys_set_default_state();\r
247         #endif\r
248 \r
249         /* Start the scheduler.\r
250 \r
251         NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.\r
252         The processor MUST be in supervisor mode when vTaskStartScheduler is\r
253         called.  The demo applications included in the FreeRTOS.org download switch\r
254         to supervisor mode prior to main being called.  If you are not using one of\r
255         these demo application projects then ensure Supervisor mode is used here. */\r
256 \r
257         vTaskStartScheduler();\r
258 \r
259         /* We should never get here as control is now taken by the scheduler. */\r
260         for( ;; );\r
261 }\r
262 /*-----------------------------------------------------------*/\r
263 \r
264 static void prvSetupHardware( void )\r
265 {\r
266         /* Configuration taken from the ST code.\r
267 \r
268         Set Flash banks size & address */\r
269         FMI_BankRemapConfig( 4, 2, 0, 0x80000 );\r
270 \r
271         /* FMI Waite States */\r
272         FMI_Config( FMI_READ_WAIT_STATE_2, FMI_WRITE_WAIT_STATE_0, FMI_PWD_ENABLE, FMI_LVD_ENABLE, FMI_FREQ_HIGH );\r
273 \r
274         /* Configure the FPLL = 96MHz, and APB to 48MHz. */\r
275         SCU_PCLKDivisorConfig( SCU_PCLK_Div2 );\r
276         SCU_PLLFactorsConfig( 192, 25, 2 );\r
277         SCU_PLLCmd( ENABLE );\r
278         SCU_MCLKSourceConfig( SCU_MCLK_PLL );\r
279 \r
280         WDG_Cmd( DISABLE );\r
281         VIC_DeInit();\r
282 \r
283         /* GPIO8 clock source enable, used by the LCD. */\r
284         SCU_APBPeriphClockConfig(__GPIO8, ENABLE);\r
285         GPIO_DeInit(GPIO8);\r
286 \r
287         /* GPIO 9 clock source enable, used by the LCD. */\r
288         SCU_APBPeriphClockConfig(__GPIO9, ENABLE);\r
289         GPIO_DeInit(GPIO9);\r
290 \r
291         /* Enable VIC clock */\r
292         SCU_AHBPeriphClockConfig(__VIC, ENABLE);\r
293         SCU_AHBPeriphReset(__VIC, DISABLE);\r
294 \r
295         /* Peripheral initialisation. */\r
296         vParTestInitialise();\r
297 }\r
298 /*-----------------------------------------------------------*/\r
299 \r
300 static void vErrorChecks( void *pvParameters )\r
301 {\r
302 static char cCheckVal[ mainMAX_FLAG_STRING_LEN ];\r
303 char *pcFlagString;\r
304 xLCDMessage xMessageToSend;\r
305 portTickType xLastWakeTime;\r
306 char *pcStringsToDisplay[] = {                                                                          \r
307                                                                         "Check status flag"\r
308                                                                  };\r
309 \r
310         /* The parameters are not used in this task. */\r
311         ( void ) pvParameters;\r
312         \r
313         pcFlagString = &cCheckVal[ 0 ]; \r
314 \r
315         /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()\r
316         functions correctly. */\r
317         xLastWakeTime = xTaskGetTickCount();\r
318 \r
319         /* Cycle for ever, delaying then checking all the other tasks are still\r
320         operating without error. */\r
321         for( ;; )\r
322         {\r
323                 /* Delay until it is time to execute again. */\r
324                 vTaskDelayUntil( &xLastWakeTime, mainCHECK_PERIOD );\r
325         \r
326                 /* Check all the other tasks to see if the error flag needs updating. */\r
327                 prvCheckOtherTasksAreStillRunning();\r
328                 \r
329                 /* Create a string indicating the error flag status. */\r
330                 sprintf( cCheckVal, "equals 0x%x       ", ulErrorFlags );\r
331                 xMessageToSend.xRow = Line2;\r
332 \r
333                 /* Send the first part of the message to the LCD task. */\r
334                 xMessageToSend.ppcMessageToDisplay = &pcStringsToDisplay[ 0 ];\r
335                 xQueueSend( xLCDQueue, ( void * ) &xMessageToSend, 0 );\r
336                 vTaskDelay( mainSTRING_WRITE_DELAY );\r
337 \r
338                 /* Send the second part of the message to the LCD task. */\r
339                 xMessageToSend.ppcMessageToDisplay = &pcFlagString;\r
340                 xQueueSend( xLCDQueue, ( void * ) &xMessageToSend, 0 );\r
341         }\r
342 }\r
343 /*-----------------------------------------------------------*/\r
344 \r
345 static void prvCheckOtherTasksAreStillRunning( void )\r
346 {\r
347         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
348         {\r
349                 ulErrorFlags |= 0x01;\r
350         }\r
351 \r
352         if( xArePollingQueuesStillRunning() != pdTRUE )\r
353         {\r
354                 ulErrorFlags |=  0x02;\r
355         }\r
356 \r
357         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
358         {\r
359                 ulErrorFlags |= 0x04;\r
360         }\r
361 \r
362         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
363         {\r
364                 ulErrorFlags |= 0x08;\r
365         }\r
366 \r
367         if( xAreComTestTasksStillRunning() != pdTRUE )\r
368         {\r
369                 ulErrorFlags |= 0x10;\r
370         }\r
371 \r
372         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
373         {\r
374                 ulErrorFlags |= 0x20;\r
375         }\r
376 \r
377         if( xAreMathsTaskStillRunning() != pdTRUE )\r
378         {\r
379                 ulErrorFlags |= 0x40;\r
380         }\r
381         \r
382         if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
383         {\r
384                 ulErrorFlags |= 0x80;\r
385         }\r
386 \r
387         if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
388         {\r
389                 ulErrorFlags |= 0x100;\r
390         }\r
391         \r
392 }\r
393 /*-----------------------------------------------------------*/\r
394 \r
395 static void prvLCDMessageTask( void * pvParameters )\r
396 {\r
397 xQueueHandle *pxLCDQueue;       \r
398 xLCDMessage xMessageToSend;\r
399 portBASE_TYPE xIndex = 0;\r
400 \r
401 /* The strings that are written to the LCD. */\r
402 char *pcStringsToDisplay[] = {                                                                          \r
403                                                                         "IAR             ",\r
404                                                                         "STR912          ",\r
405                                                                         "Demo            ",\r
406                                                                         "www.FreeRTOS.org",\r
407                                                                         ""\r
408                                                                 };\r
409 \r
410 \r
411         /* To test the parameter passing mechanism, the queue on which messages are\r
412         posted is passed in as a parameter even though it is available as a file\r
413         scope variable anyway. */\r
414         pxLCDQueue = ( xQueueHandle * ) pvParameters;\r
415 \r
416         for( ;; )\r
417         {\r
418                 /* Wait until it is time to move onto the next string. */\r
419                 vTaskDelay( mainSTRING_WRITE_DELAY );           \r
420                 \r
421                 /* Configure the message object to send to the LCD task. */\r
422                 xMessageToSend.ppcMessageToDisplay = &pcStringsToDisplay[ xIndex ];\r
423                 xMessageToSend.xRow = Line1;\r
424                 \r
425                 /* Post the message to be displayed. */\r
426                 xQueueSend( *pxLCDQueue, ( void * ) &xMessageToSend, 0 );\r
427                 \r
428                 /* Move onto the next message, wrapping when necessary. */\r
429                 xIndex++;               \r
430                 if( *( pcStringsToDisplay[ xIndex ] ) == 0x00 )\r
431                 {\r
432                         xIndex = 0;\r
433 \r
434                         /* Delay longer before going back to the start of the messages. */\r
435                         vTaskDelay( mainSTRING_WRITE_DELAY * 2 );\r
436                 }\r
437         }\r
438 }\r
439 /*-----------------------------------------------------------*/\r
440 \r
441 void prvLCDTask( void * pvParameters )\r
442 {\r
443 xQueueHandle *pxLCDQueue;\r
444 xLCDMessage xReceivedMessage;\r
445 char *pcString;\r
446 \r
447         /* To test the parameter passing mechanism, the queue on which messages are\r
448         received is passed in as a parameter even though it is available as a file\r
449         scope variable anyway. */\r
450         pxLCDQueue = ( xQueueHandle * ) pvParameters;\r
451 \r
452         LCD_Init();\r
453 \r
454         for( ;; )\r
455         {\r
456                 /* Wait for a message to arrive. */\r
457                 if( xQueueReceive( *pxLCDQueue, &xReceivedMessage, portMAX_DELAY ) )\r
458                 {\r
459                         /* Where is the string we are going to display? */\r
460                         pcString = *xReceivedMessage.ppcMessageToDisplay;\r
461                         LCD_DisplayString(xReceivedMessage.xRow, pcString, BlackText);\r
462 \r
463                         /* The delay here is just to ensure the LCD task does not starve\r
464                         out lower priority tasks as writing to the LCD can take a long\r
465                         time. */\r
466                         vTaskDelay( mainLCD_DELAY );\r
467                 }\r
468         }\r
469 }\r
470 /*-----------------------------------------------------------*/\r