2 FreeRTOS V7.2.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
70 * vMain() is effectively the demo application entry point. It is called by
\r
71 * the main() function generated by the Processor Expert application.
\r
73 * vMain() creates all the demo application tasks, then starts the scheduler.
\r
74 * The WEB documentation provides more details of the demo application tasks.
\r
76 * Main.c also creates a task called "Check". This only executes every three
\r
77 * seconds but has the highest priority so is guaranteed to get processor time.
\r
78 * Its main function is to check that all the other tasks are still operational.
\r
79 * Each task (other than the "flash" tasks) maintains a unique count that is
\r
80 * incremented each time the task successfully completes its function. Should
\r
81 * any error occur within such a task the count is permanently halted. The
\r
82 * check task inspects the count of each task to ensure it has changed since
\r
83 * the last time the check task executed. If all the count variables have
\r
84 * changed all the tasks are still executing error free, and the check task
\r
85 * toggles the onboard LED. Should any task contain an error at any time
\r
86 * the LED toggle rate will change from 3 seconds to 500ms.
\r
88 * This file also includes the functionality normally implemented within the
\r
89 * standard demo application file integer.c. Due to the limited memory
\r
90 * available on the microcontroller the functionality has been included within
\r
91 * the idle task hook [vApplicationIdleHook()] - instead of within the usual
\r
92 * separate task. See the documentation within integer.c for the rationale
\r
93 * of the integer task functionality.
\r
97 * The demo applications included with other FreeRTOS ports make use of the
\r
98 * standard ComTest tasks. These use a loopback connector to transmit and
\r
99 * receive RS232 characters between two tasks. The test is important for two
\r
102 * 1) It tests the mechanism of context switching from within an application
\r
105 * 2) It generates some randomised timing.
\r
107 * The demo board used to develop this port does not include an RS232 interface
\r
108 * so the ComTest tasks could not easily be included. Instead these two tests
\r
109 * are created using a 'Button Push' task.
\r
111 * The 'Button Push' task blocks on a queue, waiting for data to arrive. A
\r
112 * simple interrupt routine connected to the PP0 input on the demo board places
\r
113 * data in the queue each time the PP0 button is pushed (this button is built
\r
114 * onto the demo board). As the 'Button Push' task is created with a
\r
115 * relatively high priority it will unblock and want to execute as soon as data
\r
116 * arrives in the queue - resulting in a context switch within the PP0 input
\r
117 * ISR. If the data retrieved from the queue is that expected the 'Button Push'
\r
118 * task toggles LED 5. Therefore correct operation is indicated by the LED
\r
119 * toggling each time the PP0 button is pressed.
\r
121 * This test is not as satisfactory as the ComTest method - but the simple
\r
122 * nature of the port makes is just about adequate.
\r
126 /* Kernel includes. */
\r
127 #include "FreeRTOS.h"
\r
131 /* Demo application includes. */
\r
134 #include "dynamic.h"
\r
135 #include "partest.h"
\r
137 /* Processor expert includes. */
\r
138 #include "ButtonInterrupt.h"
\r
140 /*-----------------------------------------------------------
\r
142 -----------------------------------------------------------*/
\r
144 /* Priorities assigned to demo application tasks. */
\r
145 #define mainFLASH_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
146 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
147 #define mainBUTTON_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
148 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
150 /* LED that is toggled by the check task. The check task periodically checks
\r
151 that all the other tasks are operating without error. If no errors are found
\r
152 the LED is toggled with mainCHECK_PERIOD frequency. If an error is found
\r
153 then the toggle rate increases to mainERROR_CHECK_PERIOD. */
\r
154 #define mainCHECK_TASK_LED ( 7 )
\r
155 #define mainCHECK_PERIOD ( ( portTickType ) 3000 / portTICK_RATE_MS )
\r
156 #define mainERROR_CHECK_PERIOD ( ( portTickType ) 500 / portTICK_RATE_MS )
\r
158 /* LED that is toggled by the button push interrupt. */
\r
159 #define mainBUTTON_PUSH_LED ( 5 )
\r
161 /* The constants used in the idle task calculation. */
\r
162 #define intgCONST1 ( ( long ) 123 )
\r
163 #define intgCONST2 ( ( long ) 234567 )
\r
164 #define intgCONST3 ( ( long ) -3 )
\r
165 #define intgCONST4 ( ( long ) 7 )
\r
166 #define intgEXPECTED_ANSWER ( ( ( intgCONST1 + intgCONST2 ) * intgCONST3 ) / intgCONST4 )
\r
168 /* The length of the queue between is button push ISR and the Button Push task
\r
169 is greater than 1 to account for switch bounces generating multiple inputs. */
\r
170 #define mainBUTTON_QUEUE_SIZE 6
\r
172 /*-----------------------------------------------------------
\r
173 Local functions prototypes.
\r
174 -----------------------------------------------------------*/
\r
177 * The 'Check' task function. See the explanation at the top of the file.
\r
179 static void vErrorChecks( void* pvParameters );
\r
182 * The 'Button Push' task. See the explanation at the top of the file.
\r
184 static void vButtonTask( void *pvParameters );
\r
187 * The idle task hook - in which the integer task is implemented. See the
\r
188 * explanation at the top of the file.
\r
190 void vApplicationIdleHook( void );
\r
193 * Checks the unique counts of other tasks to ensure they are still operational.
\r
195 static long prvCheckOtherTasksAreStillRunning( void );
\r
199 /*-----------------------------------------------------------
\r
201 -----------------------------------------------------------*/
\r
203 /* A few tasks are defined within this file. This flag is used to indicate
\r
204 their status. If an error is detected in one of the locally defined tasks then
\r
205 this flag is set to pdTRUE. */
\r
206 portBASE_TYPE xLocalError = pdFALSE;
\r
208 /* The queue used to send data from the button push ISR to the Button Push
\r
210 static xQueueHandle xButtonQueue;
\r
213 /*-----------------------------------------------------------*/
\r
216 * This is called from the main() function generated by the Processor Expert.
\r
220 /* Start some of the standard demo tasks. */
\r
221 vStartLEDFlashTasks( mainFLASH_PRIORITY );
\r
222 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
223 vStartDynamicPriorityTasks();
\r
225 /* Start the locally defined tasks. There is also a task implemented as
\r
227 xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
228 xTaskCreate( vButtonTask, "Button", configMINIMAL_STACK_SIZE, NULL, mainBUTTON_TASK_PRIORITY, NULL );
\r
230 /* All the tasks have been created - start the scheduler. */
\r
231 vTaskStartScheduler();
\r
233 /* Should not reach here! */
\r
236 /*-----------------------------------------------------------*/
\r
238 static void vErrorChecks( void *pvParameters )
\r
240 portTickType xDelayPeriod = mainCHECK_PERIOD;
\r
241 portTickType xLastWakeTime;
\r
243 /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()
\r
244 functions correctly. */
\r
245 xLastWakeTime = xTaskGetTickCount();
\r
249 /* Delay until it is time to execute again. The delay period is
\r
250 shorter following an error. */
\r
251 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );
\r
253 /* Check all the demo application tasks are executing without
\r
254 error. If an error is found the delay period is shortened - this
\r
255 has the effect of increasing the flash rate of the 'check' task
\r
257 if( prvCheckOtherTasksAreStillRunning() == pdFAIL )
\r
259 /* An error has been detected in one of the tasks - flash faster. */
\r
260 xDelayPeriod = mainERROR_CHECK_PERIOD;
\r
263 /* Toggle the LED each cycle round. */
\r
264 vParTestToggleLED( mainCHECK_TASK_LED );
\r
267 /*-----------------------------------------------------------*/
\r
269 static long prvCheckOtherTasksAreStillRunning( void )
\r
271 portBASE_TYPE xAllTasksPassed = pdPASS;
\r
273 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
275 xAllTasksPassed = pdFAIL;
\r
278 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
280 xAllTasksPassed = pdFAIL;
\r
283 /* Also check the status flag for the tasks defined within this function. */
\r
284 if( xLocalError != pdFALSE )
\r
286 xAllTasksPassed = pdFAIL;
\r
289 return xAllTasksPassed;
\r
291 /*-----------------------------------------------------------*/
\r
293 void vApplicationIdleHook( void )
\r
295 /* This variable is effectively set to a constant so it is made volatile to
\r
296 ensure the compiler does not just get rid of it. */
\r
297 volatile long lValue;
\r
299 /* Keep performing a calculation and checking the result against a constant. */
\r
302 /* Perform the calculation. This will store partial value in
\r
303 registers, resulting in a good test of the context switch mechanism. */
\r
304 lValue = intgCONST1;
\r
305 lValue += intgCONST2;
\r
306 lValue *= intgCONST3;
\r
307 lValue /= intgCONST4;
\r
309 /* Did we perform the calculation correctly with no corruption? */
\r
310 if( lValue != intgEXPECTED_ANSWER )
\r
313 portENTER_CRITICAL();
\r
314 xLocalError = pdTRUE;
\r
315 portEXIT_CRITICAL();
\r
318 /* Yield in case cooperative scheduling is being used. */
\r
319 #if configUSE_PREEMPTION == 0
\r
326 /*-----------------------------------------------------------*/
\r
328 static void vButtonTask( void *pvParameters )
\r
330 unsigned portBASE_TYPE uxExpected = 1, uxReceived;
\r
332 /* Create the queue used by the producer and consumer. */
\r
333 xButtonQueue = xQueueCreate( mainBUTTON_QUEUE_SIZE, ( unsigned portBASE_TYPE ) sizeof( unsigned portBASE_TYPE ) );
\r
337 /* Now the queue is created it is safe to enable the button interrupt. */
\r
338 ButtonInterrupt_Enable();
\r
342 /* Simply wait for data to arrive from the button push interrupt. */
\r
343 if( xQueueReceive( xButtonQueue, &uxReceived, portMAX_DELAY ) == pdPASS )
\r
345 /* Was the data we received that expected? */
\r
346 if( uxReceived != uxExpected )
\r
349 portENTER_CRITICAL();
\r
350 xLocalError = pdTRUE;
\r
351 portEXIT_CRITICAL();
\r
355 /* Toggle the LED for every successful push. */
\r
356 vParTestToggleLED( mainBUTTON_PUSH_LED );
\r
364 /* Will only get here if the queue could not be created. */
\r
367 /*-----------------------------------------------------------*/
\r
369 #pragma CODE_SEG __NEAR_SEG NON_BANKED
\r
371 /* Button push ISR. */
\r
372 void interrupt vButtonPush( void )
\r
374 static unsigned portBASE_TYPE uxValToSend = 0;
\r
375 static unsigned long xHigherPriorityTaskWoken;
\r
377 xHigherPriorityTaskWoken = pdFALSE;
\r
379 /* Send an incrementing value to the button push task each run. */
\r
382 /* Clear the interrupt flag. */
\r
385 /* Send the incremented value down the queue. The button push task is
\r
386 blocked waiting for the data. As the button push task is high priority
\r
387 it will wake and a context switch should be performed before leaving
\r
389 xQueueSendFromISR( xButtonQueue, &uxValToSend, &xHigherPriorityTaskWoken );
\r
391 if( xHigherPriorityTaskWoken )
\r
393 /* NOTE: This macro can only be used if there are no local
\r
394 variables defined. This function uses a static variable so it's
\r
395 use is permitted. If the variable were not static portYIELD()
\r
396 would have to be used in it's place. */
\r
397 portTASK_SWITCH_FROM_ISR();
\r
401 #pragma CODE_SEG DEFAULT
\r