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