2 FreeRTOS V9.0.0rc1 - Copyright (C) 2016 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
73 * vMain() is effectively the demo application entry point. It is called by
\r
74 * the main() function generated by the Processor Expert application.
\r
76 * vMain() creates all the demo application tasks, then starts the scheduler.
\r
77 * The WEB documentation provides more details of the demo application tasks.
\r
79 * Main.c also creates a task called "Check". This only executes every three
\r
80 * seconds but has the highest priority so is guaranteed to get processor time.
\r
81 * Its main function is to check that all the other tasks are still operational.
\r
82 * Each task (other than the "flash" tasks) maintains a unique count that is
\r
83 * incremented each time the task successfully completes its function. Should
\r
84 * any error occur within such a task the count is permanently halted. The
\r
85 * check task inspects the count of each task to ensure it has changed since
\r
86 * the last time the check task executed. If all the count variables have
\r
87 * changed all the tasks are still executing error free, and the check task
\r
88 * toggles the onboard LED. Should any task contain an error at any time
\r
89 * the LED toggle rate will change from 3 seconds to 500ms.
\r
91 * This file also includes the functionality normally implemented within the
\r
92 * standard demo application file integer.c. Due to the limited memory
\r
93 * available on the microcontroller the functionality has been included within
\r
94 * the idle task hook [vApplicationIdleHook()] - instead of within the usual
\r
95 * separate task. See the documentation within integer.c for the rationale
\r
96 * of the integer task functionality.
\r
100 * The demo applications included with other FreeRTOS ports make use of the
\r
101 * standard ComTest tasks. These use a loopback connector to transmit and
\r
102 * receive RS232 characters between two tasks. The test is important for two
\r
105 * 1) It tests the mechanism of context switching from within an application
\r
108 * 2) It generates some randomised timing.
\r
110 * The demo board used to develop this port does not include an RS232 interface
\r
111 * so the ComTest tasks could not easily be included. Instead these two tests
\r
112 * are created using a 'Button Push' task.
\r
114 * The 'Button Push' task blocks on a queue, waiting for data to arrive. A
\r
115 * simple interrupt routine connected to the PP0 input on the demo board places
\r
116 * data in the queue each time the PP0 button is pushed (this button is built
\r
117 * onto the demo board). As the 'Button Push' task is created with a
\r
118 * relatively high priority it will unblock and want to execute as soon as data
\r
119 * arrives in the queue - resulting in a context switch within the PP0 input
\r
120 * ISR. If the data retrieved from the queue is that expected the 'Button Push'
\r
121 * task toggles LED 5. Therefore correct operation is indicated by the LED
\r
122 * toggling each time the PP0 button is pressed.
\r
124 * This test is not as satisfactory as the ComTest method - but the simple
\r
125 * nature of the port makes is just about adequate.
\r
129 /* Kernel includes. */
\r
130 #include "FreeRTOS.h"
\r
134 /* Demo application includes. */
\r
137 #include "dynamic.h"
\r
138 #include "partest.h"
\r
140 /* Processor expert includes. */
\r
141 #include "ButtonInterrupt.h"
\r
143 /*-----------------------------------------------------------
\r
145 -----------------------------------------------------------*/
\r
147 /* Priorities assigned to demo application tasks. */
\r
148 #define mainFLASH_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
149 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
150 #define mainBUTTON_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
151 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
153 /* LED that is toggled by the check task. The check task periodically checks
\r
154 that all the other tasks are operating without error. If no errors are found
\r
155 the LED is toggled with mainCHECK_PERIOD frequency. If an error is found
\r
156 then the toggle rate increases to mainERROR_CHECK_PERIOD. */
\r
157 #define mainCHECK_TASK_LED ( 7 )
\r
158 #define mainCHECK_PERIOD ( ( TickType_t ) 3000 / portTICK_PERIOD_MS )
\r
159 #define mainERROR_CHECK_PERIOD ( ( TickType_t ) 500 / portTICK_PERIOD_MS )
\r
161 /* LED that is toggled by the button push interrupt. */
\r
162 #define mainBUTTON_PUSH_LED ( 5 )
\r
164 /* The constants used in the idle task calculation. */
\r
165 #define intgCONST1 ( ( long ) 123 )
\r
166 #define intgCONST2 ( ( long ) 234567 )
\r
167 #define intgCONST3 ( ( long ) -3 )
\r
168 #define intgCONST4 ( ( long ) 7 )
\r
169 #define intgEXPECTED_ANSWER ( ( ( intgCONST1 + intgCONST2 ) * intgCONST3 ) / intgCONST4 )
\r
171 /* The length of the queue between is button push ISR and the Button Push task
\r
172 is greater than 1 to account for switch bounces generating multiple inputs. */
\r
173 #define mainBUTTON_QUEUE_SIZE 6
\r
175 /*-----------------------------------------------------------
\r
176 Local functions prototypes.
\r
177 -----------------------------------------------------------*/
\r
180 * The 'Check' task function. See the explanation at the top of the file.
\r
182 static void vErrorChecks( void* pvParameters );
\r
185 * The 'Button Push' task. See the explanation at the top of the file.
\r
187 static void vButtonTask( void *pvParameters );
\r
190 * The idle task hook - in which the integer task is implemented. See the
\r
191 * explanation at the top of the file.
\r
193 void vApplicationIdleHook( void );
\r
196 * Checks the unique counts of other tasks to ensure they are still operational.
\r
198 static long prvCheckOtherTasksAreStillRunning( void );
\r
202 /*-----------------------------------------------------------
\r
204 -----------------------------------------------------------*/
\r
206 /* A few tasks are defined within this file. This flag is used to indicate
\r
207 their status. If an error is detected in one of the locally defined tasks then
\r
208 this flag is set to pdTRUE. */
\r
209 portBASE_TYPE xLocalError = pdFALSE;
\r
211 /* The queue used to send data from the button push ISR to the Button Push
\r
213 static QueueHandle_t xButtonQueue;
\r
216 /*-----------------------------------------------------------*/
\r
219 * This is called from the main() function generated by the Processor Expert.
\r
223 /* Start some of the standard demo tasks. */
\r
224 vStartLEDFlashTasks( mainFLASH_PRIORITY );
\r
225 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
226 vStartDynamicPriorityTasks();
\r
228 /* Start the locally defined tasks. There is also a task implemented as
\r
230 xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
231 xTaskCreate( vButtonTask, "Button", configMINIMAL_STACK_SIZE, NULL, mainBUTTON_TASK_PRIORITY, NULL );
\r
233 /* All the tasks have been created - start the scheduler. */
\r
234 vTaskStartScheduler();
\r
236 /* Should not reach here! */
\r
239 /*-----------------------------------------------------------*/
\r
241 static void vErrorChecks( void *pvParameters )
\r
243 TickType_t xDelayPeriod = mainCHECK_PERIOD;
\r
244 TickType_t xLastWakeTime;
\r
246 /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()
\r
247 functions correctly. */
\r
248 xLastWakeTime = xTaskGetTickCount();
\r
252 /* Delay until it is time to execute again. The delay period is
\r
253 shorter following an error. */
\r
254 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );
\r
256 /* Check all the demo application tasks are executing without
\r
257 error. If an error is found the delay period is shortened - this
\r
258 has the effect of increasing the flash rate of the 'check' task
\r
260 if( prvCheckOtherTasksAreStillRunning() == pdFAIL )
\r
262 /* An error has been detected in one of the tasks - flash faster. */
\r
263 xDelayPeriod = mainERROR_CHECK_PERIOD;
\r
266 /* Toggle the LED each cycle round. */
\r
267 vParTestToggleLED( mainCHECK_TASK_LED );
\r
270 /*-----------------------------------------------------------*/
\r
272 static long prvCheckOtherTasksAreStillRunning( void )
\r
274 portBASE_TYPE xAllTasksPassed = pdPASS;
\r
276 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
278 xAllTasksPassed = pdFAIL;
\r
281 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
283 xAllTasksPassed = pdFAIL;
\r
286 /* Also check the status flag for the tasks defined within this function. */
\r
287 if( xLocalError != pdFALSE )
\r
289 xAllTasksPassed = pdFAIL;
\r
292 return xAllTasksPassed;
\r
294 /*-----------------------------------------------------------*/
\r
296 void vApplicationIdleHook( void )
\r
298 /* This variable is effectively set to a constant so it is made volatile to
\r
299 ensure the compiler does not just get rid of it. */
\r
300 volatile long lValue;
\r
302 /* Keep performing a calculation and checking the result against a constant. */
\r
305 /* Perform the calculation. This will store partial value in
\r
306 registers, resulting in a good test of the context switch mechanism. */
\r
307 lValue = intgCONST1;
\r
308 lValue += intgCONST2;
\r
309 lValue *= intgCONST3;
\r
310 lValue /= intgCONST4;
\r
312 /* Did we perform the calculation correctly with no corruption? */
\r
313 if( lValue != intgEXPECTED_ANSWER )
\r
316 portENTER_CRITICAL();
\r
317 xLocalError = pdTRUE;
\r
318 portEXIT_CRITICAL();
\r
321 /* Yield in case cooperative scheduling is being used. */
\r
322 #if configUSE_PREEMPTION == 0
\r
329 /*-----------------------------------------------------------*/
\r
331 static void vButtonTask( void *pvParameters )
\r
333 unsigned portBASE_TYPE uxExpected = 1, uxReceived;
\r
335 /* Create the queue used by the producer and consumer. */
\r
336 xButtonQueue = xQueueCreate( mainBUTTON_QUEUE_SIZE, ( unsigned portBASE_TYPE ) sizeof( unsigned portBASE_TYPE ) );
\r
340 /* Now the queue is created it is safe to enable the button interrupt. */
\r
341 ButtonInterrupt_Enable();
\r
345 /* Simply wait for data to arrive from the button push interrupt. */
\r
346 if( xQueueReceive( xButtonQueue, &uxReceived, portMAX_DELAY ) == pdPASS )
\r
348 /* Was the data we received that expected? */
\r
349 if( uxReceived != uxExpected )
\r
352 portENTER_CRITICAL();
\r
353 xLocalError = pdTRUE;
\r
354 portEXIT_CRITICAL();
\r
358 /* Toggle the LED for every successful push. */
\r
359 vParTestToggleLED( mainBUTTON_PUSH_LED );
\r
367 /* Will only get here if the queue could not be created. */
\r
370 /*-----------------------------------------------------------*/
\r
372 #pragma CODE_SEG __NEAR_SEG NON_BANKED
\r
374 /* Button push ISR. */
\r
375 void interrupt vButtonPush( void )
\r
377 static unsigned portBASE_TYPE uxValToSend = 0;
\r
378 static unsigned long xHigherPriorityTaskWoken;
\r
380 xHigherPriorityTaskWoken = pdFALSE;
\r
382 /* Send an incrementing value to the button push task each run. */
\r
385 /* Clear the interrupt flag. */
\r
388 /* Send the incremented value down the queue. The button push task is
\r
389 blocked waiting for the data. As the button push task is high priority
\r
390 it will wake and a context switch should be performed before leaving
\r
392 xQueueSendFromISR( xButtonQueue, &uxValToSend, &xHigherPriorityTaskWoken );
\r
394 if( xHigherPriorityTaskWoken )
\r
396 /* NOTE: This macro can only be used if there are no local
\r
397 variables defined. This function uses a static variable so it's
\r
398 use is permitted. If the variable were not static portYIELD()
\r
399 would have to be used in it's place. */
\r
400 portTASK_SWITCH_FROM_ISR();
\r
404 #pragma CODE_SEG DEFAULT
\r