2 FreeRTOS V7.2.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
67 /******************************************************************************
\r
68 * NOTE 1: This project provides two demo applications. A simple blinky style
\r
69 * project, and a more comprehensive test and demo application. The
\r
70 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select
\r
71 * between the two. See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY
\r
72 * in main.c. This file implements the comprehensive test and demo version.
\r
74 * NOTE 2: This file only contains the source code that is specific to the
\r
75 * full demo. Generic functions, such FreeRTOS hook functions, and functions
\r
76 * required to configure the hardware, are defined in main.c.
\r
77 ******************************************************************************
\r
79 * main_full() creates all the demo application tasks and software timers, then
\r
80 * starts the scheduler. The WEB documentation provides more details of the
\r
81 * standard demo application tasks. In addition to the standard demo tasks, the
\r
82 * following tasks and tests are defined and/or created within this file:
\r
84 * "LCD" task - the LCD task is a 'gatekeeper' task. It is the only task that
\r
85 * is permitted to access the display directly. Other tasks wishing to write a
\r
86 * message to the LCD send the message on a queue to the LCD task instead of
\r
87 * accessing the LCD themselves. The LCD task just blocks on the queue waiting
\r
88 * for messages - waking and displaying the messages as they arrive.
\r
90 * "Check" timer - The check software timer period is initially set to three
\r
91 * seconds. The callback function associated with the check software timer
\r
92 * checks that all the standard demo tasks, and the register check tasks, are
\r
93 * not only still executing, but are executing without reporting any errors. If
\r
94 * the check software timer discovers that a task has either stalled, or
\r
95 * reported an error, then it changes its own execution period from the initial
\r
96 * three seconds, to just 200ms. The check software timer callback function
\r
97 * also writes a status message to the LCD (via the LCD task). If all the demo
\r
98 * tasks are executing with their expected behaviour then the check task writes
\r
99 * a count of the number of times the high frequency interrupt has incremented
\r
100 * ulHighFrequencyTimerInterrupts - which is one in every 20,000 interrupts.
\r
102 * "Register test" tasks - These tasks are used in part to test the kernel port.
\r
103 * They set each processor register to a known value, then check that the
\r
104 * register still contains that value. Each of the tasks sets the registers
\r
105 * to different values, and will get swapping in and out between setting and
\r
106 * then subsequently checking the register values. Discovery of an incorrect
\r
107 * value would be indicative of an error in the task switching mechanism.
\r
109 * By way of demonstration, the demo application defines
\r
110 * configMAX_SYSCALL_INTERRUPT_PRIORITY to be 3, configKERNEL_INTERRUPT_PRIORITY
\r
111 * to be 1, and all other interrupts as follows:
\r
113 * + The UART is allocated a priority of 2. This means it can interrupt the
\r
114 * RTOS tick, and can also safely use queues.
\r
115 * + Two timers are configured to generate interrupts just to test the nesting
\r
116 * and queue access mechanisms. These timers are allocated priorities 2 and 3
\r
117 * respectively. Even though they both access the same two queues, the
\r
118 * priority 3 interrupt can safely interrupt the priority 2 interrupt. Both
\r
119 * can interrupt the RTOS tick.
\r
120 * + Finally a high frequency timer interrupt is configured to use priority 4 -
\r
121 * therefore kernel activity will never prevent the high frequency timer from
\r
122 * executing immediately that the interrupt is raised (within the limitations
\r
123 * of the hardware itself). It would not be safe to access a queue from this
\r
124 * interrupt as it is above configMAX_SYSCALL_INTERRUPT_PRIORITY.
\r
126 * See the online documentation for this demo for more information on interrupt
\r
130 /* Standard includes. */
\r
133 /* Scheduler includes. */
\r
134 #include "FreeRTOS.h"
\r
137 #include "timers.h"
\r
139 /* Demo application includes. */
\r
140 #include "partest.h"
\r
141 #include "blocktim.h"
\r
142 #include "flash_timer.h"
\r
143 #include "semtest.h"
\r
144 #include "GenQTest.h"
\r
147 #include "comtest2.h"
\r
148 #include "timertest.h"
\r
149 #include "IntQueue.h"
\r
151 /*-----------------------------------------------------------*/
\r
153 /* The period after which the check timer will expire, in ms, provided no errors
\r
154 have been reported by any of the standard demo tasks. ms are converted to the
\r
155 equivalent in ticks using the portTICK_RATE_MS constant. */
\r
156 #define mainCHECK_TIMER_PERIOD_MS ( 3000UL / portTICK_RATE_MS )
\r
158 /* The period at which the check timer will expire, in ms, if an error has been
\r
159 reported in one of the standard demo tasks. ms are converted to the equivalent
\r
160 in ticks using the portTICK_RATE_MS constant. */
\r
161 #define mainERROR_CHECK_TIMER_PERIOD_MS ( 200UL / portTICK_RATE_MS )
\r
163 /* The priorities of the various demo application tasks. */
\r
164 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
165 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
166 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
167 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
168 #define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
170 /* The LED controlled by the 'check' software timer. */
\r
171 #define mainCHECK_LED ( 7 )
\r
173 /* The LED used by the comtest tasks. mainCOM_TEST_LED + 1 is also used.
\r
174 See the comtest.c file for more information. */
\r
175 #define mainCOM_TEST_LED ( 4 )
\r
177 /* Baud rate used by the comtest tasks. */
\r
178 #define mainCOM_TEST_BAUD_RATE ( 115200 )
\r
181 #define mainDONT_BLOCK ( 0 )
\r
183 /* Dimension the buffer used to hold the value of the high frequency timer
\r
184 count when it is converted to a string. */
\r
185 #define mainMAX_STRING_LENGTH ( 20 )
\r
187 /* The frequency at which the "fast interrupt test" interrupt will occur. */
\r
188 #define mainTEST_INTERRUPT_FREQUENCY ( 20000 )
\r
190 /* The number of timer clocks expected to occur between each "fast interrupt
\r
191 test" interrupt. */
\r
192 #define mainEXPECTED_CLOCKS_BETWEEN_INTERRUPTS ( ( configCPU_CLOCK_HZ >> 1 ) / mainTEST_INTERRUPT_FREQUENCY )
\r
194 /* The number of nano seconds between each core clock. */
\r
195 #define mainNS_PER_CLOCK ( ( unsigned long ) ( ( 1.0 / ( double ) ( configCPU_CLOCK_HZ >> 1 ) ) * 1000000000.0 ) )
\r
197 /* The number of LEDs that should be controlled by the flash software timer
\r
199 #define mainNUM_FLASH_TIMER_LEDS ( 3 )
\r
201 /*-----------------------------------------------------------*/
\r
204 * The check timer callback function, as described at the top of this file.
\r
206 static void prvCheckTimerCallback( xTimerHandle xTimer );
\r
209 * It is important to ensure the high frequency timer test does not start before
\r
210 * the kernel. It is therefore started from inside a software timer callback
\r
211 * function, which will not execute until the timer service/daemon task is
\r
212 * executing. A one-shot timer is used, so the callback function will only
\r
213 * execute once (unless it is manually reset/restarted).
\r
215 static void prvSetupHighFrequencyTimerTest( xTimerHandle xTimer );
\r
218 * Tasks that test the context switch mechanism by filling the processor
\r
219 * registers with known values, then checking that the values contained
\r
220 * within the registers is as expected. The tasks are likely to get swapped
\r
221 * in and out between setting the register values and checking the register
\r
224 static void prvRegTestTask1( void *pvParameters );
\r
225 static void prvRegTestTask2( void *pvParameters );
\r
227 /*-----------------------------------------------------------*/
\r
229 /* The queue used to send messages to the LCD task. */
\r
230 static xQueueHandle xLCDQueue;
\r
232 /* Flag used by prvRegTestTask1() and prvRegTestTask2() to indicate their status
\r
234 volatile unsigned long ulStatus1 = pdPASS;
\r
236 /* Variables incremented by prvRegTestTask1() and prvRegTestTask2() respectively on
\r
237 each iteration of their function. This is used to detect either task stopping
\r
238 their execution.. */
\r
239 volatile unsigned long ulRegTest1Cycles = 0, ulRegTest2Cycles = 0;
\r
241 /*-----------------------------------------------------------*/
\r
244 * Create the demo tasks then start the scheduler.
\r
246 int main_full( void )
\r
248 xTimerHandle xTimer = NULL;
\r
250 /* Create the LCD task - this returns the queue to use when writing
\r
251 messages to the LCD. */
\r
252 xLCDQueue = xStartLCDTask();
\r
254 /* Create all the other standard demo tasks. */
\r
255 vStartLEDFlashTimers( mainNUM_FLASH_TIMER_LEDS );
\r
256 vCreateBlockTimeTasks();
\r
257 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
258 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
\r
259 vStartQueuePeekTasks();
\r
260 vStartInterruptQueueTasks();
\r
262 /* Create the tasks defined within this file. */
\r
263 xTaskCreate( prvRegTestTask1, ( const signed char * const ) "Reg1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
264 xTaskCreate( prvRegTestTask2, ( const signed char * const ) "Reg2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
266 /* The PIC32MX795 uses an 8 deep fifo where TX interrupts are asserted
\r
267 whilst the TX buffer is empty. This causes an issue with the test driver so
\r
268 it is not used in this demo */
\r
269 #if !defined(__32MX795F512L__)
\r
270 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
\r
273 /* Create the software timer that performs the 'check' functionality, as
\r
274 described at the top of this file. */
\r
275 xTimer = xTimerCreate( ( const signed char * ) "CheckTimer",/* A text name, purely to help debugging. */
\r
276 ( mainCHECK_TIMER_PERIOD_MS ), /* The timer period, in this case 3000ms (3s). */
\r
277 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
\r
278 ( void * ) 0, /* The ID is not used, so can be set to anything. */
\r
279 prvCheckTimerCallback /* The callback function that inspects the status of all the other tasks. */
\r
282 if( xTimer != NULL )
\r
284 xTimerStart( xTimer, mainDONT_BLOCK );
\r
287 /* A software timer is also used to start the high frequency timer test.
\r
288 This is to ensure the test does not start before the kernel. This time a
\r
289 one shot software timer is used. */
\r
290 xTimer = xTimerCreate( ( const signed char * ) "HighHzTimerSetup", 1, pdFALSE, ( void * ) 0, prvSetupHighFrequencyTimerTest );
\r
291 if( xTimer != NULL )
\r
293 xTimerStart( xTimer, mainDONT_BLOCK );
\r
296 /* Finally start the scheduler. */
\r
297 vTaskStartScheduler();
\r
299 /* If all is well, the scheduler will now be running, and the following line
\r
300 will never be reached. If the following line does execute, then there was
\r
301 insufficient FreeRTOS heap memory available for the idle and/or timer tasks
\r
302 to be created. See the memory management section on the FreeRTOS web site
\r
303 for more details. */
\r
306 /*-----------------------------------------------------------*/
\r
308 static void prvRegTestTask1( void *pvParameters )
\r
310 extern void vRegTest1( volatile unsigned long * );
\r
314 /* Perform the register test function. */
\r
315 vRegTest1( &ulStatus1 );
\r
317 /* Increment the counter so the check task knows we are still
\r
319 ulRegTest1Cycles++;
\r
322 /*-----------------------------------------------------------*/
\r
324 static void prvRegTestTask2( void *pvParameters )
\r
326 extern void vRegTest2( volatile unsigned long * );
\r
330 /* Perform the register test function. */
\r
331 vRegTest2( &ulStatus1 );
\r
333 /* Increment the counter so the check task knows we are still
\r
335 ulRegTest2Cycles++;
\r
338 /*-----------------------------------------------------------*/
\r
340 static void prvCheckTimerCallback( xTimerHandle xTimer )
\r
342 static long lChangedTimerPeriodAlready = pdFALSE;
\r
343 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;
\r
345 /* Buffer into which the high frequency timer count is written as a string. */
\r
346 static char cStringBuffer[ mainMAX_STRING_LENGTH ];
\r
348 /* The count of the high frequency timer interrupts. */
\r
349 extern unsigned long ulHighFrequencyTimerInterrupts;
\r
350 static xLCDMessage xMessage = { ( 200 / portTICK_RATE_MS ), cStringBuffer };
\r
352 /* Has either register check 1 or 2 task discovered an error? */
\r
353 if( ulStatus1 != pdPASS )
\r
355 xMessage.pcMessage = "Error: Reg test1";
\r
358 /* Check that the register test 1 task is still running. */
\r
359 if( ulLastRegTest1Value == ulRegTest1Cycles )
\r
361 xMessage.pcMessage = "Error: Reg test2";
\r
363 ulLastRegTest1Value = ulRegTest1Cycles;
\r
366 /* Check that the register test 2 task is still running. */
\r
367 if( ulLastRegTest2Value == ulRegTest2Cycles )
\r
369 xMessage.pcMessage = "Error: Reg test3";
\r
371 ulLastRegTest2Value = ulRegTest2Cycles;
\r
374 /* Have any of the standard demo tasks detected an error in their
\r
376 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
378 xMessage.pcMessage = "Error: Gen Q";
\r
380 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
382 xMessage.pcMessage = "Error: Q Peek";
\r
384 else if( xAreComTestTasksStillRunning() != pdTRUE )
\r
386 xMessage.pcMessage = "Error: COM test";
\r
388 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
390 xMessage.pcMessage = "Error: Blck time";
\r
392 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
394 xMessage.pcMessage = "Error: Sem test";
\r
396 else if( xAreIntQueueTasksStillRunning() != pdTRUE )
\r
398 xMessage.pcMessage = "Error: Int queue";
\r
401 if( xMessage.pcMessage != cStringBuffer )
\r
403 /* An error string has been logged. If the timer period has not yet
\r
404 been changed it should be changed now. Increasing the frequency of the
\r
405 LED gives visual feedback of the error status (although it is written
\r
406 to the LCD too!). */
\r
407 if( lChangedTimerPeriodAlready == pdFALSE )
\r
409 lChangedTimerPeriodAlready = pdTRUE;
\r
411 /* This call to xTimerChangePeriod() uses a zero block time.
\r
412 Functions called from inside of a timer callback function must
\r
413 *never* attempt to block as to do so could impact other software
\r
415 xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );
\r
420 /* Write the ulHighFrequencyTimerInterrupts value to the string
\r
421 buffer. It will only be displayed if no errors have been detected. */
\r
422 sprintf( cStringBuffer, "Pass %u", ( unsigned int ) ulHighFrequencyTimerInterrupts );
\r
425 /* Send the status message to the LCD task for display on the LCD. This is
\r
426 a timer callback function, so the queue send function *must not* block. */
\r
427 xQueueSend( xLCDQueue, &xMessage, mainDONT_BLOCK );
\r
428 vParTestToggleLED( mainCHECK_LED );
\r
430 /*-----------------------------------------------------------*/
\r
432 static void prvSetupHighFrequencyTimerTest( xTimerHandle xTimer )
\r
434 /* Setup the high frequency, high priority, timer test. It is setup in this
\r
435 software timer callback to ensure it does not start before the kernel does.
\r
436 This is a one shot timer - so the setup routine will only be executed once. */
\r
437 vSetupTimerTest( mainTEST_INTERRUPT_FREQUENCY );
\r