2 FreeRTOS V8.0.1 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
28 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
29 >>! obliged to provide the source code for proprietary components !<<
\r
30 >>! outside of the FreeRTOS kernel. !<<
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
69 * vMain() is effectively the demo application entry point. It is called by
\r
70 * the main() function generated by the Processor Expert application.
\r
72 * vMain() creates all the demo application tasks, then starts the scheduler.
\r
73 * The WEB documentation provides more details of the demo application tasks.
\r
75 * Main.c also creates a task called "Check". This only executes every three
\r
76 * seconds but has the highest priority so is guaranteed to get processor time.
\r
77 * Its main function is to check that all the other tasks are still operational.
\r
78 * Each task (other than the "flash" tasks) maintains a unique count that is
\r
79 * incremented each time the task successfully completes its function. Should
\r
80 * any error occur within such a task the count is permanently halted. The
\r
81 * check task inspects the count of each task to ensure it has changed since
\r
82 * the last time the check task executed. If all the count variables have
\r
83 * changed all the tasks are still executing error free, and the check task
\r
84 * toggles the onboard LED. Should any task contain an error at any time
\r
85 * the LED toggle rate will change from 3 seconds to 500ms.
\r
87 * This file also includes the functionality normally implemented within the
\r
88 * standard demo application file integer.c. Due to the limited memory
\r
89 * available on the microcontroller the functionality has been included within
\r
90 * the idle task hook [vApplicationIdleHook()] - instead of within the usual
\r
91 * separate task. See the documentation within integer.c for the rationale
\r
92 * of the integer task functionality.
\r
96 * The demo applications included with other FreeRTOS ports make use of the
\r
97 * standard ComTest tasks. These use a loopback connector to transmit and
\r
98 * receive RS232 characters between two tasks. The test is important for two
\r
101 * 1) It tests the mechanism of context switching from within an application
\r
104 * 2) It generates some randomised timing.
\r
106 * The demo board used to develop this port does not include an RS232 interface
\r
107 * so the ComTest tasks could not easily be included. Instead these two tests
\r
108 * are created using a 'Button Push' task.
\r
110 * The 'Button Push' task blocks on a queue, waiting for data to arrive. A
\r
111 * simple interrupt routine connected to the PP0 input on the demo board places
\r
112 * data in the queue each time the PP0 button is pushed (this button is built
\r
113 * onto the demo board). As the 'Button Push' task is created with a
\r
114 * relatively high priority it will unblock and want to execute as soon as data
\r
115 * arrives in the queue - resulting in a context switch within the PP0 input
\r
116 * ISR. If the data retrieved from the queue is that expected the 'Button Push'
\r
117 * task toggles LED 5. Therefore correct operation is indicated by the LED
\r
118 * toggling each time the PP0 button is pressed.
\r
120 * This test is not as satisfactory as the ComTest method - but the simple
\r
121 * nature of the port makes is just about adequate.
\r
125 /* Kernel includes. */
\r
126 #include "FreeRTOS.h"
\r
130 /* Demo application includes. */
\r
133 #include "dynamic.h"
\r
134 #include "partest.h"
\r
136 /* Processor expert includes. */
\r
137 #include "ButtonInterrupt.h"
\r
139 /*-----------------------------------------------------------
\r
141 -----------------------------------------------------------*/
\r
143 /* Priorities assigned to demo application tasks. */
\r
144 #define mainFLASH_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
145 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
146 #define mainBUTTON_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
147 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
149 /* LED that is toggled by the check task. The check task periodically checks
\r
150 that all the other tasks are operating without error. If no errors are found
\r
151 the LED is toggled with mainCHECK_PERIOD frequency. If an error is found
\r
152 then the toggle rate increases to mainERROR_CHECK_PERIOD. */
\r
153 #define mainCHECK_TASK_LED ( 7 )
\r
154 #define mainCHECK_PERIOD ( ( TickType_t ) 3000 / portTICK_PERIOD_MS )
\r
155 #define mainERROR_CHECK_PERIOD ( ( TickType_t ) 500 / portTICK_PERIOD_MS )
\r
157 /* LED that is toggled by the button push interrupt. */
\r
158 #define mainBUTTON_PUSH_LED ( 5 )
\r
160 /* The constants used in the idle task calculation. */
\r
161 #define intgCONST1 ( ( long ) 123 )
\r
162 #define intgCONST2 ( ( long ) 234567 )
\r
163 #define intgCONST3 ( ( long ) -3 )
\r
164 #define intgCONST4 ( ( long ) 7 )
\r
165 #define intgEXPECTED_ANSWER ( ( ( intgCONST1 + intgCONST2 ) * intgCONST3 ) / intgCONST4 )
\r
167 /* The length of the queue between is button push ISR and the Button Push task
\r
168 is greater than 1 to account for switch bounces generating multiple inputs. */
\r
169 #define mainBUTTON_QUEUE_SIZE 6
\r
171 /*-----------------------------------------------------------
\r
172 Local functions prototypes.
\r
173 -----------------------------------------------------------*/
\r
176 * The 'Check' task function. See the explanation at the top of the file.
\r
178 static void vErrorChecks( void* pvParameters );
\r
181 * The 'Button Push' task. See the explanation at the top of the file.
\r
183 static void vButtonTask( void *pvParameters );
\r
186 * The idle task hook - in which the integer task is implemented. See the
\r
187 * explanation at the top of the file.
\r
189 void vApplicationIdleHook( void );
\r
192 * Checks the unique counts of other tasks to ensure they are still operational.
\r
194 static long prvCheckOtherTasksAreStillRunning( void );
\r
198 /*-----------------------------------------------------------
\r
200 -----------------------------------------------------------*/
\r
202 /* A few tasks are defined within this file. This flag is used to indicate
\r
203 their status. If an error is detected in one of the locally defined tasks then
\r
204 this flag is set to pdTRUE. */
\r
205 portBASE_TYPE xLocalError = pdFALSE;
\r
207 /* The queue used to send data from the button push ISR to the Button Push
\r
209 static QueueHandle_t xButtonQueue;
\r
212 /*-----------------------------------------------------------*/
\r
215 * This is called from the main() function generated by the Processor Expert.
\r
219 /* Start some of the standard demo tasks. */
\r
220 vStartLEDFlashTasks( mainFLASH_PRIORITY );
\r
221 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
222 vStartDynamicPriorityTasks();
\r
224 /* Start the locally defined tasks. There is also a task implemented as
\r
226 xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
227 xTaskCreate( vButtonTask, "Button", configMINIMAL_STACK_SIZE, NULL, mainBUTTON_TASK_PRIORITY, NULL );
\r
229 /* All the tasks have been created - start the scheduler. */
\r
230 vTaskStartScheduler();
\r
232 /* Should not reach here! */
\r
235 /*-----------------------------------------------------------*/
\r
237 static void vErrorChecks( void *pvParameters )
\r
239 TickType_t xDelayPeriod = mainCHECK_PERIOD;
\r
240 TickType_t xLastWakeTime;
\r
242 /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()
\r
243 functions correctly. */
\r
244 xLastWakeTime = xTaskGetTickCount();
\r
248 /* Delay until it is time to execute again. The delay period is
\r
249 shorter following an error. */
\r
250 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );
\r
252 /* Check all the demo application tasks are executing without
\r
253 error. If an error is found the delay period is shortened - this
\r
254 has the effect of increasing the flash rate of the 'check' task
\r
256 if( prvCheckOtherTasksAreStillRunning() == pdFAIL )
\r
258 /* An error has been detected in one of the tasks - flash faster. */
\r
259 xDelayPeriod = mainERROR_CHECK_PERIOD;
\r
262 /* Toggle the LED each cycle round. */
\r
263 vParTestToggleLED( mainCHECK_TASK_LED );
\r
266 /*-----------------------------------------------------------*/
\r
268 static long prvCheckOtherTasksAreStillRunning( void )
\r
270 portBASE_TYPE xAllTasksPassed = pdPASS;
\r
272 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
274 xAllTasksPassed = pdFAIL;
\r
277 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
279 xAllTasksPassed = pdFAIL;
\r
282 /* Also check the status flag for the tasks defined within this function. */
\r
283 if( xLocalError != pdFALSE )
\r
285 xAllTasksPassed = pdFAIL;
\r
288 return xAllTasksPassed;
\r
290 /*-----------------------------------------------------------*/
\r
292 void vApplicationIdleHook( void )
\r
294 /* This variable is effectively set to a constant so it is made volatile to
\r
295 ensure the compiler does not just get rid of it. */
\r
296 volatile long lValue;
\r
298 /* Keep performing a calculation and checking the result against a constant. */
\r
301 /* Perform the calculation. This will store partial value in
\r
302 registers, resulting in a good test of the context switch mechanism. */
\r
303 lValue = intgCONST1;
\r
304 lValue += intgCONST2;
\r
305 lValue *= intgCONST3;
\r
306 lValue /= intgCONST4;
\r
308 /* Did we perform the calculation correctly with no corruption? */
\r
309 if( lValue != intgEXPECTED_ANSWER )
\r
312 portENTER_CRITICAL();
\r
313 xLocalError = pdTRUE;
\r
314 portEXIT_CRITICAL();
\r
317 /* Yield in case cooperative scheduling is being used. */
\r
318 #if configUSE_PREEMPTION == 0
\r
325 /*-----------------------------------------------------------*/
\r
327 static void vButtonTask( void *pvParameters )
\r
329 unsigned portBASE_TYPE uxExpected = 1, uxReceived;
\r
331 /* Create the queue used by the producer and consumer. */
\r
332 xButtonQueue = xQueueCreate( mainBUTTON_QUEUE_SIZE, ( unsigned portBASE_TYPE ) sizeof( unsigned portBASE_TYPE ) );
\r
336 /* Now the queue is created it is safe to enable the button interrupt. */
\r
337 ButtonInterrupt_Enable();
\r
341 /* Simply wait for data to arrive from the button push interrupt. */
\r
342 if( xQueueReceive( xButtonQueue, &uxReceived, portMAX_DELAY ) == pdPASS )
\r
344 /* Was the data we received that expected? */
\r
345 if( uxReceived != uxExpected )
\r
348 portENTER_CRITICAL();
\r
349 xLocalError = pdTRUE;
\r
350 portEXIT_CRITICAL();
\r
354 /* Toggle the LED for every successful push. */
\r
355 vParTestToggleLED( mainBUTTON_PUSH_LED );
\r
363 /* Will only get here if the queue could not be created. */
\r
366 /*-----------------------------------------------------------*/
\r
368 #pragma CODE_SEG __NEAR_SEG NON_BANKED
\r
370 /* Button push ISR. */
\r
371 void interrupt vButtonPush( void )
\r
373 static unsigned portBASE_TYPE uxValToSend = 0;
\r
374 static unsigned long xHigherPriorityTaskWoken;
\r
376 xHigherPriorityTaskWoken = pdFALSE;
\r
378 /* Send an incrementing value to the button push task each run. */
\r
381 /* Clear the interrupt flag. */
\r
384 /* Send the incremented value down the queue. The button push task is
\r
385 blocked waiting for the data. As the button push task is high priority
\r
386 it will wake and a context switch should be performed before leaving
\r
388 xQueueSendFromISR( xButtonQueue, &uxValToSend, &xHigherPriorityTaskWoken );
\r
390 if( xHigherPriorityTaskWoken )
\r
392 /* NOTE: This macro can only be used if there are no local
\r
393 variables defined. This function uses a static variable so it's
\r
394 use is permitted. If the variable were not static portYIELD()
\r
395 would have to be used in it's place. */
\r
396 portTASK_SWITCH_FROM_ISR();
\r
400 #pragma CODE_SEG DEFAULT
\r