2 FreeRTOS V7.5.1 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
65 #error "The batch file Demo\CORTEX_LPC1768_GCC_RedSuite\CreateProjectDirectoryStructure.bat must be executed before the first build. After executing the batch file hit F5 to refrech the Eclipse project, then delete this line."
\r
69 * This file demonstrates the use of FreeRTOS-MPU. It creates tasks in both
\r
70 * User mode and Privileged mode, and using both the original xTaskCreate() and
\r
71 * the new xTaskCreateRestricted() API functions. The purpose of each created
\r
72 * task is documented in the comments above the task function prototype (in
\r
73 * this file), with the task behaviour demonstrated and documented within the
\r
74 * task function itself. In addition a queue is used to demonstrate passing
\r
75 * data between protected/restricted tasks as well as passing data between an
\r
76 * interrupt and a protected/restricted task.
\r
81 /* Library includes. */
\r
84 /* Scheduler includes. */
\r
85 #include "FreeRTOS.h"
\r
90 /* Red Suite includes. */
\r
91 #include "lcd_driver.h"
\r
95 /*-----------------------------------------------------------*/
\r
97 /* Misc constants. */
\r
98 #define mainDONT_BLOCK ( 0 )
\r
100 /* Definitions for the messages that can be sent to the check task. */
\r
101 #define mainREG_TEST_1_STILL_EXECUTING ( 0 )
\r
102 #define mainREG_TEST_2_STILL_EXECUTING ( 1 )
\r
103 #define mainPRINT_SYSTEM_STATUS ( 2 )
\r
105 /* GCC specifics. */
\r
106 #define mainALIGN_TO( x ) __attribute__((aligned(x)))
\r
108 /* Hardware specifics. The start and end address are chosen to ensure the
\r
109 required GPIO are covered while also ensuring the necessary alignment is
\r
111 #define mainGPIO_START_ADDRESS ( ( unsigned long * ) 0x2009c000 )
\r
112 #define mainGPIO_END_ADDRESS ( mainGPIO_START_ADDRESS + ( 64 * 1024 ) )
\r
115 /*-----------------------------------------------------------*/
\r
116 /* Prototypes for functions that implement tasks. -----------*/
\r
117 /*-----------------------------------------------------------*/
\r
120 * Prototype for the reg test tasks. Amongst other things, these fill the CPU
\r
121 * registers with known values before checking that the registers still contain
\r
122 * the expected values. Each of the two tasks use different values so an error
\r
123 * in the context switch mechanism can be caught. Both reg test tasks execute
\r
124 * at the idle priority so will get preempted regularly. Each task repeatedly
\r
125 * sends a message on a queue so long as it remains functioning correctly. If
\r
126 * an error is detected within the task the task is simply deleted.
\r
128 static void prvRegTest1Task( void *pvParameters );
\r
129 static void prvRegTest2Task( void *pvParameters );
\r
132 * Prototype for the check task. The check task demonstrates various features
\r
133 * of the MPU before entering a loop where it waits for messages to arrive on a
\r
136 * Two types of messages can be processes:
\r
138 * 1) "I'm Alive" messages sent from the reg test tasks, indicating that the
\r
139 * task is still operational.
\r
141 * 2) "Print Status commands" sent periodically by the tick hook function (and
\r
142 * therefore from within an interrupt) which command the check task to write
\r
143 * either pass or fail to the terminal, depending on the status of the reg
\r
146 static void prvCheckTask( void *pvParameters );
\r
149 * Prototype for a task created in User mode using the original vTaskCreate()
\r
150 * API function. The task demonstrates the characteristics of such a task,
\r
151 * before simply deleting itself.
\r
153 static void prvOldStyleUserModeTask( void *pvParameters );
\r
156 * Prototype for a task created in Privileged mode using the original
\r
157 * vTaskCreate() API function. The task demonstrates the characteristics of
\r
158 * such a task, before simply deleting itself.
\r
160 static void prvOldStylePrivilegedModeTask( void *pvParameters );
\r
163 /*-----------------------------------------------------------*/
\r
164 /* Prototypes for other misc functions. --------------------*/
\r
165 /*-----------------------------------------------------------*/
\r
168 * Just configures any clocks and IO necessary.
\r
170 static void prvSetupHardware( void );
\r
173 * Simply deletes the calling task. The function is provided only because it
\r
174 * is simpler to call from asm code than the normal vTaskDelete() API function.
\r
175 * It has the noinline attribute because it is called from asm code.
\r
177 static void prvDeleteMe( void ) __attribute__((noinline));
\r
180 * Used by both reg test tasks to send messages to the check task. The message
\r
181 * just lets the check task know that the task is still functioning correctly.
\r
182 * If a reg test task detects an error it will delete itself, and in so doing
\r
183 * prevent itself from sending any more 'I'm Alive' messages to the check task.
\r
185 static void prvSendImAlive( xQueueHandle xHandle, unsigned long ulTaskNumber );
\r
188 * The check task is created with access to three memory regions (plus its
\r
189 * stack). Each memory region is configured with different parameters and
\r
190 * prvTestMemoryRegions() demonstrates what can and cannot be accessed for each
\r
191 * region. prvTestMemoryRegions() also demonstrates a task that was created
\r
192 * as a privileged task settings its own privilege level down to that of a user
\r
195 static void prvTestMemoryRegions( void );
\r
197 /*-----------------------------------------------------------*/
\r
199 /* The handle of the queue used to communicate between tasks and between tasks
\r
200 and interrupts. Note that this is a file scope variable that falls outside of
\r
201 any MPU region. As such other techniques have to be used to allow the tasks
\r
202 to gain access to the queue. See the comments in the tasks themselves for
\r
203 further information. */
\r
204 static xQueueHandle xFileScopeCheckQueue = NULL;
\r
208 /*-----------------------------------------------------------*/
\r
209 /* Data used by the 'check' task. ---------------------------*/
\r
210 /*-----------------------------------------------------------*/
\r
212 /* Define the constants used to allocate the check task stack. Note that the
\r
213 stack size is defined in words, not bytes. */
\r
214 #define mainCHECK_TASK_STACK_SIZE_WORDS 128
\r
215 #define mainCHECK_TASK_STACK_ALIGNMENT ( mainCHECK_TASK_STACK_SIZE_WORDS * sizeof( portSTACK_TYPE ) )
\r
217 /* Declare the stack that will be used by the check task. The kernel will
\r
218 automatically create an MPU region for the stack. The stack alignment must
\r
219 match its size, so if 128 words are reserved for the stack then it must be
\r
220 aligned to ( 128 * 4 ) bytes. */
\r
221 static portSTACK_TYPE xCheckTaskStack[ mainCHECK_TASK_STACK_SIZE_WORDS ] mainALIGN_TO( mainCHECK_TASK_STACK_ALIGNMENT );
\r
223 /* Declare three arrays - an MPU region will be created for each array
\r
224 using the xTaskParameters structure below. THIS IS JUST TO DEMONSTRATE THE
\r
225 MPU FUNCTIONALITY, the data is not used by the check tasks primary function
\r
226 of monitoring the reg test tasks and printing out status information.
\r
228 Note that the arrays allocate slightly more RAM than is actually assigned to
\r
229 the MPU region. This is to permit writes off the end of the array to be
\r
230 detected even when the arrays are placed in adjacent memory locations (with no
\r
231 gaps between them). The align size must be a power of two. */
\r
232 #define mainREAD_WRITE_ARRAY_SIZE 130
\r
233 #define mainREAD_WRITE_ALIGN_SIZE 128
\r
234 char cReadWriteArray[ mainREAD_WRITE_ARRAY_SIZE ] mainALIGN_TO( mainREAD_WRITE_ALIGN_SIZE );
\r
236 #define mainREAD_ONLY_ARRAY_SIZE 260
\r
237 #define mainREAD_ONLY_ALIGN_SIZE 256
\r
238 char cReadOnlyArray[ mainREAD_ONLY_ARRAY_SIZE ] mainALIGN_TO( mainREAD_ONLY_ALIGN_SIZE );
\r
240 #define mainPRIVILEGED_ONLY_ACCESS_ARRAY_SIZE 130
\r
241 #define mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE 128
\r
242 char cPrivilegedOnlyAccessArray[ mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE ] mainALIGN_TO( mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE );
\r
244 /* Fill in a xTaskParameters structure to define the check task - this is the
\r
245 structure passed to the xTaskCreateRestricted() function. */
\r
246 static const xTaskParameters xCheckTaskParameters =
\r
248 prvCheckTask, /* pvTaskCode - the function that implements the task. */
\r
249 ( signed char * ) "Check", /* pcName */
\r
250 mainCHECK_TASK_STACK_SIZE_WORDS, /* usStackDepth - defined in words, not bytes. */
\r
251 ( void * ) 0x12121212, /* pvParameters - this value is just to test that the parameter is being passed into the task correctly. */
\r
252 ( tskIDLE_PRIORITY + 1 ) | portPRIVILEGE_BIT,/* uxPriority - this is the highest priority task in the system. The task is created in privileged mode to demonstrate accessing the privileged only data. */
\r
253 xCheckTaskStack, /* puxStackBuffer - the array to use as the task stack, as declared above. */
\r
255 /* xRegions - In this case the xRegions array is used to create MPU regions
\r
256 for all three of the arrays declared directly above. Each MPU region is
\r
257 created with different parameters. Again, THIS IS JUST TO DEMONSTRATE THE
\r
258 MPU FUNCTIONALITY, the data is not used by the check tasks primary function
\r
259 of monitoring the reg test tasks and printing out status information.*/
\r
261 /* Base address Length Parameters */
\r
262 { cReadWriteArray, mainREAD_WRITE_ALIGN_SIZE, portMPU_REGION_READ_WRITE },
\r
263 { cReadOnlyArray, mainREAD_ONLY_ALIGN_SIZE, portMPU_REGION_READ_ONLY },
\r
264 { cPrivilegedOnlyAccessArray, mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE, portMPU_REGION_PRIVILEGED_READ_WRITE }
\r
268 /* Three MPU regions are defined for use by the 'check' task when the task is
\r
269 created. These are only used to demonstrate the MPU features and are not
\r
270 actually necessary for the check task to fulfill its primary purpose. Instead
\r
271 the MPU regions are replaced with those defined by xAltRegions prior to the
\r
272 check task receiving any data on the queue or printing any messages to the
\r
273 debug console. The MPU region defined below covers the GPIO peripherals used
\r
274 to write to the LCD. */
\r
275 static const xMemoryRegion xAltRegions[ portNUM_CONFIGURABLE_REGIONS ] =
\r
277 /* Base address Length Parameters */
\r
278 { mainGPIO_START_ADDRESS, ( 64 * 1024 ), portMPU_REGION_READ_WRITE },
\r
285 /*-----------------------------------------------------------*/
\r
286 /* Data used by the 'reg test' tasks. -----------------------*/
\r
287 /*-----------------------------------------------------------*/
\r
289 /* Define the constants used to allocate the reg test task stacks. Note that
\r
290 that stack size is defined in words, not bytes. */
\r
291 #define mainREG_TEST_STACK_SIZE_WORDS 128
\r
292 #define mainREG_TEST_STACK_ALIGNMENT ( mainREG_TEST_STACK_SIZE_WORDS * sizeof( portSTACK_TYPE ) )
\r
294 /* Declare the stacks that will be used by the reg test tasks. The kernel will
\r
295 automatically create an MPU region for the stack. The stack alignment must
\r
296 match its size, so if 128 words are reserved for the stack then it must be
\r
297 aligned to ( 128 * 4 ) bytes. */
\r
298 static portSTACK_TYPE xRegTest1Stack[ mainREG_TEST_STACK_SIZE_WORDS ] mainALIGN_TO( mainREG_TEST_STACK_ALIGNMENT );
\r
299 static portSTACK_TYPE xRegTest2Stack[ mainREG_TEST_STACK_SIZE_WORDS ] mainALIGN_TO( mainREG_TEST_STACK_ALIGNMENT );
\r
301 /* Fill in a xTaskParameters structure per reg test task to define the tasks. */
\r
302 static const xTaskParameters xRegTest1Parameters =
\r
304 prvRegTest1Task, /* pvTaskCode - the function that implements the task. */
\r
305 ( signed char * ) "RegTest1", /* pcName */
\r
306 mainREG_TEST_STACK_SIZE_WORDS, /* usStackDepth */
\r
307 ( void * ) 0x12345678, /* pvParameters - this value is just to test that the parameter is being passed into the task correctly. */
\r
308 tskIDLE_PRIORITY | portPRIVILEGE_BIT, /* uxPriority - note that this task is created with privileges to demonstrate one method of passing a queue handle into the task. */
\r
309 xRegTest1Stack, /* puxStackBuffer - the array to use as the task stack, as declared above. */
\r
310 { /* xRegions - this task does not use any non-stack data hence all members are zero. */
\r
311 /* Base address Length Parameters */
\r
312 { 0x00, 0x00, 0x00 },
\r
313 { 0x00, 0x00, 0x00 },
\r
314 { 0x00, 0x00, 0x00 }
\r
317 /*-----------------------------------------------------------*/
\r
319 static xTaskParameters xRegTest2Parameters =
\r
321 prvRegTest2Task, /* pvTaskCode - the function that implements the task. */
\r
322 ( signed char * ) "RegTest2", /* pcName */
\r
323 mainREG_TEST_STACK_SIZE_WORDS, /* usStackDepth */
\r
324 ( void * ) NULL, /* pvParameters - this task uses the parameter to pass in a queue handle, but the queue is not created yet. */
\r
325 tskIDLE_PRIORITY, /* uxPriority */
\r
326 xRegTest2Stack, /* puxStackBuffer - the array to use as the task stack, as declared above. */
\r
327 { /* xRegions - this task does not use any non-stack data hence all members are zero. */
\r
328 /* Base address Length Parameters */
\r
329 { 0x00, 0x00, 0x00 },
\r
330 { 0x00, 0x00, 0x00 },
\r
331 { 0x00, 0x00, 0x00 }
\r
335 /*-----------------------------------------------------------*/
\r
339 prvSetupHardware();
\r
341 /* Create the queue used to pass "I'm alive" messages to the check task. */
\r
342 xFileScopeCheckQueue = xQueueCreate( 1, sizeof( unsigned long ) );
\r
344 /* One check task uses the task parameter to receive the queue handle.
\r
345 This allows the file scope variable to be accessed from within the task.
\r
346 The pvParameters member of xRegTest2Parameters can only be set after the
\r
347 queue has been created so is set here. */
\r
348 xRegTest2Parameters.pvParameters = xFileScopeCheckQueue;
\r
350 /* Create the three test tasks. Handles to the created tasks are not
\r
351 required, hence the second parameter is NULL. */
\r
352 xTaskCreateRestricted( &xRegTest1Parameters, NULL );
\r
353 xTaskCreateRestricted( &xRegTest2Parameters, NULL );
\r
354 xTaskCreateRestricted( &xCheckTaskParameters, NULL );
\r
356 /* Create the tasks that are created using the original xTaskCreate() API
\r
358 xTaskCreate( prvOldStyleUserModeTask, /* The function that implements the task. */
\r
359 ( signed char * ) "Task1", /* Text name for the task. */
\r
360 100, /* Stack depth in words. */
\r
361 NULL, /* Task parameters. */
\r
362 3, /* Priority and mode (user in this case). */
\r
366 xTaskCreate( prvOldStylePrivilegedModeTask, /* The function that implements the task. */
\r
367 ( signed char * ) "Task2", /* Text name for the task. */
\r
368 100, /* Stack depth in words. */
\r
369 NULL, /* Task parameters. */
\r
370 ( 3 | portPRIVILEGE_BIT ), /* Priority and mode. */
\r
374 /* Start the scheduler. */
\r
375 vTaskStartScheduler();
\r
377 /* Will only get here if there was insufficient memory to create the idle
\r
382 /*-----------------------------------------------------------*/
\r
384 static void prvCheckTask( void *pvParameters )
\r
386 /* This task is created in privileged mode so can access the file scope
\r
387 queue variable. Take a stack copy of this before the task is set into user
\r
388 mode. Once that task is in user mode the file scope queue variable will no
\r
389 longer be accessible but the stack copy will. */
\r
390 xQueueHandle xQueue = xFileScopeCheckQueue;
\r
392 unsigned long ulStillAliveCounts[ 2 ] = { 0 };
\r
393 char *pcStatusMessage = "PASS\r\n";
\r
394 unsigned char x = 5, y = 10;
\r
396 /* Just to remove compiler warning. */
\r
397 ( void ) pvParameters;
\r
399 /* Demonstrate how the various memory regions can and can't be accessed.
\r
400 The task privilege is set down to user mode within this function. */
\r
401 prvTestMemoryRegions();
\r
403 /* Change the memory regions allocated to this task to those initially
\r
404 set up for demonstration purposes to those actually required by the task. */
\r
405 vTaskAllocateMPURegions( NULL, xAltRegions );
\r
407 /* This loop performs the main function of the task, which is blocking
\r
408 on a message queue then processing each message as it arrives. */
\r
411 /* Wait for the next message to arrive. */
\r
412 xQueueReceive( xQueue, &lMessage, portMAX_DELAY );
\r
416 case mainREG_TEST_1_STILL_EXECUTING :
\r
417 /* Message from task 1, so task 1 must still be executing. */
\r
418 ( ulStillAliveCounts[ 0 ] )++;
\r
421 case mainREG_TEST_2_STILL_EXECUTING :
\r
422 /* Message from task 2, so task 2 must still be executing. */
\r
423 ( ulStillAliveCounts[ 1 ] )++;
\r
426 case mainPRINT_SYSTEM_STATUS :
\r
427 /* Message from tick hook, time to print out the system
\r
428 status. If messages has stopped arriving from either reg
\r
429 test task then the status must be set to fail. */
\r
430 if( ( ulStillAliveCounts[ 0 ] == 0 ) || ( ulStillAliveCounts[ 1 ] == 0 ) )
\r
432 /* One or both of the test tasks are no longer sending
\r
433 'still alive' messages. */
\r
434 pcStatusMessage = "FAIL\r\n";
\r
437 /* Print a pass/fail message to the LCD - moving the
\r
438 message each time to provide feedback that the output
\r
439 is still being produced. LCD_PrintString() accesses const
\r
440 data stored in flash, which all tasks are at liberty to do,
\r
441 and GPIO for which an MPU region has been set up for it. */
\r
443 LCD_PrintString( x>>1, y>>1, pcStatusMessage, 6, COLOR_RED );
\r
447 /* Reset the count of 'still alive' messages. */
\r
448 memset( ulStillAliveCounts, 0x00, sizeof( ulStillAliveCounts ) );
\r
452 /* Something unexpected happened. Delete this task so the
\r
453 error is apparent (no output will be displayed). */
\r
459 /*-----------------------------------------------------------*/
\r
461 static void prvTestMemoryRegions( void )
\r
466 /* The check task (from which this function is called) is created in the
\r
467 Privileged mode. The privileged array can be both read from and written
\r
468 to while this task is privileged. */
\r
469 cPrivilegedOnlyAccessArray[ 0 ] = 'a';
\r
470 if( cPrivilegedOnlyAccessArray[ 0 ] != 'a' )
\r
472 /* Something unexpected happened. Delete this task so the error is
\r
473 apparent (no output will be displayed). */
\r
477 /* Writing off the end of the RAM allocated to this task will *NOT* cause a
\r
478 protection fault because the task is still executing in a privileged mode.
\r
479 Uncomment the following to test. */
\r
480 /* cPrivilegedOnlyAccessArray[ mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE ] = 'a'; */
\r
482 /* Now set the task into user mode. */
\r
483 portSWITCH_TO_USER_MODE();
\r
485 /* Accessing the privileged only array will now cause a fault. Uncomment
\r
486 the following line to test. */
\r
487 /* cPrivilegedOnlyAccessArray[ 0 ] = 'a'; */
\r
489 /* The read/write array can still be successfully read and written. */
\r
490 for( l = 0; l < mainREAD_WRITE_ALIGN_SIZE; l++ )
\r
492 cReadWriteArray[ l ] = 'a';
\r
493 if( cReadWriteArray[ l ] != 'a' )
\r
495 /* Something unexpected happened. Delete this task so the error is
\r
496 apparent (no output will be displayed). */
\r
501 /* But attempting to read or write off the end of the RAM allocated to this
\r
502 task will cause a fault. Uncomment either of the following two lines to
\r
504 /* cReadWriteArray[ 0 ] = cReadWriteArray[ -1 ]; */
\r
505 /* cReadWriteArray[ mainREAD_WRITE_ALIGN_SIZE ] = 0x00; */
\r
507 /* The read only array can be successfully read... */
\r
508 for( l = 0; l < mainREAD_ONLY_ALIGN_SIZE; l++ )
\r
510 cTemp = cReadOnlyArray[ l ];
\r
513 /* ...but cannot be written. Uncomment the following line to test. */
\r
514 /* cReadOnlyArray[ 0 ] = 'a'; */
\r
516 /* Writing to the first and last locations in the stack array should not
\r
517 cause a protection fault. Note that doing this will cause the kernel to
\r
518 detect a stack overflow if configCHECK_FOR_STACK_OVERFLOW is greater than
\r
520 xCheckTaskStack[ 0 ] = 0;
\r
521 xCheckTaskStack[ mainCHECK_TASK_STACK_SIZE_WORDS - 1 ] = 0;
\r
523 /* Writing off either end of the stack array should cause a protection
\r
524 fault, uncomment either of the following two lines to test. */
\r
525 /* xCheckTaskStack[ -1 ] = 0; */
\r
526 /* xCheckTaskStack[ mainCHECK_TASK_STACK_SIZE_WORDS ] = 0; */
\r
528 /*-----------------------------------------------------------*/
\r
530 static void prvRegTest1Task( void *pvParameters )
\r
532 /* This task is created in privileged mode so can access the file scope
\r
533 queue variable. Take a stack copy of this before the task is set into user
\r
534 mode. Once this task is in user mode the file scope queue variable will no
\r
535 longer be accessible but the stack copy will. */
\r
536 xQueueHandle xQueue = xFileScopeCheckQueue;
\r
538 /* Now the queue handle has been obtained the task can switch to user
\r
539 mode. This is just one method of passing a handle into a protected
\r
540 task, the other reg test task uses the task parameter instead. */
\r
541 portSWITCH_TO_USER_MODE();
\r
543 /* First check that the parameter value is as expected. */
\r
544 if( pvParameters != ( void * ) 0x12345678 )
\r
546 /* Error detected. Delete the task so it stops communicating with
\r
554 /* This task tests the kernel context switch mechanism by reading and
\r
555 writing directly to registers - which requires the test to be written
\r
556 in assembly code. */
\r
559 " MOV R4, #104 \n" /* Set registers to a known value. R0 to R1 are done in the loop below. */
\r
564 " MOV R10, #110 \n"
\r
565 " MOV R11, #111 \n"
\r
567 " MOV R0, #100 \n" /* Set the scratch registers to known values - done inside the loop as they get clobbered. */
\r
571 " MOV R12, #112 \n"
\r
572 " SVC #1 \n" /* Yield just to increase test coverage. */
\r
573 " CMP R0, #100 \n" /* Check all the registers still contain their expected values. */
\r
574 " BNE prvDeleteMe \n" /* Value was not as expected, delete the task so it stops communicating with the check task. */
\r
576 " BNE prvDeleteMe \n"
\r
578 " BNE prvDeleteMe \n"
\r
580 " BNE prvDeleteMe \n"
\r
581 " CMP R4, #104 \n"
\r
582 " BNE prvDeleteMe \n"
\r
584 " BNE prvDeleteMe \n"
\r
586 " BNE prvDeleteMe \n"
\r
588 " BNE prvDeleteMe \n"
\r
590 " BNE prvDeleteMe \n"
\r
591 " CMP R10, #110 \n"
\r
592 " BNE prvDeleteMe \n"
\r
593 " CMP R11, #111 \n"
\r
594 " BNE prvDeleteMe \n"
\r
595 " CMP R12, #112 \n"
\r
596 " BNE prvDeleteMe \n"
\r
597 :::"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r8", "r9", "r10", "r11", "r12"
\r
600 /* Send mainREG_TEST_1_STILL_EXECUTING to the check task to indicate that this
\r
601 task is still functioning. */
\r
602 prvSendImAlive( xQueue, mainREG_TEST_1_STILL_EXECUTING );
\r
604 /* Go back to check all the register values again. */
\r
605 __asm volatile( " B reg1loop " );
\r
608 /*-----------------------------------------------------------*/
\r
610 static void prvRegTest2Task( void *pvParameters )
\r
612 /* The queue handle is passed in as the task parameter. This is one method of
\r
613 passing data into a protected task, the other reg test task uses a different
\r
615 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
619 /* This task tests the kernel context switch mechanism by reading and
\r
620 writing directly to registers - which requires the test to be written
\r
621 in assembly code. */
\r
624 " MOV R4, #4 \n" /* Set registers to a known value. R0 to R1 are done in the loop below. */
\r
627 " MOV R8, #8 \n" /* Frame pointer is omitted as it must not be changed. */
\r
630 " MOV R11, #11 \n"
\r
632 " MOV R0, #13 \n" /* Set the scratch registers to known values - done inside the loop as they get clobbered. */
\r
637 " CMP R0, #13 \n" /* Check all the registers still contain their expected values. */
\r
638 " BNE prvDeleteMe \n" /* Value was not as expected, delete the task so it stops communicating with the check task */
\r
640 " BNE prvDeleteMe \n"
\r
642 " BNE prvDeleteMe \n"
\r
644 " BNE prvDeleteMe \n"
\r
646 " BNE prvDeleteMe \n"
\r
648 " BNE prvDeleteMe \n"
\r
650 " BNE prvDeleteMe \n"
\r
652 " BNE prvDeleteMe \n"
\r
654 " BNE prvDeleteMe \n"
\r
656 " BNE prvDeleteMe \n"
\r
658 " BNE prvDeleteMe \n"
\r
660 " BNE prvDeleteMe \n"
\r
661 :::"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r8", "r9", "r10", "r11", "r12"
\r
664 /* Send mainREG_TEST_2_STILL_EXECUTING to the check task to indicate that this
\r
665 task is still functioning. */
\r
666 prvSendImAlive( xQueue, mainREG_TEST_2_STILL_EXECUTING );
\r
668 /* Go back to check all the register values again. */
\r
669 __asm volatile( " B reg2loop " );
\r
672 /*-----------------------------------------------------------*/
\r
674 void vApplicationIdleHook( void )
\r
676 extern unsigned long __SRAM_segment_end__[];
\r
677 extern unsigned long __privileged_data_start__[];
\r
678 extern unsigned long __privileged_data_end__[];
\r
679 extern unsigned long __FLASH_segment_start__[];
\r
680 extern unsigned long __FLASH_segment_end__[];
\r
681 volatile unsigned long *pul;
\r
682 volatile unsigned long ulReadData;
\r
684 /* The idle task, and therefore this function, run in Supervisor mode and
\r
685 can therefore access all memory. Try reading from corners of flash and
\r
686 RAM to ensure a memory fault does not occur.
\r
688 Start with the edges of the privileged data area. */
\r
689 pul = __privileged_data_start__;
\r
691 pul = __privileged_data_end__ - 1;
\r
694 /* Next the standard SRAM area. */
\r
695 pul = __SRAM_segment_end__ - 1;
\r
698 /* And the standard Flash area - the start of which is marked for
\r
699 privileged access only. */
\r
700 pul = __FLASH_segment_start__;
\r
702 pul = __FLASH_segment_end__ - 1;
\r
705 /* Reading off the end of Flash or SRAM space should cause a fault.
\r
706 Uncomment one of the following two pairs of lines to test. */
\r
708 /* pul = __FLASH_segment_end__ + 4;
\r
709 ulReadData = *pul; */
\r
711 /* pul = __SRAM_segment_end__ + 1;
\r
712 ulReadData = *pul; */
\r
714 /*-----------------------------------------------------------*/
\r
716 static void prvOldStyleUserModeTask( void *pvParameters )
\r
718 extern unsigned long __privileged_data_start__[];
\r
719 extern unsigned long __privileged_data_end__[];
\r
720 extern unsigned long __SRAM_segment_end__[];
\r
721 extern unsigned long __privileged_functions_end__[];
\r
722 extern unsigned long __FLASH_segment_start__[];
\r
723 extern unsigned long __FLASH_segment_end__[];
\r
724 const volatile unsigned long *pulStandardPeripheralRegister = ( volatile unsigned long * ) 0x400FC0C4; /* PCONP */
\r
725 volatile unsigned long *pul;
\r
726 volatile unsigned long ulReadData;
\r
728 /* The following lines are commented out to prevent the unused variable
\r
729 compiler warnings when the tests that use the variable are also commented out.
\r
730 extern unsigned long __privileged_functions_start__[];
\r
731 const volatile unsigned long *pulSystemPeripheralRegister = ( volatile unsigned long * ) 0xe000e014; */
\r
733 ( void ) pvParameters;
\r
735 /* This task is created in User mode using the original xTaskCreate() API
\r
736 function. It should have access to all Flash and RAM except that marked
\r
737 as Privileged access only. Reading from the start and end of the non-
\r
738 privileged RAM should not cause a problem (the privileged RAM is the first
\r
739 block at the bottom of the RAM memory). */
\r
740 pul = __privileged_data_end__ + 1;
\r
742 pul = __SRAM_segment_end__ - 1;
\r
745 /* Likewise reading from the start and end of the non-privileged Flash
\r
746 should not be a problem (the privileged Flash is the first block at the
\r
747 bottom of the Flash memory). */
\r
748 pul = __privileged_functions_end__ + 1;
\r
750 pul = __FLASH_segment_end__ - 1;
\r
753 /* Standard peripherals are accessible. */
\r
754 ulReadData = *pulStandardPeripheralRegister;
\r
756 /* System peripherals are not accessible. Uncomment the following line
\r
757 to test. Also uncomment the declaration of pulSystemPeripheralRegister
\r
758 at the top of this function. */
\r
759 /* ulReadData = *pulSystemPeripheralRegister; */
\r
761 /* Reading from anywhere inside the privileged Flash or RAM should cause a
\r
762 fault. This can be tested by uncommenting any of the following pairs of
\r
763 lines. Also uncomment the declaration of __privileged_functions_start__
\r
764 at the top of this function. */
\r
766 /* pul = __privileged_functions_start__;
\r
767 ulReadData = *pul; */
\r
769 /* pul = __privileged_functions_end__ - 1;
\r
770 ulReadData = *pul; */
\r
772 /* pul = __privileged_data_start__;
\r
773 ulReadData = *pul; */
\r
775 /* pul = __privileged_data_end__ - 1;
\r
776 ulReadData = *pul; */
\r
778 /* Must not just run off the end of a task function, so delete this task.
\r
779 Note that because this task was created using xTaskCreate() the stack was
\r
780 allocated dynamically and I have not included any code to free it again. */
\r
781 vTaskDelete( NULL );
\r
783 /*-----------------------------------------------------------*/
\r
785 static void prvOldStylePrivilegedModeTask( void *pvParameters )
\r
787 extern unsigned long __privileged_data_start__[];
\r
788 extern unsigned long __privileged_data_end__[];
\r
789 extern unsigned long __SRAM_segment_end__[];
\r
790 extern unsigned long __privileged_functions_start__[];
\r
791 extern unsigned long __privileged_functions_end__[];
\r
792 extern unsigned long __FLASH_segment_start__[];
\r
793 extern unsigned long __FLASH_segment_end__[];
\r
794 volatile unsigned long *pul;
\r
795 volatile unsigned long ulReadData;
\r
796 const volatile unsigned long *pulSystemPeripheralRegister = ( volatile unsigned long * ) 0xe000e014; /* Systick */
\r
797 const volatile unsigned long *pulStandardPeripheralRegister = ( volatile unsigned long * ) 0x400FC0C4; /* PCONP */
\r
799 ( void ) pvParameters;
\r
801 /* This task is created in Privileged mode using the original xTaskCreate()
\r
802 API function. It should have access to all Flash and RAM including that
\r
803 marked as Privileged access only. So reading from the start and end of the
\r
804 non-privileged RAM should not cause a problem (the privileged RAM is the
\r
805 first block at the bottom of the RAM memory). */
\r
806 pul = __privileged_data_end__ + 1;
\r
808 pul = __SRAM_segment_end__ - 1;
\r
811 /* Likewise reading from the start and end of the non-privileged Flash
\r
812 should not be a problem (the privileged Flash is the first block at the
\r
813 bottom of the Flash memory). */
\r
814 pul = __privileged_functions_end__ + 1;
\r
816 pul = __FLASH_segment_end__ - 1;
\r
819 /* Reading from anywhere inside the privileged Flash or RAM should also
\r
820 not be a problem. */
\r
821 pul = __privileged_functions_start__;
\r
823 pul = __privileged_functions_end__ - 1;
\r
825 pul = __privileged_data_start__;
\r
826 ulReadData = *pul;
\r
827 pul = __privileged_data_end__ - 1;
\r
830 /* Finally, accessing both System and normal peripherals should both be
\r
832 ulReadData = *pulSystemPeripheralRegister;
\r
833 ulReadData = *pulStandardPeripheralRegister;
\r
835 /* Must not just run off the end of a task function, so delete this task.
\r
836 Note that because this task was created using xTaskCreate() the stack was
\r
837 allocated dynamically and I have not included any code to free it again. */
\r
838 vTaskDelete( NULL );
\r
840 /*-----------------------------------------------------------*/
\r
842 static void prvDeleteMe( void )
\r
844 vTaskDelete( NULL );
\r
846 /*-----------------------------------------------------------*/
\r
848 static void prvSendImAlive( xQueueHandle xHandle, unsigned long ulTaskNumber )
\r
850 if( xHandle != NULL )
\r
852 xQueueSend( xHandle, &ulTaskNumber, mainDONT_BLOCK );
\r
855 /*-----------------------------------------------------------*/
\r
857 void prvSetupHardware( void )
\r
859 /* Disable peripherals power. */
\r
862 /* Enable GPIO power. */
\r
863 SC->PCONP = PCONP_PCGPIO;
\r
865 /* Disable TPIU. */
\r
866 PINCON->PINSEL10 = 0;
\r
868 if ( SC->PLL0STAT & ( 1 << 25 ) )
\r
870 /* Enable PLL, disconnected. */
\r
872 SC->PLL0FEED = PLLFEED_FEED1;
\r
873 SC->PLL0FEED = PLLFEED_FEED2;
\r
876 /* Disable PLL, disconnected. */
\r
878 SC->PLL0FEED = PLLFEED_FEED1;
\r
879 SC->PLL0FEED = PLLFEED_FEED2;
\r
881 /* Enable main OSC. */
\r
883 while( !( SC->SCS & 0x40 ) );
\r
885 /* select main OSC, 12MHz, as the PLL clock source. */
\r
886 SC->CLKSRCSEL = 0x1;
\r
888 SC->PLL0CFG = 0x20031;
\r
889 SC->PLL0FEED = PLLFEED_FEED1;
\r
890 SC->PLL0FEED = PLLFEED_FEED2;
\r
892 /* Enable PLL, disconnected. */
\r
894 SC->PLL0FEED = PLLFEED_FEED1;
\r
895 SC->PLL0FEED = PLLFEED_FEED2;
\r
897 /* Set clock divider. */
\r
898 SC->CCLKCFG = 0x03;
\r
900 /* Configure flash accelerator. */
\r
901 SC->FLASHCFG = 0x403a;
\r
903 /* Check lock bit status. */
\r
904 while( ( ( SC->PLL0STAT & ( 1 << 26 ) ) == 0 ) );
\r
906 /* Enable and connect. */
\r
908 SC->PLL0FEED = PLLFEED_FEED1;
\r
909 SC->PLL0FEED = PLLFEED_FEED2;
\r
910 while( ( ( SC->PLL0STAT & ( 1 << 25 ) ) == 0 ) );
\r
915 /* Configure the clock for the USB. */
\r
917 if( SC->PLL1STAT & ( 1 << 9 ) )
\r
919 /* Enable PLL, disconnected. */
\r
921 SC->PLL1FEED = PLLFEED_FEED1;
\r
922 SC->PLL1FEED = PLLFEED_FEED2;
\r
925 /* Disable PLL, disconnected. */
\r
927 SC->PLL1FEED = PLLFEED_FEED1;
\r
928 SC->PLL1FEED = PLLFEED_FEED2;
\r
930 SC->PLL1CFG = 0x23;
\r
931 SC->PLL1FEED = PLLFEED_FEED1;
\r
932 SC->PLL1FEED = PLLFEED_FEED2;
\r
934 /* Enable PLL, disconnected. */
\r
936 SC->PLL1FEED = PLLFEED_FEED1;
\r
937 SC->PLL1FEED = PLLFEED_FEED2;
\r
938 while( ( ( SC->PLL1STAT & ( 1 << 10 ) ) == 0 ) );
\r
940 /* Enable and connect. */
\r
942 SC->PLL1FEED = PLLFEED_FEED1;
\r
943 SC->PLL1FEED = PLLFEED_FEED2;
\r
944 while( ( ( SC->PLL1STAT & ( 1 << 9 ) ) == 0 ) );
\r
946 /* Setup the peripheral bus to be the same as the PLL output (64 MHz). */
\r
947 SC->PCLKSEL0 = 0x05555555;
\r
949 /* Prepare the LCD. */
\r
950 LCDdriver_initialisation();
\r
951 LCD_PrintString( 5, 10, "FreeRTOS.org", 14, COLOR_GREEN);
\r
953 /*-----------------------------------------------------------*/
\r
955 void vApplicationTickHook( void )
\r
957 static unsigned long ulCallCount;
\r
958 const unsigned long ulCallsBetweenSends = 5000 / portTICK_RATE_MS;
\r
959 const unsigned long ulMessage = mainPRINT_SYSTEM_STATUS;
\r
960 portBASE_TYPE xDummy;
\r
962 /* If configUSE_TICK_HOOK is set to 1 then this function will get called
\r
963 from each RTOS tick. It is called from the tick interrupt and therefore
\r
964 will be executing in the privileged state. */
\r
968 /* Is it time to print out the pass/fail message again? */
\r
969 if( ulCallCount >= ulCallsBetweenSends )
\r
973 /* Send a message to the check task to command it to check that all
\r
974 the tasks are still running then print out the status.
\r
976 This is running in an ISR so has to use the "FromISR" version of
\r
977 xQueueSend(). Because it is in an ISR it is running with privileges
\r
978 so can access xFileScopeCheckQueue directly. */
\r
979 xQueueSendFromISR( xFileScopeCheckQueue, &ulMessage, &xDummy );
\r
982 /*-----------------------------------------------------------*/
\r
984 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )
\r
986 /* If configCHECK_FOR_STACK_OVERFLOW is set to either 1 or 2 then this
\r
987 function will automatically get called if a task overflows its stack. */
\r
989 ( void ) pcTaskName;
\r
992 /*-----------------------------------------------------------*/
\r
994 void vApplicationMallocFailedHook( void )
\r
996 /* If configUSE_MALLOC_FAILED_HOOK is set to 1 then this function will
\r
997 be called automatically if a call to pvPortMalloc() fails. pvPortMalloc()
\r
998 is called automatically when a task, queue or semaphore is created. */
\r
1001 /*-----------------------------------------------------------*/
\r