2 FreeRTOS V7.0.2 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
56 * This file demonstrates the use of FreeRTOS-MPU. It creates tasks in both
\r
57 * User mode and Privileged mode, and using both the original xTaskCreate() and
\r
58 * the new xTaskCreateRestricted() API functions. The purpose of each created
\r
59 * task is documented in the comments above the task function prototype (in
\r
60 * this file), with the task behaviour demonstrated and documented within the
\r
61 * task function itself. In addition a queue is used to demonstrate passing
\r
62 * data between protected/restricted tasks as well as passing data between an
\r
63 * interrupt and a protected/restricted task.
\r
68 /* Standard includes. */
\r
70 #include <__cross_studio_io.h>
\r
72 /* Scheduler includes. */
\r
73 #include "FreeRTOS.h"
\r
78 /* Hardware library includes. */
\r
79 #include "hw_types.h"
\r
80 #include "hw_sysctl.h"
\r
83 /*-----------------------------------------------------------*/
\r
85 /* Misc constants. */
\r
86 #define mainDONT_BLOCK ( 0 )
\r
88 /* Definitions for the messages that can be sent to the check task. */
\r
89 #define mainREG_TEST_1_STILL_EXECUTING ( 0 )
\r
90 #define mainREG_TEST_2_STILL_EXECUTING ( 1 )
\r
91 #define mainPRINT_SYSTEM_STATUS ( 2 )
\r
93 /* GCC specifics. */
\r
94 #define mainALIGN_TO( x ) __attribute__((aligned(x)))
\r
97 /*-----------------------------------------------------------*/
\r
98 /* Prototypes for functions that implement tasks. -----------*/
\r
99 /*-----------------------------------------------------------*/
\r
102 * Prototype for the reg test tasks. Amongst other things, these fill the CPU
\r
103 * registers with known values before checking that the registers still contain
\r
104 * the expected values. Each of the two tasks use different values so an error
\r
105 * in the context switch mechanism can be caught. Both reg test tasks execute
\r
106 * at the idle priority so will get preempted regularly. Each task repeatedly
\r
107 * sends a message on a queue so long as it remains functioning correctly. If
\r
108 * an error is detected within the task the task is simply deleted.
\r
110 static void prvRegTest1Task( void *pvParameters );
\r
111 static void prvRegTest2Task( void *pvParameters );
\r
114 * Prototype for the check task. The check task demonstrates various features
\r
115 * of the MPU before entering a loop where it waits for messages to arrive on a
\r
118 * Two types of messages can be processes:
\r
120 * 1) "I'm Alive" messages sent from the reg test tasks, indicating that the
\r
121 * task is still operational.
\r
123 * 2) "Print Status commands" sent periodically by the tick hook function (and
\r
124 * therefore from within an interrupt) which command the check task to write
\r
125 * either pass or fail to the terminal, depending on the status of the reg
\r
128 static void prvCheckTask( void *pvParameters );
\r
131 * Prototype for a task created in User mode using the original vTaskCreate()
\r
132 * API function. The task demonstrates the characteristics of such a task,
\r
133 * before simply deleting itself.
\r
135 static void prvOldStyleUserModeTask( void *pvParameters );
\r
138 * Prototype for a task created in Privileged mode using the original
\r
139 * vTaskCreate() API function. The task demonstrates the characteristics of
\r
140 * such a task, before simply deleting itself.
\r
142 static void prvOldStylePrivilegedModeTask( void *pvParameters );
\r
145 /*-----------------------------------------------------------*/
\r
146 /* Prototypes for other misc functions. --------------------*/
\r
147 /*-----------------------------------------------------------*/
\r
150 * Just configures any clocks and IO necessary.
\r
152 static void prvSetupHardware( void );
\r
155 * Simply deletes the calling task. The function is provided only because it
\r
156 * is simpler to call from asm code than the normal vTaskDelete() API function.
\r
157 * It has the noinline attribute because it is called from asm code.
\r
159 static void prvDeleteMe( void ) __attribute__((noinline));
\r
162 * Used by both reg test tasks to send messages to the check task. The message
\r
163 * just lets the check task know that the task is still functioning correctly.
\r
164 * If a reg test task detects an error it will delete itself, and in so doing
\r
165 * prevent itself from sending any more 'I'm Alive' messages to the check task.
\r
167 static void prvSendImAlive( xQueueHandle xHandle, unsigned long ulTaskNumber );
\r
170 * The check task is created with access to three memory regions (plus its
\r
171 * stack). Each memory region is configured with different parameters and
\r
172 * prvTestMemoryRegions() demonstrates what can and cannot be accessed for each
\r
173 * region. prvTestMemoryRegions() also demonstrates a task that was created
\r
174 * as a privileged task settings its own privilege level down to that of a user
\r
177 static void prvTestMemoryRegions( void );
\r
179 /*-----------------------------------------------------------*/
\r
181 /* The handle of the queue used to communicate between tasks and between tasks
\r
182 and interrupts. Note that this is a file scope variable that falls outside of
\r
183 any MPU region. As such other techniques have to be used to allow the tasks
\r
184 to gain access to the queue. See the comments in the tasks themselves for
\r
185 further information. */
\r
186 static xQueueHandle xFileScopeCheckQueue = NULL;
\r
189 /*-----------------------------------------------------------*/
\r
190 /* Data used by the 'check' task. ---------------------------*/
\r
191 /*-----------------------------------------------------------*/
\r
193 /* Define the constants used to allocate the check task stack. Note that the
\r
194 stack size is defined in words, not bytes. */
\r
195 #define mainCHECK_TASK_STACK_SIZE_WORDS 128
\r
196 #define mainCHECK_TASK_STACK_ALIGNMENT ( mainCHECK_TASK_STACK_SIZE_WORDS * sizeof( portSTACK_TYPE ) )
\r
198 /* Declare the stack that will be used by the check task. The kernel will
\r
199 automatically create an MPU region for the stack. The stack alignment must
\r
200 match its size, so if 128 words are reserved for the stack then it must be
\r
201 aligned to ( 128 * 4 ) bytes. */
\r
202 static portSTACK_TYPE xCheckTaskStack[ mainCHECK_TASK_STACK_SIZE_WORDS ] mainALIGN_TO( mainCHECK_TASK_STACK_ALIGNMENT );
\r
204 /* Declare three arrays - an MPU region will be created for each array
\r
205 using the xTaskParameters structure below. THIS IS JUST TO DEMONSTRATE THE
\r
206 MPU FUNCTIONALITY, the data is not used by the check tasks primary function
\r
207 of monitoring the reg test tasks and printing out status information.
\r
209 Note that the arrays allocate slightly more RAM than is actually assigned to
\r
210 the MPU region. This is to permit writes off the end of the array to be
\r
211 detected even when the arrays are placed in adjacent memory locations (with no
\r
212 gaps between them). The align size must be a power of two. */
\r
213 #define mainREAD_WRITE_ARRAY_SIZE 130
\r
214 #define mainREAD_WRITE_ALIGN_SIZE 128
\r
215 char cReadWriteArray[ mainREAD_WRITE_ARRAY_SIZE ] mainALIGN_TO( mainREAD_WRITE_ALIGN_SIZE );
\r
217 #define mainREAD_ONLY_ARRAY_SIZE 260
\r
218 #define mainREAD_ONLY_ALIGN_SIZE 256
\r
219 char cReadOnlyArray[ mainREAD_ONLY_ARRAY_SIZE ] mainALIGN_TO( mainREAD_ONLY_ALIGN_SIZE );
\r
221 #define mainPRIVILEGED_ONLY_ACCESS_ARRAY_SIZE 130
\r
222 #define mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE 128
\r
223 char cPrivilegedOnlyAccessArray[ mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE ] mainALIGN_TO( mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE );
\r
225 /* Fill in a xTaskParameters structure to define the check task - this is the
\r
226 structure passed to the xTaskCreateRestricted() function. */
\r
227 static const xTaskParameters xCheckTaskParameters =
\r
229 prvCheckTask, /* pvTaskCode - the function that implements the task. */
\r
230 ( signed char * ) "Check", /* pcName */
\r
231 mainCHECK_TASK_STACK_SIZE_WORDS, /* usStackDepth - defined in words, not bytes. */
\r
232 ( void * ) 0x12121212, /* pvParameters - this value is just to test that the parameter is being passed into the task correctly. */
\r
233 ( 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
234 xCheckTaskStack, /* puxStackBuffer - the array to use as the task stack, as declared above. */
\r
236 /* xRegions - In this case the xRegions array is used to create MPU regions
\r
237 for all three of the arrays declared directly above. Each MPU region is
\r
238 created with different parameters. Again, THIS IS JUST TO DEMONSTRATE THE
\r
239 MPU FUNCTIONALITY, the data is not used by the check tasks primary function
\r
240 of monitoring the reg test tasks and printing out status information.*/
\r
242 /* Base address Length Parameters */
\r
243 { cReadWriteArray, mainREAD_WRITE_ALIGN_SIZE, portMPU_REGION_READ_WRITE },
\r
244 { cReadOnlyArray, mainREAD_ONLY_ALIGN_SIZE, portMPU_REGION_READ_ONLY },
\r
245 { cPrivilegedOnlyAccessArray, mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE, portMPU_REGION_PRIVILEGED_READ_WRITE }
\r
249 /* Three MPU regions are defined for use by the 'check' task when the task is
\r
250 created. These are only used to demonstrate the MPU features and are not
\r
251 actually necessary for the check task to fulfill its primary purpose. Instead
\r
252 the MPU regions are replaced with those defined by xAltRegions prior to the
\r
253 check task receiving any data on the queue or printing any messages to the
\r
254 debug console. The region configured by xAltRegions just gives the check task
\r
255 access to the debug variables that form part of the Rowley library, and are
\r
256 accessed within the debug_printf() function. */
\r
257 extern unsigned long dbgCntrlWord_mempoll;
\r
258 static const xMemoryRegion xAltRegions[ portNUM_CONFIGURABLE_REGIONS ] =
\r
260 /* Base address Length Parameters */
\r
261 { ( void * ) &dbgCntrlWord_mempoll, 32, portMPU_REGION_READ_WRITE },
\r
268 /*-----------------------------------------------------------*/
\r
269 /* Data used by the 'reg test' tasks. -----------------------*/
\r
270 /*-----------------------------------------------------------*/
\r
272 /* Define the constants used to allocate the reg test task stacks. Note that
\r
273 that stack size is defined in words, not bytes. */
\r
274 #define mainREG_TEST_STACK_SIZE_WORDS 128
\r
275 #define mainREG_TEST_STACK_ALIGNMENT ( mainREG_TEST_STACK_SIZE_WORDS * sizeof( portSTACK_TYPE ) )
\r
277 /* Declare the stacks that will be used by the reg test tasks. The kernel will
\r
278 automatically create an MPU region for the stack. The stack alignment must
\r
279 match its size, so if 128 words are reserved for the stack then it must be
\r
280 aligned to ( 128 * 4 ) bytes. */
\r
281 static portSTACK_TYPE xRegTest1Stack[ mainREG_TEST_STACK_SIZE_WORDS ] mainALIGN_TO( mainREG_TEST_STACK_ALIGNMENT );
\r
282 static portSTACK_TYPE xRegTest2Stack[ mainREG_TEST_STACK_SIZE_WORDS ] mainALIGN_TO( mainREG_TEST_STACK_ALIGNMENT );
\r
284 /* Fill in a xTaskParameters structure per reg test task to define the tasks. */
\r
285 static const xTaskParameters xRegTest1Parameters =
\r
287 prvRegTest1Task, /* pvTaskCode - the function that implements the task. */
\r
288 ( signed char * ) "RegTest1", /* pcName */
\r
289 mainREG_TEST_STACK_SIZE_WORDS, /* usStackDepth */
\r
290 ( void * ) 0x12345678, /* pvParameters - this value is just to test that the parameter is being passed into the task correctly. */
\r
291 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
292 xRegTest1Stack, /* puxStackBuffer - the array to use as the task stack, as declared above. */
\r
293 { /* xRegions - this task does not use any non-stack data hence all members are zero. */
\r
294 /* Base address Length Parameters */
\r
295 { 0x00, 0x00, 0x00 },
\r
296 { 0x00, 0x00, 0x00 },
\r
297 { 0x00, 0x00, 0x00 }
\r
300 /*-----------------------------------------------------------*/
\r
302 static xTaskParameters xRegTest2Parameters =
\r
304 prvRegTest2Task, /* pvTaskCode - the function that implements the task. */
\r
305 ( signed char * ) "RegTest2", /* pcName */
\r
306 mainREG_TEST_STACK_SIZE_WORDS, /* usStackDepth */
\r
307 ( void * ) NULL, /* pvParameters - this task uses the parameter to pass in a queue handle, but the queue is not created yet. */
\r
308 tskIDLE_PRIORITY, /* uxPriority */
\r
309 xRegTest2Stack, /* 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
318 /*-----------------------------------------------------------*/
\r
322 prvSetupHardware();
\r
324 /* Create the queue used to pass "I'm alive" messages to the check task. */
\r
325 xFileScopeCheckQueue = xQueueCreate( 1, sizeof( unsigned long ) );
\r
327 /* One check task uses the task parameter to receive the queue handle.
\r
328 This allows the file scope variable to be accessed from within the task.
\r
329 The pvParameters member of xRegTest2Parameters can only be set after the
\r
330 queue has been created so is set here. */
\r
331 xRegTest2Parameters.pvParameters = xFileScopeCheckQueue;
\r
333 /* Create the three test tasks. Handles to the created tasks are not
\r
334 required, hence the second parameter is NULL. */
\r
335 xTaskCreateRestricted( &xRegTest1Parameters, NULL );
\r
336 xTaskCreateRestricted( &xRegTest2Parameters, NULL );
\r
337 xTaskCreateRestricted( &xCheckTaskParameters, NULL );
\r
339 /* Create the tasks that are created using the original xTaskCreate() API
\r
341 xTaskCreate( prvOldStyleUserModeTask, /* The function that implements the task. */
\r
342 ( signed char * ) "Task1", /* Text name for the task. */
\r
343 100, /* Stack depth in words. */
\r
344 NULL, /* Task parameters. */
\r
345 3, /* Priority and mode (user in this case). */
\r
349 xTaskCreate( prvOldStylePrivilegedModeTask, /* The function that implements the task. */
\r
350 ( signed char * ) "Task2", /* Text name for the task. */
\r
351 100, /* Stack depth in words. */
\r
352 NULL, /* Task parameters. */
\r
353 ( 3 | portPRIVILEGE_BIT ), /* Priority and mode. */
\r
357 /* Start the scheduler. */
\r
358 vTaskStartScheduler();
\r
360 /* Will only get here if there was insufficient memory to create the idle
\r
365 /*-----------------------------------------------------------*/
\r
367 static void prvCheckTask( void *pvParameters )
\r
369 /* This task is created in privileged mode so can access the file scope
\r
370 queue variable. Take a stack copy of this before the task is set into user
\r
371 mode. Once that task is in user mode the file scope queue variable will no
\r
372 longer be accessible but the stack copy will. */
\r
373 xQueueHandle xQueue = xFileScopeCheckQueue;
\r
375 unsigned long ulStillAliveCounts[ 2 ] = { 0 };
\r
376 const char *pcStatusMessage = "PASS\r\n";
\r
378 /* Just to remove compiler warning. */
\r
379 ( void ) pvParameters;
\r
381 /* Print out the amount of free heap space so configTOTAL_HEAP_SIZE can be
\r
382 tuned. The heap size is set to be very small in this example and will need
\r
383 to be increased before many more tasks, queues or semaphores can be
\r
385 debug_printf( "There are %d bytes of unused heap space.\r\n", xPortGetFreeHeapSize() );
\r
387 /* Demonstrate how the various memory regions can and can't be accessed.
\r
388 The task privilege level is set down to user mode within this function. */
\r
389 prvTestMemoryRegions();
\r
391 /* Change the memory regions allocated to this task to those initially
\r
392 set up for demonstration purposes to those actually required by the task. */
\r
393 vTaskAllocateMPURegions( NULL, xAltRegions );
\r
395 /* This loop performs the main function of the task, which is blocking
\r
396 on a message queue then processing each message as it arrives. */
\r
399 /* Wait for the next message to arrive. */
\r
400 xQueueReceive( xQueue, &lMessage, portMAX_DELAY );
\r
404 case mainREG_TEST_1_STILL_EXECUTING :
\r
405 /* Message from task 1, so task 1 must still be executing. */
\r
406 ( ulStillAliveCounts[ 0 ] )++;
\r
409 case mainREG_TEST_2_STILL_EXECUTING :
\r
410 /* Message from task 2, so task 2 must still be executing. */
\r
411 ( ulStillAliveCounts[ 1 ] )++;
\r
414 case mainPRINT_SYSTEM_STATUS :
\r
415 /* Message from tick hook, time to print out the system
\r
416 status. If messages has stopped arriving from either reg
\r
417 test task then the status must be set to fail. */
\r
418 if( ( ulStillAliveCounts[ 0 ] == 0 ) || ( ulStillAliveCounts[ 1 ] == 0 ) )
\r
420 /* One or both of the test tasks are no longer sending
\r
421 'still alive' messages. */
\r
422 pcStatusMessage = "FAIL\r\n";
\r
425 /* Print a pass/fail message to the terminal. This will be
\r
426 visible in the CrossWorks IDE. */
\r
427 debug_printf( pcStatusMessage );
\r
429 /* Reset the count of 'still alive' messages. */
\r
430 memset( ulStillAliveCounts, 0x00, sizeof( ulStillAliveCounts ) );
\r
434 /* Something unexpected happened. Delete this task so the
\r
435 error is apparent (no output will be displayed). */
\r
441 /*-----------------------------------------------------------*/
\r
443 static void prvTestMemoryRegions( void )
\r
448 /* The check task (from which this function is called) is created in the
\r
449 Privileged mode. The privileged array can be both read from and written
\r
450 to while this task is privileged. */
\r
451 cPrivilegedOnlyAccessArray[ 0 ] = 'a';
\r
452 if( cPrivilegedOnlyAccessArray[ 0 ] != 'a' )
\r
454 /* Something unexpected happened. Delete this task so the error is
\r
455 apparent (no output will be displayed). */
\r
459 /* Writing off the end of the RAM allocated to this task will *NOT* cause a
\r
460 protection fault because the task is still executing in a privileged mode.
\r
461 Uncomment the following to test. */
\r
462 /*cPrivilegedOnlyAccessArray[ mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE ] = 'a';*/
\r
464 /* Now set the task into user mode. */
\r
465 portSWITCH_TO_USER_MODE();
\r
467 /* Accessing the privileged only array will now cause a fault. Uncomment
\r
468 the following line to test. */
\r
469 /*cPrivilegedOnlyAccessArray[ 0 ] = 'a';*/
\r
471 /* The read/write array can still be successfully read and written. */
\r
472 for( l = 0; l < mainREAD_WRITE_ALIGN_SIZE; l++ )
\r
474 cReadWriteArray[ l ] = 'a';
\r
475 if( cReadWriteArray[ l ] != 'a' )
\r
477 /* Something unexpected happened. Delete this task so the error is
\r
478 apparent (no output will be displayed). */
\r
483 /* But attempting to read or write off the end of the RAM allocated to this
\r
484 task will cause a fault. Uncomment either of the following two lines to
\r
486 /* cReadWriteArray[ 0 ] = cReadWriteArray[ -1 ]; */
\r
487 /* cReadWriteArray[ mainREAD_WRITE_ALIGN_SIZE ] = 0x00; */
\r
489 /* The read only array can be successfully read... */
\r
490 for( l = 0; l < mainREAD_ONLY_ALIGN_SIZE; l++ )
\r
492 cTemp = cReadOnlyArray[ l ];
\r
495 /* ...but cannot be written. Uncomment the following line to test. */
\r
496 /* cReadOnlyArray[ 0 ] = 'a'; */
\r
498 /* Writing to the first and last locations in the stack array should not
\r
499 cause a protection fault. Note that doing this will cause the kernel to
\r
500 detect a stack overflow if configCHECK_FOR_STACK_OVERFLOW is greater than
\r
502 xCheckTaskStack[ 0 ] = 0;
\r
503 xCheckTaskStack[ mainCHECK_TASK_STACK_SIZE_WORDS - 1 ] = 0;
\r
505 /* Writing off either end of the stack array should cause a protection
\r
506 fault, uncomment either of the following two lines to test. */
\r
507 /* xCheckTaskStack[ -1 ] = 0; */
\r
508 /* xCheckTaskStack[ mainCHECK_TASK_STACK_SIZE_WORDS ] = 0; */
\r
510 /*-----------------------------------------------------------*/
\r
512 static void prvRegTest1Task( void *pvParameters )
\r
514 /* This task is created in privileged mode so can access the file scope
\r
515 queue variable. Take a stack copy of this before the task is set into user
\r
516 mode. Once this task is in user mode the file scope queue variable will no
\r
517 longer be accessible but the stack copy will. */
\r
518 xQueueHandle xQueue = xFileScopeCheckQueue;
\r
520 /* Now the queue handle has been obtained the task can switch to user
\r
521 mode. This is just one method of passing a handle into a protected
\r
522 task, the other reg test task uses the task parameter instead. */
\r
523 portSWITCH_TO_USER_MODE();
\r
525 /* First check that the parameter value is as expected. */
\r
526 if( pvParameters != ( void * ) 0x12345678 )
\r
528 /* Error detected. Delete the task so it stops communicating with
\r
536 /* This task tests the kernel context switch mechanism by reading and
\r
537 writing directly to registers - which requires the test to be written
\r
538 in assembly code. */
\r
541 " MOV R4, #104 \n" /* Set registers to a known value. R0 to R1 are done in the loop below. */
\r
546 " MOV R10, #110 \n"
\r
547 " MOV R11, #111 \n"
\r
549 " MOV R0, #100 \n" /* Set the scratch registers to known values - done inside the loop as they get clobbered. */
\r
553 " MOV R12, #112 \n"
\r
554 " SVC #1 \n" /* Yield just to increase test coverage. */
\r
555 " CMP R0, #100 \n" /* Check all the registers still contain their expected values. */
\r
556 " BNE prvDeleteMe \n" /* Value was not as expected, delete the task so it stops communicating with the check task. */
\r
558 " BNE prvDeleteMe \n"
\r
560 " BNE prvDeleteMe \n"
\r
562 " BNE prvDeleteMe \n"
\r
563 " CMP R4, #104 \n"
\r
564 " BNE prvDeleteMe \n"
\r
566 " BNE prvDeleteMe \n"
\r
568 " BNE prvDeleteMe \n"
\r
570 " BNE prvDeleteMe \n"
\r
572 " BNE prvDeleteMe \n"
\r
573 " CMP R10, #110 \n"
\r
574 " BNE prvDeleteMe \n"
\r
575 " CMP R11, #111 \n"
\r
576 " BNE prvDeleteMe \n"
\r
577 " CMP R12, #112 \n"
\r
578 " BNE prvDeleteMe \n"
\r
579 :::"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r8", "r9", "r10", "r11", "r12"
\r
582 /* Send mainREG_TEST_1_STILL_EXECUTING to the check task to indicate that this
\r
583 task is still functioning. */
\r
584 prvSendImAlive( xQueue, mainREG_TEST_1_STILL_EXECUTING );
\r
586 /* Go back to check all the register values again. */
\r
587 __asm volatile( " B reg1loop " );
\r
590 /*-----------------------------------------------------------*/
\r
592 static void prvRegTest2Task( void *pvParameters )
\r
594 /* The queue handle is passed in as the task parameter. This is one method of
\r
595 passing data into a protected task, the other reg test task uses a different
\r
597 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
601 /* This task tests the kernel context switch mechanism by reading and
\r
602 writing directly to registers - which requires the test to be written
\r
603 in assembly code. */
\r
606 " MOV R4, #4 \n" /* Set registers to a known value. R0 to R1 are done in the loop below. */
\r
609 " MOV R8, #8 \n" /* Frame pointer is omitted as it must not be changed. */
\r
612 " MOV R11, #11 \n"
\r
614 " MOV R0, #13 \n" /* Set the scratch registers to known values - done inside the loop as they get clobbered. */
\r
619 " CMP R0, #13 \n" /* Check all the registers still contain their expected values. */
\r
620 " BNE prvDeleteMe \n" /* Value was not as expected, delete the task so it stops communicating with the check task */
\r
622 " BNE prvDeleteMe \n"
\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
643 :::"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r8", "r9", "r10", "r11", "r12"
\r
646 /* Send mainREG_TEST_2_STILL_EXECUTING to the check task to indicate that this
\r
647 task is still functioning. */
\r
648 prvSendImAlive( xQueue, mainREG_TEST_2_STILL_EXECUTING );
\r
650 /* Go back to check all the register values again. */
\r
651 __asm volatile( " B reg2loop " );
\r
654 /*-----------------------------------------------------------*/
\r
656 void vApplicationIdleHook( void )
\r
658 extern unsigned long __SRAM_segment_end__[];
\r
659 extern unsigned long __privileged_data_start__[];
\r
660 extern unsigned long __privileged_data_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 idle task, and therefore this function, run in Supervisor mode and
\r
667 can therefore access all memory. Try reading from corners of flash and
\r
668 RAM to ensure a memory fault does not occur.
\r
670 Start with the edges of the privileged data area. */
\r
671 pul = __privileged_data_start__;
\r
673 pul = __privileged_data_end__ - 1;
\r
676 /* Next the standard SRAM area. */
\r
677 pul = __SRAM_segment_end__ - 1;
\r
680 /* And the standard Flash area - the start of which is marked for
\r
681 privileged access only. */
\r
682 pul = __FLASH_segment_start__;
\r
684 pul = __FLASH_segment_end__ - 1;
\r
687 /* Reading off the end of Flash or SRAM space should cause a fault.
\r
688 Uncomment one of the following two pairs of lines to test. */
\r
690 /* pul = __FLASH_segment_end__ + 4;
\r
691 ulReadData = *pul; */
\r
693 /* pul = __SRAM_segment_end__ + 1;
\r
694 ulReadData = *pul; */
\r
696 /*-----------------------------------------------------------*/
\r
698 static void prvOldStyleUserModeTask( void *pvParameters )
\r
700 extern unsigned long __privileged_data_start__[];
\r
701 extern unsigned long __privileged_data_end__[];
\r
702 extern unsigned long __SRAM_segment_end__[];
\r
703 extern unsigned long __privileged_functions_end__[];
\r
704 extern unsigned long __FLASH_segment_start__[];
\r
705 extern unsigned long __FLASH_segment_end__[];
\r
706 const volatile unsigned long *pulStandardPeripheralRegister = ( volatile unsigned long * ) 0x400FC0C4; /* PCONP */
\r
707 volatile unsigned long *pul;
\r
708 volatile unsigned long ulReadData;
\r
710 /* The following lines are commented out to prevent the unused variable
\r
711 compiler warnings when the tests that use the variable are also commented out.
\r
712 extern unsigned long __privileged_functions_start__[];
\r
713 const volatile unsigned long *pulSystemPeripheralRegister = ( volatile unsigned long * ) 0xe000e014; */
\r
715 ( void ) pvParameters;
\r
717 /* This task is created in User mode using the original xTaskCreate() API
\r
718 function. It should have access to all Flash and RAM except that marked
\r
719 as Privileged access only. Reading from the start and end of the non-
\r
720 privileged RAM should not cause a problem (the privileged RAM is the first
\r
721 block at the bottom of the RAM memory). */
\r
722 pul = __privileged_data_end__ + 1;
\r
724 pul = __SRAM_segment_end__ - 1;
\r
727 /* Likewise reading from the start and end of the non-privileged Flash
\r
728 should not be a problem (the privileged Flash is the first block at the
\r
729 bottom of the Flash memory). */
\r
730 pul = __privileged_functions_end__ + 1;
\r
732 pul = __FLASH_segment_end__ - 1;
\r
735 /* Standard peripherals are accessible. */
\r
736 ulReadData = *pulStandardPeripheralRegister;
\r
738 /* System peripherals are not accessible. Uncomment the following line
\r
739 to test. Also uncomment the declaration of pulSystemPeripheralRegister
\r
740 at the top of this function. */
\r
741 /* ulReadData = *pulSystemPeripheralRegister; */
\r
743 /* Reading from anywhere inside the privileged Flash or RAM should cause a
\r
744 fault. This can be tested by uncommenting any of the following pairs of
\r
745 lines. Also uncomment the declaration of __privileged_functions_start__
\r
746 at the top of this function. */
\r
748 /* pul = __privileged_functions_start__;
\r
749 ulReadData = *pul; */
\r
751 /* pul = __privileged_functions_end__ - 1;
\r
752 ulReadData = *pul; */
\r
754 /* pul = __privileged_data_start__;
\r
755 ulReadData = *pul; */
\r
757 /* pul = __privileged_data_end__ - 1;
\r
758 ulReadData = *pul; */
\r
760 /* Must not just run off the end of a task function, so delete this task.
\r
761 Note that because this task was created using xTaskCreate() the stack was
\r
762 allocated dynamically and I have not included any code to free it again. */
\r
763 vTaskDelete( NULL );
\r
765 /*-----------------------------------------------------------*/
\r
767 static void prvOldStylePrivilegedModeTask( void *pvParameters )
\r
769 extern unsigned long __privileged_data_start__[];
\r
770 extern unsigned long __privileged_data_end__[];
\r
771 extern unsigned long __SRAM_segment_end__[];
\r
772 extern unsigned long __privileged_functions_start__[];
\r
773 extern unsigned long __privileged_functions_end__[];
\r
774 extern unsigned long __FLASH_segment_start__[];
\r
775 extern unsigned long __FLASH_segment_end__[];
\r
776 volatile unsigned long *pul;
\r
777 volatile unsigned long ulReadData;
\r
778 const volatile unsigned long *pulSystemPeripheralRegister = ( volatile unsigned long * ) 0xe000e014; /* Systick */
\r
779 const volatile unsigned long *pulStandardPeripheralRegister = ( volatile unsigned long * ) 0x400FC0C4; /* PCONP */
\r
781 ( void ) pvParameters;
\r
783 /* This task is created in Privileged mode using the original xTaskCreate()
\r
784 API function. It should have access to all Flash and RAM including that
\r
785 marked as Privileged access only. So reading from the start and end of the
\r
786 non-privileged RAM should not cause a problem (the privileged RAM is the
\r
787 first block at the bottom of the RAM memory). */
\r
788 pul = __privileged_data_end__ + 1;
\r
790 pul = __SRAM_segment_end__ - 1;
\r
793 /* Likewise reading from the start and end of the non-privileged Flash
\r
794 should not be a problem (the privileged Flash is the first block at the
\r
795 bottom of the Flash memory). */
\r
796 pul = __privileged_functions_end__ + 1;
\r
798 pul = __FLASH_segment_end__ - 1;
\r
801 /* Reading from anywhere inside the privileged Flash or RAM should also
\r
802 not be a problem. */
\r
803 pul = __privileged_functions_start__;
\r
805 pul = __privileged_functions_end__ - 1;
\r
807 pul = __privileged_data_start__;
\r
808 ulReadData = *pul;
\r
809 pul = __privileged_data_end__ - 1;
\r
812 /* Finally, accessing both System and normal peripherals should both be
\r
814 ulReadData = *pulSystemPeripheralRegister;
\r
815 ulReadData = *pulStandardPeripheralRegister;
\r
817 /* Must not just run off the end of a task function, so delete this task.
\r
818 Note that because this task was created using xTaskCreate() the stack was
\r
819 allocated dynamically and I have not included any code to free it again. */
\r
820 vTaskDelete( NULL );
\r
822 /*-----------------------------------------------------------*/
\r
824 static void prvDeleteMe( void )
\r
826 vTaskDelete( NULL );
\r
828 /*-----------------------------------------------------------*/
\r
830 static void prvSendImAlive( xQueueHandle xHandle, unsigned long ulTaskNumber )
\r
832 if( xHandle != NULL )
\r
834 xQueueSend( xHandle, &ulTaskNumber, mainDONT_BLOCK );
\r
837 /*-----------------------------------------------------------*/
\r
839 static void prvSetupHardware( void )
\r
841 /* If running on Rev A2 silicon, turn the LDO voltage up to 2.75V. This is
\r
842 a workaround to allow the PLL to operate reliably. */
\r
843 if( DEVICE_IS_REVA2 )
\r
845 SysCtlLDOSet( SYSCTL_LDO_2_75V );
\r
848 /* Set the clocking to run from the PLL at 50 MHz */
\r
849 SysCtlClockSet( SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_8MHZ );
\r
851 /*-----------------------------------------------------------*/
\r
853 void vApplicationTickHook( void )
\r
855 static unsigned long ulCallCount;
\r
856 const unsigned long ulCallsBetweenSends = 5000 / portTICK_RATE_MS;
\r
857 const unsigned long ulMessage = mainPRINT_SYSTEM_STATUS;
\r
858 portBASE_TYPE xDummy;
\r
860 /* If configUSE_TICK_HOOK is set to 1 then this function will get called
\r
861 from each RTOS tick. It is called from the tick interrupt and therefore
\r
862 will be executing in the privileged state. */
\r
866 /* Is it time to print out the pass/fail message again? */
\r
867 if( ulCallCount >= ulCallsBetweenSends )
\r
871 /* Send a message to the check task to command it to check that all
\r
872 the tasks are still running then print out the status.
\r
874 This is running in an ISR so has to use the "FromISR" version of
\r
875 xQueueSend(). Because it is in an ISR it is running with privileges
\r
876 so can access xFileScopeCheckQueue directly. */
\r
877 xQueueSendFromISR( xFileScopeCheckQueue, &ulMessage, &xDummy );
\r
880 /*-----------------------------------------------------------*/
\r
882 void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )
\r
884 /* If configCHECK_FOR_STACK_OVERFLOW is set to either 1 or 2 then this
\r
885 function will automatically get called if a task overflows its stack. */
\r
887 ( void ) pcTaskName;
\r
890 /*-----------------------------------------------------------*/
\r
892 void vApplicationMallocFailedHook( void )
\r
894 /* If configUSE_MALLOC_FAILED_HOOK is set to 1 then this function will
\r
895 be called automatically if a call to pvPortMalloc() fails. pvPortMalloc()
\r
896 is called automatically when a task, queue or semaphore is created. */
\r
899 /*-----------------------------------------------------------*/
\r
901 /* Just to keep the linker happy. */
\r
902 void __error__( char *pcFilename, unsigned long ulLine )
\r
904 ( void ) pcFilename;
\r
908 /*-----------------------------------------------------------*/
\r
910 /* Just to keep the linker happy. */
\r
911 int uipprintf( const char *fmt, ... )
\r
916 /*-----------------------------------------------------------*/
\r
918 void hard_fault_handler(unsigned int * hardfault_args)
\r
920 volatile unsigned int stacked_r0;
\r
921 volatile unsigned int stacked_r1;
\r
922 volatile unsigned int stacked_r2;
\r
923 volatile unsigned int stacked_r3;
\r
924 volatile unsigned int stacked_r12;
\r
925 volatile unsigned int stacked_lr;
\r
926 volatile unsigned int stacked_pc;
\r
927 volatile unsigned int stacked_psr;
\r
929 stacked_r0 = ((unsigned long) hardfault_args[0]);
\r
930 stacked_r1 = ((unsigned long) hardfault_args[1]);
\r
931 stacked_r2 = ((unsigned long) hardfault_args[2]);
\r
932 stacked_r3 = ((unsigned long) hardfault_args[3]);
\r
934 stacked_r12 = ((unsigned long) hardfault_args[4]);
\r
935 stacked_lr = ((unsigned long) hardfault_args[5]);
\r
936 stacked_pc = ((unsigned long) hardfault_args[6]);
\r
937 stacked_psr = ((unsigned long) hardfault_args[7]);
\r
939 /* Inspect stacked_pc to locate the offending instruction. */
\r
942 /*-----------------------------------------------------------*/
\r
944 void Fault_ISR( void ) __attribute__((naked));
\r
945 void Fault_ISR( void )
\r
951 " mrseq r0, msp \n"
\r
952 " mrsne r0, psp \n"
\r
953 " ldr r1, [r0, #24] \n"
\r
954 " ldr r2, handler_address_const \n"
\r
956 " handler_address_const: .word hard_fault_handler \n"
\r
959 /*-----------------------------------------------------------*/
\r
961 void MPU_Fault_ISR( void ) __attribute__((naked));
\r
962 void MPU_Fault_ISR( void )
\r
968 " mrseq r0, msp \n"
\r
969 " mrsne r0, psp \n"
\r
970 " ldr r1, [r0, #24] \n"
\r
971 " ldr r2, handler_address_const \n"
\r
973 " handler2_address_const: .word hard_fault_handler \n"
\r
976 /*-----------------------------------------------------------*/