2 FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.
\r
4 This file is part of the FreeRTOS distribution.
\r
6 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
7 the terms of the GNU General Public License (version 2) as published by the
\r
8 Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS without being obliged to provide the
\r
11 source code for proprietary components outside of the FreeRTOS kernel.
\r
12 Alternative commercial license and support terms are also available upon
\r
13 request. See the licensing section of http://www.FreeRTOS.org for full
\r
16 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
21 You should have received a copy of the GNU General Public License along
\r
22 with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59
\r
23 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
26 ***************************************************************************
\r
28 * The FreeRTOS eBook and reference manual are available to purchase for a *
\r
29 * small fee. Help yourself get started quickly while also helping the *
\r
30 * FreeRTOS project! See http://www.FreeRTOS.org/Documentation for details *
\r
32 ***************************************************************************
\r
36 Please ensure to read the configuration and relevant port sections of the
\r
37 online documentation.
\r
39 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
42 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
45 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
46 licensing and training services.
\r
49 #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
53 * This file demonstrates the use of FreeRTOS-MPU. It creates tasks in both
\r
54 * User mode and Privileged mode, and using both the original xTaskCreate() and
\r
55 * the new xTaskCreateRestricted() API functions. The purpose of each created
\r
56 * task is documented in the comments above the task function prototype (in
\r
57 * this file), with the task behaviour demonstrated and documented within the
\r
58 * task function itself. In addition a queue is used to demonstrate passing
\r
59 * data between protected/restricted tasks as well as passing data between an
\r
60 * interrupt and a protected/restricted task.
\r
65 /* Library includes. */
\r
68 /* Scheduler includes. */
\r
69 #include "FreeRTOS.h"
\r
74 /* Red Suite includes. */
\r
75 #include "lcd_driver.h"
\r
79 /*-----------------------------------------------------------*/
\r
81 /* Misc constants. */
\r
82 #define mainDONT_BLOCK ( 0 )
\r
84 /* Definitions for the messages that can be sent to the check task. */
\r
85 #define mainREG_TEST_1_STILL_EXECUTING ( 0 )
\r
86 #define mainREG_TEST_2_STILL_EXECUTING ( 1 )
\r
87 #define mainPRINT_SYSTEM_STATUS ( 2 )
\r
89 /* GCC specifics. */
\r
90 #define mainALIGN_TO( x ) __attribute__((aligned(x)))
\r
92 /* Hardware specifics. The start and end address are chosen to ensure the
\r
93 required GPIO are covered while also ensuring the necessary alignment is
\r
95 #define mainGPIO_START_ADDRESS ( ( unsigned long * ) 0x2009c000 )
\r
96 #define mainGPIO_END_ADDRESS ( mainGPIO_START_ADDRESS + ( 64 * 1024 ) )
\r
99 /*-----------------------------------------------------------*/
\r
100 /* Prototypes for functions that implement tasks. -----------*/
\r
101 /*-----------------------------------------------------------*/
\r
104 * Prototype for the reg test tasks. Amongst other things, these fill the CPU
\r
105 * registers with known values before checking that the registers still contain
\r
106 * the expected values. Each of the two tasks use different values so an error
\r
107 * in the context switch mechanism can be caught. Both reg test tasks execute
\r
108 * at the idle priority so will get preempted regularly. Each task repeatedly
\r
109 * sends a message on a queue so long as it remains functioning correctly. If
\r
110 * an error is detected within the task the task is simply deleted.
\r
112 static void prvRegTest1Task( void *pvParameters );
\r
113 static void prvRegTest2Task( void *pvParameters );
\r
116 * Prototype for the check task. The check task demonstrates various features
\r
117 * of the MPU before entering a loop where it waits for messages to arrive on a
\r
120 * Two types of messages can be processes:
\r
122 * 1) "I'm Alive" messages sent from the reg test tasks, indicating that the
\r
123 * task is still operational.
\r
125 * 2) "Print Status commands" sent periodically by the tick hook function (and
\r
126 * therefore from within an interrupt) which command the check task to write
\r
127 * either pass or fail to the terminal, depending on the status of the reg
\r
130 static void prvCheckTask( void *pvParameters );
\r
133 * Prototype for a task created in User mode using the original vTaskCreate()
\r
134 * API function. The task demonstrates the characteristics of such a task,
\r
135 * before simply deleting itself.
\r
137 static void prvOldStyleUserModeTask( void *pvParameters );
\r
140 * Prototype for a task created in Privileged mode using the original
\r
141 * vTaskCreate() API function. The task demonstrates the characteristics of
\r
142 * such a task, before simply deleting itself.
\r
144 static void prvOldStylePrivilegedModeTask( void *pvParameters );
\r
147 /*-----------------------------------------------------------*/
\r
148 /* Prototypes for other misc functions. --------------------*/
\r
149 /*-----------------------------------------------------------*/
\r
152 * Just configures any clocks and IO necessary.
\r
154 static void prvSetupHardware( void );
\r
157 * Simply deletes the calling task. The function is provided only because it
\r
158 * is simpler to call from asm code than the normal vTaskDelete() API function.
\r
159 * It has the noinline attribute because it is called from asm code.
\r
161 static void prvDeleteMe( void ) __attribute__((noinline));
\r
164 * Used by both reg test tasks to send messages to the check task. The message
\r
165 * just lets the check task know that the task is still functioning correctly.
\r
166 * If a reg test task detects an error it will delete itself, and in so doing
\r
167 * prevent itself from sending any more 'I'm Alive' messages to the check task.
\r
169 static void prvSendImAlive( xQueueHandle xHandle, unsigned long ulTaskNumber );
\r
172 * The check task is created with access to three memory regions (plus its
\r
173 * stack). Each memory region is configured with different parameters and
\r
174 * prvTestMemoryRegions() demonstrates what can and cannot be accessed for each
\r
175 * region. prvTestMemoryRegions() also demonstrates a task that was created
\r
176 * as a privileged task settings its own privilege level down to that of a user
\r
179 static void prvTestMemoryRegions( void );
\r
181 /*-----------------------------------------------------------*/
\r
183 /* The handle of the queue used to communicate between tasks and between tasks
\r
184 and interrupts. Note that this is a file scope variable that falls outside of
\r
185 any MPU region. As such other techniques have to be used to allow the tasks
\r
186 to gain access to the queue. See the comments in the tasks themselves for
\r
187 further information. */
\r
188 static xQueueHandle xFileScopeCheckQueue = NULL;
\r
192 /*-----------------------------------------------------------*/
\r
193 /* Data used by the 'check' task. ---------------------------*/
\r
194 /*-----------------------------------------------------------*/
\r
196 /* Define the constants used to allocate the check task stack. Note that the
\r
197 stack size is defined in words, not bytes. */
\r
198 #define mainCHECK_TASK_STACK_SIZE_WORDS 128
\r
199 #define mainCHECK_TASK_STACK_ALIGNMENT ( mainCHECK_TASK_STACK_SIZE_WORDS * sizeof( portSTACK_TYPE ) )
\r
201 /* Declare the stack that will be used by the check task. The kernel will
\r
202 automatically create an MPU region for the stack. The stack alignment must
\r
203 match its size, so if 128 words are reserved for the stack then it must be
\r
204 aligned to ( 128 * 4 ) bytes. */
\r
205 static portSTACK_TYPE xCheckTaskStack[ mainCHECK_TASK_STACK_SIZE_WORDS ] mainALIGN_TO( mainCHECK_TASK_STACK_ALIGNMENT );
\r
207 /* Declare three arrays - an MPU region will be created for each array
\r
208 using the xTaskParameters structure below. THIS IS JUST TO DEMONSTRATE THE
\r
209 MPU FUNCTIONALITY, the data is not used by the check tasks primary function
\r
210 of monitoring the reg test tasks and printing out status information.
\r
212 Note that the arrays allocate slightly more RAM than is actually assigned to
\r
213 the MPU region. This is to permit writes off the end of the array to be
\r
214 detected even when the arrays are placed in adjacent memory locations (with no
\r
215 gaps between them). The align size must be a power of two. */
\r
216 #define mainREAD_WRITE_ARRAY_SIZE 130
\r
217 #define mainREAD_WRITE_ALIGN_SIZE 128
\r
218 char cReadWriteArray[ mainREAD_WRITE_ARRAY_SIZE ] mainALIGN_TO( mainREAD_WRITE_ALIGN_SIZE );
\r
220 #define mainREAD_ONLY_ARRAY_SIZE 260
\r
221 #define mainREAD_ONLY_ALIGN_SIZE 256
\r
222 char cReadOnlyArray[ mainREAD_ONLY_ARRAY_SIZE ] mainALIGN_TO( mainREAD_ONLY_ALIGN_SIZE );
\r
224 #define mainPRIVILEGED_ONLY_ACCESS_ARRAY_SIZE 130
\r
225 #define mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE 128
\r
226 char cPrivilegedOnlyAccessArray[ mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE ] mainALIGN_TO( mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE );
\r
228 /* Fill in a xTaskParameters structure to define the check task - this is the
\r
229 structure passed to the xTaskCreateRestricted() function. */
\r
230 static const xTaskParameters xCheckTaskParameters =
\r
232 prvCheckTask, /* pvTaskCode - the function that implements the task. */
\r
233 ( signed char * ) "Check", /* pcName */
\r
234 mainCHECK_TASK_STACK_SIZE_WORDS, /* usStackDepth - defined in words, not bytes. */
\r
235 ( void * ) 0x12121212, /* pvParameters - this value is just to test that the parameter is being passed into the task correctly. */
\r
236 ( 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
237 xCheckTaskStack, /* puxStackBuffer - the array to use as the task stack, as declared above. */
\r
239 /* xRegions - In this case the xRegions array is used to create MPU regions
\r
240 for all three of the arrays declared directly above. Each MPU region is
\r
241 created with different parameters. Again, THIS IS JUST TO DEMONSTRATE THE
\r
242 MPU FUNCTIONALITY, the data is not used by the check tasks primary function
\r
243 of monitoring the reg test tasks and printing out status information.*/
\r
245 /* Base address Length Parameters */
\r
246 { cReadWriteArray, mainREAD_WRITE_ALIGN_SIZE, portMPU_REGION_READ_WRITE },
\r
247 { cReadOnlyArray, mainREAD_ONLY_ALIGN_SIZE, portMPU_REGION_READ_ONLY },
\r
248 { cPrivilegedOnlyAccessArray, mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE, portMPU_REGION_PRIVILEGED_READ_WRITE }
\r
252 /* Three MPU regions are defined for use by the 'check' task when the task is
\r
253 created. These are only used to demonstrate the MPU features and are not
\r
254 actually necessary for the check task to fulfill its primary purpose. Instead
\r
255 the MPU regions are replaced with those defined by xAltRegions prior to the
\r
256 check task receiving any data on the queue or printing any messages to the
\r
257 debug console. The MPU region defined below covers the GPIO peripherals used
\r
258 to write to the LCD. */
\r
259 static const xMemoryRegion xAltRegions[ portNUM_CONFIGURABLE_REGIONS ] =
\r
261 /* Base address Length Parameters */
\r
262 { mainGPIO_START_ADDRESS, ( 64 * 1024 ), portMPU_REGION_READ_WRITE },
\r
269 /*-----------------------------------------------------------*/
\r
270 /* Data used by the 'reg test' tasks. -----------------------*/
\r
271 /*-----------------------------------------------------------*/
\r
273 /* Define the constants used to allocate the reg test task stacks. Note that
\r
274 that stack size is defined in words, not bytes. */
\r
275 #define mainREG_TEST_STACK_SIZE_WORDS 128
\r
276 #define mainREG_TEST_STACK_ALIGNMENT ( mainREG_TEST_STACK_SIZE_WORDS * sizeof( portSTACK_TYPE ) )
\r
278 /* Declare the stacks that will be used by the reg test tasks. The kernel will
\r
279 automatically create an MPU region for the stack. The stack alignment must
\r
280 match its size, so if 128 words are reserved for the stack then it must be
\r
281 aligned to ( 128 * 4 ) bytes. */
\r
282 static portSTACK_TYPE xRegTest1Stack[ mainREG_TEST_STACK_SIZE_WORDS ] mainALIGN_TO( mainREG_TEST_STACK_ALIGNMENT );
\r
283 static portSTACK_TYPE xRegTest2Stack[ mainREG_TEST_STACK_SIZE_WORDS ] mainALIGN_TO( mainREG_TEST_STACK_ALIGNMENT );
\r
285 /* Fill in a xTaskParameters structure per reg test task to define the tasks. */
\r
286 static const xTaskParameters xRegTest1Parameters =
\r
288 prvRegTest1Task, /* pvTaskCode - the function that implements the task. */
\r
289 ( signed char * ) "RegTest1", /* pcName */
\r
290 mainREG_TEST_STACK_SIZE_WORDS, /* usStackDepth */
\r
291 ( void * ) 0x12345678, /* pvParameters - this value is just to test that the parameter is being passed into the task correctly. */
\r
292 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
293 xRegTest1Stack, /* puxStackBuffer - the array to use as the task stack, as declared above. */
\r
294 { /* xRegions - this task does not use any non-stack data hence all members are zero. */
\r
295 /* Base address Length Parameters */
\r
296 { 0x00, 0x00, 0x00 },
\r
297 { 0x00, 0x00, 0x00 },
\r
298 { 0x00, 0x00, 0x00 }
\r
301 /*-----------------------------------------------------------*/
\r
303 static xTaskParameters xRegTest2Parameters =
\r
305 prvRegTest2Task, /* pvTaskCode - the function that implements the task. */
\r
306 ( signed char * ) "RegTest2", /* pcName */
\r
307 mainREG_TEST_STACK_SIZE_WORDS, /* usStackDepth */
\r
308 ( void * ) NULL, /* pvParameters - this task uses the parameter to pass in a queue handle, but the queue is not created yet. */
\r
309 tskIDLE_PRIORITY, /* uxPriority */
\r
310 xRegTest2Stack, /* puxStackBuffer - the array to use as the task stack, as declared above. */
\r
311 { /* xRegions - this task does not use any non-stack data hence all members are zero. */
\r
312 /* Base address Length Parameters */
\r
313 { 0x00, 0x00, 0x00 },
\r
314 { 0x00, 0x00, 0x00 },
\r
315 { 0x00, 0x00, 0x00 }
\r
319 /*-----------------------------------------------------------*/
\r
323 prvSetupHardware();
\r
325 /* Create the queue used to pass "I'm alive" messages to the check task. */
\r
326 xFileScopeCheckQueue = xQueueCreate( 1, sizeof( unsigned long ) );
\r
328 /* One check task uses the task parameter to receive the queue handle.
\r
329 This allows the file scope variable to be accessed from within the task.
\r
330 The pvParameters member of xRegTest2Parameters can only be set after the
\r
331 queue has been created so is set here. */
\r
332 xRegTest2Parameters.pvParameters = xFileScopeCheckQueue;
\r
334 /* Create the three test tasks. Handles to the created tasks are not
\r
335 required, hence the second parameter is NULL. */
\r
336 xTaskCreateRestricted( &xRegTest1Parameters, NULL );
\r
337 xTaskCreateRestricted( &xRegTest2Parameters, NULL );
\r
338 xTaskCreateRestricted( &xCheckTaskParameters, NULL );
\r
340 /* Create the tasks that are created using the original xTaskCreate() API
\r
342 xTaskCreate( prvOldStyleUserModeTask, /* The function that implements the task. */
\r
343 ( signed char * ) "Task1", /* Text name for the task. */
\r
344 100, /* Stack depth in words. */
\r
345 NULL, /* Task parameters. */
\r
346 3, /* Priority and mode (user in this case). */
\r
350 xTaskCreate( prvOldStylePrivilegedModeTask, /* The function that implements the task. */
\r
351 ( signed char * ) "Task2", /* Text name for the task. */
\r
352 100, /* Stack depth in words. */
\r
353 NULL, /* Task parameters. */
\r
354 ( 3 | portPRIVILEGE_BIT ), /* Priority and mode. */
\r
358 /* Start the scheduler. */
\r
359 vTaskStartScheduler();
\r
361 /* Will only get here if there was insufficient memory to create the idle
\r
366 /*-----------------------------------------------------------*/
\r
368 static void prvCheckTask( void *pvParameters )
\r
370 /* This task is created in privileged mode so can access the file scope
\r
371 queue variable. Take a stack copy of this before the task is set into user
\r
372 mode. Once that task is in user mode the file scope queue variable will no
\r
373 longer be accessible but the stack copy will. */
\r
374 xQueueHandle xQueue = xFileScopeCheckQueue;
\r
376 unsigned long ulStillAliveCounts[ 2 ] = { 0 };
\r
377 char *pcStatusMessage = "PASS\r\n";
\r
378 unsigned char x = 5, y = 10;
\r
380 /* Just to remove compiler warning. */
\r
381 ( void ) pvParameters;
\r
383 /* Demonstrate how the various memory regions can and can't be accessed.
\r
384 The task privilege is set down to user mode within this function. */
\r
385 prvTestMemoryRegions();
\r
387 /* Change the memory regions allocated to this task to those initially
\r
388 set up for demonstration purposes to those actually required by the task. */
\r
389 vTaskAllocateMPURegions( NULL, xAltRegions );
\r
391 /* This loop performs the main function of the task, which is blocking
\r
392 on a message queue then processing each message as it arrives. */
\r
395 /* Wait for the next message to arrive. */
\r
396 xQueueReceive( xQueue, &lMessage, portMAX_DELAY );
\r
400 case mainREG_TEST_1_STILL_EXECUTING :
\r
401 /* Message from task 1, so task 1 must still be executing. */
\r
402 ( ulStillAliveCounts[ 0 ] )++;
\r
405 case mainREG_TEST_2_STILL_EXECUTING :
\r
406 /* Message from task 2, so task 2 must still be executing. */
\r
407 ( ulStillAliveCounts[ 1 ] )++;
\r
410 case mainPRINT_SYSTEM_STATUS :
\r
411 /* Message from tick hook, time to print out the system
\r
412 status. If messages has stopped arriving from either reg
\r
413 test task then the status must be set to fail. */
\r
414 if( ( ulStillAliveCounts[ 0 ] == 0 ) || ( ulStillAliveCounts[ 1 ] == 0 ) )
\r
416 /* One or both of the test tasks are no longer sending
\r
417 'still alive' messages. */
\r
418 pcStatusMessage = "FAIL\r\n";
\r
421 /* Print a pass/fail message to the LCD - moving the
\r
422 message each time to provide feedback that the output
\r
423 is still being produced. LCD_PrintString() accesses const
\r
424 data stored in flash, which all tasks are at liberty to do,
\r
425 and GPIO for which an MPU region has been set up for it. */
\r
427 LCD_PrintString( x>>1, y>>1, pcStatusMessage, 6, COLOR_RED );
\r
431 /* Reset the count of 'still alive' messages. */
\r
432 memset( ulStillAliveCounts, 0x00, sizeof( ulStillAliveCounts ) );
\r
436 /* Something unexpected happened. Delete this task so the
\r
437 error is apparent (no output will be displayed). */
\r
443 /*-----------------------------------------------------------*/
\r
445 static void prvTestMemoryRegions( void )
\r
450 /* The check task (from which this function is called) is created in the
\r
451 Privileged mode. The privileged array can be both read from and written
\r
452 to while this task is privileged. */
\r
453 cPrivilegedOnlyAccessArray[ 0 ] = 'a';
\r
454 if( cPrivilegedOnlyAccessArray[ 0 ] != 'a' )
\r
456 /* Something unexpected happened. Delete this task so the error is
\r
457 apparent (no output will be displayed). */
\r
461 /* Writing off the end of the RAM allocated to this task will *NOT* cause a
\r
462 protection fault because the task is still executing in a privileged mode.
\r
463 Uncomment the following to test. */
\r
464 /* cPrivilegedOnlyAccessArray[ mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE ] = 'a'; */
\r
466 /* Now set the task into user mode. */
\r
467 portSWITCH_TO_USER_MODE();
\r
469 /* Accessing the privileged only array will now cause a fault. Uncomment
\r
470 the following line to test. */
\r
471 /* cPrivilegedOnlyAccessArray[ 0 ] = 'a'; */
\r
473 /* The read/write array can still be successfully read and written. */
\r
474 for( l = 0; l < mainREAD_WRITE_ALIGN_SIZE; l++ )
\r
476 cReadWriteArray[ l ] = 'a';
\r
477 if( cReadWriteArray[ l ] != 'a' )
\r
479 /* Something unexpected happened. Delete this task so the error is
\r
480 apparent (no output will be displayed). */
\r
485 /* But attempting to read or write off the end of the RAM allocated to this
\r
486 task will cause a fault. Uncomment either of the following two lines to
\r
488 /* cReadWriteArray[ 0 ] = cReadWriteArray[ -1 ]; */
\r
489 /* cReadWriteArray[ mainREAD_WRITE_ALIGN_SIZE ] = 0x00; */
\r
491 /* The read only array can be successfully read... */
\r
492 for( l = 0; l < mainREAD_ONLY_ALIGN_SIZE; l++ )
\r
494 cTemp = cReadOnlyArray[ l ];
\r
497 /* ...but cannot be written. Uncomment the following line to test. */
\r
498 /* cReadOnlyArray[ 0 ] = 'a'; */
\r
500 /* Writing to the first and last locations in the stack array should not
\r
501 cause a protection fault. Note that doing this will cause the kernel to
\r
502 detect a stack overflow if configCHECK_FOR_STACK_OVERFLOW is greater than
\r
504 xCheckTaskStack[ 0 ] = 0;
\r
505 xCheckTaskStack[ mainCHECK_TASK_STACK_SIZE_WORDS - 1 ] = 0;
\r
507 /* Writing off either end of the stack array should cause a protection
\r
508 fault, uncomment either of the following two lines to test. */
\r
509 /* xCheckTaskStack[ -1 ] = 0; */
\r
510 /* xCheckTaskStack[ mainCHECK_TASK_STACK_SIZE_WORDS ] = 0; */
\r
512 /*-----------------------------------------------------------*/
\r
514 static void prvRegTest1Task( void *pvParameters )
\r
516 /* This task is created in privileged mode so can access the file scope
\r
517 queue variable. Take a stack copy of this before the task is set into user
\r
518 mode. Once this task is in user mode the file scope queue variable will no
\r
519 longer be accessible but the stack copy will. */
\r
520 xQueueHandle xQueue = xFileScopeCheckQueue;
\r
522 /* Now the queue handle has been obtained the task can switch to user
\r
523 mode. This is just one method of passing a handle into a protected
\r
524 task, the other reg test task uses the task parameter instead. */
\r
525 portSWITCH_TO_USER_MODE();
\r
527 /* First check that the parameter value is as expected. */
\r
528 if( pvParameters != ( void * ) 0x12345678 )
\r
530 /* Error detected. Delete the task so it stops communicating with
\r
538 /* This task tests the kernel context switch mechanism by reading and
\r
539 writing directly to registers - which requires the test to be written
\r
540 in assembly code. */
\r
543 " MOV R4, #104 \n" /* Set registers to a known value. R0 to R1 are done in the loop below. */
\r
548 " MOV R10, #110 \n"
\r
549 " MOV R11, #111 \n"
\r
551 " MOV R0, #100 \n" /* Set the scratch registers to known values - done inside the loop as they get clobbered. */
\r
555 " MOV R12, #112 \n"
\r
556 " SVC #1 \n" /* Yield just to increase test coverage. */
\r
557 " CMP R0, #100 \n" /* Check all the registers still contain their expected values. */
\r
558 " BNE prvDeleteMe \n" /* Value was not as expected, delete the task so it stops communicating with the check task. */
\r
560 " BNE prvDeleteMe \n"
\r
562 " BNE prvDeleteMe \n"
\r
564 " BNE prvDeleteMe \n"
\r
565 " CMP R4, #104 \n"
\r
566 " BNE prvDeleteMe \n"
\r
568 " BNE prvDeleteMe \n"
\r
570 " BNE prvDeleteMe \n"
\r
572 " BNE prvDeleteMe \n"
\r
574 " BNE prvDeleteMe \n"
\r
575 " CMP R10, #110 \n"
\r
576 " BNE prvDeleteMe \n"
\r
577 " CMP R11, #111 \n"
\r
578 " BNE prvDeleteMe \n"
\r
579 " CMP R12, #112 \n"
\r
580 " BNE prvDeleteMe \n"
\r
581 :::"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r8", "r9", "r10", "r11", "r12"
\r
584 /* Send mainREG_TEST_1_STILL_EXECUTING to the check task to indicate that this
\r
585 task is still functioning. */
\r
586 prvSendImAlive( xQueue, mainREG_TEST_1_STILL_EXECUTING );
\r
588 /* Go back to check all the register values again. */
\r
589 __asm volatile( " B reg1loop " );
\r
592 /*-----------------------------------------------------------*/
\r
594 static void prvRegTest2Task( void *pvParameters )
\r
596 /* The queue handle is passed in as the task parameter. This is one method of
\r
597 passing data into a protected task, the other reg test task uses a different
\r
599 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
603 /* This task tests the kernel context switch mechanism by reading and
\r
604 writing directly to registers - which requires the test to be written
\r
605 in assembly code. */
\r
608 " MOV R4, #4 \n" /* Set registers to a known value. R0 to R1 are done in the loop below. */
\r
611 " MOV R8, #8 \n" /* Frame pointer is omitted as it must not be changed. */
\r
614 " MOV R11, #11 \n"
\r
616 " MOV R0, #13 \n" /* Set the scratch registers to known values - done inside the loop as they get clobbered. */
\r
621 " CMP R0, #13 \n" /* Check all the registers still contain their expected values. */
\r
622 " BNE prvDeleteMe \n" /* Value was not as expected, delete the task so it stops communicating with the check task */
\r
624 " BNE prvDeleteMe \n"
\r
626 " BNE prvDeleteMe \n"
\r
628 " BNE prvDeleteMe \n"
\r
630 " BNE prvDeleteMe \n"
\r
632 " BNE prvDeleteMe \n"
\r
634 " BNE prvDeleteMe \n"
\r
636 " BNE prvDeleteMe \n"
\r
638 " BNE prvDeleteMe \n"
\r
640 " BNE prvDeleteMe \n"
\r
642 " BNE prvDeleteMe \n"
\r
644 " BNE prvDeleteMe \n"
\r
645 :::"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r8", "r9", "r10", "r11", "r12"
\r
648 /* Send mainREG_TEST_2_STILL_EXECUTING to the check task to indicate that this
\r
649 task is still functioning. */
\r
650 prvSendImAlive( xQueue, mainREG_TEST_2_STILL_EXECUTING );
\r
652 /* Go back to check all the register values again. */
\r
653 __asm volatile( " B reg2loop " );
\r
656 /*-----------------------------------------------------------*/
\r
658 void vApplicationIdleHook( void )
\r
660 extern unsigned long __SRAM_segment_end__[];
\r
661 extern unsigned long __privileged_data_start__[];
\r
662 extern unsigned long __privileged_data_end__[];
\r
663 extern unsigned long __FLASH_segment_start__[];
\r
664 extern unsigned long __FLASH_segment_end__[];
\r
665 volatile unsigned long *pul;
\r
666 volatile unsigned long ulReadData;
\r
668 /* The idle task, and therefore this function, run in Supervisor mode and
\r
669 can therefore access all memory. Try reading from corners of flash and
\r
670 RAM to ensure a memory fault does not occur.
\r
672 Start with the edges of the privileged data area. */
\r
673 pul = __privileged_data_start__;
\r
675 pul = __privileged_data_end__ - 1;
\r
678 /* Next the standard SRAM area. */
\r
679 pul = __SRAM_segment_end__ - 1;
\r
682 /* And the standard Flash area - the start of which is marked for
\r
683 privileged access only. */
\r
684 pul = __FLASH_segment_start__;
\r
686 pul = __FLASH_segment_end__ - 1;
\r
689 /* Reading off the end of Flash or SRAM space should cause a fault.
\r
690 Uncomment one of the following two pairs of lines to test. */
\r
692 /* pul = __FLASH_segment_end__ + 4;
\r
693 ulReadData = *pul; */
\r
695 /* pul = __SRAM_segment_end__ + 1;
\r
696 ulReadData = *pul; */
\r
698 /*-----------------------------------------------------------*/
\r
700 static void prvOldStyleUserModeTask( void *pvParameters )
\r
702 extern unsigned long __privileged_data_start__[];
\r
703 extern unsigned long __privileged_data_end__[];
\r
704 extern unsigned long __SRAM_segment_end__[];
\r
705 extern unsigned long __privileged_functions_end__[];
\r
706 extern unsigned long __FLASH_segment_start__[];
\r
707 extern unsigned long __FLASH_segment_end__[];
\r
708 const volatile unsigned long *pulStandardPeripheralRegister = ( volatile unsigned long * ) 0x400FC0C4; /* PCONP */
\r
709 volatile unsigned long *pul;
\r
710 volatile unsigned long ulReadData;
\r
712 /* The following lines are commented out to prevent the unused variable
\r
713 compiler warnings when the tests that use the variable are also commented out.
\r
714 extern unsigned long __privileged_functions_start__[];
\r
715 const volatile unsigned long *pulSystemPeripheralRegister = ( volatile unsigned long * ) 0xe000e014; */
\r
717 ( void ) pvParameters;
\r
719 /* This task is created in User mode using the original xTaskCreate() API
\r
720 function. It should have access to all Flash and RAM except that marked
\r
721 as Privileged access only. Reading from the start and end of the non-
\r
722 privileged RAM should not cause a problem (the privileged RAM is the first
\r
723 block at the bottom of the RAM memory). */
\r
724 pul = __privileged_data_end__ + 1;
\r
726 pul = __SRAM_segment_end__ - 1;
\r
729 /* Likewise reading from the start and end of the non-privileged Flash
\r
730 should not be a problem (the privileged Flash is the first block at the
\r
731 bottom of the Flash memory). */
\r
732 pul = __privileged_functions_end__ + 1;
\r
734 pul = __FLASH_segment_end__ - 1;
\r
737 /* Standard peripherals are accessible. */
\r
738 ulReadData = *pulStandardPeripheralRegister;
\r
740 /* System peripherals are not accessible. Uncomment the following line
\r
741 to test. Also uncomment the declaration of pulSystemPeripheralRegister
\r
742 at the top of this function. */
\r
743 /* ulReadData = *pulSystemPeripheralRegister; */
\r
745 /* Reading from anywhere inside the privileged Flash or RAM should cause a
\r
746 fault. This can be tested by uncommenting any of the following pairs of
\r
747 lines. Also uncomment the declaration of __privileged_functions_start__
\r
748 at the top of this function. */
\r
750 /* pul = __privileged_functions_start__;
\r
751 ulReadData = *pul; */
\r
753 /* pul = __privileged_functions_end__ - 1;
\r
754 ulReadData = *pul; */
\r
756 /* pul = __privileged_data_start__;
\r
757 ulReadData = *pul; */
\r
759 /* pul = __privileged_data_end__ - 1;
\r
760 ulReadData = *pul; */
\r
762 /* Must not just run off the end of a task function, so delete this task.
\r
763 Note that because this task was created using xTaskCreate() the stack was
\r
764 allocated dynamically and I have not included any code to free it again. */
\r
765 vTaskDelete( NULL );
\r
767 /*-----------------------------------------------------------*/
\r
769 static void prvOldStylePrivilegedModeTask( void *pvParameters )
\r
771 extern unsigned long __privileged_data_start__[];
\r
772 extern unsigned long __privileged_data_end__[];
\r
773 extern unsigned long __SRAM_segment_end__[];
\r
774 extern unsigned long __privileged_functions_start__[];
\r
775 extern unsigned long __privileged_functions_end__[];
\r
776 extern unsigned long __FLASH_segment_start__[];
\r
777 extern unsigned long __FLASH_segment_end__[];
\r
778 volatile unsigned long *pul;
\r
779 volatile unsigned long ulReadData;
\r
780 const volatile unsigned long *pulSystemPeripheralRegister = ( volatile unsigned long * ) 0xe000e014; /* Systick */
\r
781 const volatile unsigned long *pulStandardPeripheralRegister = ( volatile unsigned long * ) 0x400FC0C4; /* PCONP */
\r
783 ( void ) pvParameters;
\r
785 /* This task is created in Privileged mode using the original xTaskCreate()
\r
786 API function. It should have access to all Flash and RAM including that
\r
787 marked as Privileged access only. So reading from the start and end of the
\r
788 non-privileged RAM should not cause a problem (the privileged RAM is the
\r
789 first block at the bottom of the RAM memory). */
\r
790 pul = __privileged_data_end__ + 1;
\r
792 pul = __SRAM_segment_end__ - 1;
\r
795 /* Likewise reading from the start and end of the non-privileged Flash
\r
796 should not be a problem (the privileged Flash is the first block at the
\r
797 bottom of the Flash memory). */
\r
798 pul = __privileged_functions_end__ + 1;
\r
800 pul = __FLASH_segment_end__ - 1;
\r
803 /* Reading from anywhere inside the privileged Flash or RAM should also
\r
804 not be a problem. */
\r
805 pul = __privileged_functions_start__;
\r
807 pul = __privileged_functions_end__ - 1;
\r
809 pul = __privileged_data_start__;
\r
810 ulReadData = *pul;
\r
811 pul = __privileged_data_end__ - 1;
\r
814 /* Finally, accessing both System and normal peripherals should both be
\r
816 ulReadData = *pulSystemPeripheralRegister;
\r
817 ulReadData = *pulStandardPeripheralRegister;
\r
819 /* Must not just run off the end of a task function, so delete this task.
\r
820 Note that because this task was created using xTaskCreate() the stack was
\r
821 allocated dynamically and I have not included any code to free it again. */
\r
822 vTaskDelete( NULL );
\r
824 /*-----------------------------------------------------------*/
\r
826 static void prvDeleteMe( void )
\r
828 vTaskDelete( NULL );
\r
830 /*-----------------------------------------------------------*/
\r
832 static void prvSendImAlive( xQueueHandle xHandle, unsigned long ulTaskNumber )
\r
834 if( xHandle != NULL )
\r
836 xQueueSend( xHandle, &ulTaskNumber, mainDONT_BLOCK );
\r
839 /*-----------------------------------------------------------*/
\r
841 void prvSetupHardware( void )
\r
843 /* Disable peripherals power. */
\r
846 /* Enable GPIO power. */
\r
847 SC->PCONP = PCONP_PCGPIO;
\r
849 /* Disable TPIU. */
\r
850 PINCON->PINSEL10 = 0;
\r
852 if ( SC->PLL0STAT & ( 1 << 25 ) )
\r
854 /* Enable PLL, disconnected. */
\r
856 SC->PLL0FEED = PLLFEED_FEED1;
\r
857 SC->PLL0FEED = PLLFEED_FEED2;
\r
860 /* Disable PLL, disconnected. */
\r
862 SC->PLL0FEED = PLLFEED_FEED1;
\r
863 SC->PLL0FEED = PLLFEED_FEED2;
\r
865 /* Enable main OSC. */
\r
867 while( !( SC->SCS & 0x40 ) );
\r
869 /* select main OSC, 12MHz, as the PLL clock source. */
\r
870 SC->CLKSRCSEL = 0x1;
\r
872 SC->PLL0CFG = 0x20031;
\r
873 SC->PLL0FEED = PLLFEED_FEED1;
\r
874 SC->PLL0FEED = PLLFEED_FEED2;
\r
876 /* Enable PLL, disconnected. */
\r
878 SC->PLL0FEED = PLLFEED_FEED1;
\r
879 SC->PLL0FEED = PLLFEED_FEED2;
\r
881 /* Set clock divider. */
\r
882 SC->CCLKCFG = 0x03;
\r
884 /* Configure flash accelerator. */
\r
885 SC->FLASHCFG = 0x403a;
\r
887 /* Check lock bit status. */
\r
888 while( ( ( SC->PLL0STAT & ( 1 << 26 ) ) == 0 ) );
\r
890 /* Enable and connect. */
\r
892 SC->PLL0FEED = PLLFEED_FEED1;
\r
893 SC->PLL0FEED = PLLFEED_FEED2;
\r
894 while( ( ( SC->PLL0STAT & ( 1 << 25 ) ) == 0 ) );
\r
899 /* Configure the clock for the USB. */
\r
901 if( SC->PLL1STAT & ( 1 << 9 ) )
\r
903 /* Enable PLL, disconnected. */
\r
905 SC->PLL1FEED = PLLFEED_FEED1;
\r
906 SC->PLL1FEED = PLLFEED_FEED2;
\r
909 /* Disable PLL, disconnected. */
\r
911 SC->PLL1FEED = PLLFEED_FEED1;
\r
912 SC->PLL1FEED = PLLFEED_FEED2;
\r
914 SC->PLL1CFG = 0x23;
\r
915 SC->PLL1FEED = PLLFEED_FEED1;
\r
916 SC->PLL1FEED = PLLFEED_FEED2;
\r
918 /* Enable PLL, disconnected. */
\r
920 SC->PLL1FEED = PLLFEED_FEED1;
\r
921 SC->PLL1FEED = PLLFEED_FEED2;
\r
922 while( ( ( SC->PLL1STAT & ( 1 << 10 ) ) == 0 ) );
\r
924 /* Enable and connect. */
\r
926 SC->PLL1FEED = PLLFEED_FEED1;
\r
927 SC->PLL1FEED = PLLFEED_FEED2;
\r
928 while( ( ( SC->PLL1STAT & ( 1 << 9 ) ) == 0 ) );
\r
930 /* Setup the peripheral bus to be the same as the PLL output (64 MHz). */
\r
931 SC->PCLKSEL0 = 0x05555555;
\r
933 /* Prepare the LCD. */
\r
934 LCDdriver_initialisation();
\r
935 LCD_PrintString( 5, 10, "FreeRTOS.org", 14, COLOR_GREEN);
\r
937 /*-----------------------------------------------------------*/
\r
939 void vApplicationTickHook( void )
\r
941 static unsigned long ulCallCount;
\r
942 const unsigned long ulCallsBetweenSends = 5000 / portTICK_RATE_MS;
\r
943 const unsigned long ulMessage = mainPRINT_SYSTEM_STATUS;
\r
944 portBASE_TYPE xDummy;
\r
946 /* If configUSE_TICK_HOOK is set to 1 then this function will get called
\r
947 from each RTOS tick. It is called from the tick interrupt and therefore
\r
948 will be executing in the privileged state. */
\r
952 /* Is it time to print out the pass/fail message again? */
\r
953 if( ulCallCount >= ulCallsBetweenSends )
\r
957 /* Send a message to the check task to command it to check that all
\r
958 the tasks are still running then print out the status.
\r
960 This is running in an ISR so has to use the "FromISR" version of
\r
961 xQueueSend(). Because it is in an ISR it is running with privileges
\r
962 so can access xFileScopeCheckQueue directly. */
\r
963 xQueueSendFromISR( xFileScopeCheckQueue, &ulMessage, &xDummy );
\r
966 /*-----------------------------------------------------------*/
\r
968 void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )
\r
970 /* If configCHECK_FOR_STACK_OVERFLOW is set to either 1 or 2 then this
\r
971 function will automatically get called if a task overflows its stack. */
\r
973 ( void ) pcTaskName;
\r
976 /*-----------------------------------------------------------*/
\r
978 void vApplicationMallocFailedHook( void )
\r
980 /* If configUSE_MALLOC_FAILED_HOOK is set to 1 then this function will
\r
981 be called automatically if a call to pvPortMalloc() fails. pvPortMalloc()
\r
982 is called automatically when a task, queue or semaphore is created. */
\r
985 /*-----------------------------------------------------------*/
\r