]> begriffs open source - cmsis-freertos/blob - Demo/HCS12_CodeWarrior_small/main.c
Update cmsis_os2.c
[cmsis-freertos] / Demo / HCS12_CodeWarrior_small / main.c
1 /*
2     FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
3     All rights reserved
4
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
6
7     This file is part of the FreeRTOS distribution.
8
9     FreeRTOS is free software; you can redistribute it and/or modify it under
10     the terms of the GNU General Public License (version 2) as published by the
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
12
13     ***************************************************************************
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<
16     >>!   obliged to provide the source code for proprietary components     !<<
17     >>!   outside of the FreeRTOS kernel.                                   !<<
18     ***************************************************************************
19
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following
23     link: http://www.freertos.org/a00114.html
24
25     ***************************************************************************
26      *                                                                       *
27      *    FreeRTOS provides completely free yet professionally developed,    *
28      *    robust, strictly quality controlled, supported, and cross          *
29      *    platform software that is more than just the market leader, it     *
30      *    is the industry's de facto standard.                               *
31      *                                                                       *
32      *    Help yourself get started quickly while simultaneously helping     *
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *
34      *    tutorial book, reference manual, or both:                          *
35      *    http://www.FreeRTOS.org/Documentation                              *
36      *                                                                       *
37     ***************************************************************************
38
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading
40     the FAQ page "My application does not run, what could be wrong?".  Have you
41     defined configASSERT()?
42
43     http://www.FreeRTOS.org/support - In return for receiving this top quality
44     embedded software for free we request you assist our global community by
45     participating in the support forum.
46
47     http://www.FreeRTOS.org/training - Investing in training allows your team to
48     be as productive as possible as early as possible.  Now you can receive
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
50     Ltd, and the world's leading authority on the world's leading RTOS.
51
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.
55
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
58
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS
61     licenses offer ticketed support, indemnification and commercial middleware.
62
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety
64     engineered and independently SIL3 certified version for use in safety and
65     mission critical applications that require provable dependability.
66
67     1 tab == 4 spaces!
68 */
69
70
71 /*
72  *
73  * vMain() is effectively the demo application entry point.  It is called by
74  * the main() function generated by the Processor Expert application.  
75  *
76  * vMain() creates all the demo application tasks, then starts the scheduler.
77  * The WEB      documentation provides more details of the demo application tasks.
78  *
79  * Main.c also creates a task called "Check".  This only executes every three 
80  * seconds but has the highest priority so is guaranteed to get processor time.  
81  * Its main function is to check that all the other tasks are still operational.
82  * Each task (other than the "flash" tasks) maintains a unique count that is 
83  * incremented each time the task successfully completes its function.  Should 
84  * any error occur within such a task the count is permanently halted.  The 
85  * check task inspects the count of each task to ensure it has changed since
86  * the last time the check task executed.  If all the count variables have 
87  * changed all the tasks are still executing error free, and the check task
88  * toggles the onboard LED.  Should any task contain an error at any time 
89  * the LED toggle rate will change from 3 seconds to 500ms.
90  *
91  * This file also includes the functionality normally implemented within the 
92  * standard demo application file integer.c.  Due to the limited memory 
93  * available on the microcontroller the functionality has been included within
94  * the idle task hook [vApplicationIdleHook()] - instead of within the usual
95  * separate task.  See the documentation within integer.c for the rationale 
96  * of the integer task functionality.
97  *
98  *
99  * 
100  * The demo applications included with other FreeRTOS ports make use of the
101  * standard ComTest tasks.  These use a loopback connector to transmit and
102  * receive RS232 characters between two tasks.  The test is important for two
103  * reasons:
104  *
105  *      1) It tests the mechanism of context switching from within an application
106  *         ISR.
107  *
108  *      2) It generates some randomised timing.
109  *
110  * The demo board used to develop this port does not include an RS232 interface
111  * so the ComTest tasks could not easily be included.  Instead these two tests
112  * are created using a 'Button Push' task.  
113  * 
114  * The 'Button Push' task blocks on a queue, waiting for data to arrive.  A
115  * simple interrupt routine connected to the PP0 input on the demo board places
116  * data in the queue each time the PP0 button is pushed (this button is built 
117  * onto the demo board).  As the 'Button Push' task is created with a 
118  * relatively high priority it will unblock and want to execute as soon as data
119  * arrives in the queue - resulting in a context switch within the PP0 input
120  * ISR.  If the data retrieved from the queue is that expected the 'Button Push'
121  * task toggles LED 5.  Therefore correct operation is indicated by the LED
122  * toggling each time the PP0 button is pressed.
123  *
124  * This test is not as satisfactory as the ComTest method - but the simple 
125  * nature of the port makes is just about adequate.
126  * 
127  */
128
129 /* Kernel includes. */
130 #include "FreeRTOS.h"
131 #include "task.h"
132 #include "queue.h"
133
134 /* Demo application includes. */
135 #include "flash.h"
136 #include "PollQ.h"
137 #include "dynamic.h"
138 #include "partest.h"
139
140 /* Processor expert includes. */
141 #include "ButtonInterrupt.h"
142
143 /*-----------------------------------------------------------
144         Definitions.
145 -----------------------------------------------------------*/
146
147 /* Priorities assigned to demo application tasks. */
148 #define mainFLASH_PRIORITY                      ( tskIDLE_PRIORITY + 2 )
149 #define mainCHECK_TASK_PRIORITY         ( tskIDLE_PRIORITY + 3 )
150 #define mainBUTTON_TASK_PRIORITY        ( tskIDLE_PRIORITY + 3 )
151 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 2 )
152
153 /* LED that is toggled by the check task.  The check task periodically checks
154 that all the other tasks are operating without error.  If no errors are found
155 the LED is toggled with mainCHECK_PERIOD frequency.  If an error is found 
156 then the toggle rate increases to mainERROR_CHECK_PERIOD. */
157 #define mainCHECK_TASK_LED                      ( 7 )
158 #define mainCHECK_PERIOD                        ( ( TickType_t ) 3000 / portTICK_PERIOD_MS  )
159 #define mainERROR_CHECK_PERIOD          ( ( TickType_t ) 500 / portTICK_PERIOD_MS )
160
161 /* LED that is toggled by the button push interrupt. */
162 #define mainBUTTON_PUSH_LED                     ( 5 )
163
164 /* The constants used in the idle task calculation. */
165 #define intgCONST1                              ( ( long ) 123 )
166 #define intgCONST2                              ( ( long ) 234567 )
167 #define intgCONST3                              ( ( long ) -3 )
168 #define intgCONST4                              ( ( long ) 7 )
169 #define intgEXPECTED_ANSWER             ( ( ( intgCONST1 + intgCONST2 ) * intgCONST3 ) / intgCONST4 )
170
171 /* The length of the queue between is button push ISR and the Button Push task
172 is greater than 1 to account for switch bounces generating multiple inputs. */
173 #define mainBUTTON_QUEUE_SIZE 6
174
175 /*-----------------------------------------------------------
176         Local functions prototypes.
177 -----------------------------------------------------------*/
178
179 /*
180  * The 'Check' task function.  See the explanation at the top of the file.
181  */
182 static void vErrorChecks( void* pvParameters );
183
184 /*
185  * The 'Button Push' task.  See the explanation at the top of the file.
186  */
187 static void vButtonTask( void *pvParameters );
188
189 /*
190  * The idle task hook - in which the integer task is implemented.  See the
191  * explanation at the top of the file.
192  */
193 void vApplicationIdleHook( void );
194
195 /*
196  * Checks the unique counts of other tasks to ensure they are still operational.
197  */
198 static long prvCheckOtherTasksAreStillRunning( void );
199
200
201
202 /*-----------------------------------------------------------
203         Local variables.
204 -----------------------------------------------------------*/
205
206 /* A few tasks are defined within this file.  This flag is used to indicate
207 their status.  If an error is detected in one of the locally defined tasks then
208 this flag is set to pdTRUE. */
209 portBASE_TYPE xLocalError = pdFALSE;
210
211 /* The queue used to send data from the button push ISR to the Button Push 
212 task. */
213 static QueueHandle_t xButtonQueue;
214
215
216 /*-----------------------------------------------------------*/
217
218 /* 
219  * This is called from the main() function generated by the Processor Expert.
220  */
221 void vMain( void )
222 {
223         /* Start some of the standard demo tasks. */
224         vStartLEDFlashTasks( mainFLASH_PRIORITY );
225         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
226         vStartDynamicPriorityTasks();
227         
228         /* Start the locally defined tasks.  There is also a task implemented as
229         the idle hook. */
230         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
231         xTaskCreate( vButtonTask, "Button", configMINIMAL_STACK_SIZE, NULL, mainBUTTON_TASK_PRIORITY, NULL );
232         
233         /* All the tasks have been created - start the scheduler. */
234         vTaskStartScheduler();
235         
236         /* Should not reach here! */
237         for( ;; );
238 }
239 /*-----------------------------------------------------------*/
240
241 static void vErrorChecks( void *pvParameters )
242 {
243 TickType_t xDelayPeriod = mainCHECK_PERIOD;
244 TickType_t xLastWakeTime;
245
246         /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()
247         functions correctly. */
248         xLastWakeTime = xTaskGetTickCount();
249
250         for( ;; )
251         {
252                 /* Delay until it is time to execute again.  The delay period is 
253                 shorter following an error. */
254                 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );
255
256                 /* Check all the demo application tasks are executing without 
257                 error. If an error is found the delay period is shortened - this
258                 has the effect of increasing the flash rate of the 'check' task
259                 LED. */
260                 if( prvCheckOtherTasksAreStillRunning() == pdFAIL )
261                 {
262                         /* An error has been detected in one of the tasks - flash faster. */
263                         xDelayPeriod = mainERROR_CHECK_PERIOD;
264                 }
265
266                 /* Toggle the LED each cycle round. */
267                 vParTestToggleLED( mainCHECK_TASK_LED );
268         }
269 }
270 /*-----------------------------------------------------------*/
271
272 static long prvCheckOtherTasksAreStillRunning( void )
273 {
274 portBASE_TYPE xAllTasksPassed = pdPASS;
275
276         if( xArePollingQueuesStillRunning() != pdTRUE )
277         {
278                 xAllTasksPassed = pdFAIL;
279         }
280
281         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
282         {
283                 xAllTasksPassed = pdFAIL;
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         for( ;; )
304         {
305                 /* Perform the calculation.  This will store partial value in
306                 registers, resulting in a good test of the context switch mechanism. */
307                 lValue = intgCONST1;
308                 lValue += intgCONST2;
309                 lValue *= intgCONST3;
310                 lValue /= intgCONST4;
311
312                 /* Did we perform the calculation correctly with no corruption? */
313                 if( lValue != intgEXPECTED_ANSWER )
314                 {
315                         /* Error! */
316                         portENTER_CRITICAL();
317                                 xLocalError = pdTRUE;
318                         portEXIT_CRITICAL();
319                 }
320
321                 /* Yield in case cooperative scheduling is being used. */
322                 #if configUSE_PREEMPTION == 0
323                 {
324                         taskYIELD();
325                 }
326                 #endif          
327         }
328 }
329 /*-----------------------------------------------------------*/
330
331 static void vButtonTask( void *pvParameters )
332 {
333 unsigned portBASE_TYPE uxExpected = 1, uxReceived;
334
335         /* Create the queue used by the producer and consumer. */
336         xButtonQueue = xQueueCreate( mainBUTTON_QUEUE_SIZE, ( unsigned portBASE_TYPE ) sizeof( unsigned portBASE_TYPE ) );
337
338         if( xButtonQueue )
339         {
340                 /* Now the queue is created it is safe to enable the button interrupt. */
341                 ButtonInterrupt_Enable();
342         
343                 for( ;; )
344                 {
345                         /* Simply wait for data to arrive from the button push interrupt. */
346                         if( xQueueReceive( xButtonQueue, &uxReceived, portMAX_DELAY ) == pdPASS )       
347                         {
348                                 /* Was the data we received that expected? */
349                                 if( uxReceived != uxExpected )
350                                 {
351                                         /* Error! */
352                                         portENTER_CRITICAL();
353                                                 xLocalError = pdTRUE;
354                                         portEXIT_CRITICAL();                            
355                                 }
356                                 else
357                                 {
358                                         /* Toggle the LED for every successful push. */
359                                         vParTestToggleLED( mainBUTTON_PUSH_LED );       
360                                 }
361                                 
362                                 uxExpected++;
363                         }
364                 }
365         }
366         
367         /* Will only get here if the queue could not be created. */
368         for( ;; );              
369 }
370 /*-----------------------------------------------------------*/
371
372 #pragma CODE_SEG __NEAR_SEG NON_BANKED
373
374         /* Button push ISR. */
375         void interrupt vButtonPush( void )
376         {
377                 static unsigned portBASE_TYPE uxValToSend = 0;
378                 static unsigned long xHigherPriorityTaskWoken;
379
380                 xHigherPriorityTaskWoken = pdFALSE;
381                 
382                 /* Send an incrementing value to the button push task each run. */
383                 uxValToSend++;          
384
385                 /* Clear the interrupt flag. */
386                 PIFP = 1;
387
388                 /* Send the incremented value down the queue.  The button push task is
389                 blocked waiting for the data.  As the button push task is high priority
390                 it will wake and a context switch should be performed before leaving
391                 the ISR. */
392                 xQueueSendFromISR( xButtonQueue, &uxValToSend, &xHigherPriorityTaskWoken );
393
394                 if( xHigherPriorityTaskWoken )
395                 {
396                         /* NOTE: This macro can only be used if there are no local
397                         variables defined.  This function uses a static variable so it's
398                         use is permitted.  If the variable were not static portYIELD() 
399                         would have to be used in it's place. */
400                         portTASK_SWITCH_FROM_ISR();
401                 }               
402         }
403
404 #pragma CODE_SEG DEFAULT
405
406