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