]> begriffs open source - cmsis-freertos/blob - Demo/HCS12_CodeWarrior_banked/main.c
Update cmsis_os2.c
[cmsis-freertos] / Demo / HCS12_CodeWarrior_banked / main.c
1
2 /*
3     FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
4     All rights reserved
5
6     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
7
8     This file is part of the FreeRTOS distribution.
9
10     FreeRTOS is free software; you can redistribute it and/or modify it under
11     the terms of the GNU General Public License (version 2) as published by the
12     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
13
14     ***************************************************************************
15     >>!   NOTE: The modification to the GPL is included to allow you to     !<<
16     >>!   distribute a combined work that includes FreeRTOS without being   !<<
17     >>!   obliged to provide the source code for proprietary components     !<<
18     >>!   outside of the FreeRTOS kernel.                                   !<<
19     ***************************************************************************
20
21     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
22     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
23     FOR A PARTICULAR PURPOSE.  Full license text is available on the following
24     link: http://www.freertos.org/a00114.html
25
26     ***************************************************************************
27      *                                                                       *
28      *    FreeRTOS provides completely free yet professionally developed,    *
29      *    robust, strictly quality controlled, supported, and cross          *
30      *    platform software that is more than just the market leader, it     *
31      *    is the industry's de facto standard.                               *
32      *                                                                       *
33      *    Help yourself get started quickly while simultaneously helping     *
34      *    to support the FreeRTOS project by purchasing a FreeRTOS           *
35      *    tutorial book, reference manual, or both:                          *
36      *    http://www.FreeRTOS.org/Documentation                              *
37      *                                                                       *
38     ***************************************************************************
39
40     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading
41     the FAQ page "My application does not run, what could be wrong?".  Have you
42     defined configASSERT()?
43
44     http://www.FreeRTOS.org/support - In return for receiving this top quality
45     embedded software for free we request you assist our global community by
46     participating in the support forum.
47
48     http://www.FreeRTOS.org/training - Investing in training allows your team to
49     be as productive as possible as early as possible.  Now you can receive
50     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
51     Ltd, and the world's leading authority on the world's leading RTOS.
52
53     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
54     including FreeRTOS+Trace - an indispensable productivity tool, a DOS
55     compatible FAT file system, and our tiny thread aware UDP/IP stack.
56
57     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
58     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
59
60     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
61     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS
62     licenses offer ticketed support, indemnification and commercial middleware.
63
64     http://www.SafeRTOS.com - High Integrity Systems also provide a safety
65     engineered and independently SIL3 certified version for use in safety and
66     mission critical applications that require provable dependability.
67
68     1 tab == 4 spaces!
69 */
70
71
72 /*
73  *
74  * vMain() is effectively the demo application entry point.  It is called by
75  * the main() function generated by the Processor Expert application.  
76  *
77  * vMain() creates all the demo application tasks, then starts the scheduler.
78  * The WEB      documentation provides more details of the demo application tasks.
79  *
80  * Main.c also creates a task called "Check".  This only executes every three 
81  * seconds but has the highest priority so is guaranteed to get processor time.  
82  * Its main function is to check that all the other tasks are still operational.
83  * Each task (other than the "flash" tasks) maintains a unique count that is 
84  * incremented each time the task successfully completes its function.  Should 
85  * any error occur within such a task the count is permanently halted.  The 
86  * check task inspects the count of each task to ensure it has changed since
87  * the last time the check task executed.  If all the count variables have 
88  * changed all the tasks are still executing error free, and the check task
89  * toggles the onboard LED.  Should any task contain an error at any time 
90  * the LED toggle rate will change from 3 seconds to 500ms.
91  *
92  * This file also includes the functionality implemented within the 
93  * standard demo application file integer.c.  This is done to demonstrate the
94  * use of an idle hook.  See the documentation within integer.c for the 
95  * rationale of the integer task functionality.
96  * */
97
98 /* Kernel includes. */
99 #include "FreeRTOS.h"
100 #include "task.h"
101 #include "queue.h"
102
103 /* Demo application includes. */
104 #include "flash.h"
105 #include "PollQ.h"
106 #include "dynamic.h"
107 #include "partest.h"
108 #include "comtest2.h"
109 #include "BlockQ.h"
110 #include "integer.h"
111 #include "death.h"
112
113
114 /*-----------------------------------------------------------
115         Definitions.
116 -----------------------------------------------------------*/
117
118 /* Priorities assigned to demo application tasks. */
119 #define mainFLASH_PRIORITY                      ( tskIDLE_PRIORITY + 2 )
120 #define mainCHECK_TASK_PRIORITY         ( tskIDLE_PRIORITY + 3 )
121 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 1 )
122 #define mainCOM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 2 )
123 #define mainBLOCK_Q_PRIORITY            ( tskIDLE_PRIORITY + 2 )
124 #define mainDEATH_PRIORITY                      ( tskIDLE_PRIORITY + 1 )
125
126 /* LED that is toggled by the check task.  The check task periodically checks
127 that all the other tasks are operating without error.  If no errors are found
128 the LED is toggled with mainCHECK_PERIOD frequency.  If an error is found 
129 then the toggle rate increases to mainERROR_CHECK_PERIOD. */
130 #define mainCHECK_TASK_LED                      ( 7 )
131 #define mainCHECK_PERIOD                        ( ( TickType_t ) 3000 / portTICK_PERIOD_MS  )
132 #define mainERROR_CHECK_PERIOD          ( ( TickType_t ) 500 / portTICK_PERIOD_MS )
133
134 /* The constants used in the idle task calculation. */
135 #define intgCONST1                              ( ( long ) 123 )
136 #define intgCONST2                              ( ( long ) 234567 )
137 #define intgCONST3                              ( ( long ) -3 )
138 #define intgCONST4                              ( ( long ) 7 )
139 #define intgEXPECTED_ANSWER             ( ( ( intgCONST1 + intgCONST2 ) * intgCONST3 ) / intgCONST4 )
140
141
142 /* Baud rate used by the serial port tasks (ComTest tasks).
143 IMPORTANT:  The function COM0_SetBaudRateValue() which is generated by the
144 Processor Expert is used to set the baud rate.  As configured in the FreeRTOS
145 download this value must be one of the following:
146
147 0 to configure for 38400 baud.
148 1 to configure for 19200 baud.
149 2 to configure for 9600 baud.
150 3 to configure for 4800 baud. */
151 #define mainCOM_TEST_BAUD_RATE                  ( ( unsigned long ) 2 )
152
153 /* LED used by the serial port tasks.  This is toggled on each character Tx,
154 and mainCOM_TEST_LED + 1 is toggles on each character Rx. */
155 #define mainCOM_TEST_LED                                ( 3 )
156
157 /*-----------------------------------------------------------
158         Local functions prototypes.
159 -----------------------------------------------------------*/
160
161 /*
162  * The 'Check' task function.  See the explanation at the top of the file.
163  */
164 static void vErrorChecks( void* pvParameters );
165
166 /*
167  * The idle task hook - in which the integer task is implemented.  See the
168  * explanation at the top of the file.
169  */
170 void vApplicationIdleHook( void );
171
172 /*
173  * Checks the unique counts of other tasks to ensure they are still operational.
174  */
175 static long prvCheckOtherTasksAreStillRunning( void );
176
177
178
179 /*-----------------------------------------------------------
180         Local variables.
181 -----------------------------------------------------------*/
182
183 /* A few tasks are defined within this file.  This flag is used to indicate
184 their status.  If an error is detected in one of the locally defined tasks then
185 this flag is set to pdTRUE. */
186 portBASE_TYPE xLocalError = pdFALSE;
187
188
189 /*-----------------------------------------------------------*/
190
191 /* 
192  * This is called from the main() function generated by the Processor Expert.
193  */
194 void vMain( void )
195 {
196         /* Start some of the standard demo tasks. */
197         vStartLEDFlashTasks( mainFLASH_PRIORITY );
198         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
199         vStartDynamicPriorityTasks();
200         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
201         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
202         vStartIntegerMathTasks( tskIDLE_PRIORITY );
203                 
204         /* Start the locally defined tasks.  There is also a task implemented as
205         the idle hook. */
206         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
207         
208         /* Must be the last demo created. */
209         vCreateSuicidalTasks( mainDEATH_PRIORITY );
210
211         /* All the tasks have been created - start the scheduler. */
212         vTaskStartScheduler();
213         
214         /* Should not reach here! */
215         for( ;; );
216 }
217 /*-----------------------------------------------------------*/
218
219 static void vErrorChecks( void *pvParameters )
220 {
221 TickType_t xDelayPeriod = mainCHECK_PERIOD;
222 TickType_t xLastWakeTime;
223
224         /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()
225         functions correctly. */
226         xLastWakeTime = xTaskGetTickCount();
227
228         for( ;; )
229         {
230                 /* Delay until it is time to execute again.  The delay period is 
231                 shorter following an error. */
232                 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );
233
234                 /* Check all the demo application tasks are executing without 
235                 error. If an error is found the delay period is shortened - this
236                 has the effect of increasing the flash rate of the 'check' task
237                 LED. */
238                 if( prvCheckOtherTasksAreStillRunning() == pdFAIL )
239                 {
240                         /* An error has been detected in one of the tasks - flash faster. */
241                         xDelayPeriod = mainERROR_CHECK_PERIOD;
242                 }
243
244                 /* Toggle the LED each cycle round. */
245                 vParTestToggleLED( mainCHECK_TASK_LED );
246         }
247 }
248 /*-----------------------------------------------------------*/
249
250 static long prvCheckOtherTasksAreStillRunning( void )
251 {
252 portBASE_TYPE xAllTasksPassed = pdPASS;
253
254         if( xArePollingQueuesStillRunning() != pdTRUE )
255         {
256                 xAllTasksPassed = pdFAIL;
257         }
258
259         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
260         {
261                 xAllTasksPassed = pdFAIL;
262         }
263
264         if( xAreComTestTasksStillRunning() != pdTRUE )
265         {
266                 xAllTasksPassed = pdFALSE;
267         }
268
269         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
270         {
271                 xAllTasksPassed = pdFALSE;
272         }
273         
274         if( xAreBlockingQueuesStillRunning() != pdTRUE )
275         {
276                 xAllTasksPassed = pdFALSE;
277         }       
278
279     if( xIsCreateTaskStillRunning() != pdTRUE )
280     {
281         xAllTasksPassed = pdFALSE;
282     }
283
284         /* Also check the status flag for the tasks defined within this function. */
285         if( xLocalError != pdFALSE )
286         {
287                 xAllTasksPassed = pdFAIL;
288         }
289         
290         return xAllTasksPassed;
291 }
292 /*-----------------------------------------------------------*/
293
294 void vApplicationIdleHook( void )
295 {
296 /* This variable is effectively set to a constant so it is made volatile to
297 ensure the compiler does not just get rid of it. */
298 volatile long lValue;
299
300         /* Keep performing a calculation and checking the result against a constant. */
301
302         /* Perform the calculation.  This will store partial value in
303         registers, resulting in a good test of the context switch mechanism. */
304         lValue = intgCONST1;
305         lValue += intgCONST2;
306         lValue *= intgCONST3;
307         lValue /= intgCONST4;
308
309         /* Did we perform the calculation correctly with no corruption? */
310         if( lValue != intgEXPECTED_ANSWER )
311         {
312                 /* Error! */
313                 portENTER_CRITICAL();
314                         xLocalError = pdTRUE;
315                 portEXIT_CRITICAL();
316         }
317
318         /* Yield in case cooperative scheduling is being used. */
319         #if configUSE_PREEMPTION == 0
320         {
321                 taskYIELD();
322         }
323         #endif          
324 }
325 /*-----------------------------------------------------------*/
326
327
328