2 FreeRTOS.org V5.3.0 - Copyright (C) 2003-2009 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 it
\r
7 under the terms of the GNU General Public License (version 2) as published
\r
8 by the 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.org without being obliged to provide
\r
11 the source code for any proprietary components. Alternative commercial
\r
12 license and support terms are also available upon request. See the
\r
13 licensing section of http://www.FreeRTOS.org for full details.
\r
15 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
20 You should have received a copy of the GNU General Public License along
\r
21 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
22 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
25 ***************************************************************************
\r
27 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
29 * This is a concise, step by step, 'hands on' guide that describes both *
\r
30 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
31 * explains numerous examples that are written using the FreeRTOS API. *
\r
32 * Full source code for all the examples is provided in an accompanying *
\r
35 ***************************************************************************
\r
39 Please ensure to read the configuration and relevant port sections of the
\r
40 online documentation.
\r
42 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
45 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
48 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
49 licensing and training services.
\r
55 * vMain() is effectively the demo application entry point. It is called by
\r
56 * the main() function generated by the Processor Expert application.
\r
58 * vMain() creates all the demo application tasks, then starts the scheduler.
\r
59 * The WEB documentation provides more details of the demo application tasks.
\r
61 * Main.c also creates a task called "Check". This only executes every three
\r
62 * seconds but has the highest priority so is guaranteed to get processor time.
\r
63 * Its main function is to check that all the other tasks are still operational.
\r
64 * Each task (other than the "flash" tasks) maintains a unique count that is
\r
65 * incremented each time the task successfully completes its function. Should
\r
66 * any error occur within such a task the count is permanently halted. The
\r
67 * check task inspects the count of each task to ensure it has changed since
\r
68 * the last time the check task executed. If all the count variables have
\r
69 * changed all the tasks are still executing error free, and the check task
\r
70 * toggles the onboard LED. Should any task contain an error at any time
\r
71 * the LED toggle rate will change from 3 seconds to 500ms.
\r
73 * This file also includes the functionality normally implemented within the
\r
74 * standard demo application file integer.c. Due to the limited memory
\r
75 * available on the microcontroller the functionality has been included within
\r
76 * the idle task hook [vApplicationIdleHook()] - instead of within the usual
\r
77 * separate task. See the documentation within integer.c for the rationale
\r
78 * of the integer task functionality.
\r
82 * The demo applications included with other FreeRTOS ports make use of the
\r
83 * standard ComTest tasks. These use a loopback connector to transmit and
\r
84 * receive RS232 characters between two tasks. The test is important for two
\r
87 * 1) It tests the mechanism of context switching from within an application
\r
90 * 2) It generates some randomised timing.
\r
92 * The demo board used to develop this port does not include an RS232 interface
\r
93 * so the ComTest tasks could not easily be included. Instead these two tests
\r
94 * are created using a 'Button Push' task.
\r
96 * The 'Button Push' task blocks on a queue, waiting for data to arrive. A
\r
97 * simple interrupt routine connected to the PP0 input on the demo board places
\r
98 * data in the queue each time the PP0 button is pushed (this button is built
\r
99 * onto the demo board). As the 'Button Push' task is created with a
\r
100 * relatively high priority it will unblock and want to execute as soon as data
\r
101 * arrives in the queue - resulting in a context switch within the PP0 input
\r
102 * ISR. If the data retrieved from the queue is that expected the 'Button Push'
\r
103 * task toggles LED 5. Therefore correct operation is indicated by the LED
\r
104 * toggling each time the PP0 button is pressed.
\r
106 * This test is not as satisfactory as the ComTest method - but the simple
\r
107 * nature of the port makes is just about adequate.
\r
111 /* Kernel includes. */
\r
112 #include "FreeRTOS.h"
\r
116 /* Demo application includes. */
\r
119 #include "dynamic.h"
\r
120 #include "partest.h"
\r
122 /* Processor expert includes. */
\r
123 #include "ButtonInterrupt.h"
\r
125 /*-----------------------------------------------------------
\r
127 -----------------------------------------------------------*/
\r
129 /* Priorities assigned to demo application tasks. */
\r
130 #define mainFLASH_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
131 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
132 #define mainBUTTON_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
133 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
135 /* LED that is toggled by the check task. The check task periodically checks
\r
136 that all the other tasks are operating without error. If no errors are found
\r
137 the LED is toggled with mainCHECK_PERIOD frequency. If an error is found
\r
138 then the toggle rate increases to mainERROR_CHECK_PERIOD. */
\r
139 #define mainCHECK_TASK_LED ( 7 )
\r
140 #define mainCHECK_PERIOD ( ( portTickType ) 3000 / portTICK_RATE_MS )
\r
141 #define mainERROR_CHECK_PERIOD ( ( portTickType ) 500 / portTICK_RATE_MS )
\r
143 /* LED that is toggled by the button push interrupt. */
\r
144 #define mainBUTTON_PUSH_LED ( 5 )
\r
146 /* The constants used in the idle task calculation. */
\r
147 #define intgCONST1 ( ( portLONG ) 123 )
\r
148 #define intgCONST2 ( ( portLONG ) 234567 )
\r
149 #define intgCONST3 ( ( portLONG ) -3 )
\r
150 #define intgCONST4 ( ( portLONG ) 7 )
\r
151 #define intgEXPECTED_ANSWER ( ( ( intgCONST1 + intgCONST2 ) * intgCONST3 ) / intgCONST4 )
\r
153 /* The length of the queue between is button push ISR and the Button Push task
\r
154 is greater than 1 to account for switch bounces generating multiple inputs. */
\r
155 #define mainBUTTON_QUEUE_SIZE 6
\r
157 /*-----------------------------------------------------------
\r
158 Local functions prototypes.
\r
159 -----------------------------------------------------------*/
\r
162 * The 'Check' task function. See the explanation at the top of the file.
\r
164 static void vErrorChecks( void* pvParameters );
\r
167 * The 'Button Push' task. See the explanation at the top of the file.
\r
169 static void vButtonTask( void *pvParameters );
\r
172 * The idle task hook - in which the integer task is implemented. See the
\r
173 * explanation at the top of the file.
\r
175 void vApplicationIdleHook( void );
\r
178 * Checks the unique counts of other tasks to ensure they are still operational.
\r
180 static portLONG prvCheckOtherTasksAreStillRunning( void );
\r
184 /*-----------------------------------------------------------
\r
186 -----------------------------------------------------------*/
\r
188 /* A few tasks are defined within this file. This flag is used to indicate
\r
189 their status. If an error is detected in one of the locally defined tasks then
\r
190 this flag is set to pdTRUE. */
\r
191 portBASE_TYPE xLocalError = pdFALSE;
\r
193 /* The queue used to send data from the button push ISR to the Button Push
\r
195 static xQueueHandle xButtonQueue;
\r
198 /*-----------------------------------------------------------*/
\r
201 * This is called from the main() function generated by the Processor Expert.
\r
205 /* Start some of the standard demo tasks. */
\r
206 vStartLEDFlashTasks( mainFLASH_PRIORITY );
\r
207 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
208 vStartDynamicPriorityTasks();
\r
210 /* Start the locally defined tasks. There is also a task implemented as
\r
212 xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
213 xTaskCreate( vButtonTask, "Button", configMINIMAL_STACK_SIZE, NULL, mainBUTTON_TASK_PRIORITY, NULL );
\r
215 /* All the tasks have been created - start the scheduler. */
\r
216 vTaskStartScheduler();
\r
218 /* Should not reach here! */
\r
221 /*-----------------------------------------------------------*/
\r
223 static void vErrorChecks( void *pvParameters )
\r
225 portTickType xDelayPeriod = mainCHECK_PERIOD;
\r
226 portTickType xLastWakeTime;
\r
228 /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()
\r
229 functions correctly. */
\r
230 xLastWakeTime = xTaskGetTickCount();
\r
234 /* Delay until it is time to execute again. The delay period is
\r
235 shorter following an error. */
\r
236 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );
\r
238 /* Check all the demo application tasks are executing without
\r
239 error. If an error is found the delay period is shortened - this
\r
240 has the effect of increasing the flash rate of the 'check' task
\r
242 if( prvCheckOtherTasksAreStillRunning() == pdFAIL )
\r
244 /* An error has been detected in one of the tasks - flash faster. */
\r
245 xDelayPeriod = mainERROR_CHECK_PERIOD;
\r
248 /* Toggle the LED each cycle round. */
\r
249 vParTestToggleLED( mainCHECK_TASK_LED );
\r
252 /*-----------------------------------------------------------*/
\r
254 static portLONG prvCheckOtherTasksAreStillRunning( void )
\r
256 portBASE_TYPE xAllTasksPassed = pdPASS;
\r
258 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
260 xAllTasksPassed = pdFAIL;
\r
263 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
265 xAllTasksPassed = pdFAIL;
\r
268 /* Also check the status flag for the tasks defined within this function. */
\r
269 if( xLocalError != pdFALSE )
\r
271 xAllTasksPassed = pdFAIL;
\r
274 return xAllTasksPassed;
\r
276 /*-----------------------------------------------------------*/
\r
278 void vApplicationIdleHook( void )
\r
280 /* This variable is effectively set to a constant so it is made volatile to
\r
281 ensure the compiler does not just get rid of it. */
\r
282 volatile portLONG lValue;
\r
284 /* Keep performing a calculation and checking the result against a constant. */
\r
287 /* Perform the calculation. This will store partial value in
\r
288 registers, resulting in a good test of the context switch mechanism. */
\r
289 lValue = intgCONST1;
\r
290 lValue += intgCONST2;
\r
291 lValue *= intgCONST3;
\r
292 lValue /= intgCONST4;
\r
294 /* Did we perform the calculation correctly with no corruption? */
\r
295 if( lValue != intgEXPECTED_ANSWER )
\r
298 portENTER_CRITICAL();
\r
299 xLocalError = pdTRUE;
\r
300 portEXIT_CRITICAL();
\r
303 /* Yield in case cooperative scheduling is being used. */
\r
304 #if configUSE_PREEMPTION == 0
\r
311 /*-----------------------------------------------------------*/
\r
313 static void vButtonTask( void *pvParameters )
\r
315 unsigned portBASE_TYPE uxExpected = 1, uxReceived;
\r
317 /* Create the queue used by the producer and consumer. */
\r
318 xButtonQueue = xQueueCreate( mainBUTTON_QUEUE_SIZE, ( unsigned portBASE_TYPE ) sizeof( unsigned portBASE_TYPE ) );
\r
322 /* Now the queue is created it is safe to enable the button interrupt. */
\r
323 ButtonInterrupt_Enable();
\r
327 /* Simply wait for data to arrive from the button push interrupt. */
\r
328 if( xQueueReceive( xButtonQueue, &uxReceived, portMAX_DELAY ) == pdPASS )
\r
330 /* Was the data we received that expected? */
\r
331 if( uxReceived != uxExpected )
\r
334 portENTER_CRITICAL();
\r
335 xLocalError = pdTRUE;
\r
336 portEXIT_CRITICAL();
\r
340 /* Toggle the LED for every successful push. */
\r
341 vParTestToggleLED( mainBUTTON_PUSH_LED );
\r
349 /* Will only get here if the queue could not be created. */
\r
352 /*-----------------------------------------------------------*/
\r
354 #pragma CODE_SEG __NEAR_SEG NON_BANKED
\r
356 /* Button push ISR. */
\r
357 void interrupt vButtonPush( void )
\r
359 static unsigned portBASE_TYPE uxValToSend = 0;
\r
360 static unsigned portLONG xHigherPriorityTaskWoken;
\r
362 xHigherPriorityTaskWoken = pdFALSE;
\r
364 /* Send an incrementing value to the button push task each run. */
\r
367 /* Clear the interrupt flag. */
\r
370 /* Send the incremented value down the queue. The button push task is
\r
371 blocked waiting for the data. As the button push task is high priority
\r
372 it will wake and a context switch should be performed before leaving
\r
374 xQueueSendFromISR( xButtonQueue, &uxValToSend, &xHigherPriorityTaskWoken );
\r
376 if( xHigherPriorityTaskWoken )
\r
378 /* NOTE: This macro can only be used if there are no local
\r
379 variables defined. This function uses a static variable so it's
\r
380 use is permitted. If the variable were not static portYIELD()
\r
381 would have to be used in it's place. */
\r
382 portTASK_SWITCH_FROM_ISR();
\r
386 #pragma CODE_SEG DEFAULT
\r