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