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 /* Standard includes. */
\r
51 #include <__cross_studio_io.h>
\r
53 /* Scheduler includes. */
\r
54 #include "FreeRTOS.h"
\r
59 /* Hardware library includes. */
\r
60 #include "hw_types.h"
\r
61 #include "hw_sysctl.h"
\r
65 * This file demonstrates the use of MPU using just three tasks - two 'reg test'
\r
66 * tasks and one 'check' task. Read the comments above the
\r
67 * function prototypes for more information.
\r
70 /*-----------------------------------------------------------*/
\r
72 /* Misc constants. */
\r
73 #define mainDONT_BLOCK ( 0 )
\r
75 /* Definitions for the messages that can be sent to the check task. */
\r
76 #define mainREG_TEST_1_STILL_EXECUTING ( 0 )
\r
77 #define mainREG_TEST_2_STILL_EXECUTING ( 1 )
\r
78 #define mainPRINT_SYSTEM_STATUS ( 2 )
\r
80 /* GCC specifics. */
\r
81 #define mainALIGN_TO( x ) __attribute__((aligned(x)))
\r
84 /*-----------------------------------------------------------*/
\r
85 /* Prototypes for functions that implement tasks. -----------*/
\r
86 /*-----------------------------------------------------------*/
\r
89 * Prototype for the reg test tasks. Amongst other things, these fill the CPU
\r
90 * registers with known values before checking that the registers still contain
\r
91 * the expected values. Each of the two tasks use different values so an error
\r
92 * in the context switch mechanism can be caught. Both reg test tasks execute
\r
93 * at the idle priority so will get preempted regularly.
\r
95 static void prvRegTest1Task( void *pvParameters );
\r
96 static void prvRegTest2Task( void *pvParameters );
\r
99 * Prototype for the check task. The check task demonstrates various features
\r
100 * of the MPU before entering a loop where it waits for commands to arrive on a
\r
103 * The check task will periodically be commanded to print out a status message.
\r
104 * If both the reg tests tasks are executing as expected the check task will
\r
105 * print "PASS" to the debug port, otherwise it will print 'FAIL'. Debug port
\r
106 * messages can be viewed within the CrossWorks IDE.
\r
108 static void prvCheckTask( void *pvParameters );
\r
111 * Prototype for the User mode task that is created using the original
\r
112 * vTaskCreate() API function.
\r
114 static void prvOldStyleUserModeTask( void *pvParameters );
\r
116 /*-----------------------------------------------------------*/
\r
117 /* Prototypes for other misc functions. --------------------*/
\r
118 /*-----------------------------------------------------------*/
\r
121 * Just configures any clocks and IO necessary.
\r
123 static void prvSetupHardware( void );
\r
126 * Simply deletes the calling task. The function is provided only because it
\r
127 * is simpler to call from asm code than the normal vTaskDelete() API function.
\r
128 * It has the noinline attribute because it is called from asm code.
\r
130 static void prvDeleteMe( void ) __attribute__((noinline));
\r
133 * Used by both reg test tasks to send messages to the check task. The message
\r
134 * just lets the check task know that the sending is still functioning correctly.
\r
135 * If a reg test task detects an error it will delete itself, and in so doing
\r
136 * prevent itself from sending any more 'I'm Alive' messages to the check task.
\r
138 static void prvSendImAlive( xQueueHandle xHandle, unsigned long ulTaskNumber );
\r
141 * The check task is created with access to three memory regions (plus its
\r
142 * stack). Each memory region is configured with different parameters and
\r
143 * prvTestMemoryRegions() demonstrates what can and cannot be accessed for each
\r
144 * region. prvTestMemoryRegions() also demonstrates a task that was created
\r
145 * as a privileged task settings its own privilege level down to that of a user
\r
148 static void prvTestMemoryRegions( void );
\r
150 /*-----------------------------------------------------------*/
\r
152 /* The handle of the queue used to communicate between tasks and between tasks
\r
153 and interrupts. Note that this is a file scope variable that falls outside of
\r
154 any MPU region. As such other techniques have to be used to allow the tasks
\r
155 to gain access to the queue. See the comments in the tasks themselves for
\r
156 further information. */
\r
157 static xQueueHandle xFileScopeCheckQueue = NULL;
\r
161 /*-----------------------------------------------------------*/
\r
162 /* Data used by the 'check' task. ---------------------------*/
\r
163 /*-----------------------------------------------------------*/
\r
165 /* Define the constants used to allocate the check task stack. Note that the
\r
166 stack size is defined in words, not bytes. */
\r
167 #define mainCHECK_TASK_STACK_SIZE_WORDS 128
\r
168 #define mainCHECK_TASK_STACK_ALIGNMENT ( mainCHECK_TASK_STACK_SIZE_WORDS * sizeof( portSTACK_TYPE ) )
\r
170 /* Declare the stack that will be used by the check task. The kernel will
\r
171 automatically create an MPU region for the stack. The stack alignment must
\r
172 match its size, so if 128 words are reserved for the stack then it must be
\r
173 aligned to ( 128 * 4 ) bytes. */
\r
174 static portSTACK_TYPE xCheckTaskStack[ mainCHECK_TASK_STACK_SIZE_WORDS ] mainALIGN_TO( mainCHECK_TASK_STACK_ALIGNMENT );
\r
176 /* Declare three arrays - an MPU region will be created for each array
\r
177 using the xTaskParameters structure below. Note that the arrays allocate
\r
178 slightly more RAM than is actually assigned to the MPU region. This is to
\r
179 permit writes off the end of the array to be detected even when the arrays are
\r
180 placed in adjacent memory locations (with no gaps between them). The align
\r
181 size must be a power of two. */
\r
182 #define mainREAD_WRITE_ARRAY_SIZE 130
\r
183 #define mainREAD_WRITE_ALIGN_SIZE 128
\r
184 char cReadWriteArray[ mainREAD_WRITE_ARRAY_SIZE ] mainALIGN_TO( mainREAD_WRITE_ALIGN_SIZE );
\r
186 #define mainREAD_ONLY_ARRAY_SIZE 260
\r
187 #define mainREAD_ONLY_ALIGN_SIZE 256
\r
188 char cReadOnlyArray[ mainREAD_ONLY_ARRAY_SIZE ] mainALIGN_TO( mainREAD_ONLY_ALIGN_SIZE );
\r
190 #define mainPRIVILEGED_ONLY_ACCESS_ARRAY_SIZE 130
\r
191 #define mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE 128
\r
192 char cPrivilegedOnlyAccessArray[ mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE ] mainALIGN_TO( mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE );
\r
194 /* Fill in a xTaskParameters structure to define the check task. */
\r
195 static const xTaskParameters xCheckTaskParameters =
\r
197 prvCheckTask, /* pvTaskCode - the function that implements the task. */
\r
198 ( signed char * ) "Check", /* pcName */
\r
199 mainCHECK_TASK_STACK_SIZE_WORDS, /* usStackDepth - defined in words, not bytes. */
\r
200 ( void * ) 0x12121212, /* pvParameters - this value is just to test that the parameter is being passed into the task correctly. */
\r
201 ( 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
202 xCheckTaskStack, /* puxStackBuffer - the array to use as the task stack, as declared above. */
\r
204 /* xRegions - In this case the xRegions array is used to create MPU regions
\r
205 for all three of the arrays declared directly above. Each MPU region is
\r
206 created with different parameters. */
\r
208 /* Base address Length Parameters */
\r
209 { cReadWriteArray, mainREAD_WRITE_ALIGN_SIZE, portMPU_REGION_READ_WRITE },
\r
210 { cReadOnlyArray, mainREAD_ONLY_ALIGN_SIZE, portMPU_REGION_READ_ONLY },
\r
211 { cPrivilegedOnlyAccessArray, mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE, portMPU_REGION_PRIVILEGED_READ_WRITE }
\r
215 /* Three MPU regions are defined for use by the 'check' task when the task is
\r
216 created. These are only used to demonstrate the MPU features and are not
\r
217 actually necessary for the check task to fulfill its primary purpose. Instead
\r
218 the MPU regions are replaced with those defined by xAltRegions prior to the
\r
219 check task receiving any data on the queue or printing any messages to the
\r
220 debug console. The region configured by xAltRegions just gives the check task
\r
221 access to the debug variables that form part of the Rowley library, and are
\r
222 accessed within the debug_printf() function. */
\r
223 extern unsigned long dbgCntrlWord_mempoll;
\r
224 static const xMemoryRegion xAltRegions[ portNUM_CONFIGURABLE_REGIONS ] =
\r
226 /* Base address Length Parameters */
\r
227 { ( void * ) &dbgCntrlWord_mempoll, 32, portMPU_REGION_READ_WRITE },
\r
234 /*-----------------------------------------------------------*/
\r
235 /* Data used by the 'reg test' tasks. -----------------------*/
\r
236 /*-----------------------------------------------------------*/
\r
238 /* Define the constants used to allocate the reg test task stacks. Note that
\r
239 that stack size is defined in words, not bytes. */
\r
240 #define mainREG_TEST_STACK_SIZE_WORDS 128
\r
241 #define mainREG_TEST_STACK_ALIGNMENT ( mainREG_TEST_STACK_SIZE_WORDS * sizeof( portSTACK_TYPE ) )
\r
243 /* Declare the stacks that will be used by the reg test tasks. The kernel will
\r
244 automatically create an MPU region for the stack. The stack alignment must
\r
245 match its size, so if 128 words are reserved for the stack then it must be
\r
246 aligned to ( 128 * 4 ) bytes. */
\r
247 static portSTACK_TYPE xRegTest1Stack[ mainREG_TEST_STACK_SIZE_WORDS ] mainALIGN_TO( mainREG_TEST_STACK_ALIGNMENT );
\r
248 static portSTACK_TYPE xRegTest2Stack[ mainREG_TEST_STACK_SIZE_WORDS ] mainALIGN_TO( mainREG_TEST_STACK_ALIGNMENT );
\r
250 /* Fill in a xTaskParameters structure per reg test task to define the tasks. */
\r
251 static const xTaskParameters xRegTest1Parameters =
\r
253 prvRegTest1Task, /* pvTaskCode - the function that implements the task. */
\r
254 ( signed char * ) "RegTest1", /* pcName */
\r
255 mainREG_TEST_STACK_SIZE_WORDS, /* usStackDepth */
\r
256 ( void * ) 0x12345678, /* pvParameters - this value is just to test that the parameter is being passed into the task correctly. */
\r
257 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
258 xRegTest1Stack, /* puxStackBuffer - the array to use as the task stack, as declared above. */
\r
259 { /* xRegions - this task does not use any non-stack data. */
\r
260 /* Base address Length Parameters */
\r
261 { 0x00, 0x00, 0x00 },
\r
262 { 0x00, 0x00, 0x00 },
\r
263 { 0x00, 0x00, 0x00 }
\r
266 /*-----------------------------------------------------------*/
\r
268 static xTaskParameters xRegTest2Parameters =
\r
270 prvRegTest2Task, /* pvTaskCode - the function that implements the task. */
\r
271 ( signed char * ) "RegTest2", /* pcName */
\r
272 mainREG_TEST_STACK_SIZE_WORDS, /* usStackDepth */
\r
273 ( void * ) NULL, /* pvParameters - this task uses the parameter to pass in a queue handle, but the queue is not created yet. */
\r
274 tskIDLE_PRIORITY, /* uxPriority */
\r
275 xRegTest2Stack, /* puxStackBuffer - the array to use as the task stack, as declared above. */
\r
276 { /* xRegions - this task does not use any non-stack data. */
\r
277 /* Base address Length Parameters */
\r
278 { 0x00, 0x00, 0x00 },
\r
279 { 0x00, 0x00, 0x00 },
\r
280 { 0x00, 0x00, 0x00 }
\r
284 /*-----------------------------------------------------------*/
\r
288 prvSetupHardware();
\r
290 /* Create the queue used to pass "I'm alive" messages to the check task. */
\r
291 xFileScopeCheckQueue = xQueueCreate( 1, sizeof( unsigned long ) );
\r
293 /* One check task uses the task parameter to receive the queue handle.
\r
294 This allows the file scope variable to be accessed from within the task.
\r
295 The pvParameters member of xRegTest2Parameters can only be set after the
\r
296 queue has been created. */
\r
297 xRegTest2Parameters.pvParameters = xFileScopeCheckQueue;
\r
299 /* Create the three test tasks. Handles to the created tasks are not
\r
300 required, hence the second parameter is NULL. */
\r
301 xTaskCreateRestricted( &xRegTest1Parameters, NULL );
\r
302 xTaskCreateRestricted( &xRegTest2Parameters, NULL );
\r
303 xTaskCreateRestricted( &xCheckTaskParameters, NULL );
\r
305 /* Create the tasks that are created using the original xTaskCreate() API
\r
307 xTaskCreate( prvOldStyleUserModeTask, /* The function that implements the task. */
\r
308 ( signed char * ) "Task1", /* Text name for the task. */
\r
309 100, /* Stack depth in words. */
\r
310 NULL, /* Task parameters. */
\r
315 /* Print out the amount of free heap space so configTOTAL_HEAP_SIZE can be
\r
316 tuned. The heap size is set to be very small in this example and will need
\r
317 to be increased before many more tasks, queues or semaphores can be
\r
319 debug_printf( "There are %d bytes of unused heap space, although the idle task is yet to be created.\r\n", xPortGetFreeHeapSize() );
\r
321 /* Start the scheduler. */
\r
322 vTaskStartScheduler();
\r
324 /* Will only get here if there was insufficient memory to create the idle
\r
329 /*-----------------------------------------------------------*/
\r
331 static void prvCheckTask( void *pvParameters )
\r
333 /* This task is created in privileged mode so can access the file scope
\r
334 queue variable. Take a stack copy of this before the task is set into user
\r
335 mode. Once that task is in user mode the file scope queue variable will no
\r
336 longer be accessible but the stack copy will. */
\r
337 xQueueHandle xQueue = xFileScopeCheckQueue;
\r
339 unsigned long ulStillAliveCounts[ 2 ] = { 0 };
\r
340 const char *pcStatusMessage = "PASS\r\n";
\r
342 /* Just to remove compiler warning. */
\r
343 ( void ) pvParameters;
\r
345 /* Demonstrate how the various memory regions can and can't be accessed.
\r
346 The task privilege is set down to user mode within this function. */
\r
347 prvTestMemoryRegions();
\r
349 /* Change the memory regions allocated to this task to those initially
\r
350 set up for demonstration purposes to those actually required by the task. */
\r
351 vTaskAllocateMPURegions( NULL, xAltRegions );
\r
353 /* This loop performs the main function of the task, which is blocking
\r
354 on a message queue then processing each message as it arrives. */
\r
357 /* Wait for the next message to arrive. */
\r
358 xQueueReceive( xQueue, &lMessage, portMAX_DELAY );
\r
362 case mainREG_TEST_1_STILL_EXECUTING :
\r
363 /* Message from task 1, so task 1 must still be executing. */
\r
364 ( ulStillAliveCounts[ 0 ] )++;
\r
367 case mainREG_TEST_2_STILL_EXECUTING :
\r
368 /* Message from task 2, so task 2 must still be executing. */
\r
369 ( ulStillAliveCounts[ 1 ] )++;
\r
372 case mainPRINT_SYSTEM_STATUS :
\r
373 /* Message from tick hook, time to print out the system
\r
374 status. If messages has stopped arriving from either reg
\r
375 test task then the status must be set to fail. */
\r
376 if( ( ulStillAliveCounts[ 0 ] == 0 ) || ( ulStillAliveCounts[ 1 ] == 0 ) )
\r
378 /* One or both of the test tasks are no longer sending
\r
379 'still alive' messages. */
\r
380 pcStatusMessage = "FAIL\r\n";
\r
383 /* Print a pass/fail message to the terminal. This will be
\r
384 visible in the CrossWorks IDE. */
\r
385 debug_printf( pcStatusMessage );
\r
387 /* Reset the count of 'still alive' messages. */
\r
388 memset( ulStillAliveCounts, 0x00, sizeof( ulStillAliveCounts ) );
\r
392 /* Something unexpected happened. Delete this task so the
\r
393 error is apparent (no output will be displayed). */
\r
399 /*-----------------------------------------------------------*/
\r
401 static void prvTestMemoryRegions( void )
\r
406 /* The check task is created in the privileged mode. The privileged array
\r
407 can be both read from and written to while this task is privileged. */
\r
408 cPrivilegedOnlyAccessArray[ 0 ] = 'a';
\r
409 if( cPrivilegedOnlyAccessArray[ 0 ] != 'a' )
\r
411 /* Something unexpected happened. Delete this task so the error is
\r
412 apparent (no output will be displayed). */
\r
416 /* Writing off the end of the RAM allocated to this task will *NOT* cause a
\r
417 protection fault because the task is still executing in a privileged mode.
\r
418 Uncomment the following to test. */
\r
419 /*cPrivilegedOnlyAccessArray[ mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE ] = 'a';*/
\r
421 /* Now set the task into user mode. */
\r
422 portSWITCH_TO_USER_MODE();
\r
424 /* Accessing the privileged only array will now cause a fault. Uncomment
\r
425 the following line to test. */
\r
426 /*cPrivilegedOnlyAccessArray[ 0 ] = 'a';*/
\r
428 /* The read/write array can still be successfully read and written. */
\r
429 for( l = 0; l < mainREAD_WRITE_ALIGN_SIZE; l++ )
\r
431 cReadWriteArray[ l ] = 'a';
\r
432 if( cReadWriteArray[ l ] != 'a' )
\r
434 /* Something unexpected happened. Delete this task so the error is
\r
435 apparent (no output will be displayed). */
\r
440 /* But attempting to read or write off the end of the RAM allocated to this
\r
441 task will cause a fault. Uncomment either of the following two lines to
\r
443 /* cReadWriteArray[ 0 ] = cReadWriteArray[ -1 ]; */
\r
444 /* cReadWriteArray[ mainREAD_WRITE_ALIGN_SIZE ] = 0x00; */
\r
446 /* The read only array can be successfully read... */
\r
447 for( l = 0; l < mainREAD_ONLY_ALIGN_SIZE; l++ )
\r
449 cTemp = cReadOnlyArray[ l ];
\r
452 /* ...but cannot be written. Uncomment the following line to test. */
\r
453 /* cReadOnlyArray[ 0 ] = 'a'; */
\r
455 /* Writing to the first and last locations in the stack array should not
\r
456 cause a protection fault. Note that doing this will cause the kernel to
\r
457 detect a stack overflow if configCHECK_FOR_STACK_OVERFLOW is greater than
\r
459 xCheckTaskStack[ 0 ] = 0;
\r
460 xCheckTaskStack[ mainCHECK_TASK_STACK_SIZE_WORDS - 1 ] = 0;
\r
462 /* Writing off either end of the stack array should cause a protection
\r
463 fault, uncomment either of the following two lines to test. */
\r
464 /* xCheckTaskStack[ -1 ] = 0; */
\r
465 /* xCheckTaskStack[ mainCHECK_TASK_STACK_SIZE_WORDS ] = 0; */
\r
467 /*-----------------------------------------------------------*/
\r
469 static void prvRegTest1Task( void *pvParameters )
\r
471 /* This task is created in privileged mode so can access the file scope
\r
472 queue variable. Take a stack copy of this before the task is set into user
\r
473 mode. Once that task is in user mode the file scope queue variable will no
\r
474 longer be accessible but the stack copy will. */
\r
475 xQueueHandle xQueue = xFileScopeCheckQueue;
\r
477 /* Now the queue handle has been obtained the task can switch to user
\r
478 mode. This is just one method of passing a handle into a protected
\r
479 task, the other reg test task uses the task parameter instead. */
\r
480 portSWITCH_TO_USER_MODE();
\r
482 /* First check that the parameter value is as expected. */
\r
483 if( pvParameters != ( void * ) 0x12345678 )
\r
485 /* Error detected. Delete the task so it stops communicating with
\r
493 /* This task tests the kernel context switch mechanism by reading and
\r
494 writing directly to registers - which requires the test to be written
\r
495 in assembly code. */
\r
498 " MOV R4, #104 \n" /* Set registers to a known value. R0 to R1 are done in the loop below. */
\r
503 " MOV R10, #110 \n"
\r
504 " MOV R11, #111 \n"
\r
506 " MOV R0, #100 \n" /* Set the scratch registers to known values - done inside the loop as they get clobbered. */
\r
510 " MOV R12, #112 \n"
\r
511 " SVC #1 \n" /* Yield just to increase test coverage. */
\r
512 " CMP R0, #100 \n" /* Check all the registers still contain their expected values. */
\r
513 " BNE prvDeleteMe \n" /* Value was not as expected, delete the task so it stops communicating with the check task. */
\r
515 " BNE prvDeleteMe \n"
\r
517 " BNE prvDeleteMe \n"
\r
519 " BNE prvDeleteMe \n"
\r
520 " CMP R4, #104 \n"
\r
521 " BNE prvDeleteMe \n"
\r
523 " BNE prvDeleteMe \n"
\r
525 " BNE prvDeleteMe \n"
\r
527 " BNE prvDeleteMe \n"
\r
529 " BNE prvDeleteMe \n"
\r
530 " CMP R10, #110 \n"
\r
531 " BNE prvDeleteMe \n"
\r
532 " CMP R11, #111 \n"
\r
533 " BNE prvDeleteMe \n"
\r
534 " CMP R12, #112 \n"
\r
535 " BNE prvDeleteMe \n"
\r
536 :::"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r8", "r9", "r10", "r11", "r12"
\r
539 /* Send mainREG_TEST_1_STILL_EXECUTING to the check task to indicate that this
\r
540 task is still functioning. */
\r
541 prvSendImAlive( xQueue, mainREG_TEST_1_STILL_EXECUTING );
\r
543 /* Go back to check all the register values again. */
\r
544 __asm volatile( " B reg1loop " );
\r
547 /*-----------------------------------------------------------*/
\r
549 static void prvRegTest2Task( void *pvParameters )
\r
551 /* The queue handle is passed in as the task parameter. This is one method of
\r
552 passing data into a protected task, the other check task uses a different
\r
554 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
558 /* This task tests the kernel context switch mechanism by reading and
\r
559 writing directly to registers - which requires the test to be written
\r
560 in assembly code. */
\r
563 " MOV R4, #4 \n" /* Set registers to a known value. R0 to R1 are done in the loop below. */
\r
566 " MOV R8, #8 \n" /* Frame pointer is omitted as it must not be changed. */
\r
569 " MOV R11, #11 \n"
\r
571 " MOV R0, #13 \n" /* Set the scratch registers to known values - done inside the loop as they get clobbered. */
\r
576 " CMP R0, #13 \n" /* Check all the registers still contain their expected values. */
\r
577 " BNE prvDeleteMe \n" /* Value was not as expected, delete the task so it stops communicating with the check task */
\r
579 " BNE prvDeleteMe \n"
\r
581 " BNE prvDeleteMe \n"
\r
583 " BNE prvDeleteMe \n"
\r
585 " BNE prvDeleteMe \n"
\r
587 " BNE prvDeleteMe \n"
\r
589 " BNE prvDeleteMe \n"
\r
591 " BNE prvDeleteMe \n"
\r
593 " BNE prvDeleteMe \n"
\r
595 " BNE prvDeleteMe \n"
\r
597 " BNE prvDeleteMe \n"
\r
599 " BNE prvDeleteMe \n"
\r
600 :::"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r8", "r9", "r10", "r11", "r12"
\r
603 /* Send mainREG_TEST_2_STILL_EXECUTING to the check task to indicate that this
\r
604 task is still functioning. */
\r
605 prvSendImAlive( xQueue, mainREG_TEST_2_STILL_EXECUTING );
\r
607 /* Go back to check all the register values again. */
\r
608 __asm volatile( " B reg2loop " );
\r
611 /*-----------------------------------------------------------*/
\r
613 void vApplicationIdleHook( void )
\r
615 extern unsigned long __SRAM_segment_end__[];
\r
616 extern unsigned long __privileged_data_start__[];
\r
617 extern unsigned long __privileged_data_end__[];
\r
618 extern unsigned long __FLASH_segment_start__[];
\r
619 extern unsigned long __FLASH_segment_end__[];
\r
620 volatile unsigned long *pul;
\r
621 volatile unsigned long ulReadData;
\r
623 /* The idle task, and therefore this function, run in Supervisor mode and
\r
624 can therefore access all memory. Try reading from corners of flash and
\r
625 RAM to ensure a memory fault does not occur.
\r
627 Start with the edges of the privileged data area. */
\r
628 pul = __privileged_data_start__;
\r
630 pul = __privileged_data_end__ - 1;
\r
633 /* Next the standard SRAM area. */
\r
634 pul = __SRAM_segment_end__ - 1;
\r
637 /* And the standard Flash area - the start of which is marked for
\r
638 privileged access only. */
\r
639 pul = __FLASH_segment_start__;
\r
641 pul = __FLASH_segment_end__ - 1;
\r
644 /* Reading off the end of Flash or SRAM space should cause a fault.
\r
645 Uncomment one of the following two pairs of lines to test. */
\r
647 /* pul = __FLASH_segment_end__ + 4;
\r
648 ulReadData = *pul; */
\r
650 /* pul = __SRAM_segment_end__ + 1;
\r
651 ulReadData = *pul; */
\r
653 /*-----------------------------------------------------------*/
\r
655 static void prvOldStyleUserModeTask( void *pvParameters )
\r
657 extern unsigned long __privileged_data_start__[];
\r
658 extern unsigned long __privileged_data_end__[];
\r
659 extern unsigned long __SRAM_segment_end__[];
\r
660 extern unsigned long __privileged_functions_end__[];
\r
661 extern unsigned long __FLASH_segment_start__[];
\r
662 extern unsigned long __FLASH_segment_end__[];
\r
663 volatile unsigned long *pul;
\r
664 volatile unsigned long ulReadData;
\r
666 /* The following line is commented out to prevent the unused variable compiler
\r
667 warning when the test that uses the variable is commented out.
\r
668 extern unsigned long __privileged_functions_start__[]; */
\r
670 ( void ) pvParameters;
\r
672 /* This task is created in User mode using the original xTaskCreate() API
\r
673 function. It should have access to all Flash and RAM except that marked
\r
674 as Privileged access only. Reading from the start and end of the non-
\r
675 privileged RAM should not cause a problem (the privileged RAM is the first
\r
676 block at the bottom of the RAM memory). */
\r
677 pul = __privileged_data_end__ + 1;
\r
679 pul = __SRAM_segment_end__ - 1;
\r
682 /* Likewise reading from the start and end of the non-privileged Flash
\r
683 should not be a problem (the privileged Flash is the first block at the
\r
684 bottom of the Flash memory). */
\r
685 pul = __privileged_functions_end__ + 1;
\r
687 pul = __FLASH_segment_end__ - 1;
\r
690 /* Reading from anywhere inside the privileged Flash or RAM should cause a
\r
691 fault. This can be tested by uncommenting any of the following pairs of
\r
694 /* pul = __privileged_functions_start__;
\r
695 ulReadData = *pul; */
\r
697 /* pul = __privileged_functions_end__ - 1;
\r
698 ulReadData = *pul; */
\r
700 /* pul = __privileged_data_start__;
\r
701 ulReadData = *pul; */
\r
703 /* pul = __privileged_data_end__ - 1;
\r
704 ulReadData = *pul; */
\r
706 /* Must not just run off the end of a task function, so delete this task. */
\r
707 vTaskDelete( NULL );
\r
709 /*-----------------------------------------------------------*/
\r
711 static void prvDeleteMe( void )
\r
713 vTaskDelete( NULL );
\r
715 /*-----------------------------------------------------------*/
\r
717 static void prvSendImAlive( xQueueHandle xHandle, unsigned long ulTaskNumber )
\r
719 if( xHandle != NULL )
\r
721 xQueueSend( xHandle, &ulTaskNumber, mainDONT_BLOCK );
\r
724 /*-----------------------------------------------------------*/
\r
726 static void prvSetupHardware( void )
\r
728 /* If running on Rev A2 silicon, turn the LDO voltage up to 2.75V. This is
\r
729 a workaround to allow the PLL to operate reliably. */
\r
730 if( DEVICE_IS_REVA2 )
\r
732 SysCtlLDOSet( SYSCTL_LDO_2_75V );
\r
735 /* Set the clocking to run from the PLL at 50 MHz */
\r
736 SysCtlClockSet( SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_8MHZ );
\r
738 /*-----------------------------------------------------------*/
\r
740 void vApplicationTickHook( void )
\r
742 static unsigned long ulCallCount;
\r
743 const unsigned long ulCallsBetweenSends = 5000 / portTICK_RATE_MS;
\r
744 const unsigned long ulMessage = mainPRINT_SYSTEM_STATUS;
\r
745 portBASE_TYPE xDummy;
\r
747 /* If configUSE_TICK_HOOK is set to 1 then this function will get called
\r
748 from each RTOS tick. It is called from the tick interrupt and therefore
\r
749 will be executing in the privileged state. */
\r
753 /* Is it time to print out the pass/fail message again? */
\r
754 if( ulCallCount >= ulCallsBetweenSends )
\r
758 /* Send a message to the check task to command it to check that all
\r
759 the tasks are still running then print out the status.
\r
761 This is running in an ISR so has to use the "FromISR" version of
\r
762 xQueueSend(). Because it is in an ISR it is running with privileges
\r
763 so can access xFileScopeCheckQueue directly. */
\r
764 xQueueSendFromISR( xFileScopeCheckQueue, &ulMessage, &xDummy );
\r
767 /*-----------------------------------------------------------*/
\r
769 void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )
\r
771 /* If configCHECK_FOR_STACK_OVERFLOW is set to either 1 or 2 then this
\r
772 function will automatically get called if a task overflows its stack. */
\r
774 ( void ) pcTaskName;
\r
777 /*-----------------------------------------------------------*/
\r
779 void vApplicationMallocFailedHook( void )
\r
781 /* If configUSE_MALLOC_FAILED_HOOK is set to 1 then this function will
\r
782 be called automatically if a call to pvPortMalloc() fails. pvPortMalloc()
\r
783 is called automatically when a task, queue or semaphore is created. */
\r
786 /*-----------------------------------------------------------*/
\r
788 /* Just to keep the linker happy. */
\r
789 void __error__( char *pcFilename, unsigned long ulLine )
\r
791 ( void ) pcFilename;
\r
795 /*-----------------------------------------------------------*/
\r
797 /* Just to keep the linker happy. */
\r
798 int uipprintf( const char *fmt, ... )
\r
803 /*-----------------------------------------------------------*/
\r
805 void hard_fault_handler(unsigned int * hardfault_args)
\r
807 volatile unsigned int stacked_r0;
\r
808 volatile unsigned int stacked_r1;
\r
809 volatile unsigned int stacked_r2;
\r
810 volatile unsigned int stacked_r3;
\r
811 volatile unsigned int stacked_r12;
\r
812 volatile unsigned int stacked_lr;
\r
813 volatile unsigned int stacked_pc;
\r
814 volatile unsigned int stacked_psr;
\r
816 stacked_r0 = ((unsigned long) hardfault_args[0]);
\r
817 stacked_r1 = ((unsigned long) hardfault_args[1]);
\r
818 stacked_r2 = ((unsigned long) hardfault_args[2]);
\r
819 stacked_r3 = ((unsigned long) hardfault_args[3]);
\r
821 stacked_r12 = ((unsigned long) hardfault_args[4]);
\r
822 stacked_lr = ((unsigned long) hardfault_args[5]);
\r
823 stacked_pc = ((unsigned long) hardfault_args[6]);
\r
824 stacked_psr = ((unsigned long) hardfault_args[7]);
\r
826 /* Inspect stacked_pc to locate the offending instruction. */
\r
829 /*-----------------------------------------------------------*/
\r
831 void Fault_ISR( void ) __attribute__((naked));
\r
832 void Fault_ISR( void )
\r
838 " mrseq r0, msp \n"
\r
839 " mrsne r0, psp \n"
\r
840 " ldr r1, [r0, #24] \n"
\r
841 " ldr r2, handler_address_const \n"
\r
843 " handler_address_const: .word hard_fault_handler \n"
\r
846 /*-----------------------------------------------------------*/
\r
848 void MPU_Fault_ISR( void ) __attribute__((naked));
\r
849 void MPU_Fault_ISR( void )
\r
855 " mrseq r0, msp \n"
\r
856 " mrsne r0, psp \n"
\r
857 " ldr r1, [r0, #24] \n"
\r
858 " ldr r2, handler_address_const \n"
\r
860 " handler2_address_const: .word hard_fault_handler \n"
\r
863 /*-----------------------------------------------------------*/