]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4_SimpleLink_CC3220SF_CCS/Full_Demo/main_full.c
63ff5f28a55f3e5590064645e16e2205ff42b6ab
[freertos] / FreeRTOS / Demo / CORTEX_M4_SimpleLink_CC3220SF_CCS / Full_Demo / main_full.c
1 /*\r
2  * FreeRTOS Kernel V10.2.0\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 /******************************************************************************\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_CC3220_SimpleLink_FreeRTOS_Demo.html for\r
42  * instructions.\r
43  *\r
44  ******************************************************************************\r
45  *\r
46  * main_full() creates all the demo application tasks and software timers, then\r
47  * starts the scheduler.  The web documentation provides more details of the\r
48  * standard demo application tasks, which provide no particular functionality,\r
49  * but do provide a good example of how to use the FreeRTOS API.\r
50  *\r
51  * In addition to the standard demo tasks, the following tasks and tests are\r
52  * defined and/or created within this file:\r
53  *\r
54  * "Reg test" tasks - These fill the core registers with known values, then\r
55  * check that each register maintains its expected value for the lifetime of the\r
56  * task.  Each task uses a different set of values.  The reg test tasks execute\r
57  * with a very low priority, so get preempted very frequently.  A register\r
58  * containing an unexpected value is indicative of an error in the context\r
59  * switching mechanism.\r
60  *\r
61  * "Check" task - The check task period is initially set to three seconds.  The\r
62  * task checks that all the standard demo tasks, and the register check tasks,\r
63  * are not only still executing, but are executing without reporting any errors.\r
64  * If the check task discovers that a task has either stalled, or reported an\r
65  * error, then it changes its own execution period from the initial three\r
66  * seconds, to just 200ms.  The check task also toggles an LED each time it is\r
67  * called.  This provides a visual indication of the system status:  If the LED\r
68  * toggles every three seconds, then no issues have been discovered.  If the LED\r
69  * toggles every 200ms, then an issue has been discovered with at least one\r
70  * task.\r
71  */\r
72 \r
73 /* Standard includes. */\r
74 #include <stdio.h>\r
75 \r
76 /* Kernel includes. */\r
77 #include "FreeRTOS.h"\r
78 #include "task.h"\r
79 #include "timers.h"\r
80 #include "semphr.h"\r
81 \r
82 /* Standard demo application includes. */\r
83 #include "semtest.h"\r
84 #include "countsem.h"\r
85 #include "GenQTest.h"\r
86 #include "recmutex.h"\r
87 #include "partest.h"\r
88 #include "TimerDemo.h"\r
89 #include "EventGroupsDemo.h"\r
90 #include "TaskNotify.h"\r
91 #include "IntSemTest.h"\r
92 #include "StaticAllocation.h"\r
93 #include "StreamBufferDemo.h"\r
94 #include "MessageBufferDemo.h"\r
95 \r
96 /* Priorities for the demo application tasks. */\r
97 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1UL )\r
98 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2UL )\r
99 #define mainCREATOR_TASK_PRIORITY                       ( tskIDLE_PRIORITY + 3UL )\r
100 #define mainUART_COMMAND_CONSOLE_STACK_SIZE     ( configMINIMAL_STACK_SIZE * 2UL )\r
101 #define mainCOM_TEST_TASK_PRIORITY                      ( tskIDLE_PRIORITY + 2 )\r
102 #define mainCHECK_TASK_PRIORITY                         ( configMAX_PRIORITIES - 1 )\r
103 #define mainQUEUE_OVERWRITE_PRIORITY            ( tskIDLE_PRIORITY )\r
104 \r
105 /* The priority used by the UART command console task. */\r
106 #define mainUART_COMMAND_CONSOLE_TASK_PRIORITY  ( configMAX_PRIORITIES - 2 )\r
107 \r
108 /* A block time of zero simply means "don't block". */\r
109 #define mainDONT_BLOCK                                          ( 0UL )\r
110 \r
111 /* The period of the check task, in ms, provided no errors have been reported by\r
112 any of the standard demo tasks.  ms are converted to the equivalent in ticks\r
113 using the pdMS_TO_TICKS() macro constant. */\r
114 #define mainNO_ERROR_CHECK_TASK_PERIOD          ( pdMS_TO_TICKS( 3000UL ) )\r
115 \r
116 /* The period of the check task, in ms, if an error has been reported in one of\r
117 the standard demo tasks.  ms are converted to the equivalent in ticks using the\r
118 pdMS_TO_TICKS() macro. */\r
119 #define mainERROR_CHECK_TASK_PERIOD             ( pdMS_TO_TICKS( 200UL ) )\r
120 \r
121 /* Parameters that are passed into the register check tasks solely for the\r
122 purpose of ensuring parameters are passed into tasks correctly. */\r
123 #define mainREG_TEST_TASK_1_PARAMETER           ( ( void * ) 0x12345678 )\r
124 #define mainREG_TEST_TASK_2_PARAMETER           ( ( void * ) 0x87654321 )\r
125 \r
126 /* The base period used by the timer test tasks. */\r
127 #define mainTIMER_TEST_PERIOD                           ( 50 )\r
128 \r
129 /*-----------------------------------------------------------*/\r
130 \r
131 \r
132 /*\r
133  * The check task, as described at the top of this file.\r
134  */\r
135 static void prvCheckTask( void *pvParameters );\r
136 \r
137 /*\r
138  * Register check tasks, and the tasks used to write over and check the contents\r
139  * of the FPU registers, as described at the top of this file.  The nature of\r
140  * these files necessitates that they are written in an assembly file, but the\r
141  * entry points are kept in the C file for the convenience of checking the task\r
142  * parameter.\r
143  */\r
144 static void prvRegTestTaskEntry1( void *pvParameters );\r
145 extern void vRegTest1Implementation( void );\r
146 static void prvRegTestTaskEntry2( void *pvParameters );\r
147 extern void vRegTest2Implementation( void );\r
148 \r
149 /*\r
150  * Register commands that can be used with FreeRTOS+CLI.  The commands are\r
151  * defined in CLI-Commands.c and File-Related-CLI-Command.c respectively.\r
152  */\r
153 extern void vRegisterSampleCLICommands( void );\r
154 \r
155 /*\r
156  * The task that manages the FreeRTOS+CLI input and output.\r
157  */\r
158 extern void vUARTCommandConsoleStart( uint16_t usStackSize, UBaseType_t uxPriority );\r
159 \r
160 /*\r
161  * When the full demo is build the idle hook is used to create some timers that\r
162  * cannot be created in main() because the timer demo tasks need the entire\r
163  * command queue.\r
164  */\r
165 void vFullDemoIdleHook( void );\r
166 \r
167 /*\r
168  * Toggles the LED built onto the Launchpad hardware.\r
169  */\r
170 extern void vMainToggleLED( void );\r
171 \r
172 /*-----------------------------------------------------------*/\r
173 \r
174 /* The following two variables are used to communicate the status of the\r
175 register check tasks to the check task.  If the variables keep incrementing,\r
176 then the register check tasks have not discovered any errors.  If a variable\r
177 stops incrementing, then an error has been found. */\r
178 volatile unsigned long ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;\r
179 \r
180 /*-----------------------------------------------------------*/\r
181 \r
182 void main_full( void )\r
183 {\r
184         vStartStaticallyAllocatedTasks();\r
185         vStartCountingSemaphoreTasks();\r
186         vStartGenericQueueTasks( tskIDLE_PRIORITY );\r
187         vStartRecursiveMutexTasks();\r
188         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
189         vStartTimerDemoTask( mainTIMER_TEST_PERIOD );\r
190         vStartEventGroupTasks();\r
191         vStartTaskNotifyTask();\r
192         vStartInterruptSemaphoreTasks();\r
193         vStartStreamBufferTasks();\r
194         vStartMessageBufferTasks( configMINIMAL_STACK_SIZE );\r
195 \r
196         /* Create the register check tasks, as described at the top of this     file */\r
197         xTaskCreate( prvRegTestTaskEntry1, "Reg1", configMINIMAL_STACK_SIZE, mainREG_TEST_TASK_1_PARAMETER, tskIDLE_PRIORITY, NULL );\r
198         xTaskCreate( prvRegTestTaskEntry2, "Reg2", configMINIMAL_STACK_SIZE, mainREG_TEST_TASK_2_PARAMETER, tskIDLE_PRIORITY, NULL );\r
199 \r
200         /* Create the task that performs the 'check' functionality,     as described at\r
201         the top of this file. */\r
202         xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
203 \r
204         /* Start the scheduler. */\r
205         vTaskStartScheduler();\r
206 \r
207         /* If all is well, the scheduler will now be running, and the following\r
208         line will never be reached.  If the following line does execute, then\r
209         there was insufficient FreeRTOS heap memory available for the Idle and/or\r
210         timer tasks to be created.  See the memory management section on the\r
211         FreeRTOS web site for more details on the FreeRTOS heap\r
212         http://www.freertos.org/a00111.html. */\r
213         for( ;; );\r
214 }\r
215 /*-----------------------------------------------------------*/\r
216 \r
217 static void prvCheckTask( void *pvParameters )\r
218 {\r
219 TickType_t xDelayPeriod = mainNO_ERROR_CHECK_TASK_PERIOD;\r
220 TickType_t xLastExecutionTime;\r
221 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;\r
222 unsigned long ulErrorFound = pdFALSE;\r
223 \r
224         /* Just to stop compiler warnings. */\r
225         ( void ) pvParameters;\r
226 \r
227         /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()\r
228         works correctly. */\r
229         xLastExecutionTime = xTaskGetTickCount();\r
230 \r
231         /* Cycle for ever, delaying then checking all the other tasks are still\r
232         operating without error.  The onboard LED is toggled on each iteration.\r
233         If an error is detected then the delay period is decreased from\r
234         mainNO_ERROR_CHECK_TASK_PERIOD to mainERROR_CHECK_TASK_PERIOD.  This has the\r
235         effect of increasing the rate at which the onboard LED toggles, and in so\r
236         doing gives visual feedback of the system status. */\r
237         for( ;; )\r
238         {\r
239                 /* Delay until it is time to execute again. */\r
240                 vTaskDelayUntil( &xLastExecutionTime, xDelayPeriod );\r
241 \r
242                 /* Check all the demo tasks to ensure that they are all still running,\r
243                 and that none have detected an error. */\r
244                 if( xAreStaticAllocationTasksStillRunning() != pdTRUE )\r
245                 {\r
246                     ulErrorFound |= 1UL << 0UL;\r
247                 }\r
248 \r
249                 if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
250                 {\r
251                         ulErrorFound |= 1UL << 5UL;\r
252                 }\r
253 \r
254                 if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
255                 {\r
256                         ulErrorFound |= 1UL << 6UL;\r
257                 }\r
258 \r
259                 if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
260                 {\r
261                         ulErrorFound |= 1UL << 8UL;\r
262                 }\r
263 \r
264                 if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )\r
265                 {\r
266                         ulErrorFound |= 1UL << 10UL;\r
267                 }\r
268 \r
269                 if( xAreInterruptSemaphoreTasksStillRunning() != pdTRUE )\r
270                 {\r
271                         ulErrorFound |= 1UL << 14UL;\r
272                 }\r
273 \r
274                 if( xAreTimerDemoTasksStillRunning( ( TickType_t ) mainNO_ERROR_CHECK_TASK_PERIOD ) != pdPASS )\r
275                 {\r
276                         ulErrorFound |= 1UL << 9UL;\r
277                 }\r
278 \r
279                 if( xAreEventGroupTasksStillRunning() != pdPASS )\r
280                 {\r
281                         ulErrorFound |= 1UL << 12UL;\r
282                 }\r
283 \r
284                 if( xAreTaskNotificationTasksStillRunning() != pdTRUE )\r
285                 {\r
286                         ulErrorFound |= 1UL << 13UL;\r
287                 }\r
288 \r
289                 if( xAreStreamBufferTasksStillRunning() != pdTRUE )\r
290                 {\r
291                         ulErrorFound |= 1UL << 14UL;\r
292                 }\r
293 \r
294                 if( xAreMessageBufferTasksStillRunning() != pdTRUE )\r
295                 {\r
296                         ulErrorFound |= 1UL << 15UL;\r
297                 }\r
298 \r
299                 /* Check that the register test 1 task is still running. */\r
300                 if( ulLastRegTest1Value == ulRegTest1LoopCounter )\r
301                 {\r
302                         ulErrorFound |= 1UL << 16UL;\r
303                 }\r
304                 ulLastRegTest1Value = ulRegTest1LoopCounter;\r
305 \r
306                 /* Check that the register test 2 task is still running. */\r
307                 if( ulLastRegTest2Value == ulRegTest2LoopCounter )\r
308                 {\r
309                         ulErrorFound |= 1UL << 17UL;\r
310                 }\r
311                 ulLastRegTest2Value = ulRegTest2LoopCounter;\r
312 \r
313                 /* Toggle the check LED to give an indication of the system status.  If\r
314                 the LED toggles every mainNO_ERROR_CHECK_TASK_PERIOD milliseconds then\r
315                 everything is ok.  A faster toggle indicates an error. */\r
316                 vMainToggleLED();\r
317 \r
318                 if( ulErrorFound != pdFALSE )\r
319                 {\r
320                         /* An error has been detected in one of the tasks - flash the LED\r
321                         at a higher frequency to give visible feedback that something has\r
322                         gone wrong (it might just be that the loop back connector required\r
323                         by the comtest tasks has not been fitted). */\r
324                         xDelayPeriod = mainERROR_CHECK_TASK_PERIOD;\r
325                 }\r
326         }\r
327 }\r
328 /*-----------------------------------------------------------*/\r
329 \r
330 static void prvRegTestTaskEntry1( void *pvParameters )\r
331 {\r
332         /* Although the regtest task is written in assembler, its entry point is\r
333         written in C for convenience of checking the task parameter is being passed\r
334         in correctly. */\r
335         if( pvParameters == mainREG_TEST_TASK_1_PARAMETER )\r
336         {\r
337                 /* Start the part of the test that is written in assembler. */\r
338                 vRegTest1Implementation();\r
339         }\r
340 \r
341         /* The following line will only execute if the task parameter is found to\r
342         be incorrect.  The check task will detect that the regtest loop counter is\r
343         not being incremented and flag an error. */\r
344         vTaskDelete( NULL );\r
345 }\r
346 /*-----------------------------------------------------------*/\r
347 \r
348 static void prvRegTestTaskEntry2( void *pvParameters )\r
349 {\r
350         /* Although the regtest task is written in assembler, its entry point is\r
351         written in C for convenience of checking the task parameter is being passed\r
352         in correctly. */\r
353         if( pvParameters == mainREG_TEST_TASK_2_PARAMETER )\r
354         {\r
355                 /* Start the part of the test that is written in assembler. */\r
356                 vRegTest2Implementation();\r
357         }\r
358 \r
359         /* The following line will only execute if the task parameter is found to\r
360         be incorrect.  The check task will detect that the regtest loop counter is\r
361         not being incremented and flag an error. */\r
362         vTaskDelete( NULL );\r
363 }\r
364 /*-----------------------------------------------------------*/\r
365 \r
366 #if( configCREATE_SIMPLE_TICKLESS_DEMO == 0 )\r
367 \r
368         void vApplicationTickHook( void )\r
369         {\r
370                 /* This function will be called by each tick interrupt if\r
371                 configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h.  User code can be\r
372                 added here, but the tick hook is called from an interrupt context, so\r
373                 code must not attempt to block, and only the interrupt safe FreeRTOS API\r
374                 functions can be used (those that end in FromISR()). */\r
375 \r
376                 /* The full demo includes a software timer demo/test that requires\r
377                 prodding periodically from the tick interrupt. */\r
378                 vTimerPeriodicISRTests();\r
379 \r
380                 /* Call the periodic event group from ISR demo. */\r
381                 vPeriodicEventGroupsProcessing();\r
382 \r
383                 /* Use task notifications from an interrupt. */\r
384                 xNotifyTaskFromISR();\r
385 \r
386                 /* Use mutexes from interrupts. */\r
387                 vInterruptSemaphorePeriodicTest();\r
388 \r
389                 /* Writes to stream buffer byte by byte to test the stream buffer trigger\r
390                 level functionality. */\r
391                 vPeriodicStreamBufferProcessing();\r
392         }\r
393 \r
394 #endif\r
395 /*-----------------------------------------------------------*/\r
396 \r
397 \r
398 \r
399 \r
400 \r