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