]> git.sur5r.net Git - freertos/blob - Demo/ARM9_STR91X_IAR/main.c
V4.2.1 files.
[freertos] / Demo / ARM9_STR91X_IAR / main.c
1 /*\r
2         FreeRTOS.org V4.2.1 - 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 \r
97 /* Priorities for the demo application tasks. */\r
98 #define mainLED_TASK_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
99 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
100 #define mainCHECK_TASK_PRIORITY         ( tskIDLE_PRIORITY + 4 )\r
101 #define mainSEM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
102 #define mainBLOCK_Q_PRIORITY            ( tskIDLE_PRIORITY + 2 )\r
103 #define mainCOM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 3 )\r
104 #define mainLCD_TASK_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
105 #define mainMSG_TASK_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
106 \r
107 /* Delays used by the various tasks defined in this file. */\r
108 #define mainCHECK_PERIOD                        ( ( portTickType ) 3000 / portTICK_RATE_MS  )\r
109 #define mainSTRING_WRITE_DELAY          ( 500 / portTICK_RATE_MS )\r
110 #define mainLCD_DELAY                           ( 20 / portTICK_RATE_MS )\r
111 \r
112 /* Constants for the ComTest tasks. */\r
113 #define mainCOM_TEST_BAUD_RATE          ( ( unsigned portLONG ) 115200 )\r
114 #define mainCOM_TEST_LED                        ( 3 )\r
115 \r
116 /* The maximum number of messages that can be pending to be written to the LCD. */\r
117 #define mainLCD_QUEUE_LEN                       ( 6 )\r
118 \r
119 /* Dimension the buffer used to write the error flag string. */\r
120 #define mainMAX_FLAG_STRING_LEN         ( 32 )\r
121 \r
122 /* The structure that is passed on the LCD message queue. */\r
123 typedef struct\r
124 {\r
125         portCHAR **ppcMessageToDisplay; /*<< Points to a char* pointing to the message to display. */\r
126         portBASE_TYPE xRow;                             /*<< The row on which the message should be displayed. */\r
127 } xLCDMessage;\r
128 /*-----------------------------------------------------------*/\r
129 \r
130 /*\r
131  * The task that executes at the highest priority and calls\r
132  * prvCheckOtherTasksAreStillRunning().  See the description at the top\r
133  * of the file.\r
134  */\r
135 static void vErrorChecks( void *pvParameters );\r
136 \r
137 /*\r
138  * Configure the processor clock and ports.\r
139  */\r
140 static void prvSetupHardware( void );\r
141 \r
142 /*\r
143  * Checks that all the demo application tasks are still executing without error\r
144  * - as described at the top of the file.  Called by vErrorChecks().\r
145  */\r
146 static void prvCheckOtherTasksAreStillRunning( void );\r
147 \r
148 /*\r
149  * The WEB server task prototype.  The task is created in this file but defined\r
150  * elsewhere. \r
151  */\r
152 extern void vuIP_Task(void *pvParameters);\r
153 \r
154 /*\r
155  * The task that displays text on the LCD.\r
156  */\r
157 static void prvLCDTask( void * pvParameters );\r
158 \r
159 /*\r
160  * The task that sends messages to be displayed on the top row of the LCD.\r
161  */\r
162 static void prvLCDMessageTask( void * pvParameters );\r
163 \r
164 /*-----------------------------------------------------------*/\r
165 \r
166 /* The queue used to pass messages to the LCD task. */\r
167 static xQueueHandle xLCDQueue;\r
168 \r
169 /* Error status flag. */\r
170 static unsigned portLONG ulErrorFlags = 0;\r
171 \r
172 /*-----------------------------------------------------------*/\r
173 \r
174 /*\r
175  * Starts all the other tasks, then starts the scheduler.\r
176  */\r
177 void main( void )\r
178 {\r
179         #ifdef DEBUG\r
180                 debug();\r
181         #endif\r
182         \r
183         /* Setup any hardware that has not already been configured by the low\r
184         level init routines. */\r
185         prvSetupHardware();\r
186 \r
187         /* Create the queue used to send data to the LCD task. */\r
188         xLCDQueue = xQueueCreate( mainLCD_QUEUE_LEN, sizeof( xLCDMessage ) );\r
189 \r
190         /* Start all the standard demo application tasks. */\r
191         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
192         vStartLEDFlashTasks( mainLED_TASK_PRIORITY );\r
193         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
194         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
195         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
196         vStartDynamicPriorityTasks();\r
197         vStartMathTasks( tskIDLE_PRIORITY );\r
198         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
199 \r
200         /* Start the tasks which are defined in this file. */\r
201         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
202         xTaskCreate( prvLCDTask, "LCD", configMINIMAL_STACK_SIZE, ( void * ) &xLCDQueue, mainLCD_TASK_PRIORITY, NULL );\r
203         xTaskCreate( prvLCDMessageTask, "MSG", configMINIMAL_STACK_SIZE, ( void * ) &xLCDQueue, mainMSG_TASK_PRIORITY, NULL );\r
204 \r
205         /* Finally, create the WEB server task. */\r
206         xTaskCreate( vuIP_Task, "uIP", configMINIMAL_STACK_SIZE * 3, NULL, mainCHECK_TASK_PRIORITY - 1, NULL );\r
207         \r
208 \r
209         /* Start the scheduler.\r
210 \r
211         NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.\r
212         The processor MUST be in supervisor mode when vTaskStartScheduler is\r
213         called.  The demo applications included in the FreeRTOS.org download switch\r
214         to supervisor mode prior to main being called.  If you are not using one of\r
215         these demo application projects then ensure Supervisor mode is used here. */\r
216 \r
217         vTaskStartScheduler();\r
218 \r
219         /* We should never get here as control is now taken by the scheduler. */\r
220         for( ;; );\r
221 }\r
222 /*-----------------------------------------------------------*/\r
223 \r
224 static void prvSetupHardware( void )\r
225 {\r
226         /* Configuration taken from the ST code.\r
227 \r
228         Set Flash banks size & address */\r
229         FMI_BankRemapConfig( 4, 2, 0, 0x80000 ); \r
230 \r
231         /* FMI Waite States */\r
232         FMI_Config( FMI_READ_WAIT_STATE_2, FMI_WRITE_WAIT_STATE_0, FMI_PWD_ENABLE, FMI_LVD_ENABLE, FMI_FREQ_HIGH ); \r
233 \r
234         /* Configure the FPLL = 96MHz, and APB to 48MHz. */\r
235         SCU_PCLKDivisorConfig( SCU_PCLK_Div2 );\r
236         SCU_PLLFactorsConfig( 192, 25, 2 ); \r
237         SCU_PLLCmd( ENABLE );\r
238         SCU_MCLKSourceConfig( SCU_MCLK_PLL );\r
239 \r
240         WDG_Cmd( DISABLE );\r
241         VIC_DeInit();\r
242 \r
243         /* GPIO8 clock source enable, used by the LCD. */\r
244         SCU_APBPeriphClockConfig(__GPIO8, ENABLE);\r
245         GPIO_DeInit(GPIO8);\r
246 \r
247         /* GPIO 9 clock source enable, used by the LCD. */\r
248         SCU_APBPeriphClockConfig(__GPIO9, ENABLE);\r
249         GPIO_DeInit(GPIO9);\r
250 \r
251         /* Enable VIC clock */\r
252         SCU_AHBPeriphClockConfig(__VIC, ENABLE);\r
253         SCU_AHBPeriphReset(__VIC, DISABLE);\r
254 \r
255         /* Peripheral initialisation. */\r
256         vParTestInitialise();\r
257 }\r
258 /*-----------------------------------------------------------*/\r
259 \r
260 static void vErrorChecks( void *pvParameters )\r
261 {\r
262 static portCHAR cCheckVal[ mainMAX_FLAG_STRING_LEN ];\r
263 portCHAR *pcFlagString;\r
264 xLCDMessage xMessageToSend;\r
265 portTickType xLastWakeTime;\r
266 portCHAR *pcStringsToDisplay[] = {                                                                              \r
267                                                                         "Check status flag"\r
268                                                                  };\r
269 \r
270         /* The parameters are not used in this task. */\r
271         ( void ) pvParameters;\r
272         pcFlagString = &cCheckVal[ 0 ]; \r
273 \r
274 \r
275         /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()\r
276         functions correctly. */\r
277         xLastWakeTime = xTaskGetTickCount();\r
278 \r
279         /* Cycle for ever, delaying then checking all the other tasks are still\r
280         operating without error. */\r
281 \r
282         for( ;; )\r
283         {\r
284                 /* Delay until it is time to execute again. */\r
285                 vTaskDelayUntil( &xLastWakeTime, mainCHECK_PERIOD );\r
286         \r
287                 /* Check all the other tasks to see if the error flag needs updating. */\r
288                 prvCheckOtherTasksAreStillRunning();\r
289                 \r
290                 /* Create a string indicating the error flag status. */\r
291                 sprintf( cCheckVal, "equals 0x%x       ", ulErrorFlags );\r
292                 xMessageToSend.xRow = Line2;\r
293 \r
294                 /* Send the first part of the message to the LCD task. */\r
295                 xMessageToSend.ppcMessageToDisplay = &pcStringsToDisplay[ 0 ];\r
296                 xQueueSend( xLCDQueue, ( void * ) &xMessageToSend, 0 );\r
297                 vTaskDelay( mainSTRING_WRITE_DELAY );\r
298 \r
299                 /* Send the second part of the message to the LCD task. */\r
300                 xMessageToSend.ppcMessageToDisplay = &pcFlagString;\r
301                 xQueueSend( xLCDQueue, ( void * ) &xMessageToSend, 0 );\r
302         }\r
303 }\r
304 /*-----------------------------------------------------------*/\r
305 \r
306 static void prvCheckOtherTasksAreStillRunning( void )\r
307 {\r
308         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
309         {\r
310                 ulErrorFlags |= 0x01;\r
311         }\r
312 \r
313         if( xArePollingQueuesStillRunning() != pdTRUE )\r
314         {\r
315                 ulErrorFlags |=  0x02;\r
316         }\r
317 \r
318         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
319         {\r
320                 ulErrorFlags |= 0x04;\r
321         }\r
322 \r
323         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
324         {\r
325                 ulErrorFlags |= 0x08;\r
326         }\r
327 \r
328         if( xAreComTestTasksStillRunning() != pdTRUE )\r
329         {\r
330                 ulErrorFlags |= 0x10;\r
331         }\r
332 \r
333         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
334         {\r
335                 ulErrorFlags |= 0x20;\r
336         }\r
337 \r
338         if( xAreMathsTaskStillRunning() != pdTRUE )\r
339         {\r
340                 ulErrorFlags |= 0x40;\r
341         }\r
342 }\r
343 /*-----------------------------------------------------------*/\r
344 \r
345 static void prvLCDMessageTask( void * pvParameters )\r
346 {\r
347 xQueueHandle *pxLCDQueue;       \r
348 xLCDMessage xMessageToSend;\r
349 portBASE_TYPE xIndex = 0;\r
350 \r
351 /* The strings that are written to the LCD. */\r
352 portCHAR *pcStringsToDisplay[] = {                                                                              \r
353                                                                         "IAR             ",\r
354                                                                         "STR912          ",\r
355                                                                         "Demo            ",\r
356                                                                         "www.FreeRTOS.org",\r
357                                                                         ""\r
358                                                                 };\r
359 \r
360 \r
361         /* To test the parameter passing mechanism, the queue on which messages are\r
362         posted is passed in as a parameter even though it is available as a file\r
363         scope variable anyway. */\r
364         pxLCDQueue = ( xQueueHandle * ) pvParameters;\r
365 \r
366         for( ;; )\r
367         {\r
368                 /* Wait until it is time to move onto the next string. */\r
369                 vTaskDelay( mainSTRING_WRITE_DELAY );           \r
370                 \r
371                 /* Configure the message object to send to the LCD task. */\r
372                 xMessageToSend.ppcMessageToDisplay = &pcStringsToDisplay[ xIndex ];\r
373                 xMessageToSend.xRow = Line1;\r
374                 \r
375                 /* Post the message to be displayed. */\r
376                 xQueueSend( *pxLCDQueue, ( void * ) &xMessageToSend, 0 );\r
377                 \r
378                 /* Move onto the next message, wrapping when necessary. */\r
379                 xIndex++;               \r
380                 if( *( pcStringsToDisplay[ xIndex ] ) == 0x00 )\r
381                 {\r
382                         xIndex = 0;\r
383 \r
384                         /* Delay longer before going back to the start of the messages. */\r
385                         vTaskDelay( mainSTRING_WRITE_DELAY * 2 );\r
386                 }\r
387         }\r
388 }\r
389 /*-----------------------------------------------------------*/\r
390 \r
391 void prvLCDTask( void * pvParameters )\r
392 {\r
393 xQueueHandle *pxLCDQueue;\r
394 xLCDMessage xReceivedMessage;\r
395 portCHAR *pcString;\r
396 \r
397         /* To test the parameter passing mechanism, the queue on which messages are\r
398         received is passed in as a parameter even though it is available as a file\r
399         scope variable anyway. */\r
400         pxLCDQueue = ( xQueueHandle * ) pvParameters;\r
401 \r
402         LCD_Init();\r
403 \r
404         for( ;; )    \r
405         {\r
406                 /* Wait for a message to arrive. */\r
407                 if( xQueueReceive( *pxLCDQueue, &xReceivedMessage, portMAX_DELAY ) )\r
408                 {\r
409                         /* Where is the string we are going to display? */\r
410                         pcString = *xReceivedMessage.ppcMessageToDisplay;\r
411                         LCD_DisplayString(xReceivedMessage.xRow, pcString, BlackText);\r
412 \r
413                         /* The delay here is just to ensure the LCD task does not starve\r
414                         out lower priority tasks as writhing to the LCD can take a long\r
415                         time. */\r
416                         vTaskDelay( mainLCD_DELAY );\r
417                 }\r
418         }\r
419 }\r
420 /*-----------------------------------------------------------*/\r
421 \r