]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_R4F_RZ_T_GCC_IAR/src/Full_Demo/main_full.c
bd7db3c0544f6fd7296a568358ad0f076d79c68f
[freertos] / FreeRTOS / Demo / CORTEX_R4F_RZ_T_GCC_IAR / src / Full_Demo / main_full.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  * NOTE 1:  This project provides two demo applications.  A simple blinky\r
30  * style project, and a more comprehensive test and demo application.  The\r
31  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to\r
32  * select between the two.  See the notes on using\r
33  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY in main.c.  This file implements the\r
34  * comprehensive version.\r
35  *\r
36  * NOTE 2:  This file only contains the source code that is specific to the\r
37  * full demo.  Generic functions, such FreeRTOS hook functions, and functions\r
38  * required to configure the hardware, are defined in main.c.\r
39  *\r
40  ******************************************************************************\r
41  *\r
42  * main_full() creates a set of demo application tasks and software timers, then\r
43  * starts the scheduler.  The web documentation provides more details of the\r
44  * standard demo application tasks, which provide no particular functionality,\r
45  * but do provide a good example of how to use the FreeRTOS API.\r
46  *\r
47  * In addition to the standard demo tasks, the following tasks and tests are\r
48  * defined and/or created within this file:\r
49  *\r
50  * "FreeRTOS+CLI command console" -  The command console uses SCI1 for its\r
51  * input and output.  The baud rate is set to 19200.  Type "help" to see a list\r
52  * of registered commands.  The FreeRTOS+CLI license is different to the\r
53  * FreeRTOS license, see http://www.FreeRTOS.org/cli for license and usage\r
54  * details.\r
55  *\r
56  * "Reg test" tasks - These fill both the core and floating point registers with\r
57  * known values, then check that each register maintains its expected value for\r
58  * the lifetime of the task.  Each task uses a different set of values.  The reg\r
59  * test tasks execute with a very low priority, so get preempted very\r
60  * frequently.  A register containing an unexpected value is indicative of an\r
61  * error in the context switching mechanism.\r
62  *\r
63  * "Check" task - The check task period is initially set to three seconds.  The\r
64  * task checks that all the standard demo tasks are not only still executing,\r
65  * but are executing without reporting any errors.  If the check task discovers\r
66  * that a task has either stalled, or reported an error, then it changes its own\r
67  * execution period from the initial three seconds, to just 200ms.  The check\r
68  * task also toggles an LED on each iteration of its loop.  This provides a\r
69  * visual indication of the system status:  If the LED toggles every three\r
70  * seconds, then no issues have been discovered.  If the LED toggles every\r
71  * 200ms, then an issue has been discovered with at least one task.\r
72  */\r
73 \r
74 /* Standard includes. */\r
75 #include <stdio.h>\r
76 \r
77 /* Kernel includes. */\r
78 #include "FreeRTOS.h"\r
79 #include "task.h"\r
80 #include "timers.h"\r
81 #include "semphr.h"\r
82 \r
83 /* Standard demo application includes. */\r
84 #include "flop.h"\r
85 #include "semtest.h"\r
86 #include "dynamic.h"\r
87 #include "BlockQ.h"\r
88 #include "blocktim.h"\r
89 #include "countsem.h"\r
90 #include "GenQTest.h"\r
91 #include "recmutex.h"\r
92 #include "death.h"\r
93 #include "partest.h"\r
94 #include "comtest2.h"\r
95 #include "serial.h"\r
96 #include "TimerDemo.h"\r
97 #include "QueueOverwrite.h"\r
98 #include "IntQueue.h"\r
99 #include "EventGroupsDemo.h"\r
100 #include "TaskNotify.h"\r
101 #include "IntSemTest.h"\r
102 \r
103 /* Renesas includes. */\r
104 #include "r_cg_macrodriver.h"\r
105 #include "r_cg_userdefine.h"\r
106 \r
107 /* Priorities for the demo application tasks. */\r
108 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1UL )\r
109 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2UL )\r
110 #define mainCREATOR_TASK_PRIORITY                       ( tskIDLE_PRIORITY + 3UL )\r
111 #define mainFLOP_TASK_PRIORITY                          ( tskIDLE_PRIORITY )\r
112 #define mainUART_COMMAND_CONSOLE_STACK_SIZE     ( configMINIMAL_STACK_SIZE * 3UL )\r
113 #define mainCHECK_TASK_PRIORITY                         ( configMAX_PRIORITIES - 1 )\r
114 #define mainQUEUE_OVERWRITE_PRIORITY            ( tskIDLE_PRIORITY )\r
115 \r
116 /* The priority used by the UART command console task. */\r
117 #define mainUART_COMMAND_CONSOLE_TASK_PRIORITY  ( configMAX_PRIORITIES - 2 )\r
118 \r
119 /* The period of the check task, in ms, provided no errors have been reported by\r
120 any of the standard demo tasks.  ms are converted to the equivalent in ticks\r
121 using the portTICK_PERIOD_MS constant. */\r
122 #define mainNO_ERROR_CHECK_TASK_PERIOD          pdMS_TO_TICKS( 3000UL )\r
123 \r
124 /* The period of the check task, in ms, if an error has been reported in one of\r
125 the standard demo tasks.  ms are converted to the equivalent in ticks using the\r
126 portTICK_PERIOD_MS constant. */\r
127 #define mainERROR_CHECK_TASK_PERIOD             pdMS_TO_TICKS( 200UL )\r
128 \r
129 /* Parameters that are passed into the register check tasks solely for the\r
130 purpose of ensuring parameters are passed into tasks correctly. */\r
131 #define mainREG_TEST_TASK_1_PARAMETER           ( ( void * ) 0x12345678 )\r
132 #define mainREG_TEST_TASK_2_PARAMETER           ( ( void * ) 0x87654321 )\r
133 \r
134 /* The base period used by the timer test tasks. */\r
135 #define mainTIMER_TEST_PERIOD                           ( 50 )\r
136 \r
137 /*-----------------------------------------------------------*/\r
138 \r
139 /*\r
140  * Entry point for the comprehensive demo (as opposed to the simple blinky\r
141  * demo).\r
142  */\r
143 void main_full( void );\r
144 \r
145 /*\r
146  * The full demo includes some functionality called from the tick hook.\r
147  */\r
148 void vFullDemoTickHook( void );\r
149 \r
150  /*\r
151  * The check task, as described at the top of this file.\r
152  */\r
153 static void prvCheckTask( void *pvParameters );\r
154 \r
155 /*\r
156  * Register check tasks, and the tasks used to write over and check the contents\r
157  * of the FPU registers, as described at the top of this file.  The nature of\r
158  * these files necessitates that they are written in an assembly file, but the\r
159  * entry points are kept in the C file for the convenience of checking the task\r
160  * parameter.\r
161  */\r
162 static void prvRegTestTaskEntry1( void *pvParameters );\r
163 extern void vRegTest1Implementation( void );\r
164 static void prvRegTestTaskEntry2( void *pvParameters );\r
165 extern void vRegTest2Implementation( void );\r
166 \r
167 /*\r
168  * A high priority task that does nothing other than execute at a pseudo random\r
169  * time to ensure the other test tasks don't just execute in a repeating\r
170  * pattern.\r
171  */\r
172 static void prvPseudoRandomiser( void *pvParameters );\r
173 \r
174 /*\r
175  * Register commands that can be used with FreeRTOS+CLI.  The commands are\r
176  * defined in CLI-Commands.c and File-Related-CLI-Command.c respectively.\r
177  */\r
178 extern void vRegisterSampleCLICommands( void );\r
179 \r
180 /*\r
181  * The task that manages the FreeRTOS+CLI input and output.\r
182  */\r
183 extern void vUARTCommandConsoleStart( uint16_t usStackSize, UBaseType_t uxPriority );\r
184 \r
185 /*-----------------------------------------------------------*/\r
186 \r
187 /* The following two variables are used to communicate the status of the\r
188 register check tasks to the check task.  If the variables keep incrementing,\r
189 then the register check tasks have not discovered any errors.  If a variable\r
190 stops incrementing, then an error has been found. */\r
191 volatile unsigned long ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;\r
192 \r
193 /*-----------------------------------------------------------*/\r
194 \r
195 void main_full( void )\r
196 {\r
197         /* Start all the other standard demo/test tasks.  They have no particular\r
198         functionality, but do demonstrate how to use the FreeRTOS API and test the\r
199         kernel port. */\r
200         vStartInterruptQueueTasks();\r
201         vStartDynamicPriorityTasks();\r
202         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
203         vCreateBlockTimeTasks();\r
204         vStartCountingSemaphoreTasks();\r
205         vStartGenericQueueTasks( tskIDLE_PRIORITY );\r
206         vStartRecursiveMutexTasks();\r
207         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
208         vStartMathTasks( mainFLOP_TASK_PRIORITY );\r
209         vStartTimerDemoTask( mainTIMER_TEST_PERIOD );\r
210         vStartQueueOverwriteTask( mainQUEUE_OVERWRITE_PRIORITY );\r
211         vStartEventGroupTasks();\r
212         vStartTaskNotifyTask();\r
213         vStartInterruptSemaphoreTasks();\r
214 \r
215         /* Create the register check tasks, as described at the top of this     file */\r
216         xTaskCreate( prvRegTestTaskEntry1, "Reg1", configMINIMAL_STACK_SIZE, mainREG_TEST_TASK_1_PARAMETER, tskIDLE_PRIORITY, NULL );\r
217         xTaskCreate( prvRegTestTaskEntry2, "Reg2", configMINIMAL_STACK_SIZE, mainREG_TEST_TASK_2_PARAMETER, tskIDLE_PRIORITY, NULL );\r
218 \r
219         /* Create the task that just adds a little random behaviour. */\r
220         xTaskCreate( prvPseudoRandomiser, "Rnd", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL );\r
221 \r
222         /* Start the tasks that implements the command console on the UART, as\r
223         described above. */\r
224         vUARTCommandConsoleStart( mainUART_COMMAND_CONSOLE_STACK_SIZE, mainUART_COMMAND_CONSOLE_TASK_PRIORITY );\r
225 \r
226         /* Register the standard CLI commands. */\r
227         vRegisterSampleCLICommands();\r
228 \r
229         /* Create the task that performs the 'check' functionality,     as described at\r
230         the top of this file. */\r
231         xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
232 \r
233         /* The set of tasks created by the following function call have to be\r
234         created last as they keep account of the number of tasks they expect to see\r
235         running. */\r
236         vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
237 \r
238         /* Start the scheduler. */\r
239         vTaskStartScheduler();\r
240 \r
241         /* If all is well, the scheduler will now be running, and the following\r
242         line will never be reached.  If the following line does execute, then\r
243         there was either insufficient FreeRTOS heap memory available for the idle\r
244         and/or timer tasks to be created, or vTaskStartScheduler() was called from\r
245         User mode.  See the memory management section on the FreeRTOS web site for\r
246         more details on the FreeRTOS heap http://www.freertos.org/a00111.html.  The\r
247         mode from which main() is called is set in the C start up code and must be\r
248         a privileged mode (not user mode). */\r
249         for( ;; );\r
250 }\r
251 /*-----------------------------------------------------------*/\r
252 \r
253 static void prvCheckTask( void *pvParameters )\r
254 {\r
255 TickType_t xDelayPeriod = mainNO_ERROR_CHECK_TASK_PERIOD;\r
256 TickType_t xLastExecutionTime;\r
257 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;\r
258 unsigned long ulErrorFound = pdFALSE;\r
259 \r
260         /* Just to stop compiler warnings. */\r
261         ( void ) pvParameters;\r
262 \r
263         /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()\r
264         works correctly. */\r
265         xLastExecutionTime = xTaskGetTickCount();\r
266 \r
267         /* Cycle for ever, delaying then checking all the other tasks are still\r
268         operating without error.  The onboard LED is toggled on each iteration.\r
269         If an error is detected then the delay period is decreased from\r
270         mainNO_ERROR_CHECK_TASK_PERIOD to mainERROR_CHECK_TASK_PERIOD.  This has the\r
271         effect of increasing the rate at which the onboard LED toggles, and in so\r
272         doing gives visual feedback of the system status. */\r
273         for( ;; )\r
274         {\r
275                 /* Delay until it is time to execute again. */\r
276                 vTaskDelayUntil( &xLastExecutionTime, xDelayPeriod );\r
277 \r
278                 /* Check all the demo tasks (other than the flash tasks) to ensure\r
279                 that they are all still running, and that none have detected an error. */\r
280                 if( xAreIntQueueTasksStillRunning() != pdTRUE )\r
281                 {\r
282                         ulErrorFound |= 1UL << 0UL;\r
283                 }\r
284 \r
285                 if( xAreMathsTaskStillRunning() != pdTRUE )\r
286                 {\r
287                         ulErrorFound |= 1UL << 1UL;\r
288                 }\r
289 \r
290                 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
291                 {\r
292                         ulErrorFound |= 1UL << 2UL;\r
293                 }\r
294 \r
295                 if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
296                 {\r
297                         ulErrorFound |= 1UL << 3UL;\r
298                 }\r
299 \r
300                 if ( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
301                 {\r
302                         ulErrorFound |= 1UL << 4UL;\r
303                 }\r
304 \r
305                 if ( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
306                 {\r
307                         ulErrorFound |= 1UL << 5UL;\r
308                 }\r
309 \r
310                 if ( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
311                 {\r
312                         ulErrorFound |= 1UL << 6UL;\r
313                 }\r
314 \r
315                 if( xIsCreateTaskStillRunning() != pdTRUE )\r
316                 {\r
317                         ulErrorFound |= 1UL << 7UL;\r
318                 }\r
319 \r
320                 if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
321                 {\r
322                         ulErrorFound |= 1UL << 8UL;\r
323                 }\r
324 \r
325                 if( xAreTimerDemoTasksStillRunning( ( TickType_t ) mainNO_ERROR_CHECK_TASK_PERIOD ) != pdPASS )\r
326                 {\r
327                         ulErrorFound |= 1UL << 9UL;\r
328                 }\r
329 \r
330                 if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )\r
331                 {\r
332                         ulErrorFound |= 1UL << 10UL;\r
333                 }\r
334 \r
335                 if( xIsQueueOverwriteTaskStillRunning() != pdPASS )\r
336                 {\r
337                         ulErrorFound |= 1UL << 11UL;\r
338                 }\r
339 \r
340                 if( xAreEventGroupTasksStillRunning() != pdPASS )\r
341                 {\r
342                         ulErrorFound |= 1UL << 12UL;\r
343                 }\r
344 \r
345                 if( xAreTaskNotificationTasksStillRunning() != pdTRUE )\r
346                 {\r
347                         ulErrorFound |= 1UL << 13UL;\r
348                 }\r
349 \r
350                 if( xAreInterruptSemaphoreTasksStillRunning() != pdTRUE )\r
351                 {\r
352                         ulErrorFound |= 1UL << 14UL;\r
353                 }\r
354 \r
355                 /* Check that the register test 1 task is still running. */\r
356                 if( ulLastRegTest1Value == ulRegTest1LoopCounter )\r
357                 {\r
358                         ulErrorFound |= 1UL << 15UL;\r
359                 }\r
360                 ulLastRegTest1Value = ulRegTest1LoopCounter;\r
361 \r
362                 /* Check that the register test 2 task is still running. */\r
363                 if( ulLastRegTest2Value == ulRegTest2LoopCounter )\r
364                 {\r
365                         ulErrorFound |= 1UL << 16UL;\r
366                 }\r
367                 ulLastRegTest2Value = ulRegTest2LoopCounter;\r
368 \r
369                 /* Toggle the check LED to give an indication of the system status.  If\r
370                 the LED toggles every mainNO_ERROR_CHECK_TASK_PERIOD milliseconds then\r
371                 everything is ok.  A faster toggle indicates an error. */\r
372                 LED0 = !LED0;\r
373 \r
374                 if( ulErrorFound != pdFALSE )\r
375                 {\r
376                         /* An error has been detected in one of the tasks - flash the LED\r
377                         at a higher frequency to give visible feedback that something has\r
378                         gone wrong (it might just be that the loop back connector required\r
379                         by the comtest tasks has not been fitted). */\r
380                         xDelayPeriod = mainERROR_CHECK_TASK_PERIOD;\r
381                 }\r
382         }\r
383 }\r
384 /*-----------------------------------------------------------*/\r
385 \r
386 static void prvRegTestTaskEntry1( void *pvParameters )\r
387 {\r
388         /* Although the regtest task is written in assembler, its entry point is\r
389         written in C for convenience of checking the task parameter is being passed\r
390         in correctly. */\r
391         if( pvParameters == mainREG_TEST_TASK_1_PARAMETER )\r
392         {\r
393                 /* The reg test task also tests the floating point registers.  Tasks\r
394                 that use the floating point unit must call vPortTaskUsesFPU() before\r
395                 any floating point instructions are executed. */\r
396                 vPortTaskUsesFPU();\r
397 \r
398                 /* Start the part of the test that is written in assembler. */\r
399                 vRegTest1Implementation();\r
400         }\r
401 \r
402         /* The following line will only execute if the task parameter is found to\r
403         be incorrect.  The check task will detect that the regtest loop counter is\r
404         not being incremented and flag an error. */\r
405         vTaskDelete( NULL );\r
406 }\r
407 /*-----------------------------------------------------------*/\r
408 \r
409 static void prvRegTestTaskEntry2( void *pvParameters )\r
410 {\r
411         /* Although the regtest task is written in assembler, its entry point is\r
412         written in C for convenience of checking the task parameter is being passed\r
413         in correctly. */\r
414         if( pvParameters == mainREG_TEST_TASK_2_PARAMETER )\r
415         {\r
416                 /* The reg test task also tests the floating point registers.  Tasks\r
417                 that use the floating point unit must call vPortTaskUsesFPU() before\r
418                 any floating point instructions are executed. */\r
419                 vPortTaskUsesFPU();\r
420 \r
421                 /* Start the part of the test that is written in assembler. */\r
422                 vRegTest2Implementation();\r
423         }\r
424 \r
425         /* The following line will only execute if the task parameter is found to\r
426         be incorrect.  The check task will detect that the regtest loop counter is\r
427         not being incremented and flag an error. */\r
428         vTaskDelete( NULL );\r
429 }\r
430 /*-----------------------------------------------------------*/\r
431 \r
432 static void prvPseudoRandomiser( void *pvParameters )\r
433 {\r
434 const uint32_t ulMultiplier = 0x015a4e35UL, ulIncrement = 1UL, ulMinDelay = pdMS_TO_TICKS( 35 );\r
435 volatile uint32_t ulNextRand = ( uint32_t ) &pvParameters, ulValue;\r
436 \r
437         /* This task does nothing other than ensure there is a little bit of\r
438         disruption in the scheduling pattern of the other tasks.  Normally this is\r
439         done by generating interrupts at pseudo random times. */\r
440         for( ;; )\r
441         {\r
442                 ulNextRand = ( ulMultiplier * ulNextRand ) + ulIncrement;\r
443                 ulValue = ( ulNextRand >> 16UL ) & 0xffUL;\r
444 \r
445                 if( ulValue < ulMinDelay )\r
446                 {\r
447                         ulValue = ulMinDelay;\r
448                 }\r
449 \r
450                 vTaskDelay( ulValue );\r
451 \r
452                 while( ulValue > 0 )\r
453                 {\r
454                         __asm volatile( "NOP" );\r
455                         __asm volatile( "NOP" );\r
456                         __asm volatile( "NOP" );\r
457                         __asm volatile( "NOP" );\r
458 \r
459                         ulValue--;\r
460                 }\r
461         }\r
462 }\r
463 /*-----------------------------------------------------------*/\r
464 \r
465 void vFullDemoTickHook( void )\r
466 {\r
467         /* The full demo includes a software timer demo/test that requires\r
468         prodding periodically from the tick interrupt. */\r
469         vTimerPeriodicISRTests();\r
470 \r
471         /* Call the periodic queue overwrite from ISR demo. */\r
472         vQueueOverwritePeriodicISRDemo();\r
473 \r
474         /* Call the periodic event group from ISR demo. */\r
475         vPeriodicEventGroupsProcessing();\r
476 \r
477         /* Use task notifications from an interrupt. */\r
478         xNotifyTaskFromISR();\r
479 \r
480         /* Use mutexes from interrupts. */\r
481         vInterruptSemaphorePeriodicTest();\r
482 }\r
483 \r
484 \r
485 \r
486 \r