]> begriffs open source - cmsis-freertos/blob - Demo/HCS12_GCC_banked/main.c
there was an extra ')'... caused build to fail
[cmsis-freertos] / Demo / HCS12_GCC_banked / main.c
1
2 /*
3  * FreeRTOS Kernel V10.1.1
4  * Copyright (C) 2018 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  * main() creates all the demo application tasks, then starts the scheduler.
33  * The WEB      documentation provides more details of the demo application tasks.
34  *
35  * main.c also creates a task called "Check".  This only executes every three 
36  * seconds but has the highest priority so is guaranteed to get processor time.  
37  * Its main function is to check that all the other tasks are still operational.
38  * Each task (other than the "flash" tasks) maintains a unique count that is 
39  * incremented each time the task successfully completes its function.  Should 
40  * any error occur within such a task the count is permanently halted.  The 
41  * check task inspects the count of each task to ensure it has changed since
42  * the last time the check task executed.  If all the count variables have 
43  * changed all the tasks are still executing error free, and the check task
44  * toggles the onboard LED.  Should any task contain an error at any time 
45  * the LED toggle rate will change from 3 seconds to 500ms.
46  *
47  * This file also includes the functionality implemented within the 
48  * standard demo application file integer.c.  This is done to demonstrate the
49  * use of an idle hook.  See the documentation within integer.c for the 
50  * rationale of the integer task functionality.
51  * */
52
53 /* Kernel includes. */
54 #include "FreeRTOS.h"
55 #include "task.h"
56 #include "queue.h"
57
58 #include "cpu.h"
59
60 /* special prototypes for memory-banked functions */
61 void vStartPolledQueueTasks( unsigned portBASE_TYPE uxPriority );
62 portBASE_TYPE xArePollingQueuesStillRunning( void );
63
64 /* Demo application includes. */
65 #include "flash.h"
66 #include "PollQ.h"
67 #include "dynamic.h"
68 #include "partest.h"
69 #include "comtest2.h"
70 #include "BlockQ.h"
71 #include "integer.h"
72 #include "death.h"
73
74
75 /*-----------------------------------------------------------
76         Definitions.
77 -----------------------------------------------------------*/
78
79 /* Priorities assigned to demo application tasks. */
80 #define mainFLASH_PRIORITY                      ( tskIDLE_PRIORITY + 2 )
81 #define mainCHECK_TASK_PRIORITY         ( tskIDLE_PRIORITY + 3 )
82 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 1 )
83 #define mainCOM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 2 )
84 #define mainBLOCK_Q_PRIORITY            ( tskIDLE_PRIORITY + 2 )
85 #define mainDEATH_PRIORITY                      ( tskIDLE_PRIORITY + 1 )
86
87 /* LED that is toggled by the check task.  The check task periodically checks
88 that all the other tasks are operating without error.  If no errors are found
89 the LED is toggled with mainCHECK_PERIOD frequency.  If an error is found 
90 then the toggle rate increases to mainERROR_CHECK_PERIOD. */
91 #define mainCHECK_TASK_LED                      ( 7 )
92 #define mainCHECK_PERIOD                        ( ( TickType_t ) 3000 / portTICK_PERIOD_MS  )
93 #define mainERROR_CHECK_PERIOD          ( ( TickType_t ) 500 / portTICK_PERIOD_MS )
94
95 /* The constants used in the idle task calculation. */
96 #define intgCONST1                              ( ( long ) 123 )
97 #define intgCONST2                              ( ( long ) 234567 )
98 #define intgCONST3                              ( ( long ) -3 )
99 #define intgCONST4                              ( ( long ) 7 )
100 #define intgEXPECTED_ANSWER             ( ( ( intgCONST1 + intgCONST2 ) * intgCONST3 ) / intgCONST4 )
101
102
103 /* Baud rate used by the serial port tasks (ComTest tasks).
104 IMPORTANT:  The function COM0_SetBaudRateValue() which is generated by the
105 Processor Expert is used to set the baud rate.  As configured in the FreeRTOS
106 download this value must be one of the following:
107
108 0 to configure for 38400 baud.
109 1 to configure for 19200 baud.
110 2 to configure for 9600 baud.
111 3 to configure for 4800 baud. */
112 #define mainCOM_TEST_BAUD_RATE                  ( ( unsigned long ) 2 )
113
114 /* LED used by the serial port tasks.  This is toggled on each character Tx,
115 and mainCOM_TEST_LED + 1 is toggles on each character Rx. */
116 #define mainCOM_TEST_LED                                ( 3 )
117
118 /*-----------------------------------------------------------
119         Local functions prototypes.
120 -----------------------------------------------------------*/
121
122 /*
123  * The 'Check' task function.  See the explanation at the top of the file.
124  */
125 static void ATTR_BANK1 vErrorChecks( void* pvParameters );
126
127 /*
128  * The idle task hook - in which the integer task is implemented.  See the
129  * explanation at the top of the file.
130  */
131 void ATTR_BANK0 vApplicationIdleHook( void );
132
133 /*
134  * Checks the unique counts of other tasks to ensure they are still operational.
135  */
136 static long ATTR_BANK0 prvCheckOtherTasksAreStillRunning( void );
137
138
139
140 /*-----------------------------------------------------------
141         Local variables.
142 -----------------------------------------------------------*/
143
144 /* A few tasks are defined within this file.  This flag is used to indicate
145 their status.  If an error is detected in one of the locally defined tasks then
146 this flag is set to pdTRUE. */
147 portBASE_TYPE xLocalError = pdFALSE;
148
149
150 /*-----------------------------------------------------------*/
151
152 /* This is called from startup. */
153 int ATTR_BANK0 main ( void )
154 {
155         /* Start some of the standard demo tasks. */
156         vStartLEDFlashTasks( mainFLASH_PRIORITY );
157         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
158         vStartDynamicPriorityTasks();
159         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
160         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
161         vStartIntegerMathTasks( tskIDLE_PRIORITY );
162         
163         /* Start the locally defined tasks.  There is also a task implemented as
164         the idle hook. */
165         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
166         
167         /* Must be the last demo created. */
168         vCreateSuicidalTasks( mainDEATH_PRIORITY );
169
170         /* All the tasks have been created - start the scheduler. */
171         vTaskStartScheduler();
172         
173         /* Should not reach here! */
174         for( ;; );
175         return 0;
176 }
177 /*-----------------------------------------------------------*/
178
179 static void vErrorChecks( void *pvParameters )
180 {
181 TickType_t xDelayPeriod = mainCHECK_PERIOD;
182 TickType_t xLastWakeTime;
183
184         /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()
185         functions correctly. */
186         xLastWakeTime = xTaskGetTickCount();
187
188         for( ;; )
189         {
190                 /* Delay until it is time to execute again.  The delay period is 
191                 shorter following an error. */
192                 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );
193
194                 /* Check all the demo application tasks are executing without 
195                 error. If an error is found the delay period is shortened - this
196                 has the effect of increasing the flash rate of the 'check' task
197                 LED. */
198                 if( prvCheckOtherTasksAreStillRunning() == pdFAIL )
199                 {
200                         /* An error has been detected in one of the tasks - flash faster. */
201                         xDelayPeriod = mainERROR_CHECK_PERIOD;
202                 }
203
204                 /* Toggle the LED each cycle round. */
205                 vParTestToggleLED( mainCHECK_TASK_LED );
206         }
207 }
208 /*-----------------------------------------------------------*/
209
210 static long prvCheckOtherTasksAreStillRunning( void )
211 {
212 portBASE_TYPE xAllTasksPassed = pdPASS;
213
214         if( xArePollingQueuesStillRunning() != pdTRUE )
215         {
216                 xAllTasksPassed = pdFAIL;
217         }
218
219         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
220         {
221                 xAllTasksPassed = pdFAIL;
222         }
223
224         if( xAreComTestTasksStillRunning() != pdTRUE )
225         {
226                 xAllTasksPassed = pdFALSE;
227         }
228
229         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
230         {
231                 xAllTasksPassed = pdFALSE;
232         }
233         
234         if( xAreBlockingQueuesStillRunning() != pdTRUE )
235         {
236                 xAllTasksPassed = pdFALSE;
237         }       
238
239     if( xIsCreateTaskStillRunning() != pdTRUE )
240     {
241         xAllTasksPassed = pdFALSE;
242     }
243
244         /* Also check the status flag for the tasks defined within this function. */
245         if( xLocalError != pdFALSE )
246         {
247                 xAllTasksPassed = pdFAIL;
248         }
249         
250         return xAllTasksPassed;
251 }
252 /*-----------------------------------------------------------*/
253
254 void vApplicationIdleHook( void )
255 {
256 /* This variable is effectively set to a constant so it is made volatile to
257 ensure the compiler does not just get rid of it. */
258 volatile long lValue;
259
260         /* Keep performing a calculation and checking the result against a constant. */
261
262         /* Perform the calculation.  This will store partial value in
263         registers, resulting in a good test of the context switch mechanism. */
264         lValue = intgCONST1;
265         lValue += intgCONST2;
266         lValue *= intgCONST3;
267         lValue /= intgCONST4;
268
269         /* Did we perform the calculation correctly with no corruption? */
270         if( lValue != intgEXPECTED_ANSWER )
271         {
272                 /* Error! */
273                 portENTER_CRITICAL();
274                         xLocalError = pdTRUE;
275                 portEXIT_CRITICAL();
276         }
277
278         /* Yield in case cooperative scheduling is being used. */
279         #if configUSE_PREEMPTION == 0
280         {
281                 taskYIELD();
282         }
283         #endif          
284 }
285 /*-----------------------------------------------------------*/
286