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