]> begriffs open source - cmsis-freertos/blob - Demo/HCS12_CodeWarrior_small/main.c
there was an extra ')'... caused build to fail
[cmsis-freertos] / Demo / HCS12_CodeWarrior_small / main.c
1 /*
2  * FreeRTOS Kernel V10.1.1
3  * Copyright (C) 2018 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * http://www.FreeRTOS.org
23  * http://aws.amazon.com/freertos
24  *
25  * 1 tab == 4 spaces!
26  */
27
28
29 /*
30  *
31  * vMain() is effectively the demo application entry point.  It is called by
32  * the main() function generated by the Processor Expert application.  
33  *
34  * vMain() creates all the demo application tasks, then starts the scheduler.
35  * The WEB      documentation provides more details of the demo application tasks.
36  *
37  * Main.c also creates a task called "Check".  This only executes every three 
38  * seconds but has the highest priority so is guaranteed to get processor time.  
39  * Its main function is to check that all the other tasks are still operational.
40  * Each task (other than the "flash" tasks) maintains a unique count that is 
41  * incremented each time the task successfully completes its function.  Should 
42  * any error occur within such a task the count is permanently halted.  The 
43  * check task inspects the count of each task to ensure it has changed since
44  * the last time the check task executed.  If all the count variables have 
45  * changed all the tasks are still executing error free, and the check task
46  * toggles the onboard LED.  Should any task contain an error at any time 
47  * the LED toggle rate will change from 3 seconds to 500ms.
48  *
49  * This file also includes the functionality normally implemented within the 
50  * standard demo application file integer.c.  Due to the limited memory 
51  * available on the microcontroller the functionality has been included within
52  * the idle task hook [vApplicationIdleHook()] - instead of within the usual
53  * separate task.  See the documentation within integer.c for the rationale 
54  * of the integer task functionality.
55  *
56  *
57  * 
58  * The demo applications included with other FreeRTOS ports make use of the
59  * standard ComTest tasks.  These use a loopback connector to transmit and
60  * receive RS232 characters between two tasks.  The test is important for two
61  * reasons:
62  *
63  *      1) It tests the mechanism of context switching from within an application
64  *         ISR.
65  *
66  *      2) It generates some randomised timing.
67  *
68  * The demo board used to develop this port does not include an RS232 interface
69  * so the ComTest tasks could not easily be included.  Instead these two tests
70  * are created using a 'Button Push' task.  
71  * 
72  * The 'Button Push' task blocks on a queue, waiting for data to arrive.  A
73  * simple interrupt routine connected to the PP0 input on the demo board places
74  * data in the queue each time the PP0 button is pushed (this button is built 
75  * onto the demo board).  As the 'Button Push' task is created with a 
76  * relatively high priority it will unblock and want to execute as soon as data
77  * arrives in the queue - resulting in a context switch within the PP0 input
78  * ISR.  If the data retrieved from the queue is that expected the 'Button Push'
79  * task toggles LED 5.  Therefore correct operation is indicated by the LED
80  * toggling each time the PP0 button is pressed.
81  *
82  * This test is not as satisfactory as the ComTest method - but the simple 
83  * nature of the port makes is just about adequate.
84  * 
85  */
86
87 /* Kernel includes. */
88 #include "FreeRTOS.h"
89 #include "task.h"
90 #include "queue.h"
91
92 /* Demo application includes. */
93 #include "flash.h"
94 #include "PollQ.h"
95 #include "dynamic.h"
96 #include "partest.h"
97
98 /* Processor expert includes. */
99 #include "ButtonInterrupt.h"
100
101 /*-----------------------------------------------------------
102         Definitions.
103 -----------------------------------------------------------*/
104
105 /* Priorities assigned to demo application tasks. */
106 #define mainFLASH_PRIORITY                      ( tskIDLE_PRIORITY + 2 )
107 #define mainCHECK_TASK_PRIORITY         ( tskIDLE_PRIORITY + 3 )
108 #define mainBUTTON_TASK_PRIORITY        ( tskIDLE_PRIORITY + 3 )
109 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 2 )
110
111 /* LED that is toggled by the check task.  The check task periodically checks
112 that all the other tasks are operating without error.  If no errors are found
113 the LED is toggled with mainCHECK_PERIOD frequency.  If an error is found 
114 then the toggle rate increases to mainERROR_CHECK_PERIOD. */
115 #define mainCHECK_TASK_LED                      ( 7 )
116 #define mainCHECK_PERIOD                        ( ( TickType_t ) 3000 / portTICK_PERIOD_MS  )
117 #define mainERROR_CHECK_PERIOD          ( ( TickType_t ) 500 / portTICK_PERIOD_MS )
118
119 /* LED that is toggled by the button push interrupt. */
120 #define mainBUTTON_PUSH_LED                     ( 5 )
121
122 /* The constants used in the idle task calculation. */
123 #define intgCONST1                              ( ( long ) 123 )
124 #define intgCONST2                              ( ( long ) 234567 )
125 #define intgCONST3                              ( ( long ) -3 )
126 #define intgCONST4                              ( ( long ) 7 )
127 #define intgEXPECTED_ANSWER             ( ( ( intgCONST1 + intgCONST2 ) * intgCONST3 ) / intgCONST4 )
128
129 /* The length of the queue between is button push ISR and the Button Push task
130 is greater than 1 to account for switch bounces generating multiple inputs. */
131 #define mainBUTTON_QUEUE_SIZE 6
132
133 /*-----------------------------------------------------------
134         Local functions prototypes.
135 -----------------------------------------------------------*/
136
137 /*
138  * The 'Check' task function.  See the explanation at the top of the file.
139  */
140 static void vErrorChecks( void* pvParameters );
141
142 /*
143  * The 'Button Push' task.  See the explanation at the top of the file.
144  */
145 static void vButtonTask( void *pvParameters );
146
147 /*
148  * The idle task hook - in which the integer task is implemented.  See the
149  * explanation at the top of the file.
150  */
151 void vApplicationIdleHook( void );
152
153 /*
154  * Checks the unique counts of other tasks to ensure they are still operational.
155  */
156 static long prvCheckOtherTasksAreStillRunning( void );
157
158
159
160 /*-----------------------------------------------------------
161         Local variables.
162 -----------------------------------------------------------*/
163
164 /* A few tasks are defined within this file.  This flag is used to indicate
165 their status.  If an error is detected in one of the locally defined tasks then
166 this flag is set to pdTRUE. */
167 portBASE_TYPE xLocalError = pdFALSE;
168
169 /* The queue used to send data from the button push ISR to the Button Push 
170 task. */
171 static QueueHandle_t xButtonQueue;
172
173
174 /*-----------------------------------------------------------*/
175
176 /* 
177  * This is called from the main() function generated by the Processor Expert.
178  */
179 void vMain( void )
180 {
181         /* Start some of the standard demo tasks. */
182         vStartLEDFlashTasks( mainFLASH_PRIORITY );
183         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
184         vStartDynamicPriorityTasks();
185         
186         /* Start the locally defined tasks.  There is also a task implemented as
187         the idle hook. */
188         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
189         xTaskCreate( vButtonTask, "Button", configMINIMAL_STACK_SIZE, NULL, mainBUTTON_TASK_PRIORITY, NULL );
190         
191         /* All the tasks have been created - start the scheduler. */
192         vTaskStartScheduler();
193         
194         /* Should not reach here! */
195         for( ;; );
196 }
197 /*-----------------------------------------------------------*/
198
199 static void vErrorChecks( void *pvParameters )
200 {
201 TickType_t xDelayPeriod = mainCHECK_PERIOD;
202 TickType_t xLastWakeTime;
203
204         /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()
205         functions correctly. */
206         xLastWakeTime = xTaskGetTickCount();
207
208         for( ;; )
209         {
210                 /* Delay until it is time to execute again.  The delay period is 
211                 shorter following an error. */
212                 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );
213
214                 /* Check all the demo application tasks are executing without 
215                 error. If an error is found the delay period is shortened - this
216                 has the effect of increasing the flash rate of the 'check' task
217                 LED. */
218                 if( prvCheckOtherTasksAreStillRunning() == pdFAIL )
219                 {
220                         /* An error has been detected in one of the tasks - flash faster. */
221                         xDelayPeriod = mainERROR_CHECK_PERIOD;
222                 }
223
224                 /* Toggle the LED each cycle round. */
225                 vParTestToggleLED( mainCHECK_TASK_LED );
226         }
227 }
228 /*-----------------------------------------------------------*/
229
230 static long prvCheckOtherTasksAreStillRunning( void )
231 {
232 portBASE_TYPE xAllTasksPassed = pdPASS;
233
234         if( xArePollingQueuesStillRunning() != pdTRUE )
235         {
236                 xAllTasksPassed = pdFAIL;
237         }
238
239         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
240         {
241                 xAllTasksPassed = pdFAIL;
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         for( ;; )
262         {
263                 /* Perform the calculation.  This will store partial value in
264                 registers, resulting in a good test of the context switch mechanism. */
265                 lValue = intgCONST1;
266                 lValue += intgCONST2;
267                 lValue *= intgCONST3;
268                 lValue /= intgCONST4;
269
270                 /* Did we perform the calculation correctly with no corruption? */
271                 if( lValue != intgEXPECTED_ANSWER )
272                 {
273                         /* Error! */
274                         portENTER_CRITICAL();
275                                 xLocalError = pdTRUE;
276                         portEXIT_CRITICAL();
277                 }
278
279                 /* Yield in case cooperative scheduling is being used. */
280                 #if configUSE_PREEMPTION == 0
281                 {
282                         taskYIELD();
283                 }
284                 #endif          
285         }
286 }
287 /*-----------------------------------------------------------*/
288
289 static void vButtonTask( void *pvParameters )
290 {
291 unsigned portBASE_TYPE uxExpected = 1, uxReceived;
292
293         /* Create the queue used by the producer and consumer. */
294         xButtonQueue = xQueueCreate( mainBUTTON_QUEUE_SIZE, ( unsigned portBASE_TYPE ) sizeof( unsigned portBASE_TYPE ) );
295
296         if( xButtonQueue )
297         {
298                 /* Now the queue is created it is safe to enable the button interrupt. */
299                 ButtonInterrupt_Enable();
300         
301                 for( ;; )
302                 {
303                         /* Simply wait for data to arrive from the button push interrupt. */
304                         if( xQueueReceive( xButtonQueue, &uxReceived, portMAX_DELAY ) == pdPASS )       
305                         {
306                                 /* Was the data we received that expected? */
307                                 if( uxReceived != uxExpected )
308                                 {
309                                         /* Error! */
310                                         portENTER_CRITICAL();
311                                                 xLocalError = pdTRUE;
312                                         portEXIT_CRITICAL();                            
313                                 }
314                                 else
315                                 {
316                                         /* Toggle the LED for every successful push. */
317                                         vParTestToggleLED( mainBUTTON_PUSH_LED );       
318                                 }
319                                 
320                                 uxExpected++;
321                         }
322                 }
323         }
324         
325         /* Will only get here if the queue could not be created. */
326         for( ;; );              
327 }
328 /*-----------------------------------------------------------*/
329
330 #pragma CODE_SEG __NEAR_SEG NON_BANKED
331
332         /* Button push ISR. */
333         void interrupt vButtonPush( void )
334         {
335                 static unsigned portBASE_TYPE uxValToSend = 0;
336                 static unsigned long xHigherPriorityTaskWoken;
337
338                 xHigherPriorityTaskWoken = pdFALSE;
339                 
340                 /* Send an incrementing value to the button push task each run. */
341                 uxValToSend++;          
342
343                 /* Clear the interrupt flag. */
344                 PIFP = 1;
345
346                 /* Send the incremented value down the queue.  The button push task is
347                 blocked waiting for the data.  As the button push task is high priority
348                 it will wake and a context switch should be performed before leaving
349                 the ISR. */
350                 xQueueSendFromISR( xButtonQueue, &uxValToSend, &xHigherPriorityTaskWoken );
351
352                 if( xHigherPriorityTaskWoken )
353                 {
354                         /* NOTE: This macro can only be used if there are no local
355                         variables defined.  This function uses a static variable so it's
356                         use is permitted.  If the variable were not static portYIELD() 
357                         would have to be used in it's place. */
358                         portTASK_SWITCH_FROM_ISR();
359                 }               
360         }
361
362 #pragma CODE_SEG DEFAULT
363
364