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