]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/H8S/RTOSDemo/main.c
a77e31999eab9c160e6531f24b4dbde62525c995
[freertos] / FreeRTOS / Demo / H8S / RTOSDemo / 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  * 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  * Main.c also creates a task called "Check".  This only executes every three\r
33  * seconds but has the highest priority so is guaranteed to get processor time.\r
34  * Its main function is to check that all the other tasks are still operational.\r
35  * Each task (other than the "flash" tasks) maintains a unique count that is\r
36  * incremented each time the task successfully completes its function.  Should\r
37  * any error occur within such a task the count is permanently halted.  The\r
38  * check task inspects the count of each task to ensure it has changed since\r
39  * the last time the check task executed.  If all the count variables have\r
40  * changed all the tasks are still executing error free, and the check task\r
41  * toggles the onboard LED.  Should any task contain an error at any time\r
42  * the LED toggle rate will change from 3 seconds to 500ms.\r
43  *\r
44  * To check the operation of the memory allocator the check task also\r
45  * dynamically creates a task before delaying, and deletes it again when it\r
46  * wakes.  If memory cannot be allocated for the new task the call to xTaskCreate\r
47  * will fail and an error is signalled.  The dynamically created task itself\r
48  * allocates and frees memory just to give the allocator a bit more exercise.\r
49  *\r
50  */\r
51 \r
52 /* Standard includes. */\r
53 #include <stdlib.h>\r
54 #include <string.h>\r
55 \r
56 /* Scheduler include files. */\r
57 #include "FreeRTOS.h"\r
58 #include "task.h"\r
59 \r
60 /* Demo application file headers. */\r
61 #include "flash.h"\r
62 #include "integer.h"\r
63 #include "PollQ.h"\r
64 #include "comtest2.h"\r
65 #include "semtest.h"\r
66 #include "flop.h"\r
67 #include "dynamic.h"\r
68 #include "BlockQ.h"\r
69 #include "serial.h"\r
70 #include "partest.h"\r
71 \r
72 /* Priority definitions for most of the tasks in the demo application.  Some\r
73 tasks just use the idle priority. */\r
74 #define mainLED_TASK_PRIORITY                   ( tskIDLE_PRIORITY + 1 )\r
75 #define mainCOM_TEST_PRIORITY                   ( tskIDLE_PRIORITY + 2 )\r
76 #define mainQUEUE_POLL_PRIORITY                 ( tskIDLE_PRIORITY + 2 )\r
77 #define mainCHECK_TASK_PRIORITY                 ( tskIDLE_PRIORITY + 3 )\r
78 #define mainSEM_TEST_PRIORITY                   ( tskIDLE_PRIORITY + 1 )\r
79 #define mainBLOCK_Q_PRIORITY                    ( tskIDLE_PRIORITY + 2 )\r
80 \r
81 /* Baud rate used by the serial port tasks (ComTest tasks). */\r
82 #define mainCOM_TEST_BAUD_RATE                  ( ( unsigned long ) 115200 )\r
83 \r
84 /* LED used by the serial port tasks.  This is toggled on each character Tx,\r
85 and mainCOM_TEST_LED + 1 is toggles on each character Rx. */\r
86 #define mainCOM_TEST_LED                                ( 3 )\r
87 \r
88 /* LED that is toggled by the check task.  The check task periodically checks\r
89 that all the other tasks are operating without error.  If no errors are found\r
90 the LED is toggled with mainCHECK_PERIOD frequency.  If an error is found\r
91 the the toggle rate increases to mainERROR_CHECK_PERIOD. */\r
92 #define mainCHECK_TASK_LED                              ( 5 )\r
93 #define mainCHECK_PERIOD                                ( ( TickType_t ) 3000 / portTICK_PERIOD_MS  )\r
94 #define mainERROR_CHECK_PERIOD                  ( ( TickType_t ) 500 / portTICK_PERIOD_MS )\r
95 \r
96 /* Constants used by the vMemCheckTask() task. */\r
97 #define mainCOUNT_INITIAL_VALUE         ( ( unsigned long ) 0 )\r
98 #define mainNO_TASK                                     ( 0 )\r
99 \r
100 /* The size of the memory blocks allocated by the vMemCheckTask() task. */\r
101 #define mainMEM_CHECK_SIZE_1            ( ( size_t ) 51 )\r
102 #define mainMEM_CHECK_SIZE_2            ( ( size_t ) 52 )\r
103 #define mainMEM_CHECK_SIZE_3            ( ( size_t ) 151 )\r
104 \r
105 /*\r
106  * The 'Check' task.\r
107  */\r
108 static void vErrorChecks( void *pvParameters );\r
109 \r
110 /*\r
111  * Checks the unique counts of other tasks to ensure they are still operational.\r
112  */\r
113 static long prvCheckOtherTasksAreStillRunning( unsigned long ulMemCheckTaskCount );\r
114 \r
115 /*\r
116  * Dynamically created and deleted during each cycle of the vErrorChecks()\r
117  * task.  This is done to check the operation of the memory allocator.\r
118  * See the top of vErrorChecks for more details.\r
119  */\r
120 static void vMemCheckTask( void *pvParameters );\r
121 \r
122 /*-----------------------------------------------------------*/\r
123 \r
124 /*\r
125  * Start all the tasks then start the scheduler.\r
126  */\r
127 int main( void )\r
128 {\r
129         /* Setup the LED's for output. */\r
130         vParTestInitialise();\r
131 \r
132         /* Start the various standard demo application tasks. */\r
133         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
134         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
135         vStartLEDFlashTasks( mainLED_TASK_PRIORITY );\r
136         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
137         vStartMathTasks( tskIDLE_PRIORITY );\r
138         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
139         vStartDynamicPriorityTasks();\r
140         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
141 \r
142         /* Start the 'Check' task. */\r
143         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
144 \r
145         /* In this port, to use preemptive scheduler define configUSE_PREEMPTION\r
146         as 1 in portmacro.h.  To use the cooperative scheduler define\r
147         configUSE_PREEMPTION as 0. */\r
148         vTaskStartScheduler();\r
149 \r
150         /* Should never get here! */\r
151         return 0;\r
152 }\r
153 /*-----------------------------------------------------------*/\r
154 \r
155 /*\r
156  * Cycle for ever, delaying then checking all the other tasks are still\r
157  * operating without error.  If an error is detected then the delay period\r
158  * is decreased from mainCHECK_PERIOD to mainERROR_CHECK_PERIOD so\r
159  * the on board LED flash rate will increase.\r
160  *\r
161  * In addition to the standard tests the memory allocator is tested through\r
162  * the dynamic creation and deletion of a task each cycle.  Each time the\r
163  * task is created memory must be allocated for its stack.  When the task is\r
164  * deleted this memory is returned to the heap.  If the task cannot be created\r
165  * then it is likely that the memory allocation failed.   In addition the\r
166  * dynamically created task allocates and frees memory while it runs.\r
167  */\r
168 static void vErrorChecks( void *pvParameters )\r
169 {\r
170 TickType_t xDelayPeriod = mainCHECK_PERIOD;\r
171 volatile unsigned long ulMemCheckTaskRunningCount;\r
172 TaskHandle_t xCreatedTask;\r
173 TickType_t xLastWakeTime;\r
174 \r
175         /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()\r
176         functions correctly. */\r
177         xLastWakeTime = xTaskGetTickCount();\r
178 \r
179         for( ;; )\r
180         {\r
181                 /* Set ulMemCheckTaskRunningCount to a known value so we can check\r
182                 later that it has changed. */\r
183                 ulMemCheckTaskRunningCount = mainCOUNT_INITIAL_VALUE;\r
184 \r
185                 /* Dynamically create a task - passing ulMemCheckTaskRunningCount as a\r
186                 parameter. */\r
187                 xCreatedTask = mainNO_TASK;\r
188                 if( xTaskCreate( vMemCheckTask, "MEM_CHECK", configMINIMAL_STACK_SIZE, ( void * ) &ulMemCheckTaskRunningCount, tskIDLE_PRIORITY, &xCreatedTask ) != pdPASS )\r
189                 {\r
190                         /* Could not create the task - we have probably run out of heap. */\r
191                         xDelayPeriod = mainERROR_CHECK_PERIOD;\r
192                 }\r
193 \r
194 \r
195                 /* Delay until it is time to execute again.  The delay period is\r
196                 shorter following an error. */\r
197                 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );\r
198 \r
199 \r
200                 /* Delete the dynamically created task. */\r
201                 if( xCreatedTask != mainNO_TASK )\r
202                 {\r
203                         vTaskDelete( xCreatedTask );\r
204                 }\r
205 \r
206                 /* Check all the standard demo application tasks are executing without\r
207                 error.  ulMemCheckTaskRunningCount is checked to ensure it was\r
208                 modified by the task just deleted. */\r
209                 if( prvCheckOtherTasksAreStillRunning( ulMemCheckTaskRunningCount ) != pdPASS )\r
210                 {\r
211                         /* An error has been detected in one of the tasks - flash faster. */\r
212                         xDelayPeriod = mainERROR_CHECK_PERIOD;\r
213                 }\r
214 \r
215                 vParTestToggleLED( mainCHECK_TASK_LED );\r
216         }\r
217 }\r
218 /*-----------------------------------------------------------*/\r
219 \r
220 /*\r
221  *      Check each set of tasks in turn to see if they have experienced any\r
222  *      error conditions.\r
223  */\r
224 static long prvCheckOtherTasksAreStillRunning( unsigned long ulMemCheckTaskCount )\r
225 {\r
226 long lNoErrorsDiscovered = ( long ) pdTRUE;\r
227 \r
228         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
229         {\r
230                 lNoErrorsDiscovered = pdFALSE;\r
231         }\r
232 \r
233         if( xAreComTestTasksStillRunning() != pdTRUE )\r
234         {\r
235                 lNoErrorsDiscovered = pdFALSE;\r
236         }\r
237 \r
238         if( xArePollingQueuesStillRunning() != pdTRUE )\r
239         {\r
240                 lNoErrorsDiscovered = pdFALSE;\r
241         }\r
242 \r
243         if( xAreMathsTaskStillRunning() != pdTRUE )\r
244         {\r
245                 lNoErrorsDiscovered = pdFALSE;\r
246         }\r
247 \r
248         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
249         {\r
250                 lNoErrorsDiscovered = pdFALSE;\r
251         }\r
252 \r
253         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
254         {\r
255                 lNoErrorsDiscovered = pdFALSE;\r
256         }\r
257 \r
258         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
259         {\r
260                 lNoErrorsDiscovered = pdFALSE;\r
261         }\r
262 \r
263         if( ulMemCheckTaskCount == mainCOUNT_INITIAL_VALUE )\r
264         {\r
265                 /* The vMemCheckTask task did not increment the counter - it must\r
266                 have failed. */\r
267                 lNoErrorsDiscovered = pdFALSE;\r
268         }\r
269 \r
270         return lNoErrorsDiscovered;\r
271 }\r
272 /*-----------------------------------------------------------*/\r
273 \r
274 static void vMemCheckTask( void *pvParameters )\r
275 {\r
276 unsigned long *pulMemCheckTaskRunningCounter;\r
277 void *pvMem1, *pvMem2, *pvMem3;\r
278 static long lErrorOccurred = pdFALSE;\r
279 \r
280         /* This task is dynamically created then deleted during each cycle of the\r
281         vErrorChecks task to check the operation of the memory allocator.  Each time\r
282         the task is created memory is allocated for the stack and TCB.  Each time\r
283         the task is deleted this memory is returned to the heap.  This task itself\r
284         exercises the allocator by allocating and freeing blocks.\r
285 \r
286         The task executes at the idle priority so does not require a delay.\r
287 \r
288         pulMemCheckTaskRunningCounter is incremented each cycle to indicate to the\r
289         vErrorChecks() task that this task is still executing without error. */\r
290 \r
291         pulMemCheckTaskRunningCounter = ( unsigned long * ) pvParameters;\r
292 \r
293         for( ;; )\r
294         {\r
295                 if( lErrorOccurred == pdFALSE )\r
296                 {\r
297                         /* We have never seen an error so increment the counter. */\r
298                         ( *pulMemCheckTaskRunningCounter )++;\r
299                 }\r
300                 else\r
301                 {\r
302                         /* Reset the count so an error is detected by the\r
303                         prvCheckOtherTasksAreStillRunning() function. */\r
304                         *pulMemCheckTaskRunningCounter = mainCOUNT_INITIAL_VALUE;\r
305                 }\r
306 \r
307                 /* Allocate some memory - just to give the allocator some extra\r
308                 exercise.  This has to be in a critical section to ensure the\r
309                 task does not get deleted while it has memory allocated. */\r
310                 vTaskSuspendAll();\r
311                 {\r
312                         pvMem1 = pvPortMalloc( mainMEM_CHECK_SIZE_1 );\r
313                         if( pvMem1 == NULL )\r
314                         {\r
315                                 lErrorOccurred = pdTRUE;\r
316                         }\r
317                         else\r
318                         {\r
319                                 memset( pvMem1, 0xaa, mainMEM_CHECK_SIZE_1 );\r
320                                 vPortFree( pvMem1 );\r
321                         }\r
322                 }\r
323                 xTaskResumeAll();\r
324 \r
325                 /* Again - with a different size block. */\r
326                 vTaskSuspendAll();\r
327                 {\r
328                         pvMem2 = pvPortMalloc( mainMEM_CHECK_SIZE_2 );\r
329                         if( pvMem2 == NULL )\r
330                         {\r
331                                 lErrorOccurred = pdTRUE;\r
332                         }\r
333                         else\r
334                         {\r
335                                 memset( pvMem2, 0xaa, mainMEM_CHECK_SIZE_2 );\r
336                                 vPortFree( pvMem2 );\r
337                         }\r
338                 }\r
339                 xTaskResumeAll();\r
340 \r
341                 /* Again - with a different size block. */\r
342                 vTaskSuspendAll();\r
343                 {\r
344                         pvMem3 = pvPortMalloc( mainMEM_CHECK_SIZE_3 );\r
345                         if( pvMem3 == NULL )\r
346                         {\r
347                                 lErrorOccurred = pdTRUE;\r
348                         }\r
349                         else\r
350                         {\r
351                                 memset( pvMem3, 0xaa, mainMEM_CHECK_SIZE_3 );\r
352                                 vPortFree( pvMem3 );\r
353                         }\r
354                 }\r
355                 xTaskResumeAll();\r
356         }\r
357 }\r
358 /*-----------------------------------------------------------*/\r
359 \r
360 /*\r
361  * Called by the startup code.  Initial processor setup can be placed in this\r
362  * function.\r
363  */\r
364 void hw_initialise (void)\r
365 {\r
366 }\r
367 \r