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