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