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