]> git.sur5r.net Git - freertos/blob - Demo/ARM9_STR91X_IAR/main.c
Updated version numbers to V4.1.3.
[freertos] / Demo / ARM9_STR91X_IAR / main.c
1 /*\r
2         FreeRTOS.org V4.1.3 - 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, and APB to 48MHz. */\r
232         SCU_PCLKDivisorConfig( SCU_PCLK_Div2 );\r
233         SCU_PLLFactorsConfig( 192, 25, 2 ); \r
234         SCU_PLLCmd( ENABLE );\r
235         SCU_MCLKSourceConfig( SCU_MCLK_PLL );\r
236 \r
237         WDG_Cmd( DISABLE );\r
238         VIC_DeInit();\r
239 \r
240         /* GPIO8 clock source enable, used by the LCD. */\r
241         SCU_APBPeriphClockConfig(__GPIO8, ENABLE);\r
242         GPIO_DeInit(GPIO8);\r
243 \r
244         /* GPIO 9 clock source enable, used by the LCD. */\r
245         SCU_APBPeriphClockConfig(__GPIO9, ENABLE);\r
246         GPIO_DeInit(GPIO9);\r
247 \r
248         /* Enable VIC clock */\r
249         SCU_AHBPeriphClockConfig(__VIC, ENABLE);\r
250         SCU_AHBPeriphReset(__VIC, DISABLE);\r
251 \r
252         /* Peripheral initialisation. */\r
253         LCD_Init();\r
254         vParTestInitialise();\r
255 }\r
256 /*-----------------------------------------------------------*/\r
257 \r
258 static void vErrorChecks( void *pvParameters )\r
259 {\r
260 static portCHAR cCheckVal[ mainMAX_FLAG_STRING_LEN ];\r
261 portCHAR *pcFlagString;\r
262 xLCDMessage xMessageToSend;\r
263 portTickType xLastWakeTime;\r
264 portCHAR *pcStringsToDisplay[] = {                                                                              \r
265                                                                         "Check status flag"\r
266                                                                  };\r
267 \r
268         /* The parameters are not used in this task. */\r
269         ( void ) pvParameters;\r
270         pcFlagString = &cCheckVal[ 0 ]; \r
271 \r
272 \r
273         /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()\r
274         functions correctly. */\r
275         xLastWakeTime = xTaskGetTickCount();\r
276 \r
277         /* Cycle for ever, delaying then checking all the other tasks are still\r
278         operating without error. */\r
279 \r
280         for( ;; )\r
281         {\r
282                 /* Delay until it is time to execute again. */\r
283                 vTaskDelayUntil( &xLastWakeTime, mainCHECK_PERIOD );\r
284         \r
285                 /* Check all the other tasks to see if the error flag needs updating. */\r
286                 prvCheckOtherTasksAreStillRunning();\r
287                 \r
288                 /* Create a string indicating the error flag status. */\r
289                 sprintf( cCheckVal, "equals 0x%x       ", ulErrorFlags );\r
290                 xMessageToSend.xRow = Line2;\r
291 \r
292                 /* Send the first part of the message to the LCD task. */\r
293                 xMessageToSend.ppcMessageToDisplay = &pcStringsToDisplay[ 0 ];\r
294                 xQueueSend( xLCDQueue, ( void * ) &xMessageToSend, 0 );\r
295                 vTaskDelay( mainSTRING_WRITE_DELAY );\r
296 \r
297                 /* Send the second part of the message to the LCD task. */\r
298                 xMessageToSend.ppcMessageToDisplay = &pcFlagString;\r
299                 xQueueSend( xLCDQueue, ( void * ) &xMessageToSend, 0 );\r
300         }\r
301 }\r
302 /*-----------------------------------------------------------*/\r
303 \r
304 static void prvCheckOtherTasksAreStillRunning( void )\r
305 {\r
306         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
307         {\r
308                 ulErrorFlags |= 0x01;\r
309         }\r
310 \r
311         if( xArePollingQueuesStillRunning() != pdTRUE )\r
312         {\r
313                 ulErrorFlags |=  0x02;\r
314         }\r
315 \r
316         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
317         {\r
318                 ulErrorFlags |= 0x04;\r
319         }\r
320 \r
321         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
322         {\r
323                 ulErrorFlags |= 0x08;\r
324         }\r
325 \r
326         if( xAreComTestTasksStillRunning() != pdTRUE )\r
327         {\r
328                 ulErrorFlags |= 0x10;\r
329         }\r
330 \r
331         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
332         {\r
333                 ulErrorFlags |= 0x20;\r
334         }\r
335 \r
336         if( xAreMathsTaskStillRunning() != pdTRUE )\r
337         {\r
338                 ulErrorFlags |= 0x40;\r
339         }\r
340 }\r
341 /*-----------------------------------------------------------*/\r
342 \r
343 static void prvLCDMessageTask( void * pvParameters )\r
344 {\r
345 xQueueHandle *pxLCDQueue;       \r
346 xLCDMessage xMessageToSend;\r
347 portBASE_TYPE xIndex = 0;\r
348 \r
349 /* The strings that are written to the LCD. */\r
350 portCHAR *pcStringsToDisplay[] = {                                                                              \r
351                                                                         "IAR             ",\r
352                                                                         "STR912          ",\r
353                                                                         "Demo            ",\r
354                                                                         "www.FreeRTOS.org",\r
355                                                                         ""\r
356                                                                 };\r
357 \r
358 \r
359         /* To test the parameter passing mechanism, the queue on which messages are\r
360         posted is passed in as a parameter even though it is available as a file\r
361         scope variable anyway. */\r
362         pxLCDQueue = ( xQueueHandle * ) pvParameters;\r
363 \r
364         for( ;; )\r
365         {\r
366                 /* Wait until it is time to move onto the next string. */\r
367                 vTaskDelay( mainSTRING_WRITE_DELAY );           \r
368                 \r
369                 /* Configure the message object to send to the LCD task. */\r
370                 xMessageToSend.ppcMessageToDisplay = &pcStringsToDisplay[ xIndex ];\r
371                 xMessageToSend.xRow = Line1;\r
372                 \r
373                 /* Post the message to be displayed. */\r
374                 xQueueSend( *pxLCDQueue, ( void * ) &xMessageToSend, 0 );\r
375                 \r
376                 /* Move onto the next message, wrapping when necessary. */\r
377                 xIndex++;               \r
378                 if( *( pcStringsToDisplay[ xIndex ] ) == 0x00 )\r
379                 {\r
380                         xIndex = 0;\r
381 \r
382                         /* Delay longer before going back to the start of the messages. */\r
383                         vTaskDelay( mainSTRING_WRITE_DELAY * 2 );\r
384                 }\r
385         }\r
386 }\r
387 /*-----------------------------------------------------------*/\r
388 \r
389 void prvLCDTask( void * pvParameters )\r
390 {\r
391 xQueueHandle *pxLCDQueue;\r
392 xLCDMessage xReceivedMessage;\r
393 portCHAR *pcString;\r
394 \r
395         /* To test the parameter passing mechanism, the queue on which messages are\r
396         received is passed in as a parameter even though it is available as a file\r
397         scope variable anyway. */\r
398         pxLCDQueue = ( xQueueHandle * ) pvParameters;\r
399 \r
400         for( ;; )    \r
401         {\r
402                 /* Wait for a message to arrive. */\r
403                 if( xQueueReceive( *pxLCDQueue, &xReceivedMessage, portMAX_DELAY ) )\r
404                 {\r
405                         /* Where is the string we are going to display? */\r
406                         pcString = *xReceivedMessage.ppcMessageToDisplay;\r
407                         LCD_DisplayString(xReceivedMessage.xRow, pcString, BlackText);\r
408 \r
409                         /* The delay here is just to ensure the LCD task does not starve\r
410                         out lower priority tasks as writhing to the LCD can take a long\r
411                         time. */\r
412                         vTaskDelay( mainLCD_DELAY );\r
413                 }\r
414         }\r
415 }\r
416 /*-----------------------------------------------------------*/\r
417 \r