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