]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4F_MSP432_LaunchPad_IAR_CCS_Keil/Full_Demo/main_full.c
269628439b05a573b9f36435b716eed18f83eb44
[freertos] / FreeRTOS / Demo / CORTEX_M4F_MSP432_LaunchPad_IAR_CCS_Keil / Full_Demo / main_full.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 /******************************************************************************\r
30  * NOTE 1:  This project provides two demo applications.  A simple blinky\r
31  * style project, and a more comprehensive test and demo application.  The\r
32  * configCREATE_SIMPLE_TICKLESS_DEMO setting in FreeRTOSConfig.h is used to\r
33  * select between the two.  See the notes on using\r
34  * configCREATE_SIMPLY_BLINKY_DEMO_ONLY in main.c.  This file implements the\r
35  * comprehensive version.\r
36  *\r
37  * NOTE 2:  This file only contains the source code that is specific to the\r
38  * full demo.  Generic functions, such FreeRTOS hook functions, and functions\r
39  * required to configure the hardware, are defined in main.c.\r
40  *\r
41  * See http://www.FreeRTOS.org/TI_MSP432_Free_RTOS_Demo.html for instructions.\r
42  *\r
43  ******************************************************************************\r
44  *\r
45  * main_full() creates all the demo application tasks and software timers, then\r
46  * starts the scheduler.  The web documentation provides more details of the\r
47  * standard demo application tasks, which provide no particular functionality,\r
48  * but do provide a good example of how to use the FreeRTOS API.\r
49  *\r
50  * In addition to the standard demo tasks, the following tasks and tests are\r
51  * defined and/or created within this file:\r
52  *\r
53  * FreeRTOS+CLI command console.  The command console is access through the\r
54  * UART at 19200 baud.  For reasons of robustness testing the UART driver is\r
55  * deliberately written to be inefficient and should not be used as a template\r
56  * for a production driver.  Type "help" to see a list of registered commands.\r
57  * The FreeRTOS+CLI license is different to the FreeRTOS license, see\r
58  * http://www.FreeRTOS.org/cli for license and usage details.\r
59  *\r
60  * "Reg test" tasks - These fill both the core and floating point registers with\r
61  * known values, then check that each register maintains its expected value for\r
62  * the lifetime of the task.  Each task uses a different set of values.  The reg\r
63  * test tasks execute with a very low priority, so get preempted very\r
64  * frequently.  A register containing an unexpected value is indicative of an\r
65  * error in the context switching mechanism.\r
66  *\r
67  * "Check" task - The check task period is initially set to three seconds.  The\r
68  * task checks that all the standard demo tasks, and the register check tasks,\r
69  * are not only still executing, but are executing without reporting any errors.\r
70  * If the check task discovers that a task has either stalled, or reported an\r
71  * error, then it changes its own execution period from the initial three\r
72  * seconds, to just 200ms.  The check task also toggles an LED each time it is\r
73  * called.  This provides a visual indication of the system status:  If the LED\r
74  * toggles every three seconds, then no issues have been discovered.  If the LED\r
75  * toggles every 200ms, then an issue has been discovered with at least one\r
76  * task.\r
77  */\r
78 \r
79 /* Standard includes. */\r
80 #include <stdio.h>\r
81 \r
82 /* Kernel includes. */\r
83 #include "FreeRTOS.h"\r
84 #include "task.h"\r
85 #include "timers.h"\r
86 #include "semphr.h"\r
87 \r
88 /* Standard demo application includes. */\r
89 #include "flop.h"\r
90 #include "semtest.h"\r
91 #include "countsem.h"\r
92 #include "GenQTest.h"\r
93 #include "recmutex.h"\r
94 #include "partest.h"\r
95 #include "serial.h"\r
96 #include "TimerDemo.h"\r
97 #include "IntQueue.h"\r
98 #include "EventGroupsDemo.h"\r
99 #include "TaskNotify.h"\r
100 #include "IntSemTest.h"\r
101 \r
102 /* Priorities for the demo application tasks. */\r
103 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1UL )\r
104 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2UL )\r
105 #define mainCREATOR_TASK_PRIORITY                       ( tskIDLE_PRIORITY + 3UL )\r
106 #define mainFLOP_TASK_PRIORITY                          ( tskIDLE_PRIORITY )\r
107 #define mainUART_COMMAND_CONSOLE_STACK_SIZE     ( configMINIMAL_STACK_SIZE * 2UL )\r
108 #define mainCOM_TEST_TASK_PRIORITY                      ( tskIDLE_PRIORITY + 2 )\r
109 #define mainCHECK_TASK_PRIORITY                         ( configMAX_PRIORITIES - 1 )\r
110 #define mainQUEUE_OVERWRITE_PRIORITY            ( tskIDLE_PRIORITY )\r
111 \r
112 /* The priority used by the UART command console task. */\r
113 #define mainUART_COMMAND_CONSOLE_TASK_PRIORITY  ( configMAX_PRIORITIES - 2 )\r
114 \r
115 /* A block time of zero simply means "don't block". */\r
116 #define mainDONT_BLOCK                                          ( 0UL )\r
117 \r
118 /* The period of the check task, in ms, provided no errors have been reported by\r
119 any of the standard demo tasks.  ms are converted to the equivalent in ticks\r
120 using the pdMS_TO_TICKS() macro constant. */\r
121 #define mainNO_ERROR_CHECK_TASK_PERIOD          ( pdMS_TO_TICKS( 3000UL ) )\r
122 \r
123 /* The period of the check task, in ms, if an error has been reported in one of\r
124 the standard demo tasks.  ms are converted to the equivalent in ticks using the\r
125 pdMS_TO_TICKS() macro. */\r
126 #define mainERROR_CHECK_TASK_PERIOD             ( pdMS_TO_TICKS( 200UL ) )\r
127 \r
128 /* Parameters that are passed into the register check tasks solely for the\r
129 purpose of ensuring parameters are passed into tasks correctly. */\r
130 #define mainREG_TEST_TASK_1_PARAMETER           ( ( void * ) 0x12345678 )\r
131 #define mainREG_TEST_TASK_2_PARAMETER           ( ( void * ) 0x87654321 )\r
132 \r
133 /* The base period used by the timer test tasks. */\r
134 #define mainTIMER_TEST_PERIOD                           ( 50 )\r
135 \r
136 /* Dimensions the queue in which characters received from the UART are\r
137 placed. */\r
138 #define mainRX_QUEUE_LENGTH                                     10\r
139 \r
140 /*-----------------------------------------------------------*/\r
141 \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  * When the full demo is build the idle hook is used to create some timers that\r
173  * cannot be created in main() because the timer demo tasks need the entire\r
174  * command queue.\r
175  */\r
176 void vFullDemoIdleHook( void );\r
177 \r
178 /*\r
179  * The full demo configures the clocks for maximum frequency, wheras the blinky\r
180  * demo uses a slower clock as it also uses low power features.\r
181  */\r
182 static void prvConfigureClocks( void );\r
183 \r
184 /*-----------------------------------------------------------*/\r
185 \r
186 /* The following two variables are used to communicate the status of the\r
187 register check tasks to the check task.  If the variables keep incrementing,\r
188 then the register check tasks have not discovered any errors.  If a variable\r
189 stops incrementing, then an error has been found. */\r
190 volatile unsigned long ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;\r
191 \r
192 /*-----------------------------------------------------------*/\r
193 \r
194 void main_full( void )\r
195 {\r
196         /* This demo sets the clock to its maximum.  The blinky demo uses as slower\r
197         clock as it uses low power features.  */\r
198         prvConfigureClocks();\r
199 \r
200         /* Init the serial port for use by the CLI.  The baud rate parameter is not\r
201         used so set to 0 to make this obvious. */\r
202         xSerialPortInitMinimal( 0, mainRX_QUEUE_LENGTH );\r
203 \r
204         /* Start all the other standard demo/test tasks.  They have no particular\r
205         functionality, but do demonstrate how to use the FreeRTOS API and test the\r
206         kernel port. */\r
207         vStartInterruptQueueTasks();\r
208 \r
209         vStartCountingSemaphoreTasks();\r
210         vStartGenericQueueTasks( tskIDLE_PRIORITY );\r
211         vStartRecursiveMutexTasks();\r
212         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
213         vStartTimerDemoTask( mainTIMER_TEST_PERIOD );\r
214         vStartEventGroupTasks();\r
215         vStartTaskNotifyTask();\r
216         vStartInterruptSemaphoreTasks();\r
217 \r
218         /* Note - the set of standard demo tasks contains two versions of\r
219         vStartMathTasks.c.  One is defined in flop.c, and uses double precision\r
220         floating point numbers and variables.  The other is defined in sp_flop.c,\r
221         and uses single precision floating point numbers and variables.  sp_flop.\r
222         c should be included in this project. */\r
223         vStartMathTasks( mainFLOP_TASK_PRIORITY );\r
224 \r
225         /* Start the tasks that implements the command console on the UART, as\r
226         described above. */\r
227         vUARTCommandConsoleStart( mainUART_COMMAND_CONSOLE_STACK_SIZE, mainUART_COMMAND_CONSOLE_TASK_PRIORITY );\r
228 \r
229         /* Register the standard CLI commands. */\r
230         vRegisterSampleCLICommands();\r
231 \r
232         /* Create the register check tasks, as described at the top of this     file */\r
233         xTaskCreate( prvRegTestTaskEntry1, "Reg1", configMINIMAL_STACK_SIZE, mainREG_TEST_TASK_1_PARAMETER, tskIDLE_PRIORITY, NULL );\r
234         xTaskCreate( prvRegTestTaskEntry2, "Reg2", configMINIMAL_STACK_SIZE, mainREG_TEST_TASK_2_PARAMETER, tskIDLE_PRIORITY, NULL );\r
235 \r
236         /* Create the task that performs the 'check' functionality,     as described at\r
237         the top of this file. */\r
238         xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
239 \r
240         /* Start the scheduler. */\r
241         vTaskStartScheduler();\r
242 \r
243         /* If all is well, the scheduler will now be running, and the following\r
244         line will never be reached.  If the following line does execute, then\r
245         there was insufficient FreeRTOS heap memory available for the Idle and/or\r
246         timer tasks to be created.  See the memory management section on the\r
247         FreeRTOS web site for more details on the FreeRTOS heap\r
248         http://www.freertos.org/a00111.html. */\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 to ensure that they are all still running,\r
279                 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( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
291                 {\r
292                         ulErrorFound |= 1UL << 5UL;\r
293                 }\r
294 \r
295                 if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
296                 {\r
297                         ulErrorFound |= 1UL << 6UL;\r
298                 }\r
299 \r
300                 if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
301                 {\r
302                         ulErrorFound |= 1UL << 8UL;\r
303                 }\r
304 \r
305                 if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )\r
306                 {\r
307                         ulErrorFound |= 1UL << 10UL;\r
308                 }\r
309 \r
310                 if( xAreInterruptSemaphoreTasksStillRunning() != pdTRUE )\r
311                 {\r
312                         ulErrorFound |= 1UL << 14UL;\r
313                 }\r
314 \r
315                 if( xAreTimerDemoTasksStillRunning( ( TickType_t ) mainNO_ERROR_CHECK_TASK_PERIOD ) != pdPASS )\r
316                 {\r
317                         ulErrorFound |= 1UL << 9UL;\r
318                 }\r
319 \r
320                 if( xAreEventGroupTasksStillRunning() != pdPASS )\r
321                 {\r
322                         ulErrorFound |= 1UL << 12UL;\r
323                 }\r
324 \r
325                 if( xAreTaskNotificationTasksStillRunning() != pdTRUE )\r
326                 {\r
327                         ulErrorFound |= 1UL << 13UL;\r
328                 }\r
329 \r
330                 /* Check that the register test 1 task is still running. */\r
331                 if( ulLastRegTest1Value == ulRegTest1LoopCounter )\r
332                 {\r
333                         ulErrorFound |= 1UL << 15UL;\r
334                 }\r
335                 ulLastRegTest1Value = ulRegTest1LoopCounter;\r
336 \r
337                 /* Check that the register test 2 task is still running. */\r
338                 if( ulLastRegTest2Value == ulRegTest2LoopCounter )\r
339                 {\r
340                         ulErrorFound |= 1UL << 16UL;\r
341                 }\r
342                 ulLastRegTest2Value = ulRegTest2LoopCounter;\r
343 \r
344                 /* Toggle the check LED to give an indication of the system status.  If\r
345                 the LED toggles every mainNO_ERROR_CHECK_TASK_PERIOD milliseconds then\r
346                 everything is ok.  A faster toggle indicates an error. */\r
347                 configTOGGLE_LED();\r
348 \r
349                 if( ulErrorFound != pdFALSE )\r
350                 {\r
351                         /* An error has been detected in one of the tasks - flash the LED\r
352                         at a higher frequency to give visible feedback that something has\r
353                         gone wrong (it might just be that the loop back connector required\r
354                         by the comtest tasks has not been fitted). */\r
355                         xDelayPeriod = mainERROR_CHECK_TASK_PERIOD;\r
356                 }\r
357         }\r
358 }\r
359 /*-----------------------------------------------------------*/\r
360 \r
361 static void prvRegTestTaskEntry1( void *pvParameters )\r
362 {\r
363         /* Although the regtest task is written in assembler, its entry point is\r
364         written in C for convenience of checking the task parameter is being passed\r
365         in correctly. */\r
366         if( pvParameters == mainREG_TEST_TASK_1_PARAMETER )\r
367         {\r
368                 /* Start the part of the test that is written in assembler. */\r
369                 vRegTest1Implementation();\r
370         }\r
371 \r
372         /* The following line will only execute if the task parameter is found to\r
373         be incorrect.  The check task will detect that the regtest loop counter is\r
374         not being incremented and flag an error. */\r
375         vTaskDelete( NULL );\r
376 }\r
377 /*-----------------------------------------------------------*/\r
378 \r
379 static void prvRegTestTaskEntry2( void *pvParameters )\r
380 {\r
381         /* Although the regtest task is written in assembler, its entry point is\r
382         written in C for convenience of checking the task parameter is being passed\r
383         in correctly. */\r
384         if( pvParameters == mainREG_TEST_TASK_2_PARAMETER )\r
385         {\r
386                 /* Start the part of the test that is written in assembler. */\r
387                 vRegTest2Implementation();\r
388         }\r
389 \r
390         /* The following line will only execute if the task parameter is found to\r
391         be incorrect.  The check task will detect that the regtest loop counter is\r
392         not being incremented and flag an error. */\r
393         vTaskDelete( NULL );\r
394 }\r
395 /*-----------------------------------------------------------*/\r
396 \r
397 static void prvConfigureClocks( void )\r
398 {\r
399         /* Set Flash wait state for high clock frequency.  Refer to datasheet for\r
400         more details. */\r
401         FlashCtl_setWaitState( FLASH_BANK0, 2 );\r
402         FlashCtl_setWaitState( FLASH_BANK1, 2 );\r
403 \r
404         /* The full demo configures the clocks for maximum frequency, wheras the\r
405         blinky demo uses a slower clock as it also uses low power features.  Maximum\r
406         freqency also needs more voltage.\r
407 \r
408         From the datashee:  For AM_LDO_VCORE1 and AM_DCDC_VCORE1 modes, the maximum\r
409         CPU operating frequency is 48 MHz and maximum input clock frequency for\r
410         peripherals is 24 MHz. */\r
411         PCM_setCoreVoltageLevel( PCM_VCORE1 );\r
412         CS_setDCOCenteredFrequency( CS_DCO_FREQUENCY_48 );\r
413         CS_initClockSignal( CS_HSMCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_1 );\r
414         CS_initClockSignal( CS_SMCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_1 );\r
415         CS_initClockSignal( CS_MCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_1 );\r
416         CS_initClockSignal( CS_ACLK, CS_REFOCLK_SELECT, CS_CLOCK_DIVIDER_1 );\r
417 }\r
418 /*-----------------------------------------------------------*/\r
419 \r
420 #if( configCREATE_SIMPLE_TICKLESS_DEMO == 0 )\r
421 \r
422         void vApplicationTickHook( void )\r
423         {\r
424                 /* This function will be called by each tick interrupt if\r
425                 configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h.  User code can be\r
426                 added here, but the tick hook is called from an interrupt context, so\r
427                 code must not attempt to block, and only the interrupt safe FreeRTOS API\r
428                 functions can be used (those that end in FromISR()). */\r
429 \r
430                 /* The full demo includes a software timer demo/test that requires\r
431                 prodding periodically from the tick interrupt. */\r
432                 vTimerPeriodicISRTests();\r
433 \r
434                 /* Call the periodic event group from ISR demo. */\r
435                 vPeriodicEventGroupsProcessing();\r
436 \r
437                 /* Use task notifications from an interrupt. */\r
438                 xNotifyTaskFromISR();\r
439 \r
440                 /* Use mutexes from interrupts. */\r
441                 vInterruptSemaphorePeriodicTest();\r
442         }\r
443 \r
444 #endif\r
445 /*-----------------------------------------------------------*/\r
446 \r
447 \r
448 \r
449 \r
450 \r
451 \r