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