2 FreeRTOS.org V5.2.0 - Copyright (C) 2003-2009 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify it
\r
7 under the terms of the GNU General Public License (version 2) as published
\r
8 by the Free Software Foundation and modified by the FreeRTOS exception.
\r
10 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
15 You should have received a copy of the GNU General Public License along
\r
16 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
17 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
19 A special exception to the GPL is included to allow you to distribute a
\r
20 combined work that includes FreeRTOS.org without being obliged to provide
\r
21 the source code for any proprietary components. See the licensing section
\r
22 of http://www.FreeRTOS.org for full details.
\r
25 ***************************************************************************
\r
27 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
29 * This is a concise, step by step, 'hands on' guide that describes both *
\r
30 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
31 * explains numerous examples that are written using the FreeRTOS API. *
\r
32 * Full source code for all the examples is provided in an accompanying *
\r
35 ***************************************************************************
\r
39 Please ensure to read the configuration and relevant port sections of the
\r
40 online documentation.
\r
42 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
45 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
48 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
49 licensing and training services.
\r
53 * Creates all the demo application tasks, then starts the scheduler. The WEB
\r
54 * documentation provides more details of the standard demo application tasks.
\r
55 * In addition to the standard demo tasks, the following tasks and tests are
\r
56 * defined and/or created within this file:
\r
58 * "LCD" task - the LCD task is a 'gatekeeper' task. It is the only task that
\r
59 * is permitted to access the display directly. Other tasks wishing to write a
\r
60 * message to the LCD send the message on a queue to the LCD task instead of
\r
61 * accessing the LCD themselves. The LCD task just blocks on the queue waiting
\r
62 * for messages - waking and displaying the messages as they arrive.
\r
64 * "Check" task - This only executes every three seconds but has the highest
\r
65 * priority so is guaranteed to get processor time. Its main function is to
\r
66 * check that all the standard demo tasks are still operational. Should any
\r
67 * unexpected behaviour within a demo task be discovered the check task will
\r
68 * write an error to the LCD (via the LCD task). If all the demo tasks are
\r
69 * executing with their expected behaviour then the check task instead writes
\r
70 * a count of the number of times the high frequency interrupt has incremented
\r
71 * ulHighFrequencyTimerInterrupts - which is one in every 20,000 interrupts.
\r
73 * "Register test" tasks - These tasks are used in part to test the kernel port.
\r
74 * They set each processor register to a known value, then check that the
\r
75 * register still contains that value. Each of the tasks sets the registers
\r
76 * to different values, and will get swapping in and out between setting and
\r
77 * then subsequently checking the register values. Discovery of an incorrect
\r
78 * value would be indicative of an error in the task switching mechanism.
\r
80 * By way of demonstration, the demo application defines
\r
81 * configMAX_SYSCALL_INTERRUPT_PRIORITY to be 3, configKERNEL_INTERRUPT_PRIORITY
\r
82 * to be 1, and all other interrupts as follows:
\r
84 * + The UART is allocated a priority of 2. This means it can interrupt the
\r
85 * RTOS tick, and can also safely use queues.
\r
86 * + Two timers are configured to generate interrupts just to test the nesting
\r
87 * and queue access mechanisms. These timers are allocated priorities 2 and 3
\r
88 * respectively. Even though they both access the same two queues, the
\r
89 * priority 3 interrupt can safely interrupt the priority 2 interrupt. Both
\r
90 * can interrupt the RTOS tick.
\r
91 * + Finally a high frequency timer interrupt is configured to use priority 4 -
\r
92 * therefore kernel activity will never prevent the high frequency timer from
\r
93 * executing immediately that the interrupt is raised (within the limitations
\r
94 * of the hardware itself). It would not be safe to access a queue from this
\r
95 * interrupt as it is above configMAX_SYSCALL_INTERRUPT_PRIORITY.
\r
97 * See the online documentation for this demo for more information on interrupt
\r
101 /* Standard includes. */
\r
104 /* Scheduler includes. */
\r
105 #include "FreeRTOS.h"
\r
109 /* Demo application includes. */
\r
110 #include "partest.h"
\r
111 #include "blocktim.h"
\r
113 #include "semtest.h"
\r
114 #include "GenQTest.h"
\r
117 #include "comtest2.h"
\r
118 #include "timertest.h"
\r
119 #include "IntQueue.h"
\r
121 #pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF
\r
122 #pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_2
\r
124 /*-----------------------------------------------------------*/
\r
126 /* The rate at which the LED controlled by the 'check' task will flash when no
\r
127 errors have been detected. */
\r
128 #define mainNO_ERROR_PERIOD ( 3000 / portTICK_RATE_MS )
\r
130 /* The rate at which the LED controlled by the 'check' task will flash when an
\r
131 error has been detected. */
\r
132 #define mainERROR_PERIOD ( 500 / portTICK_RATE_MS )
\r
134 /* The priorities of the various demo application tasks. */
\r
135 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 4 )
\r
136 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
137 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
138 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
139 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
140 #define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
142 /* The LED controlled by the 'check' task. */
\r
143 #define mainCHECK_LED ( 7 )
\r
145 /* The LED used by the comtest tasks. mainCOM_TEST_LED + 1 is also used.
\r
146 See the comtest.c file for more information. */
\r
147 #define mainCOM_TEST_LED ( 4 )
\r
149 /* Baud rate used by the comtest tasks. */
\r
150 #define mainCOM_TEST_BAUD_RATE ( 115200 )
\r
153 #define mainDONT_WAIT ( 0 )
\r
155 /* Dimension the buffer used to hold the value of the high frequency timer
\r
156 count when it is converted to a string. */
\r
157 #define mainMAX_STRING_LENGTH ( 20 )
\r
159 /* The frequency at which the "fast interrupt test" interrupt will occur. */
\r
160 #define mainTEST_INTERRUPT_FREQUENCY ( 20000 )
\r
162 /* The number of timer clocks we expect to occur between each "fast
\r
163 interrupt test" interrupt. */
\r
164 #define mainEXPECTED_CLOCKS_BETWEEN_INTERRUPTS ( ( configCPU_CLOCK_HZ >> 1 ) / mainTEST_INTERRUPT_FREQUENCY )
\r
166 /* The number of nano seconds between each core clock. */
\r
167 #define mainNS_PER_CLOCK ( ( unsigned portLONG ) ( ( 1.0 / ( double ) ( configCPU_CLOCK_HZ >> 1 ) ) * 1000000000.0 ) )
\r
169 /*-----------------------------------------------------------*/
\r
172 * Setup the processor ready for the demo.
\r
174 static void prvSetupHardware( void );
\r
177 * Implements the 'check' task functionality as described at the top of this
\r
180 static void prvCheckTask( void *pvParameters ) __attribute__((noreturn));
\r
183 * Tasks that test the context switch mechanism by filling the processor
\r
184 * registers with known values, then checking that the values contained
\r
185 * within the registers is as expected. The tasks are likely to get swapped
\r
186 * in and out between setting the register values and checking the register
\r
188 static void prvTestTask1( void *pvParameters );
\r
189 static void prvTestTask2( void *pvParameters );
\r
191 /*-----------------------------------------------------------*/
\r
193 /* The queue used to send messages to the LCD task. */
\r
194 static xQueueHandle xLCDQueue;
\r
196 /* Flag used by prvTestTask1() and prvTestTask2() to indicate their status
\r
198 unsigned portLONG ulStatus1 = pdPASS;
\r
200 /* Variables incremented by prvTestTask1() and prvTestTask2() respectively on
\r
201 each iteration of their function. This is used to detect either task stopping
\r
202 their execution.. */
\r
203 unsigned portLONG ulRegTest1Cycles = 0, ulRegTest2Cycles = 0;
\r
205 /*-----------------------------------------------------------*/
\r
208 * Create the demo tasks then start the scheduler.
\r
212 /* Configure any hardware required for this demo. */
\r
213 prvSetupHardware();
\r
215 /* Create the LCD task - this returns the queue to use when writing
\r
216 messages to the LCD. */
\r
217 xLCDQueue = xStartLCDTask();
\r
219 /* Create all the other standard demo tasks. */
\r
220 vStartLEDFlashTasks( tskIDLE_PRIORITY );
\r
221 vCreateBlockTimeTasks();
\r
222 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
223 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
\r
224 vStartQueuePeekTasks();
\r
225 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
\r
226 vStartInterruptQueueTasks();
\r
228 /* Create the tasks defined within this file. */
\r
229 xTaskCreate( prvTestTask1, "Tst1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
230 xTaskCreate( prvTestTask2, "Tst2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
232 /* prvCheckTask uses sprintf so requires more stack. */
\r
233 xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
235 /* Finally start the scheduler. */
\r
236 vTaskStartScheduler();
\r
238 /* Will only reach here if there is insufficient heap available to start
\r
242 /*-----------------------------------------------------------*/
\r
244 static void prvTestTask1( void *pvParameters )
\r
246 extern void vRegTest1( unsigned long * );
\r
250 /* Perform the register test function. */
\r
251 vRegTest1( &ulStatus1 );
\r
253 /* Increment the counter so the check task knows we are still
\r
255 ulRegTest1Cycles++;
\r
258 /*-----------------------------------------------------------*/
\r
260 static void prvTestTask2( void *pvParameters )
\r
262 extern void vRegTest2( unsigned long * );
\r
266 /* Perform the register test function. */
\r
267 vRegTest2( &ulStatus1 );
\r
269 /* Increment the counter so the check task knows we are still
\r
271 ulRegTest2Cycles++;
\r
274 /*-----------------------------------------------------------*/
\r
276 static void prvSetupHardware( void )
\r
278 /* Set the system and peripheral bus speeds and enable the program cache*/
\r
279 SYSTEMConfigPerformance( configCPU_CLOCK_HZ - 1 );
\r
280 mOSCSetPBDIV( OSC_PB_DIV_2 );
\r
282 /* Setup to use the external interrupt controller. */
\r
283 INTEnableSystemMultiVectoredInt();
\r
285 portDISABLE_INTERRUPTS();
\r
287 /* Setup the digital IO for the LED's. */
\r
288 vParTestInitialise();
\r
290 /*-----------------------------------------------------------*/
\r
292 static void prvCheckTask( void *pvParameters )
\r
294 unsigned portLONG ulLastRegTest1Value = 0, ulLastRegTest2Value = 0, ulTicksToWait = mainNO_ERROR_PERIOD;
\r
295 portTickType xLastExecutionTime;
\r
297 /* Buffer into which the high frequency timer count is written as a string. */
\r
298 static portCHAR cStringBuffer[ mainMAX_STRING_LENGTH ];
\r
300 /* The count of the high frequency timer interrupts. */
\r
301 extern unsigned portLONG ulHighFrequencyTimerInterrupts;
\r
302 xLCDMessage xMessage = { ( 200 / portTICK_RATE_MS ), cStringBuffer };
\r
304 /* Setup the high frequency, high priority, timer test. It is setup here
\r
305 to ensure it does not fire before the scheduler is started. */
\r
306 vSetupTimerTest( mainTEST_INTERRUPT_FREQUENCY );
\r
308 /* Initialise the variable used to control our iteration rate prior to
\r
310 xLastExecutionTime = xTaskGetTickCount();
\r
314 /* Wait until it is time to run the tests again. */
\r
315 vTaskDelayUntil( &xLastExecutionTime, ulTicksToWait );
\r
317 /* Has either register check 1 or 2 task discovered an error? */
\r
318 if( ulStatus1 != pdPASS )
\r
320 ulTicksToWait = mainERROR_PERIOD;
\r
321 xMessage.pcMessage = "Error: Reg test1";
\r
324 /* Check that the register test 1 task is still running. */
\r
325 if( ulLastRegTest1Value == ulRegTest1Cycles )
\r
327 ulTicksToWait = mainERROR_PERIOD;
\r
328 xMessage.pcMessage = "Error: Reg test2";
\r
330 ulLastRegTest1Value = ulRegTest1Cycles;
\r
333 /* Check that the register test 2 task is still running. */
\r
334 if( ulLastRegTest2Value == ulRegTest2Cycles )
\r
336 ulTicksToWait = mainERROR_PERIOD;
\r
337 xMessage.pcMessage = "Error: Reg test3";
\r
339 ulLastRegTest2Value = ulRegTest2Cycles;
\r
342 /* Have any of the standard demo tasks detected an error in their
\r
344 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
346 ulTicksToWait = mainERROR_PERIOD;
\r
347 xMessage.pcMessage = "Error: Gen Q";
\r
349 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
351 ulTicksToWait = mainERROR_PERIOD;
\r
352 xMessage.pcMessage = "Error: Q Peek";
\r
354 else if( xAreComTestTasksStillRunning() != pdTRUE )
\r
356 ulTicksToWait = mainERROR_PERIOD;
\r
357 xMessage.pcMessage = "Error: COM test";
\r
359 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
361 ulTicksToWait = mainERROR_PERIOD;
\r
362 xMessage.pcMessage = "Error: Blck time";
\r
364 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
366 ulTicksToWait = mainERROR_PERIOD;
\r
367 xMessage.pcMessage = "Error: Sem test";
\r
369 else if( xAreIntQueueTasksStillRunning() != pdTRUE )
\r
371 ulTicksToWait = mainERROR_PERIOD;
\r
372 xMessage.pcMessage = "Error: Int queue";
\r
375 /* Write the ulHighFrequencyTimerInterrupts value to the string
\r
376 buffer. It will only be displayed if no errors have been detected. */
\r
377 sprintf( cStringBuffer, "Pass %u", ( unsigned int ) ulHighFrequencyTimerInterrupts );
\r
379 xQueueSend( xLCDQueue, &xMessage, mainDONT_WAIT );
\r
380 vParTestToggleLED( mainCHECK_LED );
\r
383 /*-----------------------------------------------------------*/
\r
385 void vApplicationStackOverflowHook( void )
\r
387 /* Look at pxCurrentTCB to see which task overflowed its stack. */
\r
390 /*-----------------------------------------------------------*/
\r
392 void _general_exception_handler( unsigned portLONG ulCause, unsigned portLONG ulStatus )
\r
394 /* This overrides the definition provided by the kernel. Other exceptions
\r
395 should be handled here. */
\r
398 /*-----------------------------------------------------------*/
\r