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