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