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