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