]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ARM9_STR91X_IAR/main.c
62b3bf7b248104090f49e7a2a94400583e4a1a2b
[freertos] / FreeRTOS / Demo / ARM9_STR91X_IAR / main.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /*\r
30         NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.\r
31         The processor MUST be in supervisor mode when vTaskStartScheduler is\r
32         called.  The demo applications included in the FreeRTOS.org download switch\r
33         to supervisor mode prior to main being called.  If you are not using one of\r
34         these demo application projects then ensure Supervisor mode is used.\r
35 */\r
36 \r
37 /*\r
38  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
39  * documentation provides more details of the demo application tasks.\r
40  *\r
41  * A few tasks are created that are not part of the standard demo.  These are\r
42  * the 'LCD' task, the 'LCD Message' task, a WEB server task and the 'Check'\r
43  * task.\r
44  *\r
45  * The LCD task is the only task that accesses the LCD directly, so mutual\r
46  * exclusion is ensured.  Any task wishing to display text sends the LCD task\r
47  * a message containing a pointer to the string that should be displayed.\r
48  * The LCD task itself just blocks on a queue waiting for such a message to\r
49  * arrive - processing each in turn.\r
50  *\r
51  * The LCD Message task does nothing other than periodically send messages to\r
52  * the LCD task.  The messages originating from the LCD Message task are\r
53  * displayed on the top row of the LCD.\r
54  *\r
55  * The Check task only executes every three seconds but has the highest\r
56  * priority so is guaranteed to get processor time.  Its main function is to\r
57  * check that all the other tasks are still operational. Most tasks maintain\r
58  * a unique count that is incremented each time the task successfully completes\r
59  * a cycle of its function.  Should any error occur within such a task the\r
60  * count is permanently halted.  The check task sets a bit in an error status\r
61  * flag should it find any counter variable at a value that indicates an\r
62  * error has occurred.  The error flag value is converted to a string and sent\r
63  * to the LCD task for display on the bottom row on the LCD.\r
64  */\r
65 \r
66 /* Standard includes. */\r
67 #include <stdio.h>\r
68 \r
69 /* Library includes. */\r
70 #include "91x_lib.h"\r
71 \r
72 /* Scheduler includes. */\r
73 #include "FreeRTOS.h"\r
74 #include "task.h"\r
75 #include "queue.h"\r
76 \r
77 /* Demo application includes. */\r
78 #include "lcd.h"\r
79 #include "flash.h"\r
80 #include "integer.h"\r
81 #include "PollQ.h"\r
82 #include "BlockQ.h"\r
83 #include "semtest.h"\r
84 #include "dynamic.h"\r
85 #include "partest.h"\r
86 #include "flop.h"\r
87 #include "comtest2.h"\r
88 #include "serial.h"\r
89 #include "GenQTest.h"\r
90 #include "QPeek.h"\r
91 \r
92 #ifdef STACK_LWIP\r
93         #include "BasicWEB.h"\r
94         #include "sys.h"\r
95 #endif\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 #define mainGENERIC_QUEUE_PRIORITY      ( tskIDLE_PRIORITY )\r
107 \r
108 /* Delays used by the various tasks defined in this file. */\r
109 #define mainCHECK_PERIOD                        ( ( TickType_t ) 3000 / portTICK_PERIOD_MS  )\r
110 #define mainSTRING_WRITE_DELAY          ( 500 / portTICK_PERIOD_MS )\r
111 #define mainLCD_DELAY                           ( 20 / portTICK_PERIOD_MS )\r
112 \r
113 /* Constants for the ComTest tasks. */\r
114 #define mainCOM_TEST_BAUD_RATE          ( ( unsigned long ) 115200 )\r
115 #define mainCOM_TEST_LED                        ( 3 )\r
116 \r
117 /* The maximum number of messages that can be pending to be written to the LCD. */\r
118 #define mainLCD_QUEUE_LEN                       ( 6 )\r
119 \r
120 /* Dimension the buffer used to write the error flag string. */\r
121 #define mainMAX_FLAG_STRING_LEN         ( 32 )\r
122 \r
123 /* The structure that is passed on the LCD message queue. */\r
124 typedef struct\r
125 {\r
126         char **ppcMessageToDisplay; /*<< Points to a char* pointing to the message to display. */\r
127         portBASE_TYPE xRow;                             /*<< The row on which the message should be displayed. */\r
128 } xLCDMessage;\r
129 /*-----------------------------------------------------------*/\r
130 \r
131 /*\r
132  * The task that executes at the highest priority and calls\r
133  * prvCheckOtherTasksAreStillRunning().  See the description at the top\r
134  * of the file.\r
135  */\r
136 static void vErrorChecks( void *pvParameters );\r
137 \r
138 /*\r
139  * Configure the processor clock and ports.\r
140  */\r
141 static void prvSetupHardware( void );\r
142 \r
143 /*\r
144  * Checks that all the demo application tasks are still executing without error\r
145  * - as described at the top of the file.  Called by vErrorChecks().\r
146  */\r
147 static void prvCheckOtherTasksAreStillRunning( void );\r
148 \r
149 #ifdef STACK_UIP\r
150         /*\r
151          * The WEB server task prototype.  The task is created in this file but defined\r
152          * elsewhere.  STACK_UIP is defined when the uIP stack is used in preference\r
153          * to the lwIP stack.\r
154          */\r
155         extern void vuIP_Task(void *pvParameters);\r
156 #endif\r
157 \r
158 /*\r
159  * The task that displays text on the LCD.\r
160  */\r
161 static void prvLCDTask( void * pvParameters );\r
162 \r
163 /*\r
164  * The task that sends messages to be displayed on the top row of the LCD.\r
165  */\r
166 static void prvLCDMessageTask( void * pvParameters );\r
167 \r
168 /*-----------------------------------------------------------*/\r
169 \r
170 /* The queue used to pass messages to the LCD task. */\r
171 static QueueHandle_t xLCDQueue;\r
172 \r
173 /* Error status flag. */\r
174 static unsigned long ulErrorFlags = 0;\r
175 \r
176 /*-----------------------------------------------------------*/\r
177 \r
178 /*\r
179  * Starts all the other tasks, then starts the scheduler.\r
180  */\r
181 void main( void )\r
182 {\r
183         #ifdef DEBUG\r
184                 debug();\r
185         #endif\r
186         \r
187         /* Setup any hardware that has not already been configured by the low\r
188         level init routines. */\r
189         prvSetupHardware();\r
190         /* Create the queue used to send data to the LCD task. */\r
191         xLCDQueue = xQueueCreate( mainLCD_QUEUE_LEN, sizeof( xLCDMessage ) );\r
192         \r
193         /* Start all the standard demo application tasks. */\r
194         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
195         vStartLEDFlashTasks( mainLED_TASK_PRIORITY );\r
196         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
197         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
198         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
199         vStartDynamicPriorityTasks();\r
200         vStartMathTasks( tskIDLE_PRIORITY );\r
201         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
202         vStartGenericQueueTasks( mainGENERIC_QUEUE_PRIORITY );\r
203         vStartQueuePeekTasks(); \r
204 \r
205         /* Start the tasks which are defined in this file. */\r
206         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
207         xTaskCreate( prvLCDTask, "LCD", configMINIMAL_STACK_SIZE, ( void * ) &xLCDQueue, mainLCD_TASK_PRIORITY, NULL );\r
208         xTaskCreate( prvLCDMessageTask, "MSG", configMINIMAL_STACK_SIZE, ( void * ) &xLCDQueue, mainMSG_TASK_PRIORITY, NULL );\r
209 \r
210         /* Start either the uIP TCP/IP stack or the lwIP TCP/IP stack. */\r
211         #ifdef STACK_UIP\r
212                 /* Finally, create the WEB server task. */\r
213                 xTaskCreate( vuIP_Task, "uIP", configMINIMAL_STACK_SIZE * 3, NULL, mainCHECK_TASK_PRIORITY - 1, NULL );\r
214         #endif\r
215 \r
216         #ifdef STACK_LWIP       \r
217                 /* Create the lwIP task.  This uses the lwIP RTOS abstraction layer.*/\r
218                 vlwIPInit();\r
219                 sys_set_state(  ( signed char * ) "httpd", lwipBASIC_SERVER_STACK_SIZE );\r
220                 sys_thread_new( vBasicWEBServer, ( void * ) NULL, basicwebWEBSERVER_PRIORITY );\r
221                 sys_set_default_state();\r
222         #endif\r
223 \r
224         /* Start the scheduler.\r
225 \r
226         NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.\r
227         The processor MUST be in supervisor mode when vTaskStartScheduler is\r
228         called.  The demo applications included in the FreeRTOS.org download switch\r
229         to supervisor mode prior to main being called.  If you are not using one of\r
230         these demo application projects then ensure Supervisor mode is used here. */\r
231 \r
232         vTaskStartScheduler();\r
233 \r
234         /* We should never get here as control is now taken by the scheduler. */\r
235         for( ;; );\r
236 }\r
237 /*-----------------------------------------------------------*/\r
238 \r
239 static void prvSetupHardware( void )\r
240 {\r
241         /* Configuration taken from the ST code.\r
242 \r
243         Set Flash banks size & address */\r
244         FMI_BankRemapConfig( 4, 2, 0, 0x80000 );\r
245 \r
246         /* FMI Waite States */\r
247         FMI_Config( FMI_READ_WAIT_STATE_2, FMI_WRITE_WAIT_STATE_0, FMI_PWD_ENABLE, FMI_LVD_ENABLE, FMI_FREQ_HIGH );\r
248 \r
249         /* Configure the FPLL = 96MHz, and APB to 48MHz. */\r
250         SCU_PCLKDivisorConfig( SCU_PCLK_Div2 );\r
251         SCU_PLLFactorsConfig( 192, 25, 2 );\r
252         SCU_PLLCmd( ENABLE );\r
253         SCU_MCLKSourceConfig( SCU_MCLK_PLL );\r
254 \r
255         WDG_Cmd( DISABLE );\r
256         VIC_DeInit();\r
257 \r
258         /* GPIO8 clock source enable, used by the LCD. */\r
259         SCU_APBPeriphClockConfig(__GPIO8, ENABLE);\r
260         GPIO_DeInit(GPIO8);\r
261 \r
262         /* GPIO 9 clock source enable, used by the LCD. */\r
263         SCU_APBPeriphClockConfig(__GPIO9, ENABLE);\r
264         GPIO_DeInit(GPIO9);\r
265 \r
266         /* Enable VIC clock */\r
267         SCU_AHBPeriphClockConfig(__VIC, ENABLE);\r
268         SCU_AHBPeriphReset(__VIC, DISABLE);\r
269 \r
270         /* Peripheral initialisation. */\r
271         vParTestInitialise();\r
272 }\r
273 /*-----------------------------------------------------------*/\r
274 \r
275 static void vErrorChecks( void *pvParameters )\r
276 {\r
277 static char cCheckVal[ mainMAX_FLAG_STRING_LEN ];\r
278 char *pcFlagString;\r
279 xLCDMessage xMessageToSend;\r
280 TickType_t xLastWakeTime;\r
281 char *pcStringsToDisplay[] = {                                                                          \r
282                                                                         "Check status flag"\r
283                                                                  };\r
284 \r
285         /* The parameters are not used in this task. */\r
286         ( void ) pvParameters;\r
287         \r
288         pcFlagString = &cCheckVal[ 0 ]; \r
289 \r
290         /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()\r
291         functions correctly. */\r
292         xLastWakeTime = xTaskGetTickCount();\r
293 \r
294         /* Cycle for ever, delaying then checking all the other tasks are still\r
295         operating without error. */\r
296         for( ;; )\r
297         {\r
298                 /* Delay until it is time to execute again. */\r
299                 vTaskDelayUntil( &xLastWakeTime, mainCHECK_PERIOD );\r
300         \r
301                 /* Check all the other tasks to see if the error flag needs updating. */\r
302                 prvCheckOtherTasksAreStillRunning();\r
303                 \r
304                 /* Create a string indicating the error flag status. */\r
305                 sprintf( cCheckVal, "equals 0x%x       ", ulErrorFlags );\r
306                 xMessageToSend.xRow = Line2;\r
307 \r
308                 /* Send the first part of the message to the LCD task. */\r
309                 xMessageToSend.ppcMessageToDisplay = &pcStringsToDisplay[ 0 ];\r
310                 xQueueSend( xLCDQueue, ( void * ) &xMessageToSend, 0 );\r
311                 vTaskDelay( mainSTRING_WRITE_DELAY );\r
312 \r
313                 /* Send the second part of the message to the LCD task. */\r
314                 xMessageToSend.ppcMessageToDisplay = &pcFlagString;\r
315                 xQueueSend( xLCDQueue, ( void * ) &xMessageToSend, 0 );\r
316         }\r
317 }\r
318 /*-----------------------------------------------------------*/\r
319 \r
320 static void prvCheckOtherTasksAreStillRunning( void )\r
321 {\r
322         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
323         {\r
324                 ulErrorFlags |= 0x01;\r
325         }\r
326 \r
327         if( xArePollingQueuesStillRunning() != pdTRUE )\r
328         {\r
329                 ulErrorFlags |=  0x02;\r
330         }\r
331 \r
332         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
333         {\r
334                 ulErrorFlags |= 0x04;\r
335         }\r
336 \r
337         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
338         {\r
339                 ulErrorFlags |= 0x08;\r
340         }\r
341 \r
342         if( xAreComTestTasksStillRunning() != pdTRUE )\r
343         {\r
344                 ulErrorFlags |= 0x10;\r
345         }\r
346 \r
347         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
348         {\r
349                 ulErrorFlags |= 0x20;\r
350         }\r
351 \r
352         if( xAreMathsTaskStillRunning() != pdTRUE )\r
353         {\r
354                 ulErrorFlags |= 0x40;\r
355         }\r
356         \r
357         if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
358         {\r
359                 ulErrorFlags |= 0x80;\r
360         }\r
361 \r
362         if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
363         {\r
364                 ulErrorFlags |= 0x100;\r
365         }\r
366         \r
367 }\r
368 /*-----------------------------------------------------------*/\r
369 \r
370 static void prvLCDMessageTask( void * pvParameters )\r
371 {\r
372 QueueHandle_t *pxLCDQueue;      \r
373 xLCDMessage xMessageToSend;\r
374 portBASE_TYPE xIndex = 0;\r
375 \r
376 /* The strings that are written to the LCD. */\r
377 char *pcStringsToDisplay[] = {                                                                          \r
378                                                                         "IAR             ",\r
379                                                                         "STR912          ",\r
380                                                                         "Demo            ",\r
381                                                                         "www.FreeRTOS.org",\r
382                                                                         ""\r
383                                                                 };\r
384 \r
385 \r
386         /* To test the parameter passing mechanism, the queue on which messages are\r
387         posted is passed in as a parameter even though it is available as a file\r
388         scope variable anyway. */\r
389         pxLCDQueue = ( QueueHandle_t * ) pvParameters;\r
390 \r
391         for( ;; )\r
392         {\r
393                 /* Wait until it is time to move onto the next string. */\r
394                 vTaskDelay( mainSTRING_WRITE_DELAY );           \r
395                 \r
396                 /* Configure the message object to send to the LCD task. */\r
397                 xMessageToSend.ppcMessageToDisplay = &pcStringsToDisplay[ xIndex ];\r
398                 xMessageToSend.xRow = Line1;\r
399                 \r
400                 /* Post the message to be displayed. */\r
401                 xQueueSend( *pxLCDQueue, ( void * ) &xMessageToSend, 0 );\r
402                 \r
403                 /* Move onto the next message, wrapping when necessary. */\r
404                 xIndex++;               \r
405                 if( *( pcStringsToDisplay[ xIndex ] ) == 0x00 )\r
406                 {\r
407                         xIndex = 0;\r
408 \r
409                         /* Delay longer before going back to the start of the messages. */\r
410                         vTaskDelay( mainSTRING_WRITE_DELAY * 2 );\r
411                 }\r
412         }\r
413 }\r
414 /*-----------------------------------------------------------*/\r
415 \r
416 void prvLCDTask( void * pvParameters )\r
417 {\r
418 QueueHandle_t *pxLCDQueue;\r
419 xLCDMessage xReceivedMessage;\r
420 char *pcString;\r
421 \r
422         /* To test the parameter passing mechanism, the queue on which messages are\r
423         received is passed in as a parameter even though it is available as a file\r
424         scope variable anyway. */\r
425         pxLCDQueue = ( QueueHandle_t * ) pvParameters;\r
426 \r
427         LCD_Init();\r
428 \r
429         for( ;; )\r
430         {\r
431                 /* Wait for a message to arrive. */\r
432                 if( xQueueReceive( *pxLCDQueue, &xReceivedMessage, portMAX_DELAY ) )\r
433                 {\r
434                         /* Where is the string we are going to display? */\r
435                         pcString = *xReceivedMessage.ppcMessageToDisplay;\r
436                         LCD_DisplayString(xReceivedMessage.xRow, pcString, BlackText);\r
437 \r
438                         /* The delay here is just to ensure the LCD task does not starve\r
439                         out lower priority tasks as writing to the LCD can take a long\r
440                         time. */\r
441                         vTaskDelay( mainLCD_DELAY );\r
442                 }\r
443         }\r
444 }\r
445 /*-----------------------------------------------------------*/\r