2 FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.
\r
4 This file is part of the FreeRTOS distribution.
\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
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
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
26 ***************************************************************************
\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
32 ***************************************************************************
\r
36 Please ensure to read the configuration and relevant port sections of the
\r
37 online documentation.
\r
39 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
42 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
45 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
46 licensing and training services.
\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
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
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
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
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
84 * 1) It tests the mechanism of context switching from within an application
\r
87 * 2) It generates some randomised timing.
\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
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
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
108 /* Kernel includes. */
\r
109 #include "FreeRTOS.h"
\r
113 /* Demo application includes. */
\r
116 #include "dynamic.h"
\r
117 #include "partest.h"
\r
119 /* Processor expert includes. */
\r
120 #include "ButtonInterrupt.h"
\r
122 /*-----------------------------------------------------------
\r
124 -----------------------------------------------------------*/
\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
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
140 /* LED that is toggled by the button push interrupt. */
\r
141 #define mainBUTTON_PUSH_LED ( 5 )
\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
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
154 /*-----------------------------------------------------------
\r
155 Local functions prototypes.
\r
156 -----------------------------------------------------------*/
\r
159 * The 'Check' task function. See the explanation at the top of the file.
\r
161 static void vErrorChecks( void* pvParameters );
\r
164 * The 'Button Push' task. See the explanation at the top of the file.
\r
166 static void vButtonTask( void *pvParameters );
\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
172 void vApplicationIdleHook( void );
\r
175 * Checks the unique counts of other tasks to ensure they are still operational.
\r
177 static long prvCheckOtherTasksAreStillRunning( void );
\r
181 /*-----------------------------------------------------------
\r
183 -----------------------------------------------------------*/
\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
190 /* The queue used to send data from the button push ISR to the Button Push
\r
192 static xQueueHandle xButtonQueue;
\r
195 /*-----------------------------------------------------------*/
\r
198 * This is called from the main() function generated by the Processor Expert.
\r
202 /* Start some of the standard demo tasks. */
\r
203 vStartLEDFlashTasks( mainFLASH_PRIORITY );
\r
204 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
205 vStartDynamicPriorityTasks();
\r
207 /* Start the locally defined tasks. There is also a task implemented as
\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
212 /* All the tasks have been created - start the scheduler. */
\r
213 vTaskStartScheduler();
\r
215 /* Should not reach here! */
\r
218 /*-----------------------------------------------------------*/
\r
220 static void vErrorChecks( void *pvParameters )
\r
222 portTickType xDelayPeriod = mainCHECK_PERIOD;
\r
223 portTickType xLastWakeTime;
\r
225 /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()
\r
226 functions correctly. */
\r
227 xLastWakeTime = xTaskGetTickCount();
\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
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
239 if( prvCheckOtherTasksAreStillRunning() == pdFAIL )
\r
241 /* An error has been detected in one of the tasks - flash faster. */
\r
242 xDelayPeriod = mainERROR_CHECK_PERIOD;
\r
245 /* Toggle the LED each cycle round. */
\r
246 vParTestToggleLED( mainCHECK_TASK_LED );
\r
249 /*-----------------------------------------------------------*/
\r
251 static long prvCheckOtherTasksAreStillRunning( void )
\r
253 portBASE_TYPE xAllTasksPassed = pdPASS;
\r
255 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
257 xAllTasksPassed = pdFAIL;
\r
260 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
262 xAllTasksPassed = pdFAIL;
\r
265 /* Also check the status flag for the tasks defined within this function. */
\r
266 if( xLocalError != pdFALSE )
\r
268 xAllTasksPassed = pdFAIL;
\r
271 return xAllTasksPassed;
\r
273 /*-----------------------------------------------------------*/
\r
275 void vApplicationIdleHook( void )
\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
281 /* Keep performing a calculation and checking the result against a constant. */
\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
291 /* Did we perform the calculation correctly with no corruption? */
\r
292 if( lValue != intgEXPECTED_ANSWER )
\r
295 portENTER_CRITICAL();
\r
296 xLocalError = pdTRUE;
\r
297 portEXIT_CRITICAL();
\r
300 /* Yield in case cooperative scheduling is being used. */
\r
301 #if configUSE_PREEMPTION == 0
\r
308 /*-----------------------------------------------------------*/
\r
310 static void vButtonTask( void *pvParameters )
\r
312 unsigned portBASE_TYPE uxExpected = 1, uxReceived;
\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
319 /* Now the queue is created it is safe to enable the button interrupt. */
\r
320 ButtonInterrupt_Enable();
\r
324 /* Simply wait for data to arrive from the button push interrupt. */
\r
325 if( xQueueReceive( xButtonQueue, &uxReceived, portMAX_DELAY ) == pdPASS )
\r
327 /* Was the data we received that expected? */
\r
328 if( uxReceived != uxExpected )
\r
331 portENTER_CRITICAL();
\r
332 xLocalError = pdTRUE;
\r
333 portEXIT_CRITICAL();
\r
337 /* Toggle the LED for every successful push. */
\r
338 vParTestToggleLED( mainBUTTON_PUSH_LED );
\r
346 /* Will only get here if the queue could not be created. */
\r
349 /*-----------------------------------------------------------*/
\r
351 #pragma CODE_SEG __NEAR_SEG NON_BANKED
\r
353 /* Button push ISR. */
\r
354 void interrupt vButtonPush( void )
\r
356 static unsigned portBASE_TYPE uxValToSend = 0;
\r
357 static unsigned long xHigherPriorityTaskWoken;
\r
359 xHigherPriorityTaskWoken = pdFALSE;
\r
361 /* Send an incrementing value to the button push task each run. */
\r
364 /* Clear the interrupt flag. */
\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
371 xQueueSendFromISR( xButtonQueue, &uxValToSend, &xHigherPriorityTaskWoken );
\r
373 if( xHigherPriorityTaskWoken )
\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
383 #pragma CODE_SEG DEFAULT
\r