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