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