]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RX600_RX64M_RSK_GCC_e2studio/src/main_full.c
7f30f33f46d63fb5a5b3b882ec6c3be6f809dfb6
[freertos] / FreeRTOS / Demo / RX600_RX64M_RSK_GCC_e2studio / src / main_full.c
1 /*\r
2     FreeRTOS V8.2.0rc1 - Copyright (C) 2014 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
12 \r
13     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
14     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
15     >>!   obliged to provide the source code for proprietary components     !<<\r
16     >>!   outside of the FreeRTOS kernel.                                   !<<\r
17 \r
18     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
19     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
20     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
21     link: http://www.freertos.org/a00114.html\r
22 \r
23     1 tab == 4 spaces!\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    Having a problem?  Start by reading the FAQ "My application does   *\r
28      *    not run, what could be wrong?".  Have you defined configASSERT()?  *\r
29      *                                                                       *\r
30      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
31      *                                                                       *\r
32     ***************************************************************************\r
33 \r
34     ***************************************************************************\r
35      *                                                                       *\r
36      *    FreeRTOS provides completely free yet professionally developed,    *\r
37      *    robust, strictly quality controlled, supported, and cross          *\r
38      *    platform software that is more than just the market leader, it     *\r
39      *    is the industry's de facto standard.                               *\r
40      *                                                                       *\r
41      *    Help yourself get started quickly while simultaneously helping     *\r
42      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
43      *    tutorial book, reference manual, or both:                          *\r
44      *    http://www.FreeRTOS.org/Documentation                              *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     ***************************************************************************\r
49      *                                                                       *\r
50      *   Investing in training allows your team to be as productive as       *\r
51      *   possible as early as possible, lowering your overall development    *\r
52      *   cost, and enabling you to bring a more robust product to market     *\r
53      *   earlier than would otherwise be possible.  Richard Barry is both    *\r
54      *   the architect and key author of FreeRTOS, and so also the world's   *\r
55      *   leading authority on what is the world's most popular real time     *\r
56      *   kernel for deeply embedded MCU designs.  Obtaining your training    *\r
57      *   from Richard ensures your team will gain directly from his in-depth *\r
58      *   product knowledge and years of usage experience.  Contact Real Time *\r
59      *   Engineers Ltd to enquire about the FreeRTOS Masterclass, presented  *\r
60      *   by Richard Barry:  http://www.FreeRTOS.org/contact\r
61      *                                                                       *\r
62     ***************************************************************************\r
63 \r
64     ***************************************************************************\r
65      *                                                                       *\r
66      *    You are receiving this top quality software for free.  Please play *\r
67      *    fair and reciprocate by reporting any suspected issues and         *\r
68      *    participating in the community forum:                              *\r
69      *    http://www.FreeRTOS.org/support                                    *\r
70      *                                                                       *\r
71      *    Thank you!                                                         *\r
72      *                                                                       *\r
73     ***************************************************************************\r
74 \r
75     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
76     license and Real Time Engineers Ltd. contact details.\r
77 \r
78     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
79     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
80     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
81 \r
82     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
83     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
84 \r
85     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
86     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
87     licenses offer ticketed support, indemnification and commercial middleware.\r
88 \r
89     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
90     engineered and independently SIL3 certified version for use in safety and\r
91     mission critical applications that require provable dependability.\r
92 \r
93     1 tab == 4 spaces!\r
94 */\r
95 \r
96 /******************************************************************************\r
97  * NOTE 1:  This project provides two demo applications.  A simple blinky style\r
98  * project, and a more comprehensive test and demo application.  The\r
99  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select\r
100  * between the two.  See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY\r
101  * in main.c.  This file implements the comprehensive test and demo version.\r
102  *\r
103  * NOTE 2:  This file only contains the source code that is specific to the\r
104  * full demo.  Generic functions, such FreeRTOS hook functions, and functions\r
105  * required to configure the hardware, are defined in main.c.\r
106  *\r
107  ******************************************************************************\r
108  *\r
109  * main_full() creates all the demo application tasks and software timers, then\r
110  * starts the scheduler.  The web documentation provides more details of the\r
111  * standard demo application tasks, which provide no particular functionality,\r
112  * but do provide a good example of how to use the FreeRTOS API.\r
113  *\r
114  * In addition to the standard demo tasks, the following tasks and tests are\r
115  * defined and/or created within this file:\r
116  *\r
117  * "Reg test" tasks - These fill the core registers with known values, then\r
118  * check that each register maintains its expected value for the lifetime of the\r
119  * task.  Each task uses a different set of values.  The reg test tasks execute\r
120  * with a very low priority, so get preempted very frequently.  A register\r
121  * containing an unexpected value is indicative of an error in the context\r
122  * switching mechanism.\r
123  *\r
124  * "Check" task - The check task period is initially set to three seconds.  The\r
125  * task checks that all the standard demo tasks, and the register check tasks,\r
126  * are not only still executing, but are executing without reporting any errors.\r
127  * If the check task discovers that a task has either stalled, or reported an\r
128  * error, then it changes its own execution period from the initial three\r
129  * seconds, to just 200ms.  The check task also toggles an LED each time it is\r
130  * called.  This provides a visual indication of the system status:  If the LED\r
131  * toggles every three seconds, then no issues have been discovered.  If the LED\r
132  * toggles every 200ms, then an issue has been discovered with at least one\r
133  * task.\r
134  */\r
135 \r
136 /* Standard includes. */\r
137 #include <stdio.h>\r
138 \r
139 /* Kernel includes. */\r
140 #include "FreeRTOS.h"\r
141 #include "task.h"\r
142 #include "timers.h"\r
143 #include "semphr.h"\r
144 \r
145 /* Standard demo application includes. */\r
146 #include "flop.h"\r
147 #include "semtest.h"\r
148 #include "dynamic.h"\r
149 #include "BlockQ.h"\r
150 #include "blocktim.h"\r
151 #include "countsem.h"\r
152 #include "GenQTest.h"\r
153 #include "recmutex.h"\r
154 #include "death.h"\r
155 #include "partest.h"\r
156 #include "comtest2.h"\r
157 #include "serial.h"\r
158 #include "TimerDemo.h"\r
159 #include "QueueOverwrite.h"\r
160 #include "IntQueue.h"\r
161 #include "EventGroupsDemo.h"\r
162 #include "flash.h"\r
163 \r
164 /* Priorities for the demo application tasks. */\r
165 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1UL )\r
166 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2UL )\r
167 #define mainCREATOR_TASK_PRIORITY                       ( tskIDLE_PRIORITY + 3UL )\r
168 #define mainFLOP_TASK_PRIORITY                          ( tskIDLE_PRIORITY )\r
169 #define mainUART_COMMAND_CONSOLE_STACK_SIZE     ( configMINIMAL_STACK_SIZE * 3UL )\r
170 #define mainCOM_TEST_TASK_PRIORITY                      ( tskIDLE_PRIORITY + 2 )\r
171 #define mainCHECK_TASK_PRIORITY                         ( configMAX_PRIORITIES - 1 )\r
172 #define mainQUEUE_OVERWRITE_PRIORITY            ( tskIDLE_PRIORITY )\r
173 #define mainFLASH_PRIORITY                                      ( tskIDLE_PRIORITY )\r
174 \r
175 /* The priority used by the UART command console task. */\r
176 #define mainUART_COMMAND_CONSOLE_TASK_PRIORITY  ( configMAX_PRIORITIES - 2 )\r
177 \r
178 /* The LED used by the check timer. */\r
179 #define mainCHECK_LED                                           ( 3 )\r
180 \r
181 /* A block time of zero simply means "don't block". */\r
182 #define mainDONT_BLOCK                                          ( 0UL )\r
183 \r
184 /* The period after which the check timer will expire, in ms, provided no errors\r
185 have been reported by any of the standard demo tasks.  ms are converted to the\r
186 equivalent in ticks using the portTICK_PERIOD_MS constant. */\r
187 #define mainNO_ERROR_CHECK_TASK_PERIOD          ( 3000UL / portTICK_PERIOD_MS )\r
188 \r
189 /* The period at which the check timer will expire, in ms, if an error has been\r
190 reported in one of the standard demo tasks.  ms are converted to the equivalent\r
191 in ticks using the portTICK_PERIOD_MS constant. */\r
192 #define mainERROR_CHECK_TASK_PERIOD             ( 200UL / portTICK_PERIOD_MS )\r
193 \r
194 /* Parameters that are passed into the register check tasks solely for the\r
195 purpose of ensuring parameters are passed into tasks correctly. */\r
196 #define mainREG_TEST_TASK_1_PARAMETER           ( ( void * ) 0x12345678 )\r
197 #define mainREG_TEST_TASK_2_PARAMETER           ( ( void * ) 0x87654321 )\r
198 \r
199 /* The base period used by the timer test tasks. */\r
200 #define mainTIMER_TEST_PERIOD                           ( 50 )\r
201 \r
202 /*-----------------------------------------------------------*/\r
203 \r
204 /*\r
205  * Called by main() to run the full demo (as opposed to the blinky demo) when\r
206  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.\r
207  */\r
208 void main_full( void );\r
209 \r
210 /*\r
211  * The check task, as described at the top of this file.\r
212  */\r
213 static void prvCheckTask( void *pvParameters );\r
214 \r
215 /*\r
216  * Register check tasks, and the tasks used to write over and check the contents\r
217  * of the FPU registers, as described at the top of this file.  The nature of\r
218  * these files necessitates that they are written in an assembly file, but the\r
219  * entry points are kept in the C file for the convenience of checking the task\r
220  * parameter.\r
221  */\r
222 static void prvRegTestTaskEntry1( void *pvParameters );\r
223 extern void vRegTest1Implementation( void );\r
224 static void prvRegTestTaskEntry2( void *pvParameters );\r
225 extern void vRegTest2Implementation( void );\r
226 \r
227 /*\r
228  * Register commands that can be used with FreeRTOS+CLI.  The commands are\r
229  * defined in CLI-Commands.c and File-Related-CLI-Command.c respectively.\r
230  */\r
231 extern void vRegisterSampleCLICommands( void );\r
232 \r
233 /*\r
234  * The task that manages the FreeRTOS+CLI input and output.\r
235  */\r
236 extern void vUARTCommandConsoleStart( uint16_t usStackSize, UBaseType_t uxPriority );\r
237 \r
238 /*\r
239  * A high priority task that does nothing other than execute at a pseudo random\r
240  * time to ensure the other test tasks don't just execute in a repeating\r
241  * pattern.\r
242  */\r
243 static void prvPseudoRandomiser( void *pvParameters );\r
244 \r
245 /*-----------------------------------------------------------*/\r
246 \r
247 /* The following two variables are used to communicate the status of the\r
248 register check tasks to the check task.  If the variables keep incrementing,\r
249 then the register check tasks has not discovered any errors.  If a variable\r
250 stops incrementing, then an error has been found. */\r
251 volatile uint32_t ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;\r
252 \r
253 /*-----------------------------------------------------------*/\r
254 \r
255 void main_full( void )\r
256 {\r
257         /* Start all the other standard demo/test tasks.  They have not particular\r
258         functionality, but do demonstrate how to use the FreeRTOS API and test the\r
259         kernel port. */\r
260         vStartInterruptQueueTasks();\r
261         vStartDynamicPriorityTasks();\r
262         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
263         vCreateBlockTimeTasks();\r
264         vStartCountingSemaphoreTasks();\r
265         vStartGenericQueueTasks( tskIDLE_PRIORITY );\r
266         vStartRecursiveMutexTasks();\r
267         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
268         vStartMathTasks( mainFLOP_TASK_PRIORITY );\r
269         vStartTimerDemoTask( mainTIMER_TEST_PERIOD );\r
270         vStartQueueOverwriteTask( mainQUEUE_OVERWRITE_PRIORITY );\r
271         vStartEventGroupTasks();\r
272         vStartLEDFlashTasks( mainFLASH_PRIORITY );\r
273 \r
274         /* Create the register check tasks, as described at the top of this     file */\r
275         xTaskCreate( prvRegTestTaskEntry1, "Reg1", configMINIMAL_STACK_SIZE, mainREG_TEST_TASK_1_PARAMETER, tskIDLE_PRIORITY, NULL );\r
276         xTaskCreate( prvRegTestTaskEntry2, "Reg2", configMINIMAL_STACK_SIZE, mainREG_TEST_TASK_2_PARAMETER, tskIDLE_PRIORITY, NULL );\r
277 \r
278         /* Create the task that just adds a little random behaviour. */\r
279         xTaskCreate( prvPseudoRandomiser, "Rnd", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL );\r
280 \r
281         /* Create the task that performs the 'check' functionality,     as described at\r
282         the top of this file. */\r
283         xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
284 \r
285         /* The set of tasks created by the following function call have to be\r
286         created last as they keep account of the number of tasks they expect to see\r
287         running. */\r
288         vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
289 \r
290         /* Start the scheduler. */\r
291         vTaskStartScheduler();\r
292 \r
293         /* If all is well, the scheduler will now be running, and the following\r
294         line will never be reached.  If the following line does execute, then\r
295         there was either insufficient FreeRTOS heap memory available for the idle\r
296         and/or timer tasks to be created, or vTaskStartScheduler() was called from\r
297         User mode.  See the memory management section on the FreeRTOS web site for\r
298         more details on the FreeRTOS heap http://www.freertos.org/a00111.html.  The\r
299         mode from which main() is called is set in the C start up code and must be\r
300         a privileged mode (not user mode). */\r
301         for( ;; );\r
302 }\r
303 /*-----------------------------------------------------------*/\r
304 \r
305 static void prvCheckTask( void *pvParameters )\r
306 {\r
307 TickType_t xDelayPeriod = mainNO_ERROR_CHECK_TASK_PERIOD;\r
308 TickType_t xLastExecutionTime;\r
309 static uint32_t ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;\r
310 uint32_t ulErrorFound = pdFALSE;\r
311 \r
312         /* Just to stop compiler warnings. */\r
313         ( void ) pvParameters;\r
314 \r
315         /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()\r
316         works correctly. */\r
317         xLastExecutionTime = xTaskGetTickCount();\r
318 \r
319         /* Cycle for ever, delaying then checking all the other tasks are still\r
320         operating without error.  The onboard LED is toggled on each iteration.\r
321         If an error is detected then the delay period is decreased from\r
322         mainNO_ERROR_CHECK_TASK_PERIOD to mainERROR_CHECK_TASK_PERIOD.  This has the\r
323         effect of increasing the rate at which the onboard LED toggles, and in so\r
324         doing gives visual feedback of the system status. */\r
325         for( ;; )\r
326         {\r
327                 /* Delay until it is time to execute again. */\r
328                 vTaskDelayUntil( &xLastExecutionTime, xDelayPeriod );\r
329 \r
330                 /* Check all the demo tasks (other than the flash tasks) to ensure\r
331                 that they are all still running, and that none have detected an error. */\r
332                 if( xAreIntQueueTasksStillRunning() != pdTRUE )\r
333                 {\r
334                         ulErrorFound = pdTRUE;\r
335                 }\r
336 \r
337                 if( xAreMathsTaskStillRunning() != pdTRUE )\r
338                 {\r
339                         ulErrorFound = pdTRUE;\r
340                 }\r
341 \r
342                 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
343                 {\r
344                         ulErrorFound = pdTRUE;\r
345                 }\r
346 \r
347                 if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
348                 {\r
349                         ulErrorFound = pdTRUE;\r
350                 }\r
351 \r
352                 if ( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
353                 {\r
354                         ulErrorFound = pdTRUE;\r
355                 }\r
356 \r
357                 if ( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
358                 {\r
359                         ulErrorFound = pdTRUE;\r
360                 }\r
361 \r
362                 if ( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
363                 {\r
364                         ulErrorFound = pdTRUE;\r
365                 }\r
366 \r
367                 if( xIsCreateTaskStillRunning() != pdTRUE )\r
368                 {\r
369                         ulErrorFound = pdTRUE;\r
370                 }\r
371 \r
372                 if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
373                 {\r
374                         ulErrorFound = pdTRUE;\r
375                 }\r
376 \r
377                 if( xAreTimerDemoTasksStillRunning( ( TickType_t ) mainNO_ERROR_CHECK_TASK_PERIOD ) != pdPASS )\r
378                 {\r
379                         ulErrorFound = pdTRUE;\r
380                 }\r
381 \r
382                 if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )\r
383                 {\r
384                         ulErrorFound = pdTRUE;\r
385                 }\r
386 \r
387                 if( xIsQueueOverwriteTaskStillRunning() != pdPASS )\r
388                 {\r
389                         ulErrorFound = pdTRUE;\r
390                 }\r
391 \r
392                 if( xAreEventGroupTasksStillRunning() != pdPASS )\r
393                 {\r
394                         ulErrorFound = pdTRUE;\r
395                 }\r
396 \r
397                 /* Check that the register test 1 task is still running. */\r
398                 if( ulLastRegTest1Value == ulRegTest1LoopCounter )\r
399                 {\r
400                         ulErrorFound = pdTRUE;\r
401                 }\r
402                 ulLastRegTest1Value = ulRegTest1LoopCounter;\r
403 \r
404                 /* Check that the register test 2 task is still running. */\r
405                 if( ulLastRegTest2Value == ulRegTest2LoopCounter )\r
406                 {\r
407                         ulErrorFound = pdTRUE;\r
408                 }\r
409                 ulLastRegTest2Value = ulRegTest2LoopCounter;\r
410 \r
411                 /* Toggle the check LED to give an indication of the system status.  If\r
412                 the LED toggles every mainNO_ERROR_CHECK_TASK_PERIOD milliseconds then\r
413                 everything is ok.  A faster toggle indicates an error. */\r
414                 vParTestToggleLED( mainCHECK_LED );\r
415 \r
416                 if( ulErrorFound != pdFALSE )\r
417                 {\r
418                         /* An error has been detected in one of the tasks - flash the LED\r
419                         at a higher frequency to give visible feedback that something has\r
420                         gone wrong (it might just be that the loop back connector required\r
421                         by the comtest tasks has not been fitted). */\r
422                         xDelayPeriod = mainERROR_CHECK_TASK_PERIOD;\r
423                 }\r
424         }\r
425 }\r
426 /*-----------------------------------------------------------*/\r
427 \r
428 static void prvRegTestTaskEntry1( void *pvParameters )\r
429 {\r
430         /* Although the regtest task is written in assembler, its entry point is\r
431         written in C for convenience of checking the task parameter is being passed\r
432         in correctly. */\r
433         if( pvParameters == mainREG_TEST_TASK_1_PARAMETER )\r
434         {\r
435                 /* Start the part of the test that is written in assembler. */\r
436                 vRegTest1Implementation();\r
437         }\r
438 \r
439         /* The following line will only execute if the task parameter is found to\r
440         be incorrect.  The check timer will detect that the regtest loop counter is\r
441         not being incremented and flag an error. */\r
442         vTaskDelete( NULL );\r
443 }\r
444 /*-----------------------------------------------------------*/\r
445 \r
446 static void prvRegTestTaskEntry2( void *pvParameters )\r
447 {\r
448         /* Although the regtest task is written in assembler, its entry point is\r
449         written in C for convenience of checking the task parameter is being passed\r
450         in correctly. */\r
451         if( pvParameters == mainREG_TEST_TASK_2_PARAMETER )\r
452         {\r
453                 /* Start the part of the test that is written in assembler. */\r
454                 vRegTest2Implementation();\r
455         }\r
456 \r
457         /* The following line will only execute if the task parameter is found to\r
458         be incorrect.  The check timer will detect that the regtest loop counter is\r
459         not being incremented and flag an error. */\r
460         vTaskDelete( NULL );\r
461 }\r
462 /*-----------------------------------------------------------*/\r
463 \r
464 static void prvPseudoRandomiser( void *pvParameters )\r
465 {\r
466 const uint32_t ulMultiplier = 0x015a4e35UL, ulIncrement = 1UL, ulMinDelay = ( 35 / portTICK_PERIOD_MS );\r
467 volatile uint32_t ulNextRand = ( uint32_t ) &pvParameters, ulValue;\r
468 \r
469         /* This task does nothing other than ensure there is a little bit of\r
470         disruption in the scheduling pattern of the other tasks.  Normally this is\r
471         done by generating interrupts at pseudo random times. */\r
472         for( ;; )\r
473         {\r
474                 ulNextRand = ( ulMultiplier * ulNextRand ) + ulIncrement;\r
475                 ulValue = ( ulNextRand >> 16UL ) & 0xffUL;\r
476 \r
477                 if( ulValue < ulMinDelay )\r
478                 {\r
479                         ulValue = ulMinDelay;\r
480                 }\r
481 \r
482                 vTaskDelay( ulValue );\r
483 \r
484                 while( ulValue > 0 )\r
485                 {\r
486                         __asm volatile( "NOP" );\r
487                         __asm volatile( "NOP" );\r
488                         __asm volatile( "NOP" );\r
489 \r
490                         ulValue--;\r
491                 }\r
492         }\r
493 }\r
494 \r
495 \r
496 \r
497 \r
498 \r
499 \r