]> begriffs open source - cmsis-freertos/blob - Demo/HCS12_CodeWarrior_banked/main.c
Updated pack to FreeRTOS 10.4.3
[cmsis-freertos] / Demo / HCS12_CodeWarrior_banked / main.c
1
2 /*
3  * FreeRTOS V202104.00
4  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy of
7  * this software and associated documentation files (the "Software"), to deal in
8  * the Software without restriction, including without limitation the rights to
9  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10  * the Software, and to permit persons to whom the Software is furnished to do so,
11  * subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  *
23  * http://www.FreeRTOS.org
24  * http://aws.amazon.com/freertos
25  *
26  * 1 tab == 4 spaces!
27  */
28
29
30 /*
31  *
32  * vMain() is effectively the demo application entry point.  It is called by
33  * the main() function generated by the Processor Expert application.  
34  *
35  * vMain() creates all the demo application tasks, then starts the scheduler.
36  * The WEB      documentation provides more details of the demo application tasks.
37  *
38  * Main.c also creates a task called "Check".  This only executes every three 
39  * seconds but has the highest priority so is guaranteed to get processor time.  
40  * Its main function is to check that all the other tasks are still operational.
41  * Each task (other than the "flash" tasks) maintains a unique count that is 
42  * incremented each time the task successfully completes its function.  Should 
43  * any error occur within such a task the count is permanently halted.  The 
44  * check task inspects the count of each task to ensure it has changed since
45  * the last time the check task executed.  If all the count variables have 
46  * changed all the tasks are still executing error free, and the check task
47  * toggles the onboard LED.  Should any task contain an error at any time 
48  * the LED toggle rate will change from 3 seconds to 500ms.
49  *
50  * This file also includes the functionality implemented within the 
51  * standard demo application file integer.c.  This is done to demonstrate the
52  * use of an idle hook.  See the documentation within integer.c for the 
53  * rationale of the integer task functionality.
54  * */
55
56 /* Kernel includes. */
57 #include "FreeRTOS.h"
58 #include "task.h"
59 #include "queue.h"
60
61 /* Demo application includes. */
62 #include "flash.h"
63 #include "PollQ.h"
64 #include "dynamic.h"
65 #include "partest.h"
66 #include "comtest2.h"
67 #include "BlockQ.h"
68 #include "integer.h"
69 #include "death.h"
70
71
72 /*-----------------------------------------------------------
73         Definitions.
74 -----------------------------------------------------------*/
75
76 /* Priorities assigned to demo application tasks. */
77 #define mainFLASH_PRIORITY                      ( tskIDLE_PRIORITY + 2 )
78 #define mainCHECK_TASK_PRIORITY         ( tskIDLE_PRIORITY + 3 )
79 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 1 )
80 #define mainCOM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 2 )
81 #define mainBLOCK_Q_PRIORITY            ( tskIDLE_PRIORITY + 2 )
82 #define mainDEATH_PRIORITY                      ( tskIDLE_PRIORITY + 1 )
83
84 /* LED that is toggled by the check task.  The check task periodically checks
85 that all the other tasks are operating without error.  If no errors are found
86 the LED is toggled with mainCHECK_PERIOD frequency.  If an error is found 
87 then the toggle rate increases to mainERROR_CHECK_PERIOD. */
88 #define mainCHECK_TASK_LED                      ( 7 )
89 #define mainCHECK_PERIOD                        ( ( TickType_t ) 3000 / portTICK_PERIOD_MS  )
90 #define mainERROR_CHECK_PERIOD          ( ( TickType_t ) 500 / portTICK_PERIOD_MS )
91
92 /* The constants used in the idle task calculation. */
93 #define intgCONST1                              ( ( long ) 123 )
94 #define intgCONST2                              ( ( long ) 234567 )
95 #define intgCONST3                              ( ( long ) -3 )
96 #define intgCONST4                              ( ( long ) 7 )
97 #define intgEXPECTED_ANSWER             ( ( ( intgCONST1 + intgCONST2 ) * intgCONST3 ) / intgCONST4 )
98
99
100 /* Baud rate used by the serial port tasks (ComTest tasks).
101 IMPORTANT:  The function COM0_SetBaudRateValue() which is generated by the
102 Processor Expert is used to set the baud rate.  As configured in the FreeRTOS
103 download this value must be one of the following:
104
105 0 to configure for 38400 baud.
106 1 to configure for 19200 baud.
107 2 to configure for 9600 baud.
108 3 to configure for 4800 baud. */
109 #define mainCOM_TEST_BAUD_RATE                  ( ( unsigned long ) 2 )
110
111 /* LED used by the serial port tasks.  This is toggled on each character Tx,
112 and mainCOM_TEST_LED + 1 is toggles on each character Rx. */
113 #define mainCOM_TEST_LED                                ( 3 )
114
115 /*-----------------------------------------------------------
116         Local functions prototypes.
117 -----------------------------------------------------------*/
118
119 /*
120  * The 'Check' task function.  See the explanation at the top of the file.
121  */
122 static void vErrorChecks( void* pvParameters );
123
124 /*
125  * The idle task hook - in which the integer task is implemented.  See the
126  * explanation at the top of the file.
127  */
128 void vApplicationIdleHook( void );
129
130 /*
131  * Checks the unique counts of other tasks to ensure they are still operational.
132  */
133 static long prvCheckOtherTasksAreStillRunning( void );
134
135
136
137 /*-----------------------------------------------------------
138         Local variables.
139 -----------------------------------------------------------*/
140
141 /* A few tasks are defined within this file.  This flag is used to indicate
142 their status.  If an error is detected in one of the locally defined tasks then
143 this flag is set to pdTRUE. */
144 portBASE_TYPE xLocalError = pdFALSE;
145
146
147 /*-----------------------------------------------------------*/
148
149 /* 
150  * This is called from the main() function generated by the Processor Expert.
151  */
152 void vMain( void )
153 {
154         /* Start some of the standard demo tasks. */
155         vStartLEDFlashTasks( mainFLASH_PRIORITY );
156         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
157         vStartDynamicPriorityTasks();
158         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
159         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
160         vStartIntegerMathTasks( tskIDLE_PRIORITY );
161                 
162         /* Start the locally defined tasks.  There is also a task implemented as
163         the idle hook. */
164         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
165         
166         /* Must be the last demo created. */
167         vCreateSuicidalTasks( mainDEATH_PRIORITY );
168
169         /* All the tasks have been created - start the scheduler. */
170         vTaskStartScheduler();
171         
172         /* Should not reach here! */
173         for( ;; );
174 }
175 /*-----------------------------------------------------------*/
176
177 static void vErrorChecks( void *pvParameters )
178 {
179 TickType_t xDelayPeriod = mainCHECK_PERIOD;
180 TickType_t xLastWakeTime;
181
182         /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()
183         functions correctly. */
184         xLastWakeTime = xTaskGetTickCount();
185
186         for( ;; )
187         {
188                 /* Delay until it is time to execute again.  The delay period is 
189                 shorter following an error. */
190                 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );
191
192                 /* Check all the demo application tasks are executing without 
193                 error. If an error is found the delay period is shortened - this
194                 has the effect of increasing the flash rate of the 'check' task
195                 LED. */
196                 if( prvCheckOtherTasksAreStillRunning() == pdFAIL )
197                 {
198                         /* An error has been detected in one of the tasks - flash faster. */
199                         xDelayPeriod = mainERROR_CHECK_PERIOD;
200                 }
201
202                 /* Toggle the LED each cycle round. */
203                 vParTestToggleLED( mainCHECK_TASK_LED );
204         }
205 }
206 /*-----------------------------------------------------------*/
207
208 static long prvCheckOtherTasksAreStillRunning( void )
209 {
210 portBASE_TYPE xAllTasksPassed = pdPASS;
211
212         if( xArePollingQueuesStillRunning() != pdTRUE )
213         {
214                 xAllTasksPassed = pdFAIL;
215         }
216
217         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
218         {
219                 xAllTasksPassed = pdFAIL;
220         }
221
222         if( xAreComTestTasksStillRunning() != pdTRUE )
223         {
224                 xAllTasksPassed = pdFALSE;
225         }
226
227         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
228         {
229                 xAllTasksPassed = pdFALSE;
230         }
231         
232         if( xAreBlockingQueuesStillRunning() != pdTRUE )
233         {
234                 xAllTasksPassed = pdFALSE;
235         }       
236
237     if( xIsCreateTaskStillRunning() != pdTRUE )
238     {
239         xAllTasksPassed = pdFALSE;
240     }
241
242         /* Also check the status flag for the tasks defined within this function. */
243         if( xLocalError != pdFALSE )
244         {
245                 xAllTasksPassed = pdFAIL;
246         }
247         
248         return xAllTasksPassed;
249 }
250 /*-----------------------------------------------------------*/
251
252 void vApplicationIdleHook( void )
253 {
254 /* This variable is effectively set to a constant so it is made volatile to
255 ensure the compiler does not just get rid of it. */
256 volatile long lValue;
257
258         /* Keep performing a calculation and checking the result against a constant. */
259
260         /* Perform the calculation.  This will store partial value in
261         registers, resulting in a good test of the context switch mechanism. */
262         lValue = intgCONST1;
263         lValue += intgCONST2;
264         lValue *= intgCONST3;
265         lValue /= intgCONST4;
266
267         /* Did we perform the calculation correctly with no corruption? */
268         if( lValue != intgEXPECTED_ANSWER )
269         {
270                 /* Error! */
271                 portENTER_CRITICAL();
272                         xLocalError = pdTRUE;
273                 portEXIT_CRITICAL();
274         }
275
276         /* Yield in case cooperative scheduling is being used. */
277         #if configUSE_PREEMPTION == 0
278         {
279                 taskYIELD();
280         }
281         #endif          
282 }
283 /*-----------------------------------------------------------*/
284
285
286