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