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