2 FreeRTOS.org V4.8.0 - Copyright (C) 2003-2008 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\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
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
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
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
26 ***************************************************************************
\r
27 ***************************************************************************
\r
29 * SAVE TIME AND MONEY! We can port FreeRTOS.org to your own hardware, *
\r
30 * and even write all or part of your application on your behalf. *
\r
31 * See http://www.OpenRTOS.com for details of the services we provide to *
\r
32 * expedite your project. *
\r
34 ***************************************************************************
\r
35 ***************************************************************************
\r
37 Please ensure to read the configuration and relevant port sections of the
\r
38 online documentation.
\r
40 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
43 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
46 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
47 licensing and training services.
\r
53 * vMain() is effectively the demo application entry point. It is called by
\r
54 * the main() function generated by the Processor Expert application.
\r
56 * vMain() creates all the demo application tasks, then starts the scheduler.
\r
57 * The WEB documentation provides more details of the demo application tasks.
\r
59 * Main.c also creates a task called "Check". This only executes every three
\r
60 * seconds but has the highest priority so is guaranteed to get processor time.
\r
61 * Its main function is to check that all the other tasks are still operational.
\r
62 * Each task (other than the "flash" tasks) maintains a unique count that is
\r
63 * incremented each time the task successfully completes its function. Should
\r
64 * any error occur within such a task the count is permanently halted. The
\r
65 * check task inspects the count of each task to ensure it has changed since
\r
66 * the last time the check task executed. If all the count variables have
\r
67 * changed all the tasks are still executing error free, and the check task
\r
68 * toggles the onboard LED. Should any task contain an error at any time
\r
69 * the LED toggle rate will change from 3 seconds to 500ms.
\r
71 * This file also includes the functionality normally implemented within the
\r
72 * standard demo application file integer.c. Due to the limited memory
\r
73 * available on the microcontroller the functionality has been included within
\r
74 * the idle task hook [vApplicationIdleHook()] - instead of within the usual
\r
75 * separate task. See the documentation within integer.c for the rationale
\r
76 * of the integer task functionality.
\r
80 * The demo applications included with other FreeRTOS ports make use of the
\r
81 * standard ComTest tasks. These use a loopback connector to transmit and
\r
82 * receive RS232 characters between two tasks. The test is important for two
\r
85 * 1) It tests the mechanism of context switching from within an application
\r
88 * 2) It generates some randomised timing.
\r
90 * The demo board used to develop this port does not include an RS232 interface
\r
91 * so the ComTest tasks could not easily be included. Instead these two tests
\r
92 * are created using a 'Button Push' task.
\r
94 * The 'Button Push' task blocks on a queue, waiting for data to arrive. A
\r
95 * simple interrupt routine connected to the PP0 input on the demo board places
\r
96 * data in the queue each time the PP0 button is pushed (this button is built
\r
97 * onto the demo board). As the 'Button Push' task is created with a
\r
98 * relatively high priority it will unblock and want to execute as soon as data
\r
99 * arrives in the queue - resulting in a context switch within the PP0 input
\r
100 * ISR. If the data retrieved from the queue is that expected the 'Button Push'
\r
101 * task toggles LED 5. Therefore correct operation is indicated by the LED
\r
102 * toggling each time the PP0 button is pressed.
\r
104 * This test is not as satisfactory as the ComTest method - but the simple
\r
105 * nature of the port makes is just about adequate.
\r
109 /* Kernel includes. */
\r
110 #include "FreeRTOS.h"
\r
114 /* Demo application includes. */
\r
117 #include "dynamic.h"
\r
118 #include "partest.h"
\r
120 /* Processor expert includes. */
\r
121 #include "ButtonInterrupt.h"
\r
123 /*-----------------------------------------------------------
\r
125 -----------------------------------------------------------*/
\r
127 /* Priorities assigned to demo application tasks. */
\r
128 #define mainFLASH_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
129 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
130 #define mainBUTTON_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
131 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
133 /* LED that is toggled by the check task. The check task periodically checks
\r
134 that all the other tasks are operating without error. If no errors are found
\r
135 the LED is toggled with mainCHECK_PERIOD frequency. If an error is found
\r
136 then the toggle rate increases to mainERROR_CHECK_PERIOD. */
\r
137 #define mainCHECK_TASK_LED ( 7 )
\r
138 #define mainCHECK_PERIOD ( ( portTickType ) 3000 / portTICK_RATE_MS )
\r
139 #define mainERROR_CHECK_PERIOD ( ( portTickType ) 500 / portTICK_RATE_MS )
\r
141 /* LED that is toggled by the button push interrupt. */
\r
142 #define mainBUTTON_PUSH_LED ( 5 )
\r
144 /* The constants used in the idle task calculation. */
\r
145 #define intgCONST1 ( ( portLONG ) 123 )
\r
146 #define intgCONST2 ( ( portLONG ) 234567 )
\r
147 #define intgCONST3 ( ( portLONG ) -3 )
\r
148 #define intgCONST4 ( ( portLONG ) 7 )
\r
149 #define intgEXPECTED_ANSWER ( ( ( intgCONST1 + intgCONST2 ) * intgCONST3 ) / intgCONST4 )
\r
151 /* The length of the queue between is button push ISR and the Button Push task
\r
152 is greater than 1 to account for switch bounces generating multiple inputs. */
\r
153 #define mainBUTTON_QUEUE_SIZE 6
\r
155 /*-----------------------------------------------------------
\r
156 Local functions prototypes.
\r
157 -----------------------------------------------------------*/
\r
160 * The 'Check' task function. See the explanation at the top of the file.
\r
162 static void vErrorChecks( void* pvParameters );
\r
165 * The 'Button Push' task. See the explanation at the top of the file.
\r
167 static void vButtonTask( void *pvParameters );
\r
170 * The idle task hook - in which the integer task is implemented. See the
\r
171 * explanation at the top of the file.
\r
173 void vApplicationIdleHook( void );
\r
176 * Checks the unique counts of other tasks to ensure they are still operational.
\r
178 static portLONG prvCheckOtherTasksAreStillRunning( void );
\r
182 /*-----------------------------------------------------------
\r
184 -----------------------------------------------------------*/
\r
186 /* A few tasks are defined within this file. This flag is used to indicate
\r
187 their status. If an error is detected in one of the locally defined tasks then
\r
188 this flag is set to pdTRUE. */
\r
189 portBASE_TYPE xLocalError = pdFALSE;
\r
191 /* The queue used to send data from the button push ISR to the Button Push
\r
193 static xQueueHandle xButtonQueue;
\r
196 /*-----------------------------------------------------------*/
\r
199 * This is called from the main() function generated by the Processor Expert.
\r
203 /* Start some of the standard demo tasks. */
\r
204 vStartLEDFlashTasks( mainFLASH_PRIORITY );
\r
205 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
206 vStartDynamicPriorityTasks();
\r
208 /* Start the locally defined tasks. There is also a task implemented as
\r
210 xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
211 xTaskCreate( vButtonTask, "Button", configMINIMAL_STACK_SIZE, NULL, mainBUTTON_TASK_PRIORITY, NULL );
\r
213 /* All the tasks have been created - start the scheduler. */
\r
214 vTaskStartScheduler();
\r
216 /* Should not reach here! */
\r
219 /*-----------------------------------------------------------*/
\r
221 static void vErrorChecks( void *pvParameters )
\r
223 portTickType xDelayPeriod = mainCHECK_PERIOD;
\r
224 portTickType xLastWakeTime;
\r
226 /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()
\r
227 functions correctly. */
\r
228 xLastWakeTime = xTaskGetTickCount();
\r
232 /* Delay until it is time to execute again. The delay period is
\r
233 shorter following an error. */
\r
234 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );
\r
236 /* Check all the demo application tasks are executing without
\r
237 error. If an error is found the delay period is shortened - this
\r
238 has the effect of increasing the flash rate of the 'check' task
\r
240 if( prvCheckOtherTasksAreStillRunning() == pdFAIL )
\r
242 /* An error has been detected in one of the tasks - flash faster. */
\r
243 xDelayPeriod = mainERROR_CHECK_PERIOD;
\r
246 /* Toggle the LED each cycle round. */
\r
247 vParTestToggleLED( mainCHECK_TASK_LED );
\r
250 /*-----------------------------------------------------------*/
\r
252 static portLONG prvCheckOtherTasksAreStillRunning( void )
\r
254 portBASE_TYPE xAllTasksPassed = pdPASS;
\r
256 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
258 xAllTasksPassed = pdFAIL;
\r
261 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
263 xAllTasksPassed = pdFAIL;
\r
266 /* Also check the status flag for the tasks defined within this function. */
\r
267 if( xLocalError != pdFALSE )
\r
269 xAllTasksPassed = pdFAIL;
\r
272 return xAllTasksPassed;
\r
274 /*-----------------------------------------------------------*/
\r
276 void vApplicationIdleHook( void )
\r
278 /* This variable is effectively set to a constant so it is made volatile to
\r
279 ensure the compiler does not just get rid of it. */
\r
280 volatile portLONG lValue;
\r
282 /* Keep performing a calculation and checking the result against a constant. */
\r
285 /* Perform the calculation. This will store partial value in
\r
286 registers, resulting in a good test of the context switch mechanism. */
\r
287 lValue = intgCONST1;
\r
288 lValue += intgCONST2;
\r
289 lValue *= intgCONST3;
\r
290 lValue /= intgCONST4;
\r
292 /* Did we perform the calculation correctly with no corruption? */
\r
293 if( lValue != intgEXPECTED_ANSWER )
\r
296 portENTER_CRITICAL();
\r
297 xLocalError = pdTRUE;
\r
298 portEXIT_CRITICAL();
\r
301 /* Yield in case cooperative scheduling is being used. */
\r
302 #if configUSE_PREEMPTION == 0
\r
309 /*-----------------------------------------------------------*/
\r
311 static void vButtonTask( void *pvParameters )
\r
313 unsigned portBASE_TYPE uxExpected = 1, uxReceived;
\r
315 /* Create the queue used by the producer and consumer. */
\r
316 xButtonQueue = xQueueCreate( mainBUTTON_QUEUE_SIZE, ( unsigned portBASE_TYPE ) sizeof( unsigned portBASE_TYPE ) );
\r
320 /* Now the queue is created it is safe to enable the button interrupt. */
\r
321 ButtonInterrupt_Enable();
\r
325 /* Simply wait for data to arrive from the button push interrupt. */
\r
326 if( xQueueReceive( xButtonQueue, &uxReceived, portMAX_DELAY ) == pdPASS )
\r
328 /* Was the data we received that expected? */
\r
329 if( uxReceived != uxExpected )
\r
332 portENTER_CRITICAL();
\r
333 xLocalError = pdTRUE;
\r
334 portEXIT_CRITICAL();
\r
338 /* Toggle the LED for every successful push. */
\r
339 vParTestToggleLED( mainBUTTON_PUSH_LED );
\r
347 /* Will only get here if the queue could not be created. */
\r
350 /*-----------------------------------------------------------*/
\r
352 #pragma CODE_SEG __NEAR_SEG NON_BANKED
\r
354 /* Button push ISR. */
\r
355 void interrupt vButtonPush( void )
\r
357 static unsigned portBASE_TYPE uxValToSend = 0;
\r
359 /* Send an incrementing value to the button push task each run. */
\r
362 /* Clear the interrupt flag. */
\r
365 /* Send the incremented value down the queue. The button push task is
\r
366 blocked waiting for the data. As the button push task is high priority
\r
367 it will wake and a context switch should be performed before leaving
\r
369 if( xQueueSendFromISR( xButtonQueue, &uxValToSend, pdFALSE ) )
\r
371 /* NOTE: This macro can only be used if there are no local
\r
372 variables defined. This function uses a static variable so it's
\r
373 use is permitted. If the variable were not static portYIELD()
\r
374 would have to be used in it's place. */
\r
375 portTASK_SWITCH_FROM_ISR();
\r
379 #pragma CODE_SEG DEFAULT
\r