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