]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_R4_RM48_TMS570_CCS5/main_full.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / CORTEX_R4_RM48_TMS570_CCS5 / 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  * main_full() creates all the demo application tasks and software timers,\r
42  * then starts the scheduler.  The web documentation provides more details of\r
43  * the standard demo application tasks, which provide no particular\r
44  * functionality, but do provide a good example of how to use the FreeRTOS API.\r
45  *\r
46  * In addition to the standard demo tasks, the following tasks and tests are\r
47  * defined and/or created within this file:\r
48  *\r
49  * "Check" timer - The check software timer period is set to three seconds.\r
50  * The callback function associated with the check software timer checks that\r
51  * all the standard demo tasks are not only still executing, but are executing\r
52  * without reporting any errors.  If the check software timer discovers that a\r
53  * task has either stalled, or reported an error, then the error is logged and\r
54  * the check software timer toggles the red LEDs.  If an error has never been\r
55  * latched, the check software timer toggles the green LEDs.  Therefore, if the\r
56  * system is executing correctly, the green LEDs will toggle every three\r
57  * seconds, and if an error has ever been detected, the red LEDs will toggle\r
58  * every three seconds.\r
59  *\r
60  * "Reg test" tasks - These fill both the core and floating point registers\r
61  * with known values, then check that each register maintains its expected\r
62  * value for the lifetime of the tasks.  Each task uses a different set of\r
63  * values.  The reg test tasks execute with a very low priority, so get\r
64  * preempted very frequently.  A register containing an unexpected value is\r
65  * indicative of an error in the context switching mechanism.\r
66  *\r
67  * "LED" software timer - The callback function associated with the LED\r
68  * software time maintains a pattern of spinning white LEDs.\r
69  *\r
70  * See the documentation page for this demo on the FreeRTOS.org web site for\r
71  * full information, including hardware setup requirements.\r
72  */\r
73 \r
74 /* Standard includes. */\r
75 #include <stdio.h>\r
76 \r
77 /* Kernel includes. */\r
78 #include "FreeRTOS.h"\r
79 #include "task.h"\r
80 #include "timers.h"\r
81 #include "semphr.h"\r
82 \r
83 /* Standard demo application includes. */\r
84 #include "integer.h"\r
85 #include "PollQ.h"\r
86 #include "semtest.h"\r
87 #include "dynamic.h"\r
88 #include "BlockQ.h"\r
89 #include "blocktim.h"\r
90 #include "countsem.h"\r
91 #include "GenQTest.h"\r
92 #include "recmutex.h"\r
93 #include "death.h"\r
94 #include "partest.h"\r
95 #include "flop.h"\r
96 #include "serial.h"\r
97 #include "comtest.h"\r
98 \r
99 /* Priorities for the demo application tasks. */\r
100 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2UL )\r
101 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1UL )\r
102 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2UL )\r
103 #define mainCREATOR_TASK_PRIORITY                       ( tskIDLE_PRIORITY + 3UL )\r
104 #define mainFLOP_TASK_PRIORITY                          ( tskIDLE_PRIORITY )\r
105 #define mainCOM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 2 )\r
106 #define mainFLOP_TASK_PRIORITY                          ( tskIDLE_PRIORITY )\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 after which the check timer will expire, converted to ticks. */\r
112 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_PERIOD_MS )\r
113 \r
114 /* The period after which the LED timer will expire, converted to ticks. */\r
115 #define mainLED_TIMER_PERIOD_MS                         ( 75UL / portTICK_PERIOD_MS )\r
116 \r
117 /* Constants for the ComTest tasks. */\r
118 #define mainCOM_TEST_BAUD_RATE                          ( ( unsigned long ) 19200 )\r
119 #define mainCOM_TEST_LED                                        ( 100 )\r
120 \r
121 /*-----------------------------------------------------------*/\r
122 \r
123 /*\r
124  * The check timer callback function, as described at the top of this file.\r
125  */\r
126 static void prvCheckTimerCallback( TimerHandle_t xTimer );\r
127 \r
128 /*\r
129  * The LED timer callback function, as described at the top of this file.\r
130  */\r
131 static void prvLEDTimerCallback( TimerHandle_t xTimer );\r
132 \r
133 /*\r
134  * The reg test tasks, as described at the top of this file.\r
135  */\r
136 extern void vRegTestTask1( void *pvParameters );\r
137 extern void vRegTestTask2( void *pvParameters );\r
138 \r
139 /*-----------------------------------------------------------*/\r
140 \r
141 /* Variables that are incremented on each iteration of the reg test tasks -\r
142 provided the tasks have not reported any errors.  The check task inspects these\r
143 variables to ensure they are still incrementing as expected.  If a variable\r
144 stops incrementing then it is likely that its associate task has stalled. */\r
145 volatile unsigned long ulRegTest1Counter = 0, ulRegTest2Counter = 0;\r
146 \r
147 /*-----------------------------------------------------------*/\r
148 \r
149 void main_full( void )\r
150 {\r
151 TimerHandle_t xTimer = NULL;\r
152 \r
153         /* Start all the standard demo/test tasks.  These have not particular\r
154         functionality, but do demonstrate how to use the FreeRTOS API, and test the\r
155         kernel port. */\r
156         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
157         vStartDynamicPriorityTasks();\r
158         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
159         vCreateBlockTimeTasks();\r
160         vStartCountingSemaphoreTasks();\r
161         vStartGenericQueueTasks( tskIDLE_PRIORITY );\r
162         vStartRecursiveMutexTasks();\r
163         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
164         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
165         vStartMathTasks( mainFLOP_TASK_PRIORITY );\r
166         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
167 \r
168         /* Create the register test tasks, as described at the top of this file. */\r
169         xTaskCreate( vRegTestTask1, "Reg1...", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
170         xTaskCreate( vRegTestTask2, "Reg2...", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
171 \r
172 \r
173         /* Create the software timer that performs the 'check' functionality,\r
174         as described at the top of this file. */\r
175         xTimer = xTimerCreate( "CheckTimer",                                    /* A text name, purely to help debugging. */\r
176                                                         ( mainCHECK_TIMER_PERIOD_MS ),  /* The timer period, in this case 3000ms (3s). */\r
177                                                         pdTRUE,                                                 /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
178                                                         ( void * ) 0,                                   /* The ID is not used, so can be set to anything. */\r
179                                                         prvCheckTimerCallback                   /* The callback function that inspects the status of all the other tasks. */\r
180                                                  );\r
181 \r
182         if( xTimer != NULL )\r
183         {\r
184                 xTimerStart( xTimer, mainDONT_BLOCK );\r
185         }\r
186 \r
187         /* Create the software timer that performs the 'LED spin' functionality,\r
188         as described at the top of this file. */\r
189         xTimer = xTimerCreate( "LEDTimer",                                      /* A text name, purely to help debugging. */\r
190                                                         ( mainLED_TIMER_PERIOD_MS ),/* The timer period, in this case 75ms. */\r
191                                                         pdTRUE,                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
192                                                         ( void * ) 0,                           /* The ID is not used, so can be set to anything. */\r
193                                                         prvLEDTimerCallback                     /* The callback function that toggles the white LEDs. */\r
194                                                  );\r
195 \r
196         if( xTimer != NULL )\r
197         {\r
198                 xTimerStart( xTimer, mainDONT_BLOCK );\r
199         }\r
200 \r
201         /* The set of tasks created by the following function call have to be\r
202         created last as they keep account of the number of tasks they expect to see\r
203         running. */\r
204         vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
205 \r
206         /* Start the scheduler. */\r
207         vTaskStartScheduler();\r
208 \r
209         /* If all is well, the scheduler will now be running, and the following line\r
210         will never be reached.  If the following line does execute, then there was\r
211         insufficient FreeRTOS heap memory available for the idle and/or timer tasks\r
212         to be created.  See the memory management section on the FreeRTOS web site\r
213         for more details. */\r
214         for( ;; );\r
215 }\r
216 /*-----------------------------------------------------------*/\r
217 \r
218 static void prvCheckTimerCallback( TimerHandle_t xTimer )\r
219 {\r
220 static long lChangeToRedLEDsAlready = pdFALSE;\r
221 static unsigned long ulLastRegTest1Counter = 0, ulLastRegTest2Counter = 0;\r
222 unsigned long ulErrorFound = pdFALSE;\r
223 /* LEDs are defaulted to use the Green LEDs.  The Red LEDs are used if an error\r
224 is found. */\r
225 static unsigned long ulLED1 = 8, ulLED2 = 11;\r
226 const unsigned long ulRedLED1 = 6, ulRedLED2 = 9;\r
227 \r
228         /* Check all the demo tasks (other than the flash tasks) to ensure\r
229         they are all still running, and that none have detected an error. */\r
230 \r
231         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
232         {\r
233                 ulErrorFound = pdTRUE;\r
234         }\r
235 \r
236         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
237         {\r
238                 ulErrorFound = pdTRUE;\r
239         }\r
240 \r
241         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
242         {\r
243                 ulErrorFound = pdTRUE;\r
244         }\r
245 \r
246         if ( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
247         {\r
248                 ulErrorFound = pdTRUE;\r
249         }\r
250 \r
251         if ( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
252         {\r
253                 ulErrorFound = pdTRUE;\r
254         }\r
255 \r
256         if ( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
257         {\r
258                 ulErrorFound = pdTRUE;\r
259         }\r
260 \r
261         if( xIsCreateTaskStillRunning() != pdTRUE )\r
262         {\r
263                 ulErrorFound = pdTRUE;\r
264         }\r
265 \r
266         if( xArePollingQueuesStillRunning() != pdTRUE )\r
267         {\r
268                 ulErrorFound = pdTRUE;\r
269         }\r
270 \r
271         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
272         {\r
273                 ulErrorFound = pdTRUE;\r
274         }\r
275 \r
276         if( xAreMathsTaskStillRunning() != pdTRUE )\r
277         {\r
278                 ulErrorFound = pdTRUE;\r
279         }\r
280 \r
281         if( xAreComTestTasksStillRunning() != pdTRUE )\r
282         {\r
283                 ulErrorFound = pdTRUE;\r
284         }\r
285 \r
286         /* Check the reg test tasks are still cycling.  They will stop\r
287         incrementing their loop counters if they encounter an error. */\r
288         if( ulRegTest1Counter == ulLastRegTest1Counter )\r
289         {\r
290                 ulErrorFound = pdTRUE;\r
291         }\r
292 \r
293         if( ulRegTest2Counter == ulLastRegTest2Counter )\r
294         {\r
295                 ulErrorFound = pdTRUE;\r
296         }\r
297 \r
298         ulLastRegTest1Counter = ulRegTest1Counter;\r
299         ulLastRegTest2Counter = ulRegTest2Counter;\r
300 \r
301         /* Toggle the check LEDs to give an indication of the system status.  If\r
302         the green LEDs are toggling, then no errors have been detected.  If the red\r
303         LEDs are toggling, then an error has been reported in at least one task. */\r
304         vParTestToggleLED( ulLED1 );\r
305         vParTestToggleLED( ulLED2 );\r
306 \r
307         /* Have any errors been latch in ulErrorFound?  If so, ensure the gree LEDs\r
308         are off, then switch to using the red LEDs. */\r
309         if( ulErrorFound != pdFALSE )\r
310         {\r
311                 if( lChangeToRedLEDsAlready == pdFALSE )\r
312                 {\r
313                         lChangeToRedLEDsAlready = pdTRUE;\r
314 \r
315                         /* An error has been found.  Switch to use the red LEDs. */\r
316                         vParTestSetLED( ulLED1, pdFALSE );\r
317                         vParTestSetLED( ulLED2, pdFALSE );\r
318                         ulLED1 = ulRedLED1;\r
319                         ulLED2 = ulRedLED2;\r
320                 }\r
321         }\r
322 }\r
323 /*-----------------------------------------------------------*/\r
324 \r
325 static void prvLEDTimerCallback( TimerHandle_t xTimer )\r
326 {\r
327 const unsigned long ulNumWhiteLEDs = 6;\r
328 static unsigned long ulLit1 = 2, ulLit2 = 1;\r
329 \r
330         vParTestSetLED( ulLit2, pdFALSE );\r
331 \r
332         ulLit2 = ulLit1;\r
333         ulLit1++;\r
334 \r
335         if( ulLit1 >= ulNumWhiteLEDs )\r
336         {\r
337                 ulLit1 = 0;\r
338         }\r
339 \r
340         vParTestSetLED( ulLit1, pdTRUE );\r
341 }\r
342 /*-----------------------------------------------------------*/\r
343 \r