]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/IA32_flat_GCC_Galileo_Gen_2/Full_Demo/main_full.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / IA32_flat_GCC_Galileo_Gen_2 / Full_Demo / 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  * See http://www.FreeRTOS.org/RTOS_Intel_Quark_Galileo_GCC.html for usage\r
43  * instructions.\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  * "Reg test" tasks - These fill both the core and floating point registers with\r
54  * known values, then check that each register maintains its expected value for\r
55  * the lifetime of the task.  Each task uses a different set of values.  The reg\r
56  * test tasks execute with a very low priority, so get preempted very\r
57  * frequently.  A register containing an unexpected value is indicative of an\r
58  * error in the context switching mechanism.\r
59  *\r
60  * "Check" task - The check task period is set to five seconds.  The task checks\r
61  * that all the standard demo tasks, and the register check tasks, are not only\r
62  * still executing, but are executing without reporting any errors.  The check\r
63  * task toggles an LED on each iteration.  If the LED toggles every 5 seconds\r
64  * then no errors have been found.  If the LED toggles every 1 second then a\r
65  * potential error has been detected.\r
66  */\r
67 \r
68 /* Standard includes. */\r
69 #include <stdio.h>\r
70 #include <stdlib.h>\r
71 #include <string.h>\r
72 #include <stdint.h>\r
73 \r
74 /* FreeRTOS includes. */\r
75 #include "FreeRTOS.h"\r
76 #include "task.h"\r
77 #include "timers.h"\r
78 \r
79 /* Standard demo includes. */\r
80 #include "blocktim.h"\r
81 #include "flash_timer.h"\r
82 #include "semtest.h"\r
83 #include "GenQTest.h"\r
84 #include "QPeek.h"\r
85 #include "countsem.h"\r
86 #include "dynamic.h"\r
87 #include "QueueOverwrite.h"\r
88 #include "QueueSet.h"\r
89 #include "recmutex.h"\r
90 #include "EventGroupsDemo.h"\r
91 #include "death.h"\r
92 #include "TimerDemo.h"\r
93 #include "BlockQ.h"\r
94 #include "flop.h"\r
95 #include "TaskNotify.h"\r
96 #include "IntQueue.h"\r
97 \r
98 /* Galileo includes. */\r
99 #include "galileo_support.h"\r
100 \r
101 /* The rate at which the check task cycles if no errors have been detected, and\r
102 if a [potential] error has been detected.  Increasing the toggle rate in the\r
103 presense of an error gives visual feedback of the system status. */\r
104 #define mainNO_ERROR_CHECK_TASK_PERIOD          pdMS_TO_TICKS( 5000UL )\r
105 #define mainERROR_CHECK_TASK_PERIOD                     pdMS_TO_TICKS( 1000UL )\r
106 \r
107 /* The priorities of the various demo application tasks. */\r
108 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )\r
109 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 1 )\r
110 #define mainGEN_QUEUE_TASK_PRIORITY                     ( tskIDLE_PRIORITY )\r
111 #define mainQUEUE_OVERWRITE_TASK_PRIORITY       ( tskIDLE_PRIORITY )\r
112 #define mainMATHS_TASK_PRIORITY                         ( tskIDLE_PRIORITY )\r
113 \r
114 /* The base period used by the timer test tasks. */\r
115 #define mainTIMER_TEST_PERIOD                           ( 50 )\r
116 \r
117 /* Parameters that are passed into the check tasks for no other purpose other\r
118 than to check the port does this correctly. */\r
119 #define mainREG_TEST_1_PARAMETER                        ( 0x12345678UL )\r
120 #define mainREG_TEST_2_PARAMETER                        ( 0x87654321UL )\r
121 \r
122 /*-----------------------------------------------------------*/\r
123 \r
124 /*\r
125  * The function that implements the check task, as described at the top of this\r
126  * file.\r
127  */\r
128 static void prvCheckTask( void *pvParameters );\r
129 \r
130 /*\r
131  * Entry points for the register check tasks, as described at the top of this\r
132  * file.\r
133  */\r
134 static void prvRegTest1Entry( void *pvParameters );\r
135 static void prvRegTest2Entry( void *pvParameters );\r
136 \r
137 /*\r
138  * The implementation of the register check tasks, which are implemented in\r
139  * RegTest.S.  These functions are called by prvRegTest1Entry() and\r
140  * prvRegTest2Entry() respectively.\r
141  */\r
142 extern void vRegTest1( void );\r
143 extern void vRegTest2( void );\r
144 \r
145 /*-----------------------------------------------------------*/\r
146 \r
147 /* Constants used by the register check tasks when checking the FPU registers. */\r
148 const double dRegTest1_st7 = 7.0, dRegTest1_st6 = 6.0, dRegTest1_st5 = 5.0, dRegTest1_st4 = 4.0, dRegTest1_st3 = 3.0, dRegTest1_st2 = 2.0, dRegTest1_st1 = 1.0;\r
149 const double dRegTest2_st7 = 700.0, dRegTest2_st6 = 600.0, dRegTest2_st5 = 500.0, dRegTest2_st4 = 400.0, dRegTest2_st3 = 300.0, dRegTest2_st2 = 200.0, dRegTest2_st1 = 100.0;\r
150 \r
151 /* Counters used by the register check tasks to indicate that they are still\r
152 executing without having discovered any errors. */\r
153 volatile uint32_t ulRegTest1Counter, ulRegTest2Counter;\r
154 volatile uint32_t ulCheckLoops = 0;\r
155 \r
156 /*-----------------------------------------------------------*/\r
157 \r
158 /* See http://www.FreeRTOS.org/RTOS_Intel_Quark_Galileo_GCC.html for usage\r
159 instructions. */\r
160 void main_full( void )\r
161 {\r
162         /* Create all the other standard demo tasks. */\r
163         vCreateBlockTimeTasks();\r
164         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
165         vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
166         vStartQueuePeekTasks();\r
167         vStartCountingSemaphoreTasks();\r
168         vStartDynamicPriorityTasks();\r
169         vStartQueueOverwriteTask( mainQUEUE_OVERWRITE_TASK_PRIORITY );\r
170         vStartQueueSetTasks();\r
171         vStartRecursiveMutexTasks();\r
172         vStartEventGroupTasks();\r
173         vStartTimerDemoTask( mainTIMER_TEST_PERIOD );\r
174         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
175         vStartTaskNotifyTask();\r
176         vStartInterruptQueueTasks();\r
177 \r
178         #if configSUPPORT_FPU == 1\r
179         {\r
180                 vStartMathTasks( mainMATHS_TASK_PRIORITY );\r
181         }\r
182         #endif /* configSUPPORT_FPU */\r
183 \r
184         /* Create the 'check' task, as described at the top of this file. */\r
185         xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE * 2, NULL, configMAX_PRIORITIES - 1, NULL );\r
186 \r
187         /* Create the register test tasks, as described at the top of this file. */\r
188         xTaskCreate( prvRegTest1Entry, "Reg1", configMINIMAL_STACK_SIZE, ( void * ) mainREG_TEST_1_PARAMETER, tskIDLE_PRIORITY, NULL );\r
189         xTaskCreate( prvRegTest2Entry, "Reg2", configMINIMAL_STACK_SIZE, ( void * ) mainREG_TEST_2_PARAMETER, tskIDLE_PRIORITY, NULL );\r
190 \r
191         /* Death tasks must be created last as they check the number of tasks\r
192         running against the number of tasks expected to be running as part of their\r
193         sanity checks. */\r
194         vCreateSuicidalTasks( tskIDLE_PRIORITY );\r
195 \r
196         /* Display HPET Information (Disable in HPET.H). */\r
197         vCreateHPETInfoUpdateTask();\r
198 \r
199         /* Start the scheduler itself. */\r
200         vTaskStartScheduler();\r
201 \r
202         /* If all is well, the scheduler will now be running, and the following line\r
203         will never be reached.  If the following line does execute, then there was\r
204         insufficient FreeRTOS heap memory available for the idle and/or timer tasks\r
205         to be created.  See the memory management section on the FreeRTOS web site\r
206         for more details. */\r
207         for( ;; );\r
208 }\r
209 /*-----------------------------------------------------------*/\r
210 \r
211 static void prvRegTest1Entry( void *pvParameters )\r
212 {\r
213         /* Remove compiler warning if configASSERT() is not defined. */\r
214         ( void ) pvParameters;\r
215 \r
216         /* Check the parameter is passed in correctly. */\r
217         configASSERT( ( ( uint32_t) pvParameters ) == mainREG_TEST_1_PARAMETER );\r
218 \r
219         /* Tell FreeRTOS that this task needs a floating point context. */\r
220         portTASK_USES_FLOATING_POINT();\r
221 \r
222         /* Call the assembly file routine that performs the 'reg test' functionality\r
223         as described at the top of this file. */\r
224         vRegTest1();\r
225 }\r
226 /*-----------------------------------------------------------*/\r
227 \r
228 static void prvRegTest2Entry( void *pvParameters )\r
229 {\r
230         /* Remove compiler warning if configASSERT() is not defined. */\r
231         ( void ) pvParameters;\r
232 \r
233         /* Check the parameter is passed in correctly. */\r
234         configASSERT( ( ( uint32_t) pvParameters ) == mainREG_TEST_2_PARAMETER );\r
235 \r
236         /* Tell FreeRTOS that this task needs a floating point context. */\r
237         portTASK_USES_FLOATING_POINT();\r
238 \r
239         /* Call the assembly file routine that performs the 'reg test' functionality\r
240         as described at the top of this file. */\r
241         vRegTest2();\r
242 }\r
243 /*-----------------------------------------------------------*/\r
244 \r
245 static void prvCheckTask( void *pvParameters )\r
246 {\r
247 uint32_t ulLastRegTest1Counter = 0UL, ulLastRegTest2Counter = 0UL;\r
248 uint32_t ulErrorOccurred, ulElapsedTimeInSeconds = 0UL;\r
249 TickType_t xLastExpireTime, xBlockTime = mainNO_ERROR_CHECK_TASK_PERIOD;\r
250 BaseType_t xErrorFlag = pdFALSE;\r
251 \r
252         /* Avoid compiler warnings. */\r
253         ( void ) pvParameters;\r
254 \r
255         /* Initialise the last expire time to the current time. */\r
256         xLastExpireTime = xTaskGetTickCount();\r
257 \r
258         /* Message to wait for an update - first update won't happen for X seconds. */\r
259         g_printf_rcc( 5, 2, DEFAULT_SCREEN_COLOR, "Starting task check loop - Please wait for a status update." );\r
260         g_printf_rcc( 6, 2, DEFAULT_SCREEN_COLOR, "No task errors encountered." );\r
261 \r
262         for( ;; )\r
263         {\r
264                 vTaskDelayUntil( &xLastExpireTime, xBlockTime );\r
265                 ulElapsedTimeInSeconds += xBlockTime;\r
266 \r
267                 /* Have any of the standard demo tasks detected an error in their\r
268                 operation?  If so, latch the offending test in a bit map so it can be\r
269                 printed to the terminal.  Once one error has occurred the cycle rate is\r
270                 increased to increase the rate at which the LED toggles, which can cause\r
271                 further errors to be detected (as some tests will not expect the\r
272                 increased cycle rate). */\r
273 \r
274                 ulErrorOccurred = 0UL;\r
275 \r
276                 if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
277                 {\r
278                         ulErrorOccurred |= ( 0x01UL << 0UL );\r
279                 }\r
280 \r
281                 if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
282                 {\r
283                         ulErrorOccurred |= ( 0x01UL << 1UL );\r
284                 }\r
285 \r
286                 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
287                 {\r
288                         ulErrorOccurred |= ( 0x01UL << 2UL );\r
289                 }\r
290 \r
291                 if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
292                 {\r
293                         ulErrorOccurred |= ( 0x01UL << 3UL );\r
294                 }\r
295 \r
296                 if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )\r
297                 {\r
298                         ulErrorOccurred |= ( 0x01UL << 4UL );\r
299                 }\r
300 \r
301                 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
302                 {\r
303                         ulErrorOccurred |= ( 0x01UL << 5UL );\r
304                 }\r
305 \r
306                 if( xIsQueueOverwriteTaskStillRunning() != pdTRUE )\r
307                 {\r
308                         ulErrorOccurred |= ( 0x01UL << 6UL );\r
309                 }\r
310 \r
311                 if( xAreQueueSetTasksStillRunning() != pdTRUE )\r
312                 {\r
313                         ulErrorOccurred |= ( 0x01UL << 7UL );\r
314                 }\r
315 \r
316                 if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
317                 {\r
318                         ulErrorOccurred |= ( 0x01UL << 8UL );\r
319                 }\r
320 \r
321                 if( xAreEventGroupTasksStillRunning() != pdTRUE )\r
322                 {\r
323                         ulErrorOccurred |= ( 0x01UL << 9UL );\r
324                 }\r
325 \r
326                 if( xIsCreateTaskStillRunning() != pdTRUE )\r
327                 {\r
328                         ulErrorOccurred |= ( 0x01UL << 10UL );\r
329                 }\r
330 \r
331                 if( xAreTimerDemoTasksStillRunning( xBlockTime ) != pdTRUE )\r
332                 {\r
333                         ulErrorOccurred |= ( 0x01UL << 11UL );\r
334                 }\r
335 \r
336                 if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
337                 {\r
338                         ulErrorOccurred |= ( 0x01UL << 12UL );\r
339                 }\r
340 \r
341                 if( xAreTaskNotificationTasksStillRunning() != pdTRUE )\r
342                 {\r
343                         ulErrorOccurred |= ( 1UL << 13UL );\r
344                 }\r
345 \r
346                 if( xAreIntQueueTasksStillRunning() != pdTRUE )\r
347                 {\r
348                         ulErrorOccurred |= ( 1UL << 14UL );\r
349                 }\r
350 \r
351                 #if configSUPPORT_FPU == 1\r
352                 {\r
353                         if( xAreMathsTaskStillRunning() != pdTRUE )\r
354                         {\r
355                                 ulErrorOccurred |= ( 0x01UL << 15UL );\r
356                         }\r
357                 }\r
358                 #endif /* configSUPPORT_FPU */\r
359 \r
360                 /* Check the register test tasks are still looping. */\r
361                 if( ulRegTest1Counter == ulLastRegTest1Counter )\r
362                 {\r
363                         ulErrorOccurred |= ( 0x01UL << 16UL );\r
364                 }\r
365                 else\r
366                 {\r
367                         ulLastRegTest1Counter = ulRegTest1Counter;\r
368                 }\r
369 \r
370                 if( ulRegTest2Counter == ulLastRegTest2Counter )\r
371                 {\r
372                         ulErrorOccurred |= ( 0x01UL << 17UL );\r
373                 }\r
374                 else\r
375                 {\r
376                         ulLastRegTest2Counter = ulRegTest2Counter;\r
377                 }\r
378 \r
379                 if( ulErrorOccurred != 0UL )\r
380                 {\r
381                         /* Decrease the block time, which will increase the rate at\r
382                         which the LED blinks - and in so doing - give visual feedback of\r
383                         the error status. */\r
384                         xBlockTime = mainERROR_CHECK_TASK_PERIOD;\r
385                 }\r
386 \r
387                 /* Print the hex bit pattern, time, and the loop number - just to make\r
388                 sure the task is still cycling. */\r
389                 g_printf_rcc( 5, 2, DEFAULT_SCREEN_COLOR,\r
390                         "Status code: 0x%08x at task check time : %8ds,  loop #: %8d\r",\r
391                         ulErrorOccurred, ( ulElapsedTimeInSeconds / 1000 ), ( ulCheckLoops + 1 ) );\r
392 \r
393                 /* Print the current free heap size and the minimum ever free heap\r
394                 size. */\r
395                 g_printf_rcc( 6, 2, DEFAULT_SCREEN_COLOR,\r
396                         "Current free heap: %d bytes, Min. free heap: %d bytes\r",\r
397                         xPortGetFreeHeapSize(), xPortGetMinimumEverFreeHeapSize() );\r
398 \r
399                 /* Show the first error that occurred on a separate line. */\r
400                 if( ( xErrorFlag == pdFALSE ) && ( ulErrorOccurred != pdFALSE ) )\r
401                 {\r
402                         xErrorFlag = pdTRUE;\r
403                         g_printf_rcc( 7, 2, ANSI_COLOR_RED,\r
404                                 "Error  code: 0x%08x at check time : %8ds (First Error),  loop#: %8d \r",\r
405                                 ulErrorOccurred, (  ulElapsedTimeInSeconds / 1000 ), ( ulCheckLoops + 1 ) );\r
406                 }\r
407 \r
408                 /* Flash the LED */\r
409                 ulBlinkLED();\r
410 \r
411                 /* Crude Overflow check to keep printf() statements <= 8 digits long */\r
412                 ulCheckLoops++;\r
413                 if( ulCheckLoops > 10000000UL )\r
414                 {\r
415                         ulCheckLoops = 0UL;\r
416                 }\r
417         }\r
418 }\r
419 /*-----------------------------------------------------------*/\r
420 \r