2 FreeRTOS V8.1.2 - 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
66 /******************************************************************************
\r
67 * NOTE 1: This project provides two demo applications. A simple blinky style
\r
68 * project, and a more comprehensive test and demo application. The
\r
69 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select
\r
70 * between the two. See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY
\r
71 * in main.c. This file implements the comprehensive test and demo version.
\r
73 * NOTE 2: This file only contains the source code that is specific to the
\r
74 * full demo. Generic functions, such FreeRTOS hook functions, and functions
\r
75 * required to configure the hardware, are defined in main.c.
\r
76 ******************************************************************************
\r
78 * main_full() creates a set of standard demo tasks, some application specific
\r
79 * tasks, and a timer. It then starts the scheduler. The web documentation
\r
80 * provides more details of the standard demo application tasks, which provide
\r
81 * no particular functionality, but do provide a good example of how to use the
\r
84 * In addition to the standard demo tasks, the following tasks and timer are
\r
85 * defined and/or created within this file:
\r
87 * "Command console task" - This uses the Atmel USART driver to provide the
\r
88 * input and output to FreeRTOS+CLI - the FreeRTOS Command Line Interface. To
\r
90 * - Power the SAMD20 XPlained board through the USB debugger connector. This
\r
91 * will create a virtual COM port through the USB.
\r
92 * - Build and run the demo application.
\r
93 * - Start a dumb terminal program such as TerraTerm or Hyper Terminal.
\r
94 * - In the dumb terminal select the UART port associated with the XPlained
\r
95 * debugger connection, using 19200 baud.
\r
96 * - Type 'help' in the terminal window to see a lit of command registered by
\r
99 * "Reg test" tasks - These fill the registers with known values, then check
\r
100 * that each register maintains its expected value for the lifetime of the
\r
101 * task. Each task uses a different set of values. The reg test tasks execute
\r
102 * with a very low priority, so get preempted very frequently. A register
\r
103 * containing an unexpected value is indicative of an error in the context
\r
104 * switching mechanism.
\r
106 * "Check" software timer - The check timer period is initially set to three
\r
107 * seconds. Its callback function checks that all the standard demo tasks, and
\r
108 * the register check tasks, are not only still executing, but are executing
\r
109 * without reporting any errors. If the check timer callback discovers that a
\r
110 * task has either stalled, or reported an error, then it changes the period of
\r
111 * the check timer from the initial three seconds, to just 200ms. The callback
\r
112 * function also toggles the LED each time it is called. This provides a
\r
113 * visual indication of the system status: If the LED toggles every three
\r
114 * seconds then no issues have been discovered. If the LED toggles every 200ms,
\r
115 * then an issue has been discovered with at least one task.
\r
118 /* Kernel includes. */
\r
119 #include "FreeRTOS.h"
\r
122 #include "semphr.h"
\r
123 #include "timers.h"
\r
125 /* Common demo includes. */
\r
126 #include "blocktim.h"
\r
127 #include "countsem.h"
\r
128 #include "recmutex.h"
\r
129 #include "ParTest.h"
\r
130 #include "dynamic.h"
\r
131 #include "QueueOverwrite.h"
\r
132 #include "QueueSet.h"
\r
133 #include "GenQTest.h"
\r
136 /* The period after which the check timer will expire provided no errors have
\r
137 been reported by any of the standard demo tasks. ms are converted to the
\r
138 equivalent in ticks using the portTICK_PERIOD_MS constant. */
\r
139 #define mainCHECK_TIMER_PERIOD_MS ( 3000UL / portTICK_PERIOD_MS )
\r
141 /* The period at which the check timer will expire if an error has been
\r
142 reported in one of the standard demo tasks. ms are converted to the equivalent
\r
143 in ticks using the portTICK_PERIOD_MS constant. */
\r
144 #define mainERROR_CHECK_TIMER_PERIOD_MS ( 200UL / portTICK_PERIOD_MS )
\r
146 /* A block time of zero simply means "don't block". */
\r
147 #define mainDONT_BLOCK ( 0UL )
\r
150 /*-----------------------------------------------------------*/
\r
153 * Register check tasks, as described at the top of this file. The nature of
\r
154 * these files necessitates that they are written in an assembly.
\r
156 extern void vRegTest1Task( void *pvParameters );
\r
157 extern void vRegTest2Task( void *pvParameters );
\r
160 * Function that starts the command console task.
\r
162 extern void vUARTCommandConsoleStart( uint16_t usStackSize, unsigned portBASE_TYPE uxPriority );
\r
165 * The check timer callback function, as described at the top of this file.
\r
167 static void prvCheckTimerCallback( TimerHandle_t xTimer );
\r
170 * Called by main() to create the comprehensive test/demo application if
\r
171 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is not set to 1.
\r
173 void main_full( void );
\r
175 /*-----------------------------------------------------------*/
\r
177 /* The following two variables are used to communicate the status of the
\r
178 register check tasks to the check software timer. If the variables keep
\r
179 incrementing, then the register check tasks has not discovered any errors. If
\r
180 a variable stops incrementing, then an error has been found. */
\r
181 volatile unsigned long ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;
\r
183 /*-----------------------------------------------------------*/
\r
185 void main_full( void )
\r
187 TimerHandle_t xTimer = NULL;
\r
189 /* The register test tasks are asm functions that don't use a stack. The
\r
190 stack allocated just has to be large enough to hold the task context, and
\r
191 for the additional required for the stack overflow checking to work (if
\r
193 const size_t xRegTestStackSize = 25U;
\r
195 /* Create the standard demo tasks */
\r
196 vCreateBlockTimeTasks();
\r
197 vStartDynamicPriorityTasks();
\r
198 vStartCountingSemaphoreTasks();
\r
199 vStartRecursiveMutexTasks();
\r
200 vStartQueueOverwriteTask( tskIDLE_PRIORITY );
\r
201 vStartQueueSetTasks();
\r
202 vStartGenericQueueTasks( tskIDLE_PRIORITY );
\r
203 vStartQueuePeekTasks();
\r
205 /* Start the task that manages the command console for FreeRTOS+CLI. */
\r
206 vUARTCommandConsoleStart( ( configMINIMAL_STACK_SIZE * 3 ), tskIDLE_PRIORITY );
\r
208 /* Create the register test tasks as described at the top of this file.
\r
209 These are naked functions that don't use any stack. A stack still has
\r
210 to be allocated to hold the task context. */
\r
211 xTaskCreate( vRegTest1Task, /* Function that implements the task. */
\r
212 "Reg1", /* Text name of the task. */
\r
213 xRegTestStackSize, /* Stack allocated to the task. */
\r
214 NULL, /* The task parameter is not used. */
\r
215 tskIDLE_PRIORITY, /* The priority to assign to the task. */
\r
216 NULL ); /* Don't receive a handle back, it is not needed. */
\r
218 xTaskCreate( vRegTest2Task, /* Function that implements the task. */
\r
219 "Reg2", /* Text name of the task. */
\r
220 xRegTestStackSize, /* Stack allocated to the task. */
\r
221 NULL, /* The task parameter is not used. */
\r
222 tskIDLE_PRIORITY, /* The priority to assign to the task. */
\r
223 NULL ); /* Don't receive a handle back, it is not needed. */
\r
225 /* Create the software timer that performs the 'check' functionality,
\r
226 as described at the top of this file. */
\r
227 xTimer = xTimerCreate( "CheckTimer", /* A text name, purely to help debugging. */
\r
228 ( mainCHECK_TIMER_PERIOD_MS ), /* The timer period, in this case 3000ms (3s). */
\r
229 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
\r
230 ( void * ) 0, /* The ID is not used, so can be set to anything. */
\r
231 prvCheckTimerCallback /* The callback function that inspects the status of all the other tasks. */
\r
234 /* If the software timer was created successfully, start it. It won't
\r
235 actually start running until the scheduler starts. A block time of
\r
236 zero is used in this call, although any value could be used as the block
\r
237 time will be ignored because the scheduler has not started yet. */
\r
238 configASSERT( xTimer );
\r
239 if( xTimer != NULL )
\r
241 xTimerStart( xTimer, mainDONT_BLOCK );
\r
244 /* Start the kernel. From here on, only tasks and interrupts will run. */
\r
245 vTaskStartScheduler();
\r
247 /* If all is well, the scheduler will now be running, and the following
\r
248 line will never be reached. If the following line does execute, then there
\r
249 was insufficient FreeRTOS heap memory available for the idle and/or timer
\r
250 tasks to be created. See the memory management section on the FreeRTOS web
\r
251 site, or the FreeRTOS tutorial books for more details. */
\r
254 /*-----------------------------------------------------------*/
\r
256 /* See the description at the top of this file. */
\r
257 static void prvCheckTimerCallback( TimerHandle_t xTimer )
\r
259 static long lChangedTimerPeriodAlready = pdFALSE;
\r
260 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;
\r
261 unsigned long ulErrorFound = pdFALSE;
\r
263 /* Check all the demo and test tasks to ensure that they are all still
\r
264 running, and that none have detected an error. */
\r
265 if( xAreDynamicPriorityTasksStillRunning() != pdPASS )
\r
267 ulErrorFound = pdTRUE;
\r
270 if( xAreBlockTimeTestTasksStillRunning() != pdPASS )
\r
272 ulErrorFound = pdTRUE;
\r
275 if( xAreCountingSemaphoreTasksStillRunning() != pdPASS )
\r
277 ulErrorFound = pdTRUE;
\r
280 if( xAreRecursiveMutexTasksStillRunning() != pdPASS )
\r
282 ulErrorFound = pdTRUE;
\r
285 /* Check that the register test 1 task is still running. */
\r
286 if( ulLastRegTest1Value == ulRegTest1LoopCounter )
\r
288 ulErrorFound = pdTRUE;
\r
290 ulLastRegTest1Value = ulRegTest1LoopCounter;
\r
292 /* Check that the register test 2 task is still running. */
\r
293 if( ulLastRegTest2Value == ulRegTest2LoopCounter )
\r
295 ulErrorFound = pdTRUE;
\r
297 ulLastRegTest2Value = ulRegTest2LoopCounter;
\r
300 if( xAreQueueSetTasksStillRunning() != pdPASS )
\r
302 ulErrorFound = pdTRUE;
\r
305 if( xIsQueueOverwriteTaskStillRunning() != pdPASS )
\r
307 ulErrorFound = pdTRUE;
\r
310 if( xAreGenericQueueTasksStillRunning() != pdPASS )
\r
312 ulErrorFound = pdTRUE;
\r
315 if( xAreQueuePeekTasksStillRunning() != pdPASS )
\r
317 ulErrorFound = pdTRUE;
\r
320 /* Toggle the check LED to give an indication of the system status. If
\r
321 the LED toggles every mainCHECK_TIMER_PERIOD_MS milliseconds then
\r
322 everything is ok. A faster toggle indicates an error. */
\r
323 port_pin_toggle_output_level( LED_0_PIN );
\r
325 /* Have any errors been latched in ulErrorFound? If so, shorten the
\r
326 period of the check timer to mainERROR_CHECK_TIMER_PERIOD_MS milliseconds.
\r
327 This will result in an increase in the rate at which the LED toggles. */
\r
328 if( ulErrorFound != pdFALSE )
\r
330 if( lChangedTimerPeriodAlready == pdFALSE )
\r
332 lChangedTimerPeriodAlready = pdTRUE;
\r
334 /* This call to xTimerChangePeriod() uses a zero block time.
\r
335 Functions called from inside of a timer callback function must
\r
336 *never* attempt to block. */
\r
337 xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );
\r
341 /*-----------------------------------------------------------*/
\r