2 FreeRTOS.org V4.4.0 - Copyright (C) 2003-2007 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 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
31 Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along
\r
32 with commercial development and support options.
\r
33 ***************************************************************************
\r
39 * vMain() is effectively the demo application entry point. It is called by
\r
40 * the main() function generated by the Processor Expert application.
\r
42 * vMain() creates all the demo application tasks, then starts the scheduler.
\r
43 * The WEB documentation provides more details of the demo application tasks.
\r
45 * Main.c also creates a task called "Check". This only executes every three
\r
46 * seconds but has the highest priority so is guaranteed to get processor time.
\r
47 * Its main function is to check that all the other tasks are still operational.
\r
48 * Each task (other than the "flash" tasks) maintains a unique count that is
\r
49 * incremented each time the task successfully completes its function. Should
\r
50 * any error occur within such a task the count is permanently halted. The
\r
51 * check task inspects the count of each task to ensure it has changed since
\r
52 * the last time the check task executed. If all the count variables have
\r
53 * changed all the tasks are still executing error free, and the check task
\r
54 * toggles the onboard LED. Should any task contain an error at any time
\r
55 * the LED toggle rate will change from 3 seconds to 500ms.
\r
57 * This file also includes the functionality normally implemented within the
\r
58 * standard demo application file integer.c. Due to the limited memory
\r
59 * available on the microcontroller the functionality has been included within
\r
60 * the idle task hook [vApplicationIdleHook()] - instead of within the usual
\r
61 * separate task. See the documentation within integer.c for the rationale
\r
62 * of the integer task functionality.
\r
66 * The demo applications included with other FreeRTOS ports make use of the
\r
67 * standard ComTest tasks. These use a loopback connector to transmit and
\r
68 * receive RS232 characters between two tasks. The test is important for two
\r
71 * 1) It tests the mechanism of context switching from within an application
\r
74 * 2) It generates some randomised timing.
\r
76 * The demo board used to develop this port does not include an RS232 interface
\r
77 * so the ComTest tasks could not easily be included. Instead these two tests
\r
78 * are created using a 'Button Push' task.
\r
80 * The 'Button Push' task blocks on a queue, waiting for data to arrive. A
\r
81 * simple interrupt routine connected to the PP0 input on the demo board places
\r
82 * data in the queue each time the PP0 button is pushed (this button is built
\r
83 * onto the demo board). As the 'Button Push' task is created with a
\r
84 * relatively high priority it will unblock and want to execute as soon as data
\r
85 * arrives in the queue - resulting in a context switch within the PP0 input
\r
86 * ISR. If the data retrieved from the queue is that expected the 'Button Push'
\r
87 * task toggles LED 5. Therefore correct operation is indicated by the LED
\r
88 * toggling each time the PP0 button is pressed.
\r
90 * This test is not as satisfactory as the ComTest method - but the simple
\r
91 * nature of the port makes is just about adequate.
\r
95 /* Kernel includes. */
\r
96 #include "FreeRTOS.h"
\r
100 /* Demo application includes. */
\r
103 #include "dynamic.h"
\r
104 #include "partest.h"
\r
106 /* Processor expert includes. */
\r
107 #include "ButtonInterrupt.h"
\r
109 /*-----------------------------------------------------------
\r
111 -----------------------------------------------------------*/
\r
113 /* Priorities assigned to demo application tasks. */
\r
114 #define mainFLASH_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
115 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
116 #define mainBUTTON_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
117 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
119 /* LED that is toggled by the check task. The check task periodically checks
\r
120 that all the other tasks are operating without error. If no errors are found
\r
121 the LED is toggled with mainCHECK_PERIOD frequency. If an error is found
\r
122 then the toggle rate increases to mainERROR_CHECK_PERIOD. */
\r
123 #define mainCHECK_TASK_LED ( 7 )
\r
124 #define mainCHECK_PERIOD ( ( portTickType ) 3000 / portTICK_RATE_MS )
\r
125 #define mainERROR_CHECK_PERIOD ( ( portTickType ) 500 / portTICK_RATE_MS )
\r
127 /* LED that is toggled by the button push interrupt. */
\r
128 #define mainBUTTON_PUSH_LED ( 5 )
\r
130 /* The constants used in the idle task calculation. */
\r
131 #define intgCONST1 ( ( portLONG ) 123 )
\r
132 #define intgCONST2 ( ( portLONG ) 234567 )
\r
133 #define intgCONST3 ( ( portLONG ) -3 )
\r
134 #define intgCONST4 ( ( portLONG ) 7 )
\r
135 #define intgEXPECTED_ANSWER ( ( ( intgCONST1 + intgCONST2 ) * intgCONST3 ) / intgCONST4 )
\r
137 /* The length of the queue between is button push ISR and the Button Push task
\r
138 is greater than 1 to account for switch bounces generating multiple inputs. */
\r
139 #define mainBUTTON_QUEUE_SIZE 6
\r
141 /*-----------------------------------------------------------
\r
142 Local functions prototypes.
\r
143 -----------------------------------------------------------*/
\r
146 * The 'Check' task function. See the explanation at the top of the file.
\r
148 static void vErrorChecks( void* pvParameters );
\r
151 * The 'Button Push' task. See the explanation at the top of the file.
\r
153 static void vButtonTask( void *pvParameters );
\r
156 * The idle task hook - in which the integer task is implemented. See the
\r
157 * explanation at the top of the file.
\r
159 void vApplicationIdleHook( void );
\r
162 * Checks the unique counts of other tasks to ensure they are still operational.
\r
164 static portLONG prvCheckOtherTasksAreStillRunning( void );
\r
168 /*-----------------------------------------------------------
\r
170 -----------------------------------------------------------*/
\r
172 /* A few tasks are defined within this file. This flag is used to indicate
\r
173 their status. If an error is detected in one of the locally defined tasks then
\r
174 this flag is set to pdTRUE. */
\r
175 portBASE_TYPE xLocalError = pdFALSE;
\r
177 /* The queue used to send data from the button push ISR to the Button Push
\r
179 static xQueueHandle xButtonQueue;
\r
182 /*-----------------------------------------------------------*/
\r
185 * This is called from the main() function generated by the Processor Expert.
\r
189 /* Start some of the standard demo tasks. */
\r
190 vStartLEDFlashTasks( mainFLASH_PRIORITY );
\r
191 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
192 vStartDynamicPriorityTasks();
\r
194 /* Start the locally defined tasks. There is also a task implemented as
\r
196 xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
197 xTaskCreate( vButtonTask, "Button", configMINIMAL_STACK_SIZE, NULL, mainBUTTON_TASK_PRIORITY, NULL );
\r
199 /* All the tasks have been created - start the scheduler. */
\r
200 vTaskStartScheduler();
\r
202 /* Should not reach here! */
\r
205 /*-----------------------------------------------------------*/
\r
207 static void vErrorChecks( void *pvParameters )
\r
209 portTickType xDelayPeriod = mainCHECK_PERIOD;
\r
210 portTickType xLastWakeTime;
\r
212 /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()
\r
213 functions correctly. */
\r
214 xLastWakeTime = xTaskGetTickCount();
\r
218 /* Delay until it is time to execute again. The delay period is
\r
219 shorter following an error. */
\r
220 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );
\r
222 /* Check all the demo application tasks are executing without
\r
223 error. If an error is found the delay period is shortened - this
\r
224 has the effect of increasing the flash rate of the 'check' task
\r
226 if( prvCheckOtherTasksAreStillRunning() == pdFAIL )
\r
228 /* An error has been detected in one of the tasks - flash faster. */
\r
229 xDelayPeriod = mainERROR_CHECK_PERIOD;
\r
232 /* Toggle the LED each cycle round. */
\r
233 vParTestToggleLED( mainCHECK_TASK_LED );
\r
236 /*-----------------------------------------------------------*/
\r
238 static portLONG prvCheckOtherTasksAreStillRunning( void )
\r
240 portBASE_TYPE xAllTasksPassed = pdPASS;
\r
242 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
244 xAllTasksPassed = pdFAIL;
\r
247 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
249 xAllTasksPassed = pdFAIL;
\r
252 /* Also check the status flag for the tasks defined within this function. */
\r
253 if( xLocalError != pdFALSE )
\r
255 xAllTasksPassed = pdFAIL;
\r
258 return xAllTasksPassed;
\r
260 /*-----------------------------------------------------------*/
\r
262 void vApplicationIdleHook( void )
\r
264 /* This variable is effectively set to a constant so it is made volatile to
\r
265 ensure the compiler does not just get rid of it. */
\r
266 volatile portLONG lValue;
\r
268 /* Keep performing a calculation and checking the result against a constant. */
\r
271 /* Perform the calculation. This will store partial value in
\r
272 registers, resulting in a good test of the context switch mechanism. */
\r
273 lValue = intgCONST1;
\r
274 lValue += intgCONST2;
\r
275 lValue *= intgCONST3;
\r
276 lValue /= intgCONST4;
\r
278 /* Did we perform the calculation correctly with no corruption? */
\r
279 if( lValue != intgEXPECTED_ANSWER )
\r
282 portENTER_CRITICAL();
\r
283 xLocalError = pdTRUE;
\r
284 portEXIT_CRITICAL();
\r
287 /* Yield in case cooperative scheduling is being used. */
\r
288 #if configUSE_PREEMPTION == 0
\r
295 /*-----------------------------------------------------------*/
\r
297 static void vButtonTask( void *pvParameters )
\r
299 unsigned portBASE_TYPE uxExpected = 1, uxReceived;
\r
301 /* Create the queue used by the producer and consumer. */
\r
302 xButtonQueue = xQueueCreate( mainBUTTON_QUEUE_SIZE, ( unsigned portBASE_TYPE ) sizeof( unsigned portBASE_TYPE ) );
\r
306 /* Now the queue is created it is safe to enable the button interrupt. */
\r
307 ButtonInterrupt_Enable();
\r
311 /* Simply wait for data to arrive from the button push interrupt. */
\r
312 if( xQueueReceive( xButtonQueue, &uxReceived, portMAX_DELAY ) == pdPASS )
\r
314 /* Was the data we received that expected? */
\r
315 if( uxReceived != uxExpected )
\r
318 portENTER_CRITICAL();
\r
319 xLocalError = pdTRUE;
\r
320 portEXIT_CRITICAL();
\r
324 /* Toggle the LED for every successful push. */
\r
325 vParTestToggleLED( mainBUTTON_PUSH_LED );
\r
333 /* Will only get here if the queue could not be created. */
\r
336 /*-----------------------------------------------------------*/
\r
338 #pragma CODE_SEG __NEAR_SEG NON_BANKED
\r
340 /* Button push ISR. */
\r
341 void interrupt vButtonPush( void )
\r
343 static unsigned portBASE_TYPE uxValToSend = 0;
\r
345 /* Send an incrementing value to the button push task each run. */
\r
348 /* Clear the interrupt flag. */
\r
351 /* Send the incremented value down the queue. The button push task is
\r
352 blocked waiting for the data. As the button push task is high priority
\r
353 it will wake and a context switch should be performed before leaving
\r
355 if( xQueueSendFromISR( xButtonQueue, &uxValToSend, pdFALSE ) )
\r
357 /* NOTE: This macro can only be used if there are no local
\r
358 variables defined. This function uses a static variable so it's
\r
359 use is permitted. If the variable were not static portYIELD()
\r
360 would have to be used in it's place. */
\r
361 portTASK_SWITCH_FROM_ISR();
\r
365 #pragma CODE_SEG DEFAULT
\r