2 FreeRTOS V8.2.0rc1 - Copyright (C) 2014 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 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
14 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
15 >>! obliged to provide the source code for proprietary components !<<
\r
16 >>! outside of the FreeRTOS kernel. !<<
\r
18 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
19 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
20 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
21 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * Having a problem? Start by reading the FAQ "My application does *
\r
28 * not run, what could be wrong?". Have you defined configASSERT()? *
\r
30 * http://www.FreeRTOS.org/FAQHelp.html *
\r
32 ***************************************************************************
\r
34 ***************************************************************************
\r
36 * FreeRTOS provides completely free yet professionally developed, *
\r
37 * robust, strictly quality controlled, supported, and cross *
\r
38 * platform software that is more than just the market leader, it *
\r
39 * is the industry's de facto standard. *
\r
41 * Help yourself get started quickly while simultaneously helping *
\r
42 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
43 * tutorial book, reference manual, or both: *
\r
44 * http://www.FreeRTOS.org/Documentation *
\r
46 ***************************************************************************
\r
48 ***************************************************************************
\r
50 * Investing in training allows your team to be as productive as *
\r
51 * possible as early as possible, lowering your overall development *
\r
52 * cost, and enabling you to bring a more robust product to market *
\r
53 * earlier than would otherwise be possible. Richard Barry is both *
\r
54 * the architect and key author of FreeRTOS, and so also the world's *
\r
55 * leading authority on what is the world's most popular real time *
\r
56 * kernel for deeply embedded MCU designs. Obtaining your training *
\r
57 * from Richard ensures your team will gain directly from his in-depth *
\r
58 * product knowledge and years of usage experience. Contact Real Time *
\r
59 * Engineers Ltd to enquire about the FreeRTOS Masterclass, presented *
\r
60 * by Richard Barry: http://www.FreeRTOS.org/contact
\r
62 ***************************************************************************
\r
64 ***************************************************************************
\r
66 * You are receiving this top quality software for free. Please play *
\r
67 * fair and reciprocate by reporting any suspected issues and *
\r
68 * participating in the community forum: *
\r
69 * http://www.FreeRTOS.org/support *
\r
73 ***************************************************************************
\r
75 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
76 license and Real Time Engineers Ltd. contact details.
\r
78 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
79 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
80 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
82 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
83 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
85 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
86 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
87 licenses offer ticketed support, indemnification and commercial middleware.
\r
89 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
90 engineered and independently SIL3 certified version for use in safety and
\r
91 mission critical applications that require provable dependability.
\r
99 * vMain() is effectively the demo application entry point. It is called by
\r
100 * the main() function generated by the Processor Expert application.
\r
102 * vMain() creates all the demo application tasks, then starts the scheduler.
\r
103 * The WEB documentation provides more details of the demo application tasks.
\r
105 * Main.c also creates a task called "Check". This only executes every three
\r
106 * seconds but has the highest priority so is guaranteed to get processor time.
\r
107 * Its main function is to check that all the other tasks are still operational.
\r
108 * Each task (other than the "flash" tasks) maintains a unique count that is
\r
109 * incremented each time the task successfully completes its function. Should
\r
110 * any error occur within such a task the count is permanently halted. The
\r
111 * check task inspects the count of each task to ensure it has changed since
\r
112 * the last time the check task executed. If all the count variables have
\r
113 * changed all the tasks are still executing error free, and the check task
\r
114 * toggles the onboard LED. Should any task contain an error at any time
\r
115 * the LED toggle rate will change from 3 seconds to 500ms.
\r
117 * This file also includes the functionality normally implemented within the
\r
118 * standard demo application file integer.c. Due to the limited memory
\r
119 * available on the microcontroller the functionality has been included within
\r
120 * the idle task hook [vApplicationIdleHook()] - instead of within the usual
\r
121 * separate task. See the documentation within integer.c for the rationale
\r
122 * of the integer task functionality.
\r
126 * The demo applications included with other FreeRTOS ports make use of the
\r
127 * standard ComTest tasks. These use a loopback connector to transmit and
\r
128 * receive RS232 characters between two tasks. The test is important for two
\r
131 * 1) It tests the mechanism of context switching from within an application
\r
134 * 2) It generates some randomised timing.
\r
136 * The demo board used to develop this port does not include an RS232 interface
\r
137 * so the ComTest tasks could not easily be included. Instead these two tests
\r
138 * are created using a 'Button Push' task.
\r
140 * The 'Button Push' task blocks on a queue, waiting for data to arrive. A
\r
141 * simple interrupt routine connected to the PP0 input on the demo board places
\r
142 * data in the queue each time the PP0 button is pushed (this button is built
\r
143 * onto the demo board). As the 'Button Push' task is created with a
\r
144 * relatively high priority it will unblock and want to execute as soon as data
\r
145 * arrives in the queue - resulting in a context switch within the PP0 input
\r
146 * ISR. If the data retrieved from the queue is that expected the 'Button Push'
\r
147 * task toggles LED 5. Therefore correct operation is indicated by the LED
\r
148 * toggling each time the PP0 button is pressed.
\r
150 * This test is not as satisfactory as the ComTest method - but the simple
\r
151 * nature of the port makes is just about adequate.
\r
155 /* Kernel includes. */
\r
156 #include "FreeRTOS.h"
\r
160 /* Demo application includes. */
\r
163 #include "dynamic.h"
\r
164 #include "partest.h"
\r
166 /* Processor expert includes. */
\r
167 #include "ButtonInterrupt.h"
\r
169 /*-----------------------------------------------------------
\r
171 -----------------------------------------------------------*/
\r
173 /* Priorities assigned to demo application tasks. */
\r
174 #define mainFLASH_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
175 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
176 #define mainBUTTON_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
177 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
179 /* LED that is toggled by the check task. The check task periodically checks
\r
180 that all the other tasks are operating without error. If no errors are found
\r
181 the LED is toggled with mainCHECK_PERIOD frequency. If an error is found
\r
182 then the toggle rate increases to mainERROR_CHECK_PERIOD. */
\r
183 #define mainCHECK_TASK_LED ( 7 )
\r
184 #define mainCHECK_PERIOD ( ( TickType_t ) 3000 / portTICK_PERIOD_MS )
\r
185 #define mainERROR_CHECK_PERIOD ( ( TickType_t ) 500 / portTICK_PERIOD_MS )
\r
187 /* LED that is toggled by the button push interrupt. */
\r
188 #define mainBUTTON_PUSH_LED ( 5 )
\r
190 /* The constants used in the idle task calculation. */
\r
191 #define intgCONST1 ( ( long ) 123 )
\r
192 #define intgCONST2 ( ( long ) 234567 )
\r
193 #define intgCONST3 ( ( long ) -3 )
\r
194 #define intgCONST4 ( ( long ) 7 )
\r
195 #define intgEXPECTED_ANSWER ( ( ( intgCONST1 + intgCONST2 ) * intgCONST3 ) / intgCONST4 )
\r
197 /* The length of the queue between is button push ISR and the Button Push task
\r
198 is greater than 1 to account for switch bounces generating multiple inputs. */
\r
199 #define mainBUTTON_QUEUE_SIZE 6
\r
201 /*-----------------------------------------------------------
\r
202 Local functions prototypes.
\r
203 -----------------------------------------------------------*/
\r
206 * The 'Check' task function. See the explanation at the top of the file.
\r
208 static void vErrorChecks( void* pvParameters );
\r
211 * The 'Button Push' task. See the explanation at the top of the file.
\r
213 static void vButtonTask( void *pvParameters );
\r
216 * The idle task hook - in which the integer task is implemented. See the
\r
217 * explanation at the top of the file.
\r
219 void vApplicationIdleHook( void );
\r
222 * Checks the unique counts of other tasks to ensure they are still operational.
\r
224 static long prvCheckOtherTasksAreStillRunning( void );
\r
228 /*-----------------------------------------------------------
\r
230 -----------------------------------------------------------*/
\r
232 /* A few tasks are defined within this file. This flag is used to indicate
\r
233 their status. If an error is detected in one of the locally defined tasks then
\r
234 this flag is set to pdTRUE. */
\r
235 portBASE_TYPE xLocalError = pdFALSE;
\r
237 /* The queue used to send data from the button push ISR to the Button Push
\r
239 static QueueHandle_t xButtonQueue;
\r
242 /*-----------------------------------------------------------*/
\r
245 * This is called from the main() function generated by the Processor Expert.
\r
249 /* Start some of the standard demo tasks. */
\r
250 vStartLEDFlashTasks( mainFLASH_PRIORITY );
\r
251 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
252 vStartDynamicPriorityTasks();
\r
254 /* Start the locally defined tasks. There is also a task implemented as
\r
256 xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
257 xTaskCreate( vButtonTask, "Button", configMINIMAL_STACK_SIZE, NULL, mainBUTTON_TASK_PRIORITY, NULL );
\r
259 /* All the tasks have been created - start the scheduler. */
\r
260 vTaskStartScheduler();
\r
262 /* Should not reach here! */
\r
265 /*-----------------------------------------------------------*/
\r
267 static void vErrorChecks( void *pvParameters )
\r
269 TickType_t xDelayPeriod = mainCHECK_PERIOD;
\r
270 TickType_t xLastWakeTime;
\r
272 /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()
\r
273 functions correctly. */
\r
274 xLastWakeTime = xTaskGetTickCount();
\r
278 /* Delay until it is time to execute again. The delay period is
\r
279 shorter following an error. */
\r
280 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );
\r
282 /* Check all the demo application tasks are executing without
\r
283 error. If an error is found the delay period is shortened - this
\r
284 has the effect of increasing the flash rate of the 'check' task
\r
286 if( prvCheckOtherTasksAreStillRunning() == pdFAIL )
\r
288 /* An error has been detected in one of the tasks - flash faster. */
\r
289 xDelayPeriod = mainERROR_CHECK_PERIOD;
\r
292 /* Toggle the LED each cycle round. */
\r
293 vParTestToggleLED( mainCHECK_TASK_LED );
\r
296 /*-----------------------------------------------------------*/
\r
298 static long prvCheckOtherTasksAreStillRunning( void )
\r
300 portBASE_TYPE xAllTasksPassed = pdPASS;
\r
302 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
304 xAllTasksPassed = pdFAIL;
\r
307 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
309 xAllTasksPassed = pdFAIL;
\r
312 /* Also check the status flag for the tasks defined within this function. */
\r
313 if( xLocalError != pdFALSE )
\r
315 xAllTasksPassed = pdFAIL;
\r
318 return xAllTasksPassed;
\r
320 /*-----------------------------------------------------------*/
\r
322 void vApplicationIdleHook( void )
\r
324 /* This variable is effectively set to a constant so it is made volatile to
\r
325 ensure the compiler does not just get rid of it. */
\r
326 volatile long lValue;
\r
328 /* Keep performing a calculation and checking the result against a constant. */
\r
331 /* Perform the calculation. This will store partial value in
\r
332 registers, resulting in a good test of the context switch mechanism. */
\r
333 lValue = intgCONST1;
\r
334 lValue += intgCONST2;
\r
335 lValue *= intgCONST3;
\r
336 lValue /= intgCONST4;
\r
338 /* Did we perform the calculation correctly with no corruption? */
\r
339 if( lValue != intgEXPECTED_ANSWER )
\r
342 portENTER_CRITICAL();
\r
343 xLocalError = pdTRUE;
\r
344 portEXIT_CRITICAL();
\r
347 /* Yield in case cooperative scheduling is being used. */
\r
348 #if configUSE_PREEMPTION == 0
\r
355 /*-----------------------------------------------------------*/
\r
357 static void vButtonTask( void *pvParameters )
\r
359 unsigned portBASE_TYPE uxExpected = 1, uxReceived;
\r
361 /* Create the queue used by the producer and consumer. */
\r
362 xButtonQueue = xQueueCreate( mainBUTTON_QUEUE_SIZE, ( unsigned portBASE_TYPE ) sizeof( unsigned portBASE_TYPE ) );
\r
366 /* Now the queue is created it is safe to enable the button interrupt. */
\r
367 ButtonInterrupt_Enable();
\r
371 /* Simply wait for data to arrive from the button push interrupt. */
\r
372 if( xQueueReceive( xButtonQueue, &uxReceived, portMAX_DELAY ) == pdPASS )
\r
374 /* Was the data we received that expected? */
\r
375 if( uxReceived != uxExpected )
\r
378 portENTER_CRITICAL();
\r
379 xLocalError = pdTRUE;
\r
380 portEXIT_CRITICAL();
\r
384 /* Toggle the LED for every successful push. */
\r
385 vParTestToggleLED( mainBUTTON_PUSH_LED );
\r
393 /* Will only get here if the queue could not be created. */
\r
396 /*-----------------------------------------------------------*/
\r
398 #pragma CODE_SEG __NEAR_SEG NON_BANKED
\r
400 /* Button push ISR. */
\r
401 void interrupt vButtonPush( void )
\r
403 static unsigned portBASE_TYPE uxValToSend = 0;
\r
404 static unsigned long xHigherPriorityTaskWoken;
\r
406 xHigherPriorityTaskWoken = pdFALSE;
\r
408 /* Send an incrementing value to the button push task each run. */
\r
411 /* Clear the interrupt flag. */
\r
414 /* Send the incremented value down the queue. The button push task is
\r
415 blocked waiting for the data. As the button push task is high priority
\r
416 it will wake and a context switch should be performed before leaving
\r
418 xQueueSendFromISR( xButtonQueue, &uxValToSend, &xHigherPriorityTaskWoken );
\r
420 if( xHigherPriorityTaskWoken )
\r
422 /* NOTE: This macro can only be used if there are no local
\r
423 variables defined. This function uses a static variable so it's
\r
424 use is permitted. If the variable were not static portYIELD()
\r
425 would have to be used in it's place. */
\r
426 portTASK_SWITCH_FROM_ISR();
\r
430 #pragma CODE_SEG DEFAULT
\r