]> begriffs open source - cmsis-freertos/blob - Demo/ColdFire_MCF52221_CodeWarrior/sources/main.c
Update README.md - branch main is now the base branch
[cmsis-freertos] / Demo / ColdFire_MCF52221_CodeWarrior / sources / main.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 /*
30  * Creates all the demo application tasks, then starts the scheduler.  The WEB
31  * documentation provides more details of the standard demo application tasks.
32  * In addition to the standard demo tasks, the following tasks and tests are
33  * defined and/or created within this file:
34  *
35  * "Check" task -  This only executes every five seconds but has a high priority
36  * to ensure it gets processor time.  Its main function is to check that all the
37  * standard demo tasks are still operational.  While no errors have been
38  * discovered the check task will toggle an LED every 5 seconds - the toggle
39  * rate increasing to 500ms being a visual indication that at least one task has
40  * reported unexpected behaviour.
41  *
42  * "Reg test" tasks - These fill the registers with known values, then check
43  * that each register still contains its expected value.  Each task uses
44  * different values.  The tasks run with very low priority so get preempted very
45  * frequently.  A register containing an unexpected value is indicative of an
46  * error in the context switching mechanism.
47  *
48  */
49
50 /* Standard includes. */
51 #include <stdio.h>
52
53 /* Scheduler includes. */
54 #include "FreeRTOS.h"
55 #include "task.h"
56 #include "queue.h"
57 #include "semphr.h"
58
59 /* Demo app includes. */
60 #include "BlockQ.h"
61 #include "crflash.h"
62 #include "partest.h"
63 #include "semtest.h"
64 #include "GenQTest.h"
65 #include "QPeek.h"
66 #include "comtest2.h"
67
68 /*-----------------------------------------------------------*/
69
70 /* The time between cycles of the 'check' functionality - as described at the
71 top of this file. */
72 #define mainNO_ERROR_PERIOD                                     ( ( TickType_t ) 5000 / portTICK_PERIOD_MS )
73
74 /* The rate at which the LED controlled by the 'check' task will flash should an
75 error have been detected. */
76 #define mainERROR_PERIOD                                        ( ( TickType_t ) 500 / portTICK_PERIOD_MS )
77
78 /* The LED controlled by the 'check' task. */
79 #define mainCHECK_LED                                           ( 3 )
80
81 /* ComTest constants - there is no free LED for the comtest tasks. */
82 #define mainCOM_TEST_BAUD_RATE                          ( ( unsigned long ) 19200 )
83 #define mainCOM_TEST_LED                                        ( 5 )
84
85 /* Task priorities. */
86 #define mainCOM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 2 )
87 #define mainCHECK_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 3 )
88 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )
89 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )
90 #define mainGEN_QUEUE_TASK_PRIORITY                     ( tskIDLE_PRIORITY )
91
92 /* Co-routines are used to flash the LEDs. */
93 #define mainNUM_FLASH_CO_ROUTINES                       ( 3 )
94
95 /* The baud rate used by the comtest tasks. */
96 #define mainBAUD_RATE                                           ( 38400 )
97
98 /* There is no spare LED for the comtest tasks, so this is set to an invalid
99 number. */
100 #define mainCOM_LED                                                     ( 4 )
101
102 /*
103  * Configure the hardware for the demo.
104  */
105 static void prvSetupHardware( void );
106
107 /*
108  * Implements the 'check' task functionality as described at the top of this
109  * file.
110  */
111 static void prvCheckTask( void *pvParameters );
112
113 /*
114  * Implement the 'Reg test' functionality as described at the top of this file.
115  */
116 static void vRegTest1Task( void *pvParameters );
117 static void vRegTest2Task( void *pvParameters );
118
119 /*-----------------------------------------------------------*/
120
121 /* Counters used to detect errors within the reg test tasks. */
122 static volatile unsigned long ulRegTest1Counter = 0x11111111, ulRegTest2Counter = 0x22222222;
123
124 /*-----------------------------------------------------------*/
125
126 int main( void )
127 {
128         /* Setup the hardware ready for this demo. */
129         prvSetupHardware();
130
131         /* Start the standard demo tasks. */
132         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
133         vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
134         vStartQueuePeekTasks();
135         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
136         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainBAUD_RATE, mainCOM_LED );
137
138         /* For demo purposes use some co-routines to flash the LEDs. */
139         vStartFlashCoRoutines( mainNUM_FLASH_CO_ROUTINES );
140
141         /* Create the check task. */
142         xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
143
144
145         /* Start the reg test tasks - defined in this file. */
146         xTaskCreate( vRegTest1Task, "Reg1", configMINIMAL_STACK_SIZE, ( void * ) &ulRegTest1Counter, tskIDLE_PRIORITY, NULL );
147         xTaskCreate( vRegTest2Task, "Reg2", configMINIMAL_STACK_SIZE, ( void * ) &ulRegTest2Counter, tskIDLE_PRIORITY, NULL );
148
149         /* Start the scheduler. */
150         vTaskStartScheduler();
151
152     /* Will only get here if there was insufficient memory to create the idle
153     task. */
154         for( ;; )
155         {
156         }
157 }
158 /*-----------------------------------------------------------*/
159
160 static void prvCheckTask( void *pvParameters )
161 {
162 unsigned long ulTicksToWait = mainNO_ERROR_PERIOD, ulError = 0, ulLastRegTest1Count = 0, ulLastRegTest2Count = 0;
163 TickType_t xLastExecutionTime;
164 volatile unsigned portBASE_TYPE uxUnusedStack;
165
166         ( void ) pvParameters;
167
168         /* Initialise the variable used to control our iteration rate prior to
169         its first use. */
170         xLastExecutionTime = xTaskGetTickCount();
171
172         for( ;; )
173         {
174                 /* Wait until it is time to run the tests again. */
175                 vTaskDelayUntil( &xLastExecutionTime, ulTicksToWait );
176
177                 /* Has an error been found in any task? */
178                 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
179                 {
180                         ulError |= 0x01UL;
181                 }
182
183                 if( xAreQueuePeekTasksStillRunning() != pdTRUE )
184                 {
185                         ulError |= 0x02UL;
186                 }
187
188                 if( xAreBlockingQueuesStillRunning() != pdTRUE )
189                 {
190                         ulError |= 0x04UL;
191                 }
192
193                 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
194             {
195                 ulError |= 0x20UL;
196             }
197
198             if( xAreComTestTasksStillRunning() != pdTRUE )
199             {
200                 ulError |= 0x40UL;
201             }
202
203                 if( ulLastRegTest1Count == ulRegTest1Counter )
204                 {
205                         ulError |= 0x1000UL;
206                 }
207
208                 if( ulLastRegTest2Count == ulRegTest2Counter )
209                 {
210                         ulError |= 0x1000UL;
211                 }
212
213                 ulLastRegTest1Count = ulRegTest1Counter;
214                 ulLastRegTest2Count = ulRegTest2Counter;
215
216                 /* If an error has been found then increase our cycle rate, and in so
217                 doing increase the rate at which the check task LED toggles. */
218                 if( ulError != 0 )
219                 {
220                 ulTicksToWait = mainERROR_PERIOD;
221                 }
222
223                 /* Toggle the LED each iteration. */
224                 vParTestToggleLED( mainCHECK_LED );
225
226                 /* For demo only - how much unused stack does this task have? */
227                 uxUnusedStack = uxTaskGetStackHighWaterMark( NULL );
228         }
229 }
230 /*-----------------------------------------------------------*/
231
232 void prvSetupHardware( void )
233 {
234         portDISABLE_INTERRUPTS();
235
236         /* Setup the port used to toggle LEDs. */
237         vParTestInitialise();
238 }
239 /*-----------------------------------------------------------*/
240
241 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
242 {
243         /* This will get called if a stack overflow is detected during the context
244         switch.  Set configCHECK_FOR_STACK_OVERFLOWS to 2 to also check for stack
245         problems within nested interrupts, but only do this for debug purposes as
246         it will increase the context switch time. */
247
248         ( void ) pxTask;
249         ( void ) pcTaskName;
250
251         for( ;; )
252         {
253         }
254 }
255 /*-----------------------------------------------------------*/
256
257 void vApplicationIdleHook( void );
258 void vApplicationIdleHook( void )
259 {
260         /* The co-routines run in the idle task. */
261         vCoRoutineSchedule();
262 }
263 /*-----------------------------------------------------------*/
264
265 void exit( int n )
266 {
267         /* To keep the linker happy only as the libraries have been removed from
268         the build. */
269         ( void ) n;
270         for( ;; ) {}
271 }
272 /*-----------------------------------------------------------*/
273
274 static void vRegTest1Task( void *pvParameters )
275 {
276         /* Sanity check - did we receive the parameter expected? */
277         if( pvParameters != &ulRegTest1Counter )
278         {
279                 /* Change here so the check task can detect that an error occurred. */
280                 for( ;; )
281                 {
282                 }
283         }
284
285         /* Set all the registers to known values, then check that each retains its
286         expected value - as described at the top of this file.  If an error is
287         found then the loop counter will no longer be incremented allowing the check
288         task to recognise the error. */
289         asm volatile    (       "reg_test_1_start:                                              \n\t"
290                                                 "       moveq           #1, d0                                  \n\t"
291                                                 "       moveq           #2, d1                                  \n\t"
292                                                 "       moveq           #3, d2                                  \n\t"
293                                                 "       moveq           #4, d3                                  \n\t"
294                                                 "       moveq           #5, d4                                  \n\t"
295                                                 "       moveq           #6, d5                                  \n\t"
296                                                 "       moveq           #7, d6                                  \n\t"
297                                                 "       moveq           #8, d7                                  \n\t"
298                                                 "       move            #9, a0                                  \n\t"
299                                                 "       move            #10, a1                                 \n\t"
300                                                 "       move            #11, a2                                 \n\t"
301                                                 "       move            #12, a3                                 \n\t"
302                                                 "       move            #13, a4                                 \n\t"
303                                                 "       move            #14, a5                                 \n\t"
304                                                 "       move            #15, a6                                 \n\t"
305                                                 "                                                                               \n\t"
306                                                 "       cmpi.l          #1, d0                                  \n\t"
307                                                 "       bne                     reg_test_1_error                \n\t"
308                                                 "       cmpi.l          #2, d1                                  \n\t"
309                                                 "       bne                     reg_test_1_error                \n\t"
310                                                 "       cmpi.l          #3, d2                                  \n\t"
311                                                 "       bne                     reg_test_1_error                \n\t"
312                                                 "       cmpi.l          #4, d3                                  \n\t"
313                                                 "       bne                     reg_test_1_error                \n\t"
314                                                 "       cmpi.l          #5, d4                                  \n\t"
315                                                 "       bne                     reg_test_1_error                \n\t"
316                                                 "       cmpi.l          #6, d5                                  \n\t"
317                                                 "       bne                     reg_test_1_error                \n\t"
318                                                 "       cmpi.l          #7, d6                                  \n\t"
319                                                 "       bne                     reg_test_1_error                \n\t"
320                                                 "       cmpi.l          #8, d7                                  \n\t"
321                                                 "       bne                     reg_test_1_error                \n\t"
322                                                 "       move            a0, d0                                  \n\t"
323                                                 "       cmpi.l          #9, d0                                  \n\t"
324                                                 "       bne                     reg_test_1_error                \n\t"
325                                                 "       move            a1, d0                                  \n\t"
326                                                 "       cmpi.l          #10, d0                                 \n\t"
327                                                 "       bne                     reg_test_1_error                \n\t"
328                                                 "       move            a2, d0                                  \n\t"
329                                                 "       cmpi.l          #11, d0                                 \n\t"
330                                                 "       bne                     reg_test_1_error                \n\t"
331                                                 "       move            a3, d0                                  \n\t"
332                                                 "       cmpi.l          #12, d0                                 \n\t"
333                                                 "       bne                     reg_test_1_error                \n\t"
334                                                 "       move            a4, d0                                  \n\t"
335                                                 "       cmpi.l          #13, d0                                 \n\t"
336                                                 "       bne                     reg_test_1_error                \n\t"
337                                                 "       move            a5, d0                                  \n\t"
338                                                 "       cmpi.l          #14, d0                                 \n\t"
339                                                 "       bne                     reg_test_1_error                \n\t"
340                                                 "       move            a6, d0                                  \n\t"
341                                                 "       cmpi.l          #15, d0                                 \n\t"
342                                                 "       bne                     reg_test_1_error                \n\t"
343                                                 "       move            ulRegTest1Counter, d0   \n\t"
344                                                 "       addq            #1, d0                                  \n\t"
345                                                 "       move            d0, ulRegTest1Counter   \n\t"
346                                                 "       bra                     reg_test_1_start                \n\t"
347                                                 "reg_test_1_error:                                              \n\t"
348                                                 "       bra                     reg_test_1_error                \n\t"
349                                         );
350 }
351 /*-----------------------------------------------------------*/
352
353 static void vRegTest2Task( void *pvParameters )
354 {
355         /* Sanity check - did we receive the parameter expected? */
356         if( pvParameters != &ulRegTest2Counter )
357         {
358                 /* Change here so the check task can detect that an error occurred. */
359                 for( ;; )
360                 {
361                 }
362         }
363
364         /* Set all the registers to known values, then check that each retains its
365         expected value - as described at the top of this file.  If an error is
366         found then the loop counter will no longer be incremented allowing the check
367         task to recognise the error. */
368         asm volatile    (       "reg_test_2_start:                                              \n\t"
369                                                 "       moveq           #10, d0                                 \n\t"
370                                                 "       moveq           #20, d1                                 \n\t"
371                                                 "       moveq           #30, d2                                 \n\t"
372                                                 "       moveq           #40, d3                                 \n\t"
373                                                 "       moveq           #50, d4                                 \n\t"
374                                                 "       moveq           #60, d5                                 \n\t"
375                                                 "       moveq           #70, d6                                 \n\t"
376                                                 "       moveq           #80, d7                                 \n\t"
377                                                 "       move            #90, a0                                 \n\t"
378                                                 "       move            #100, a1                                \n\t"
379                                                 "       move            #110, a2                                \n\t"
380                                                 "       move            #120, a3                                \n\t"
381                                                 "       move            #130, a4                                \n\t"
382                                                 "       move            #140, a5                                \n\t"
383                                                 "       move            #150, a6                                \n\t"
384                                                 "                                                                               \n\t"
385                                                 "       cmpi.l          #10, d0                                 \n\t"
386                                                 "       bne                     reg_test_2_error                \n\t"
387                                                 "       cmpi.l          #20, d1                                 \n\t"
388                                                 "       bne                     reg_test_2_error                \n\t"
389                                                 "       cmpi.l          #30, d2                                 \n\t"
390                                                 "       bne                     reg_test_2_error                \n\t"
391                                                 "       cmpi.l          #40, d3                                 \n\t"
392                                                 "       bne                     reg_test_2_error                \n\t"
393                                                 "       cmpi.l          #50, d4                                 \n\t"
394                                                 "       bne                     reg_test_2_error                \n\t"
395                                                 "       cmpi.l          #60, d5                                 \n\t"
396                                                 "       bne                     reg_test_2_error                \n\t"
397                                                 "       cmpi.l          #70, d6                                 \n\t"
398                                                 "       bne                     reg_test_2_error                \n\t"
399                                                 "       cmpi.l          #80, d7                                 \n\t"
400                                                 "       bne                     reg_test_2_error                \n\t"
401                                                 "       move            a0, d0                                  \n\t"
402                                                 "       cmpi.l          #90, d0                                 \n\t"
403                                                 "       bne                     reg_test_2_error                \n\t"
404                                                 "       move            a1, d0                                  \n\t"
405                                                 "       cmpi.l          #100, d0                                \n\t"
406                                                 "       bne                     reg_test_2_error                \n\t"
407                                                 "       move            a2, d0                                  \n\t"
408                                                 "       cmpi.l          #110, d0                                \n\t"
409                                                 "       bne                     reg_test_2_error                \n\t"
410                                                 "       move            a3, d0                                  \n\t"
411                                                 "       cmpi.l          #120, d0                                \n\t"
412                                                 "       bne                     reg_test_2_error                \n\t"
413                                                 "       move            a4, d0                                  \n\t"
414                                                 "       cmpi.l          #130, d0                                \n\t"
415                                                 "       bne                     reg_test_2_error                \n\t"
416                                                 "       move            a5, d0                                  \n\t"
417                                                 "       cmpi.l          #140, d0                                \n\t"
418                                                 "       bne                     reg_test_2_error                \n\t"
419                                                 "       move            a6, d0                                  \n\t"
420                                                 "       cmpi.l          #150, d0                                \n\t"
421                                                 "       bne                     reg_test_2_error                \n\t"
422                                                 "       move            ulRegTest1Counter, d0   \n\t"
423                                                 "       addq            #1, d0                                  \n\t"
424                                                 "       move            d0, ulRegTest2Counter   \n\t"
425                                                 "       bra                     reg_test_2_start                \n\t"
426                                                 "reg_test_2_error:                                              \n\t"
427                                                 "       bra                     reg_test_2_error                \n\t"
428                                         );
429 }
430 /*-----------------------------------------------------------*/
431
432
433