]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/MB96340_Softune/FreeRTOS_96348hs_SK16FX100PMC/Src/main.c
f1363e052a84c44cd76dc32f1041c0703c157608
[freertos] / FreeRTOS / Demo / MB96340_Softune / FreeRTOS_96348hs_SK16FX100PMC / Src / main.c
1 /*\r
2  * FreeRTOS Kernel V10.2.1\r
3  * Copyright (C) 2019 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  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
30  * documentation provides more details of the demo application tasks.\r
31  *\r
32  * In addition to the standard demo tasks, the follow demo specific tasks are\r
33  * create:\r
34  *\r
35  * The "Check" task.  This only executes every three seconds but has the highest\r
36  * priority so is guaranteed to get processor time.  Its main function is to\r
37  * check that all the other tasks are still operational.  Most tasks maintain\r
38  * a unique count that is incremented each time the task successfully completes\r
39  * its function.  Should any error occur within such a task the count is\r
40  * permanently halted.  The check task inspects the count of each task to ensure\r
41  * it has changed since the last time the check task executed.  If all the count\r
42  * variables have changed all the tasks are still executing error free, and the\r
43  * check task toggles the onboard LED.  Should any task contain an error at any time\r
44  * the LED toggle rate will change from 3 seconds to 500ms.\r
45  *\r
46  * The "Trace Utility" task.  This can be used to obtain trace and debug\r
47  * information via UART1.\r
48  */\r
49 \r
50 \r
51 /* Scheduler includes. */\r
52 #include "FreeRTOS.h"\r
53 #include "task.h"\r
54 #include "semphr.h"\r
55 \r
56 /* Demo application includes. */\r
57 #include "flash.h"\r
58 #include "integer.h"\r
59 #include "comtest2.h"\r
60 #include "PollQ.h"\r
61 #include "semtest.h"\r
62 #include "BlockQ.h"\r
63 #include "dynamic.h"\r
64 #include "flop.h"\r
65 #include "GenQTest.h"\r
66 #include "QPeek.h"\r
67 #include "blocktim.h"\r
68 #include "death.h"\r
69 #include "taskutility.h"\r
70 #include "partest.h"\r
71 #include "crflash.h"\r
72 #include "watchdog.h"\r
73 \r
74 /* Library includes. */\r
75 #include <watchdog.h>\r
76 \r
77 /*-----------------------------------------------------------*/\r
78 \r
79 /* Demo task priorities. */\r
80 #define WTC_TASK_PRIORITY                       ( tskIDLE_PRIORITY + 5 )\r
81 #define mainCHECK_TASK_PRIORITY         ( tskIDLE_PRIORITY + 4 )\r
82 #define TASK_UTILITY_PRIORITY           ( tskIDLE_PRIORITY )\r
83 #define mainSEM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 3 )\r
84 #define mainCOM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 2 )\r
85 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
86 #define mainQUEUE_BLOCK_PRIORITY        ( tskIDLE_PRIORITY + 2 )\r
87 #define mainDEATH_PRIORITY                      ( tskIDLE_PRIORITY + 1 )\r
88 #define mainLED_TASK_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
89 #define mainGENERIC_QUEUE_PRIORITY      ( tskIDLE_PRIORITY )\r
90 \r
91 /* Baud rate used by the COM test tasks. */\r
92 #define mainCOM_TEST_BAUD_RATE  ( ( unsigned long ) 19200 )\r
93 \r
94 /* The frequency at which the 'Check' tasks executes.  See the comments at the\r
95 top of the page.  When the system is operating error free the 'Check' task\r
96 toggles an LED every three seconds.  If an error is discovered in any task the\r
97 rate is increased to 500 milliseconds.  [in this case the '*' characters on the\r
98 LCD represent LED's] */\r
99 #define mainNO_ERROR_CHECK_DELAY        ( (TickType_t) 3000 / portTICK_PERIOD_MS )\r
100 #define mainERROR_CHECK_DELAY           ( (TickType_t) 500 / portTICK_PERIOD_MS )\r
101 \r
102 /* LED assignments for the demo tasks. */\r
103 #define mainNUM_FLASH_CO_ROUTINES       8\r
104 #define mainCOM_TEST_LED                        0x05\r
105 #define mainCHECK_TEST_LED                      0x07\r
106 \r
107 /*-----------------------------------------------------------*/\r
108 \r
109 /*\r
110  * The function that implements the Check task.  See the comments at the head\r
111  * of the page for implementation details.\r
112  */\r
113 static void     vErrorChecks( void *pvParameters );\r
114 \r
115 /*\r
116  * Called by the Check task.  Returns pdPASS if all the other tasks are found\r
117  * to be operating without error - otherwise returns pdFAIL.\r
118  */\r
119 static short prvCheckOtherTasksAreStillRunning( void );\r
120 \r
121 /*\r
122  * Perform any hardware setup necessary for the demo.\r
123  */\r
124 static void prvSetupHardware( void );\r
125 \r
126 /*-----------------------------------------------------------*/\r
127 \r
128 void main( void )\r
129 {\r
130         InitIrqLevels();                /* Initialize interrupts */\r
131         __set_il( 7 );                  /* Allow all levels      */\r
132 \r
133         prvSetupHardware();\r
134 \r
135         #if WATCHDOG == WTC_IN_TASK\r
136                 vStartWatchdogTask( WTC_TASK_PRIORITY );\r
137         #endif\r
138 \r
139         /* Start the standard demo application tasks. */\r
140         vStartLEDFlashTasks( mainLED_TASK_PRIORITY );\r
141         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
142         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
143         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
144         vStartBlockingQueueTasks( mainQUEUE_BLOCK_PRIORITY );\r
145         vStartDynamicPriorityTasks();\r
146         vStartFlashCoRoutines( mainNUM_FLASH_CO_ROUTINES );\r
147         vStartGenericQueueTasks( mainGENERIC_QUEUE_PRIORITY );\r
148         vCreateBlockTimeTasks();\r
149 \r
150         /* The definition INCLUDE_TraceListTasks is set within FreeRTOSConfig.h. */\r
151         #if INCLUDE_TraceListTasks == 1\r
152                 vUtilityStartTraceTask( TASK_UTILITY_PRIORITY );\r
153         #else\r
154                 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED - 1 );\r
155         #endif\r
156 \r
157         /* Start the 'Check' task which is defined in this file. */\r
158         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
159 \r
160         /* The suicide tasks must be started last as they record the number of other\r
161         tasks that exist within the system.  The value is then used to ensure at run\r
162         time the number of tasks that exists is within expected bounds. */\r
163         vCreateSuicidalTasks( mainDEATH_PRIORITY );\r
164 \r
165         /* Now start the scheduler.  Following this call the created tasks should\r
166         be executing. */\r
167         vTaskStartScheduler( );\r
168 \r
169         /* vTaskStartScheduler() will only return if an error occurs while the\r
170         idle task is being created. */\r
171         for( ;; );\r
172 }\r
173 /*-----------------------------------------------------------*/\r
174 \r
175 static void prvSetupHardware( void )\r
176 {\r
177         /* Initialise the port used by the LEDs. */\r
178         vParTestInitialise();\r
179 \r
180         /* See watchdog.h for definitions relating to the watchdog use. */\r
181         #if WATCHDOG != WTC_NONE\r
182                 InitWatchdog();\r
183         #endif\r
184 }\r
185 /*-----------------------------------------------------------*/\r
186 \r
187 static void vErrorChecks( void *pvParameters )\r
188 {\r
189 TickType_t xDelayPeriod = mainNO_ERROR_CHECK_DELAY;\r
190 \r
191         /* Just to remove compiler warnings. */\r
192         ( void ) pvParameters;\r
193 \r
194         /* Cycle for ever, delaying then checking all the other tasks are still\r
195         operating without error. */\r
196         for( ;; )\r
197         {\r
198                 /* Wait until it is time to check again.  The time we wait here depends\r
199                 on whether an error has been detected or not.  When an error is\r
200                 detected the time is shortened resulting in a faster LED flash rate. */\r
201                 vTaskDelay( xDelayPeriod );\r
202 \r
203                 /* See if the other tasks are all ok. */\r
204                 if( prvCheckOtherTasksAreStillRunning() != pdPASS )\r
205                 {\r
206                         /* An error occurred in one of the tasks so shorten the delay\r
207                         period - which has the effect of increasing the frequency of the\r
208                         LED toggle. */\r
209                         xDelayPeriod = mainERROR_CHECK_DELAY;\r
210                 }\r
211 \r
212                 /* Flash! */\r
213                 vParTestToggleLED( mainCHECK_TEST_LED );\r
214         }\r
215 }\r
216 /*-----------------------------------------------------------*/\r
217 \r
218 static short prvCheckOtherTasksAreStillRunning( void )\r
219 {\r
220         static short    sNoErrorFound = pdTRUE;\r
221 \r
222         /* The demo tasks maintain a count that increments every cycle of the task\r
223         provided that the task has never encountered an error.  This function\r
224         checks the counts maintained by the tasks to ensure they are still being\r
225         incremented.  A count remaining at the same value between calls therefore\r
226         indicates that an error has been detected.  Only tasks that do not flash\r
227         an LED are checked. */\r
228         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
229         {\r
230                 sNoErrorFound = pdFALSE;\r
231         }\r
232 \r
233         if( xArePollingQueuesStillRunning() != pdTRUE )\r
234         {\r
235                 sNoErrorFound = pdFALSE;\r
236         }\r
237 \r
238         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
239         {\r
240                 sNoErrorFound = pdFALSE;\r
241         }\r
242 \r
243         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
244         {\r
245                 sNoErrorFound = pdFALSE;\r
246         }\r
247 \r
248         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
249         {\r
250                 sNoErrorFound = pdFALSE;\r
251         }\r
252 \r
253         if( xAreFlashCoRoutinesStillRunning() != pdTRUE )\r
254         {\r
255                 sNoErrorFound = pdFALSE;\r
256         }\r
257 \r
258         if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
259         {\r
260                 sNoErrorFound = pdFALSE;\r
261         }\r
262 \r
263         if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
264         {\r
265                 sNoErrorFound = pdFALSE;\r
266         }\r
267 \r
268         if( xIsCreateTaskStillRunning() != pdTRUE )\r
269         {\r
270                 sNoErrorFound = pdFALSE;\r
271         }\r
272 \r
273         #if INCLUDE_TraceListTasks == 0\r
274         {\r
275                 if( xAreComTestTasksStillRunning() != pdTRUE )\r
276                 {\r
277                         sNoErrorFound = pdFALSE;\r
278                 }\r
279         }\r
280         #endif\r
281 \r
282         return sNoErrorFound;\r
283 }\r
284 /*-----------------------------------------------------------*/\r
285 \r
286 /* Idle hook function. */\r
287 #if configUSE_IDLE_HOOK == 1\r
288         void vApplicationIdleHook( void )\r
289         {\r
290                 /* Are we using the idle task to kick the watchdog?  See watchdog.h\r
291                 for watchdog kicking options. Note this is for demonstration only\r
292                 and is not a suggested method of servicing the watchdog in a real\r
293                 application. */\r
294                 #if WATCHDOG == WTC_IN_IDLE\r
295                         Kick_Watchdog();\r
296                 #endif\r
297 \r
298                 vCoRoutineSchedule();\r
299         }\r
300 #else\r
301         #if WATCHDOG == WTC_IN_IDLE\r
302                 #error configUSE_IDLE_HOOK must be set to 1 in FreeRTOSConfig.h if the watchdog is being cleared in the idle task hook.\r
303         #endif\r
304 #endif\r
305 \r
306 /*-----------------------------------------------------------*/\r
307 \r
308 /* Tick hook function. */\r
309 #if configUSE_TICK_HOOK == 1\r
310         void vApplicationTickHook( void )\r
311         {\r
312                 /* Are we using the tick to kick the watchdog?  See watchdog.h\r
313                 for watchdog kicking options.  Note this is for demonstration\r
314                 only and is not a suggested method of servicing the watchdog in\r
315                 a real application. */\r
316                 #if WATCHDOG == WTC_IN_TICK\r
317                         Kick_Watchdog();\r
318                 #endif\r
319         }\r
320 #else\r
321         #if WATCHDOG == WTC_IN_TICK\r
322                 #error configUSE_TICK_HOOK must be set to 1 in FreeRTOSConfig.h if the watchdog is being cleared in the tick hook.\r
323         #endif\r
324 #endif\r
325 /*-----------------------------------------------------------*/\r