2 FreeRTOS V7.5.2 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
68 * vMain() is effectively the demo application entry point. It is called by
\r
69 * the main() function generated by the Processor Expert application.
\r
71 * vMain() creates all the demo application tasks, then starts the scheduler.
\r
72 * The WEB documentation provides more details of the demo application tasks.
\r
74 * Main.c also creates a task called "Check". This only executes every three
\r
75 * seconds but has the highest priority so is guaranteed to get processor time.
\r
76 * Its main function is to check that all the other tasks are still operational.
\r
77 * Each task (other than the "flash" tasks) maintains a unique count that is
\r
78 * incremented each time the task successfully completes its function. Should
\r
79 * any error occur within such a task the count is permanently halted. The
\r
80 * check task inspects the count of each task to ensure it has changed since
\r
81 * the last time the check task executed. If all the count variables have
\r
82 * changed all the tasks are still executing error free, and the check task
\r
83 * toggles the onboard LED. Should any task contain an error at any time
\r
84 * the LED toggle rate will change from 3 seconds to 500ms.
\r
86 * This file also includes the functionality normally implemented within the
\r
87 * standard demo application file integer.c. Due to the limited memory
\r
88 * available on the microcontroller the functionality has been included within
\r
89 * the idle task hook [vApplicationIdleHook()] - instead of within the usual
\r
90 * separate task. See the documentation within integer.c for the rationale
\r
91 * of the integer task functionality.
\r
95 * The demo applications included with other FreeRTOS ports make use of the
\r
96 * standard ComTest tasks. These use a loopback connector to transmit and
\r
97 * receive RS232 characters between two tasks. The test is important for two
\r
100 * 1) It tests the mechanism of context switching from within an application
\r
103 * 2) It generates some randomised timing.
\r
105 * The demo board used to develop this port does not include an RS232 interface
\r
106 * so the ComTest tasks could not easily be included. Instead these two tests
\r
107 * are created using a 'Button Push' task.
\r
109 * The 'Button Push' task blocks on a queue, waiting for data to arrive. A
\r
110 * simple interrupt routine connected to the PP0 input on the demo board places
\r
111 * data in the queue each time the PP0 button is pushed (this button is built
\r
112 * onto the demo board). As the 'Button Push' task is created with a
\r
113 * relatively high priority it will unblock and want to execute as soon as data
\r
114 * arrives in the queue - resulting in a context switch within the PP0 input
\r
115 * ISR. If the data retrieved from the queue is that expected the 'Button Push'
\r
116 * task toggles LED 5. Therefore correct operation is indicated by the LED
\r
117 * toggling each time the PP0 button is pressed.
\r
119 * This test is not as satisfactory as the ComTest method - but the simple
\r
120 * nature of the port makes is just about adequate.
\r
124 /* Kernel includes. */
\r
125 #include "FreeRTOS.h"
\r
129 /* Demo application includes. */
\r
132 #include "dynamic.h"
\r
133 #include "partest.h"
\r
135 /* Processor expert includes. */
\r
136 #include "ButtonInterrupt.h"
\r
138 /*-----------------------------------------------------------
\r
140 -----------------------------------------------------------*/
\r
142 /* Priorities assigned to demo application tasks. */
\r
143 #define mainFLASH_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
144 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
145 #define mainBUTTON_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
146 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
148 /* LED that is toggled by the check task. The check task periodically checks
\r
149 that all the other tasks are operating without error. If no errors are found
\r
150 the LED is toggled with mainCHECK_PERIOD frequency. If an error is found
\r
151 then the toggle rate increases to mainERROR_CHECK_PERIOD. */
\r
152 #define mainCHECK_TASK_LED ( 7 )
\r
153 #define mainCHECK_PERIOD ( ( portTickType ) 3000 / portTICK_RATE_MS )
\r
154 #define mainERROR_CHECK_PERIOD ( ( portTickType ) 500 / portTICK_RATE_MS )
\r
156 /* LED that is toggled by the button push interrupt. */
\r
157 #define mainBUTTON_PUSH_LED ( 5 )
\r
159 /* The constants used in the idle task calculation. */
\r
160 #define intgCONST1 ( ( long ) 123 )
\r
161 #define intgCONST2 ( ( long ) 234567 )
\r
162 #define intgCONST3 ( ( long ) -3 )
\r
163 #define intgCONST4 ( ( long ) 7 )
\r
164 #define intgEXPECTED_ANSWER ( ( ( intgCONST1 + intgCONST2 ) * intgCONST3 ) / intgCONST4 )
\r
166 /* The length of the queue between is button push ISR and the Button Push task
\r
167 is greater than 1 to account for switch bounces generating multiple inputs. */
\r
168 #define mainBUTTON_QUEUE_SIZE 6
\r
170 /*-----------------------------------------------------------
\r
171 Local functions prototypes.
\r
172 -----------------------------------------------------------*/
\r
175 * The 'Check' task function. See the explanation at the top of the file.
\r
177 static void vErrorChecks( void* pvParameters );
\r
180 * The 'Button Push' task. See the explanation at the top of the file.
\r
182 static void vButtonTask( void *pvParameters );
\r
185 * The idle task hook - in which the integer task is implemented. See the
\r
186 * explanation at the top of the file.
\r
188 void vApplicationIdleHook( void );
\r
191 * Checks the unique counts of other tasks to ensure they are still operational.
\r
193 static long prvCheckOtherTasksAreStillRunning( void );
\r
197 /*-----------------------------------------------------------
\r
199 -----------------------------------------------------------*/
\r
201 /* A few tasks are defined within this file. This flag is used to indicate
\r
202 their status. If an error is detected in one of the locally defined tasks then
\r
203 this flag is set to pdTRUE. */
\r
204 portBASE_TYPE xLocalError = pdFALSE;
\r
206 /* The queue used to send data from the button push ISR to the Button Push
\r
208 static xQueueHandle xButtonQueue;
\r
211 /*-----------------------------------------------------------*/
\r
214 * This is called from the main() function generated by the Processor Expert.
\r
218 /* Start some of the standard demo tasks. */
\r
219 vStartLEDFlashTasks( mainFLASH_PRIORITY );
\r
220 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
221 vStartDynamicPriorityTasks();
\r
223 /* Start the locally defined tasks. There is also a task implemented as
\r
225 xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
226 xTaskCreate( vButtonTask, "Button", configMINIMAL_STACK_SIZE, NULL, mainBUTTON_TASK_PRIORITY, NULL );
\r
228 /* All the tasks have been created - start the scheduler. */
\r
229 vTaskStartScheduler();
\r
231 /* Should not reach here! */
\r
234 /*-----------------------------------------------------------*/
\r
236 static void vErrorChecks( void *pvParameters )
\r
238 portTickType xDelayPeriod = mainCHECK_PERIOD;
\r
239 portTickType xLastWakeTime;
\r
241 /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()
\r
242 functions correctly. */
\r
243 xLastWakeTime = xTaskGetTickCount();
\r
247 /* Delay until it is time to execute again. The delay period is
\r
248 shorter following an error. */
\r
249 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );
\r
251 /* Check all the demo application tasks are executing without
\r
252 error. If an error is found the delay period is shortened - this
\r
253 has the effect of increasing the flash rate of the 'check' task
\r
255 if( prvCheckOtherTasksAreStillRunning() == pdFAIL )
\r
257 /* An error has been detected in one of the tasks - flash faster. */
\r
258 xDelayPeriod = mainERROR_CHECK_PERIOD;
\r
261 /* Toggle the LED each cycle round. */
\r
262 vParTestToggleLED( mainCHECK_TASK_LED );
\r
265 /*-----------------------------------------------------------*/
\r
267 static long prvCheckOtherTasksAreStillRunning( void )
\r
269 portBASE_TYPE xAllTasksPassed = pdPASS;
\r
271 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
273 xAllTasksPassed = pdFAIL;
\r
276 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
278 xAllTasksPassed = pdFAIL;
\r
281 /* Also check the status flag for the tasks defined within this function. */
\r
282 if( xLocalError != pdFALSE )
\r
284 xAllTasksPassed = pdFAIL;
\r
287 return xAllTasksPassed;
\r
289 /*-----------------------------------------------------------*/
\r
291 void vApplicationIdleHook( void )
\r
293 /* This variable is effectively set to a constant so it is made volatile to
\r
294 ensure the compiler does not just get rid of it. */
\r
295 volatile long lValue;
\r
297 /* Keep performing a calculation and checking the result against a constant. */
\r
300 /* Perform the calculation. This will store partial value in
\r
301 registers, resulting in a good test of the context switch mechanism. */
\r
302 lValue = intgCONST1;
\r
303 lValue += intgCONST2;
\r
304 lValue *= intgCONST3;
\r
305 lValue /= intgCONST4;
\r
307 /* Did we perform the calculation correctly with no corruption? */
\r
308 if( lValue != intgEXPECTED_ANSWER )
\r
311 portENTER_CRITICAL();
\r
312 xLocalError = pdTRUE;
\r
313 portEXIT_CRITICAL();
\r
316 /* Yield in case cooperative scheduling is being used. */
\r
317 #if configUSE_PREEMPTION == 0
\r
324 /*-----------------------------------------------------------*/
\r
326 static void vButtonTask( void *pvParameters )
\r
328 unsigned portBASE_TYPE uxExpected = 1, uxReceived;
\r
330 /* Create the queue used by the producer and consumer. */
\r
331 xButtonQueue = xQueueCreate( mainBUTTON_QUEUE_SIZE, ( unsigned portBASE_TYPE ) sizeof( unsigned portBASE_TYPE ) );
\r
335 /* Now the queue is created it is safe to enable the button interrupt. */
\r
336 ButtonInterrupt_Enable();
\r
340 /* Simply wait for data to arrive from the button push interrupt. */
\r
341 if( xQueueReceive( xButtonQueue, &uxReceived, portMAX_DELAY ) == pdPASS )
\r
343 /* Was the data we received that expected? */
\r
344 if( uxReceived != uxExpected )
\r
347 portENTER_CRITICAL();
\r
348 xLocalError = pdTRUE;
\r
349 portEXIT_CRITICAL();
\r
353 /* Toggle the LED for every successful push. */
\r
354 vParTestToggleLED( mainBUTTON_PUSH_LED );
\r
362 /* Will only get here if the queue could not be created. */
\r
365 /*-----------------------------------------------------------*/
\r
367 #pragma CODE_SEG __NEAR_SEG NON_BANKED
\r
369 /* Button push ISR. */
\r
370 void interrupt vButtonPush( void )
\r
372 static unsigned portBASE_TYPE uxValToSend = 0;
\r
373 static unsigned long xHigherPriorityTaskWoken;
\r
375 xHigherPriorityTaskWoken = pdFALSE;
\r
377 /* Send an incrementing value to the button push task each run. */
\r
380 /* Clear the interrupt flag. */
\r
383 /* Send the incremented value down the queue. The button push task is
\r
384 blocked waiting for the data. As the button push task is high priority
\r
385 it will wake and a context switch should be performed before leaving
\r
387 xQueueSendFromISR( xButtonQueue, &uxValToSend, &xHigherPriorityTaskWoken );
\r
389 if( xHigherPriorityTaskWoken )
\r
391 /* NOTE: This macro can only be used if there are no local
\r
392 variables defined. This function uses a static variable so it's
\r
393 use is permitted. If the variable were not static portYIELD()
\r
394 would have to be used in it's place. */
\r
395 portTASK_SWITCH_FROM_ISR();
\r
399 #pragma CODE_SEG DEFAULT
\r