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