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