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