]> begriffs open source - cmsis-freertos/blob - Demo/CORTEX_R4_RM48_TMS570_CCS5/main_full.c
Update README.md - branch main is now the base branch
[cmsis-freertos] / Demo / CORTEX_R4_RM48_TMS570_CCS5 / main_full.c
1 /*
2  * FreeRTOS V202111.00
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * http://www.FreeRTOS.org
23  * http://aws.amazon.com/freertos
24  *
25  * 1 tab == 4 spaces!
26  */
27
28 /******************************************************************************
29  * NOTE 1:  This project provides two demo applications.  A simple blinky style
30  * project, and a more comprehensive test and demo application.  The
31  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select
32  * between the two.  See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY
33  * in main.c.  This file implements the comprehensive test and demo version.
34  *
35  * NOTE 2:  This file only contains the source code that is specific to the
36  * full demo.  Generic functions, such FreeRTOS hook functions, and functions
37  * required to configure the hardware, are defined in main.c.
38  ******************************************************************************
39  *
40  * main_full() creates all the demo application tasks and software timers,
41  * then starts the scheduler.  The web documentation provides more details of
42  * the standard demo application tasks, which provide no particular
43  * functionality, but do provide a good example of how to use the FreeRTOS API.
44  *
45  * In addition to the standard demo tasks, the following tasks and tests are
46  * defined and/or created within this file:
47  *
48  * "Check" timer - The check software timer period is set to three seconds.
49  * The callback function associated with the check software timer checks that
50  * all the standard demo tasks are not only still executing, but are executing
51  * without reporting any errors.  If the check software timer discovers that a
52  * task has either stalled, or reported an error, then the error is logged and
53  * the check software timer toggles the red LEDs.  If an error has never been
54  * latched, the check software timer toggles the green LEDs.  Therefore, if the
55  * system is executing correctly, the green LEDs will toggle every three
56  * seconds, and if an error has ever been detected, the red LEDs will toggle
57  * every three seconds.
58  *
59  * "Reg test" tasks - These fill both the core and floating point registers
60  * with known values, then check that each register maintains its expected
61  * value for the lifetime of the tasks.  Each task uses a different set of
62  * values.  The reg test tasks execute with a very low priority, so get
63  * preempted very frequently.  A register containing an unexpected value is
64  * indicative of an error in the context switching mechanism.
65  *
66  * "LED" software timer - The callback function associated with the LED
67  * software time maintains a pattern of spinning white LEDs.
68  *
69  * See the documentation page for this demo on the FreeRTOS.org web site for
70  * full information, including hardware setup requirements.
71  */
72
73 /* Standard includes. */
74 #include <stdio.h>
75
76 /* Kernel includes. */
77 #include "FreeRTOS.h"
78 #include "task.h"
79 #include "timers.h"
80 #include "semphr.h"
81
82 /* Standard demo application includes. */
83 #include "integer.h"
84 #include "PollQ.h"
85 #include "semtest.h"
86 #include "dynamic.h"
87 #include "BlockQ.h"
88 #include "blocktim.h"
89 #include "countsem.h"
90 #include "GenQTest.h"
91 #include "recmutex.h"
92 #include "death.h"
93 #include "partest.h"
94 #include "flop.h"
95 #include "serial.h"
96 #include "comtest.h"
97
98 /* Priorities for the demo application tasks. */
99 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2UL )
100 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1UL )
101 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2UL )
102 #define mainCREATOR_TASK_PRIORITY                       ( tskIDLE_PRIORITY + 3UL )
103 #define mainFLOP_TASK_PRIORITY                          ( tskIDLE_PRIORITY )
104 #define mainCOM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 2 )
105 #define mainFLOP_TASK_PRIORITY                          ( tskIDLE_PRIORITY )
106
107 /* A block time of zero simply means "don't block". */
108 #define mainDONT_BLOCK                                          ( 0UL )
109
110 /* The period after which the check timer will expire, converted to ticks. */
111 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_PERIOD_MS )
112
113 /* The period after which the LED timer will expire, converted to ticks. */
114 #define mainLED_TIMER_PERIOD_MS                         ( 75UL / portTICK_PERIOD_MS )
115
116 /* Constants for the ComTest tasks. */
117 #define mainCOM_TEST_BAUD_RATE                          ( ( unsigned long ) 19200 )
118 #define mainCOM_TEST_LED                                        ( 100 )
119
120 /*-----------------------------------------------------------*/
121
122 /*
123  * The check timer callback function, as described at the top of this file.
124  */
125 static void prvCheckTimerCallback( TimerHandle_t xTimer );
126
127 /*
128  * The LED timer callback function, as described at the top of this file.
129  */
130 static void prvLEDTimerCallback( TimerHandle_t xTimer );
131
132 /*
133  * The reg test tasks, as described at the top of this file.
134  */
135 extern void vRegTestTask1( void *pvParameters );
136 extern void vRegTestTask2( void *pvParameters );
137
138 /*-----------------------------------------------------------*/
139
140 /* Variables that are incremented on each iteration of the reg test tasks -
141 provided the tasks have not reported any errors.  The check task inspects these
142 variables to ensure they are still incrementing as expected.  If a variable
143 stops incrementing then it is likely that its associate task has stalled. */
144 volatile unsigned long ulRegTest1Counter = 0, ulRegTest2Counter = 0;
145
146 /*-----------------------------------------------------------*/
147
148 void main_full( void )
149 {
150 TimerHandle_t xTimer = NULL;
151
152         /* Start all the standard demo/test tasks.  These have not particular
153         functionality, but do demonstrate how to use the FreeRTOS API, and test the
154         kernel port. */
155         vStartIntegerMathTasks( tskIDLE_PRIORITY );
156         vStartDynamicPriorityTasks();
157         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
158         vCreateBlockTimeTasks();
159         vStartCountingSemaphoreTasks();
160         vStartGenericQueueTasks( tskIDLE_PRIORITY );
161         vStartRecursiveMutexTasks();
162         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
163         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
164         vStartMathTasks( mainFLOP_TASK_PRIORITY );
165         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
166
167         /* Create the register test tasks, as described at the top of this file. */
168         xTaskCreate( vRegTestTask1, "Reg1...", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
169         xTaskCreate( vRegTestTask2, "Reg2...", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
170
171
172         /* Create the software timer that performs the 'check' functionality,
173         as described at the top of this file. */
174         xTimer = xTimerCreate( "CheckTimer",                                    /* A text name, purely to help debugging. */
175                                                         ( mainCHECK_TIMER_PERIOD_MS ),  /* The timer period, in this case 3000ms (3s). */
176                                                         pdTRUE,                                                 /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
177                                                         ( void * ) 0,                                   /* The ID is not used, so can be set to anything. */
178                                                         prvCheckTimerCallback                   /* The callback function that inspects the status of all the other tasks. */
179                                                  );
180
181         if( xTimer != NULL )
182         {
183                 xTimerStart( xTimer, mainDONT_BLOCK );
184         }
185
186         /* Create the software timer that performs the 'LED spin' functionality,
187         as described at the top of this file. */
188         xTimer = xTimerCreate( "LEDTimer",                                      /* A text name, purely to help debugging. */
189                                                         ( mainLED_TIMER_PERIOD_MS ),/* The timer period, in this case 75ms. */
190                                                         pdTRUE,                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
191                                                         ( void * ) 0,                           /* The ID is not used, so can be set to anything. */
192                                                         prvLEDTimerCallback                     /* The callback function that toggles the white LEDs. */
193                                                  );
194
195         if( xTimer != NULL )
196         {
197                 xTimerStart( xTimer, mainDONT_BLOCK );
198         }
199
200         /* The set of tasks created by the following function call have to be
201         created last as they keep account of the number of tasks they expect to see
202         running. */
203         vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
204
205         /* Start the scheduler. */
206         vTaskStartScheduler();
207
208         /* If all is well, the scheduler will now be running, and the following line
209         will never be reached.  If the following line does execute, then there was
210         insufficient FreeRTOS heap memory available for the idle and/or timer tasks
211         to be created.  See the memory management section on the FreeRTOS web site
212         for more details. */
213         for( ;; );
214 }
215 /*-----------------------------------------------------------*/
216
217 static void prvCheckTimerCallback( TimerHandle_t xTimer )
218 {
219 static long lChangeToRedLEDsAlready = pdFALSE;
220 static unsigned long ulLastRegTest1Counter = 0, ulLastRegTest2Counter = 0;
221 unsigned long ulErrorFound = pdFALSE;
222 /* LEDs are defaulted to use the Green LEDs.  The Red LEDs are used if an error
223 is found. */
224 static unsigned long ulLED1 = 8, ulLED2 = 11;
225 const unsigned long ulRedLED1 = 6, ulRedLED2 = 9;
226
227         /* Check all the demo tasks (other than the flash tasks) to ensure
228         they are all still running, and that none have detected an error. */
229
230         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
231         {
232                 ulErrorFound = pdTRUE;
233         }
234
235         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
236         {
237                 ulErrorFound = pdTRUE;
238         }
239
240         if( xAreBlockingQueuesStillRunning() != pdTRUE )
241         {
242                 ulErrorFound = pdTRUE;
243         }
244
245         if ( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
246         {
247                 ulErrorFound = pdTRUE;
248         }
249
250         if ( xAreGenericQueueTasksStillRunning() != pdTRUE )
251         {
252                 ulErrorFound = pdTRUE;
253         }
254
255         if ( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
256         {
257                 ulErrorFound = pdTRUE;
258         }
259
260         if( xIsCreateTaskStillRunning() != pdTRUE )
261         {
262                 ulErrorFound = pdTRUE;
263         }
264
265         if( xArePollingQueuesStillRunning() != pdTRUE )
266         {
267                 ulErrorFound = pdTRUE;
268         }
269
270         if( xAreSemaphoreTasksStillRunning() != pdTRUE )
271         {
272                 ulErrorFound = pdTRUE;
273         }
274
275         if( xAreMathsTaskStillRunning() != pdTRUE )
276         {
277                 ulErrorFound = pdTRUE;
278         }
279
280         if( xAreComTestTasksStillRunning() != pdTRUE )
281         {
282                 ulErrorFound = pdTRUE;
283         }
284
285         /* Check the reg test tasks are still cycling.  They will stop
286         incrementing their loop counters if they encounter an error. */
287         if( ulRegTest1Counter == ulLastRegTest1Counter )
288         {
289                 ulErrorFound = pdTRUE;
290         }
291
292         if( ulRegTest2Counter == ulLastRegTest2Counter )
293         {
294                 ulErrorFound = pdTRUE;
295         }
296
297         ulLastRegTest1Counter = ulRegTest1Counter;
298         ulLastRegTest2Counter = ulRegTest2Counter;
299
300         /* Toggle the check LEDs to give an indication of the system status.  If
301         the green LEDs are toggling, then no errors have been detected.  If the red
302         LEDs are toggling, then an error has been reported in at least one task. */
303         vParTestToggleLED( ulLED1 );
304         vParTestToggleLED( ulLED2 );
305
306         /* Have any errors been latch in ulErrorFound?  If so, ensure the gree LEDs
307         are off, then switch to using the red LEDs. */
308         if( ulErrorFound != pdFALSE )
309         {
310                 if( lChangeToRedLEDsAlready == pdFALSE )
311                 {
312                         lChangeToRedLEDsAlready = pdTRUE;
313
314                         /* An error has been found.  Switch to use the red LEDs. */
315                         vParTestSetLED( ulLED1, pdFALSE );
316                         vParTestSetLED( ulLED2, pdFALSE );
317                         ulLED1 = ulRedLED1;
318                         ulLED2 = ulRedLED2;
319                 }
320         }
321 }
322 /*-----------------------------------------------------------*/
323
324 static void prvLEDTimerCallback( TimerHandle_t xTimer )
325 {
326 const unsigned long ulNumWhiteLEDs = 6;
327 static unsigned long ulLit1 = 2, ulLit2 = 1;
328
329         vParTestSetLED( ulLit2, pdFALSE );
330
331         ulLit2 = ulLit1;
332         ulLit1++;
333
334         if( ulLit1 >= ulNumWhiteLEDs )
335         {
336                 ulLit1 = 0;
337         }
338
339         vParTestSetLED( ulLit1, pdTRUE );
340 }
341 /*-----------------------------------------------------------*/
342