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