2 FreeRTOS V9.0.0rc2 - Copyright (C) 2016 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
72 * This file demonstrates the use of FreeRTOS-MPU. It creates tasks in both
\r
73 * User mode and Privileged mode, and using both the original xTaskCreate() and
\r
74 * the new xTaskCreateRestricted() API functions. The purpose of each created
\r
75 * task is documented in the comments above the task function prototype (in
\r
76 * this file), with the task behaviour demonstrated and documented within the
\r
77 * task function itself. In addition a queue is used to demonstrate passing
\r
78 * data between protected/restricted tasks as well as passing data between an
\r
79 * interrupt and a protected/restricted task.
\r
82 /* Standard includes. */
\r
85 /* Scheduler includes. */
\r
86 #include "FreeRTOS.h"
\r
91 /*-----------------------------------------------------------*/
\r
93 /* Misc constants. */
\r
94 #define mainDONT_BLOCK ( 0 )
\r
96 /* Definitions for the messages that can be sent to the check task. */
\r
97 #define mainREG_TEST_1_STILL_EXECUTING ( 0 )
\r
98 #define mainREG_TEST_2_STILL_EXECUTING ( 1 )
\r
99 #define mainPRINT_SYSTEM_STATUS ( 2 )
\r
101 /* GCC specifics. */
\r
102 #define mainALIGN_TO( x ) __attribute__((aligned(x)))
\r
104 /* Hardware register addresses. */
\r
105 #define mainVTOR ( * ( volatile uint32_t * ) 0xE000ED08 )
\r
106 #define mainNVIC_AUX_ACTLR ( * ( volatile uint32_t * ) 0xE000E008 )
\r
107 #define mainEC_INTERRUPT_CONTROL ( * ( volatile uint32_t * ) 0x4000FC18 )
\r
109 /*-----------------------------------------------------------*/
\r
110 /* Prototypes for functions that implement tasks. -----------*/
\r
111 /*-----------------------------------------------------------*/
\r
114 * Prototype for the reg test tasks. Amongst other things, these fill the CPU
\r
115 * registers with known values before checking that the registers still contain
\r
116 * the expected values. Each of the two tasks use different values so an error
\r
117 * in the context switch mechanism can be caught. Both reg test tasks execute
\r
118 * at the idle priority so will get preempted regularly. Each task repeatedly
\r
119 * sends a message on a queue so long as it remains functioning correctly. If
\r
120 * an error is detected within the task the task is simply deleted.
\r
122 static void prvRegTest1Task( void *pvParameters );
\r
123 static void prvRegTest2Task( void *pvParameters );
\r
126 * Prototype for the check task. The check task demonstrates various features
\r
127 * of the MPU before entering a loop where it waits for messages to arrive on a
\r
130 * Two types of messages can be processes:
\r
132 * 1) "I'm Alive" messages sent from the reg test tasks, indicating that the
\r
133 * task is still operational.
\r
135 * 2) "Print Status commands" sent periodically by the tick hook function (and
\r
136 * therefore from within an interrupt) which command the check task to write
\r
137 * either pass or fail to the terminal, depending on the status of the reg
\r
140 static void prvCheckTask( void *pvParameters );
\r
143 * Prototype for a task created in User mode using the original vTaskCreate()
\r
144 * API function. The task demonstrates the characteristics of such a task,
\r
145 * before simply deleting itself.
\r
147 static void prvOldStyleUserModeTask( void *pvParameters );
\r
150 * Prototype for a task created in Privileged mode using the original
\r
151 * vTaskCreate() API function. The task demonstrates the characteristics of
\r
152 * such a task, before simply deleting itself.
\r
154 static void prvOldStylePrivilegedModeTask( void *pvParameters );
\r
157 /*-----------------------------------------------------------*/
\r
158 /* Prototypes for other misc functions. --------------------*/
\r
159 /*-----------------------------------------------------------*/
\r
162 * Just configures any clocks and IO necessary.
\r
164 static void prvSetupHardware( void );
\r
167 * Simply deletes the calling task. The function is provided only because it
\r
168 * is simpler to call from asm code than the normal vTaskDelete() API function.
\r
169 * It has the noinline attribute because it is called from asm code.
\r
171 static void prvDeleteMe( void ) __attribute__((noinline));
\r
174 * Used by both reg test tasks to send messages to the check task. The message
\r
175 * just lets the check task know that the task is still functioning correctly.
\r
176 * If a reg test task detects an error it will delete itself, and in so doing
\r
177 * prevent itself from sending any more 'I'm Alive' messages to the check task.
\r
179 static void prvSendImAlive( QueueHandle_t xHandle, unsigned long ulTaskNumber );
\r
182 * The check task is created with access to three memory regions (plus its
\r
183 * stack). Each memory region is configured with different parameters and
\r
184 * prvTestMemoryRegions() demonstrates what can and cannot be accessed for each
\r
185 * region. prvTestMemoryRegions() also demonstrates a task that was created
\r
186 * as a privileged task settings its own privilege level down to that of a user
\r
189 static void prvTestMemoryRegions( void );
\r
191 /*-----------------------------------------------------------*/
\r
193 /* The handle of the queue used to communicate between tasks and between tasks
\r
194 and interrupts. Note that this is a file scope variable that falls outside of
\r
195 any MPU region. As such other techniques have to be used to allow the tasks
\r
196 to gain access to the queue. See the comments in the tasks themselves for
\r
197 further information. */
\r
198 static QueueHandle_t xFileScopeCheckQueue = NULL;
\r
201 /*-----------------------------------------------------------*/
\r
202 /* Data used by the 'check' task. ---------------------------*/
\r
203 /*-----------------------------------------------------------*/
\r
205 /* Define the constants used to allocate the check task stack. Note that the
\r
206 stack size is defined in words, not bytes. */
\r
207 #define mainCHECK_TASK_STACK_SIZE_WORDS 128
\r
208 #define mainCHECK_TASK_STACK_ALIGNMENT ( mainCHECK_TASK_STACK_SIZE_WORDS * sizeof( portSTACK_TYPE ) )
\r
210 /* Declare the stack that will be used by the check task. The kernel will
\r
211 automatically create an MPU region for the stack. The stack alignment must
\r
212 match its size, so if 128 words are reserved for the stack then it must be
\r
213 aligned to ( 128 * 4 ) bytes. */
\r
214 static portSTACK_TYPE xCheckTaskStack[ mainCHECK_TASK_STACK_SIZE_WORDS ] mainALIGN_TO( mainCHECK_TASK_STACK_ALIGNMENT );
\r
216 /* Declare three arrays - an MPU region will be created for each array
\r
217 using the TaskParameters_t structure below. THIS IS JUST TO DEMONSTRATE THE
\r
218 MPU FUNCTIONALITY, the data is not used by the check tasks primary function
\r
219 of monitoring the reg test tasks and printing out status information.
\r
221 Note that the arrays allocate slightly more RAM than is actually assigned to
\r
222 the MPU region. This is to permit writes off the end of the array to be
\r
223 detected even when the arrays are placed in adjacent memory locations (with no
\r
224 gaps between them). The align size must be a power of two. */
\r
225 #define mainREAD_WRITE_ARRAY_SIZE 130
\r
226 #define mainREAD_WRITE_ALIGN_SIZE 128
\r
227 char cReadWriteArray[ mainREAD_WRITE_ARRAY_SIZE ] mainALIGN_TO( mainREAD_WRITE_ALIGN_SIZE );
\r
229 #define mainREAD_ONLY_ARRAY_SIZE 260
\r
230 #define mainREAD_ONLY_ALIGN_SIZE 256
\r
231 char cReadOnlyArray[ mainREAD_ONLY_ARRAY_SIZE ] mainALIGN_TO( mainREAD_ONLY_ALIGN_SIZE );
\r
233 #define mainPRIVILEGED_ONLY_ACCESS_ARRAY_SIZE 130
\r
234 #define mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE 128
\r
235 char cPrivilegedOnlyAccessArray[ mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE ] mainALIGN_TO( mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE );
\r
237 /* Fill in a TaskParameters_t structure to define the check task - this is the
\r
238 structure passed to the xTaskCreateRestricted() function. */
\r
239 static const TaskParameters_t xCheckTaskParameters =
\r
241 prvCheckTask, /* pvTaskCode - the function that implements the task. */
\r
242 "Check", /* pcName */
\r
243 mainCHECK_TASK_STACK_SIZE_WORDS, /* usStackDepth - defined in words, not bytes. */
\r
244 ( void * ) 0x12121212, /* pvParameters - this value is just to test that the parameter is being passed into the task correctly. */
\r
245 ( 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
246 xCheckTaskStack, /* puxStackBuffer - the array to use as the task stack, as declared above. */
\r
248 /* xRegions - In this case the xRegions array is used to create MPU regions
\r
249 for all three of the arrays declared directly above. Each MPU region is
\r
250 created with different parameters. Again, THIS IS JUST TO DEMONSTRATE THE
\r
251 MPU FUNCTIONALITY, the data is not used by the check tasks primary function
\r
252 of monitoring the reg test tasks and printing out status information.*/
\r
254 /* Base address Length Parameters */
\r
255 { cReadWriteArray, mainREAD_WRITE_ALIGN_SIZE, portMPU_REGION_READ_WRITE },
\r
256 { cReadOnlyArray, mainREAD_ONLY_ALIGN_SIZE, portMPU_REGION_READ_ONLY },
\r
257 { cPrivilegedOnlyAccessArray, mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE, portMPU_REGION_PRIVILEGED_READ_WRITE }
\r
263 /*-----------------------------------------------------------*/
\r
264 /* Data used by the 'reg test' tasks. -----------------------*/
\r
265 /*-----------------------------------------------------------*/
\r
267 /* Define the constants used to allocate the reg test task stacks. Note that
\r
268 that stack size is defined in words, not bytes. */
\r
269 #define mainREG_TEST_STACK_SIZE_WORDS 128
\r
270 #define mainREG_TEST_STACK_ALIGNMENT ( mainREG_TEST_STACK_SIZE_WORDS * sizeof( portSTACK_TYPE ) )
\r
272 /* Declare the stacks that will be used by the reg test tasks. The kernel will
\r
273 automatically create an MPU region for the stack. The stack alignment must
\r
274 match its size, so if 128 words are reserved for the stack then it must be
\r
275 aligned to ( 128 * 4 ) bytes. */
\r
276 static portSTACK_TYPE xRegTest1Stack[ mainREG_TEST_STACK_SIZE_WORDS ] mainALIGN_TO( mainREG_TEST_STACK_ALIGNMENT );
\r
277 static portSTACK_TYPE xRegTest2Stack[ mainREG_TEST_STACK_SIZE_WORDS ] mainALIGN_TO( mainREG_TEST_STACK_ALIGNMENT );
\r
279 /* Fill in a TaskParameters_t structure per reg test task to define the tasks. */
\r
280 static const TaskParameters_t xRegTest1Parameters =
\r
282 prvRegTest1Task, /* pvTaskCode - the function that implements the task. */
\r
283 "RegTest1", /* pcName */
\r
284 mainREG_TEST_STACK_SIZE_WORDS, /* usStackDepth */
\r
285 ( void * ) 0x12345678, /* pvParameters - this value is just to test that the parameter is being passed into the task correctly. */
\r
286 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
287 xRegTest1Stack, /* puxStackBuffer - the array to use as the task stack, as declared above. */
\r
288 { /* xRegions - this task does not use any non-stack data hence all members are zero. */
\r
289 /* Base address Length Parameters */
\r
290 { 0x00, 0x00, 0x00 },
\r
291 { 0x00, 0x00, 0x00 },
\r
292 { 0x00, 0x00, 0x00 }
\r
295 /*-----------------------------------------------------------*/
\r
297 static TaskParameters_t xRegTest2Parameters =
\r
299 prvRegTest2Task, /* pvTaskCode - the function that implements the task. */
\r
300 "RegTest2", /* pcName */
\r
301 mainREG_TEST_STACK_SIZE_WORDS, /* usStackDepth */
\r
302 ( void * ) NULL, /* pvParameters - this task uses the parameter to pass in a queue handle, but the queue is not created yet. */
\r
303 tskIDLE_PRIORITY, /* uxPriority */
\r
304 xRegTest2Stack, /* puxStackBuffer - the array to use as the task stack, as declared above. */
\r
305 { /* xRegions - this task does not use any non-stack data hence all members are zero. */
\r
306 /* Base address Length Parameters */
\r
307 { 0x00, 0x00, 0x00 },
\r
308 { 0x00, 0x00, 0x00 },
\r
309 { 0x00, 0x00, 0x00 }
\r
313 /*-----------------------------------------------------------*/
\r
317 prvSetupHardware();
\r
319 /* Create the queue used to pass "I'm alive" messages to the check task. */
\r
320 xFileScopeCheckQueue = xQueueCreate( 1, sizeof( unsigned long ) );
\r
322 /* One check task uses the task parameter to receive the queue handle.
\r
323 This allows the file scope variable to be accessed from within the task.
\r
324 The pvParameters member of xRegTest2Parameters can only be set after the
\r
325 queue has been created so is set here. */
\r
326 xRegTest2Parameters.pvParameters = xFileScopeCheckQueue;
\r
328 /* Create the three test tasks. Handles to the created tasks are not
\r
329 required, hence the second parameter is NULL. */
\r
330 xTaskCreateRestricted( &xRegTest1Parameters, NULL );
\r
331 xTaskCreateRestricted( &xRegTest2Parameters, NULL );
\r
332 xTaskCreateRestricted( &xCheckTaskParameters, NULL );
\r
334 /* Create the tasks that are created using the original xTaskCreate() API
\r
336 xTaskCreate( prvOldStyleUserModeTask, /* The function that implements the task. */
\r
337 "Task1", /* Text name for the task. */
\r
338 100, /* Stack depth in words. */
\r
339 NULL, /* Task parameters. */
\r
340 3, /* Priority and mode (user in this case). */
\r
344 xTaskCreate( prvOldStylePrivilegedModeTask, /* The function that implements the task. */
\r
345 "Task2", /* Text name for the task. */
\r
346 100, /* Stack depth in words. */
\r
347 NULL, /* Task parameters. */
\r
348 ( 3 | portPRIVILEGE_BIT ), /* Priority and mode. */
\r
352 /* Start the scheduler. */
\r
353 vTaskStartScheduler();
\r
355 /* Will only get here if there was insufficient memory to create the idle
\r
360 /*-----------------------------------------------------------*/
\r
362 static void prvCheckTask( void *pvParameters )
\r
364 /* This task is created in privileged mode so can access the file scope
\r
365 queue variable. Take a stack copy of this before the task is set into user
\r
366 mode. Once that task is in user mode the file scope queue variable will no
\r
367 longer be accessible but the stack copy will. */
\r
368 QueueHandle_t xQueue = xFileScopeCheckQueue;
\r
370 unsigned long ulStillAliveCounts[ 2 ] = { 0 };
\r
371 const char *pcStatusMessage = "PASS\r\n";
\r
374 /* Just to remove compiler warning. */
\r
375 ( void ) pvParameters;
\r
377 /* Demonstrate how the various memory regions can and can't be accessed.
\r
378 The task privilege level is set down to user mode within this function. */
\r
379 prvTestMemoryRegions();
\r
381 /* Tests are done so lower the privilege status. */
\r
382 portSWITCH_TO_USER_MODE();
\r
384 /* This loop performs the main function of the task, which is blocking
\r
385 on a message queue then processing each message as it arrives. */
\r
388 /* Wait for the next message to arrive. */
\r
389 xQueueReceive( xQueue, &lMessage, portMAX_DELAY );
\r
393 case mainREG_TEST_1_STILL_EXECUTING :
\r
394 /* Message from task 1, so task 1 must still be executing. */
\r
395 ( ulStillAliveCounts[ 0 ] )++;
\r
398 case mainREG_TEST_2_STILL_EXECUTING :
\r
399 /* Message from task 2, so task 2 must still be executing. */
\r
400 ( ulStillAliveCounts[ 1 ] )++;
\r
403 case mainPRINT_SYSTEM_STATUS :
\r
404 /* Message from tick hook, time to print out the system
\r
405 status. If messages has stopped arriving from either reg
\r
406 test task then the status must be set to fail. */
\r
407 if( ( ulStillAliveCounts[ 0 ] == 0 ) || ( ulStillAliveCounts[ 1 ] == 0 ) )
\r
409 /* One or both of the test tasks are no longer sending
\r
410 'still alive' messages. */
\r
411 pcStatusMessage = "FAIL\r\n";
\r
414 /* Print a pass/fail message to the terminal. This will be
\r
415 visible in the CrossWorks IDE. */
\r
416 // MPU_debug_printf( pcStatusMessage );
\r
417 ( void ) pcStatusMessage;
\r
419 /* Reset the count of 'still alive' messages. */
\r
420 memset( ulStillAliveCounts, 0x00, sizeof( ulStillAliveCounts ) );
\r
424 /* Something unexpected happened. Delete this task so the
\r
425 error is apparent (no output will be displayed). */
\r
431 /*-----------------------------------------------------------*/
\r
433 static void prvTestMemoryRegions( void )
\r
438 /* The check task (from which this function is called) is created in the
\r
439 Privileged mode. The privileged array can be both read from and written
\r
440 to while this task is privileged. */
\r
441 cPrivilegedOnlyAccessArray[ 0 ] = 'a';
\r
442 if( cPrivilegedOnlyAccessArray[ 0 ] != 'a' )
\r
444 /* Something unexpected happened. Delete this task so the error is
\r
445 apparent (no output will be displayed). */
\r
449 /* Writing off the end of the RAM allocated to this task will *NOT* cause a
\r
450 protection fault because the task is still executing in a privileged mode.
\r
451 Uncomment the following to test. */
\r
452 /*cPrivilegedOnlyAccessArray[ mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE ] = 'a';*/
\r
454 /* Now set the task into user mode. */
\r
455 portSWITCH_TO_USER_MODE();
\r
457 /* Accessing the privileged only array will now cause a fault. Uncomment
\r
458 the following line to test. */
\r
459 /*cPrivilegedOnlyAccessArray[ 0 ] = 'a';*/
\r
461 /* The read/write array can still be successfully read and written. */
\r
462 for( l = 0; l < mainREAD_WRITE_ALIGN_SIZE; l++ )
\r
464 cReadWriteArray[ l ] = 'a';
\r
465 if( cReadWriteArray[ l ] != 'a' )
\r
467 /* Something unexpected happened. Delete this task so the error is
\r
468 apparent (no output will be displayed). */
\r
473 /* But attempting to read or write off the end of the RAM allocated to this
\r
474 task will cause a fault. Uncomment either of the following two lines to
\r
476 /* cReadWriteArray[ 0 ] = cReadWriteArray[ -1 ]; */
\r
477 /* cReadWriteArray[ mainREAD_WRITE_ALIGN_SIZE ] = 0x00; */
\r
479 /* The read only array can be successfully read... */
\r
480 for( l = 0; l < mainREAD_ONLY_ALIGN_SIZE; l++ )
\r
482 cTemp = cReadOnlyArray[ l ];
\r
485 /* ...but cannot be written. Uncomment the following line to test. */
\r
486 /* cReadOnlyArray[ 0 ] = 'a'; */
\r
488 /* Writing to the first and last locations in the stack array should not
\r
489 cause a protection fault. Note that doing this will cause the kernel to
\r
490 detect a stack overflow if configCHECK_FOR_STACK_OVERFLOW is greater than
\r
491 1, hence the test is commented out by default. */
\r
492 /* xCheckTaskStack[ 0 ] = 0;
\r
493 xCheckTaskStack[ mainCHECK_TASK_STACK_SIZE_WORDS - 1 ] = 0; */
\r
495 /* Writing off either end of the stack array should cause a protection
\r
496 fault, uncomment either of the following two lines to test. */
\r
497 /* xCheckTaskStack[ -1 ] = 0; */
\r
498 /* xCheckTaskStack[ mainCHECK_TASK_STACK_SIZE_WORDS ] = 0; */
\r
502 /*-----------------------------------------------------------*/
\r
504 static void prvRegTest1Task( void *pvParameters )
\r
506 /* This task is created in privileged mode so can access the file scope
\r
507 queue variable. Take a stack copy of this before the task is set into user
\r
508 mode. Once this task is in user mode the file scope queue variable will no
\r
509 longer be accessible but the stack copy will. */
\r
510 QueueHandle_t xQueue = xFileScopeCheckQueue;
\r
512 /* Now the queue handle has been obtained the task can switch to user
\r
513 mode. This is just one method of passing a handle into a protected
\r
514 task, the other reg test task uses the task parameter instead. */
\r
515 portSWITCH_TO_USER_MODE();
\r
517 /* First check that the parameter value is as expected. */
\r
518 if( pvParameters != ( void * ) 0x12345678 )
\r
520 /* Error detected. Delete the task so it stops communicating with
\r
528 /* This task tests the kernel context switch mechanism by reading and
\r
529 writing directly to registers - which requires the test to be written
\r
530 in assembly code. */
\r
533 " MOV R4, #104 \n" /* Set registers to a known value. R0 to R1 are done in the loop below. */
\r
538 " MOV R10, #110 \n"
\r
539 " MOV R11, #111 \n"
\r
541 " MOV R0, #100 \n" /* Set the scratch registers to known values - done inside the loop as they get clobbered. */
\r
545 " MOV R12, #112 \n"
\r
546 " SVC #1 \n" /* Yield just to increase test coverage. */
\r
547 " CMP R0, #100 \n" /* Check all the registers still contain their expected values. */
\r
548 " BNE prvDeleteMe \n" /* Value was not as expected, delete the task so it stops communicating with the check task. */
\r
550 " BNE prvDeleteMe \n"
\r
552 " BNE prvDeleteMe \n"
\r
554 " BNE prvDeleteMe \n"
\r
556 " BNE prvDeleteMe \n"
\r
558 " BNE prvDeleteMe \n"
\r
560 " BNE prvDeleteMe \n"
\r
562 " BNE prvDeleteMe \n"
\r
564 " BNE prvDeleteMe \n"
\r
565 " CMP R10, #110 \n"
\r
566 " BNE prvDeleteMe \n"
\r
567 " CMP R11, #111 \n"
\r
568 " BNE prvDeleteMe \n"
\r
569 " CMP R12, #112 \n"
\r
570 " BNE prvDeleteMe \n"
\r
571 :::"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r8", "r9", "r10", "r11", "r12"
\r
574 /* Send mainREG_TEST_1_STILL_EXECUTING to the check task to indicate that this
\r
575 task is still functioning. */
\r
576 prvSendImAlive( xQueue, mainREG_TEST_1_STILL_EXECUTING );
\r
578 /* Go back to check all the register values again. */
\r
579 __asm volatile( " B reg1loop " );
\r
582 /*-----------------------------------------------------------*/
\r
584 static void prvRegTest2Task( void *pvParameters )
\r
586 /* The queue handle is passed in as the task parameter. This is one method of
\r
587 passing data into a protected task, the other reg test task uses a different
\r
589 QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;
\r
593 /* This task tests the kernel context switch mechanism by reading and
\r
594 writing directly to registers - which requires the test to be written
\r
595 in assembly code. */
\r
598 " MOV R4, #4 \n" /* Set registers to a known value. R0 to R1 are done in the loop below. */
\r
601 " MOV R8, #8 \n" /* Frame pointer is omitted as it must not be changed. */
\r
606 " MOV R0, #13 \n" /* Set the scratch registers to known values - done inside the loop as they get clobbered. */
\r
611 " CMP R0, #13 \n" /* Check all the registers still contain their expected values. */
\r
612 " BNE prvDeleteMe \n" /* Value was not as expected, delete the task so it stops communicating with the check task */
\r
614 " BNE prvDeleteMe \n"
\r
616 " BNE prvDeleteMe \n"
\r
618 " BNE prvDeleteMe \n"
\r
620 " BNE prvDeleteMe \n"
\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
635 :::"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r8", "r9", "r10", "r11", "r12"
\r
638 /* Send mainREG_TEST_2_STILL_EXECUTING to the check task to indicate that this
\r
639 task is still functioning. */
\r
640 prvSendImAlive( xQueue, mainREG_TEST_2_STILL_EXECUTING );
\r
642 /* Go back to check all the register values again. */
\r
643 __asm volatile( " B reg2loop " );
\r
646 /*-----------------------------------------------------------*/
\r
648 void vApplicationIdleHook( void )
\r
650 extern unsigned long __SRAM_segment_end__[];
\r
651 extern unsigned long __privileged_data_start__[];
\r
652 extern unsigned long __privileged_data_end__[];
\r
653 extern unsigned long __FLASH_segment_start__[];
\r
654 extern unsigned long __FLASH_segment_end__[];
\r
655 volatile unsigned long *pul;
\r
656 volatile unsigned long ulReadData;
\r
658 /* The idle task, and therefore this function, run in Supervisor mode and
\r
659 can therefore access all memory. Try reading from corners of flash and
\r
660 RAM to ensure a memory fault does not occur.
\r
662 Start with the edges of the privileged data area. */
\r
663 pul = __privileged_data_start__;
\r
665 pul = __privileged_data_end__ - 1;
\r
668 /* Next the standard SRAM area. */
\r
669 pul = __SRAM_segment_end__ - 1;
\r
672 /* And the standard Flash area - the start of which is marked for
\r
673 privileged access only. */
\r
674 pul = __FLASH_segment_start__;
\r
676 pul = __FLASH_segment_end__ - 1;
\r
679 /* Reading off the end of Flash or SRAM space should cause a fault.
\r
680 Uncomment one of the following two pairs of lines to test. */
\r
682 /* pul = __FLASH_segment_end__ + 4;
\r
683 ulReadData = *pul; */
\r
685 /* pul = __SRAM_segment_end__ + 1;
\r
686 ulReadData = *pul; */
\r
688 ( void ) ulReadData;
\r
690 /*-----------------------------------------------------------*/
\r
692 static void prvOldStyleUserModeTask( void *pvParameters )
\r
694 extern unsigned long __privileged_data_start__[];
\r
695 extern unsigned long __privileged_data_end__[];
\r
696 extern unsigned long __SRAM_segment_end__[];
\r
697 extern unsigned long __privileged_functions_end__[];
\r
698 extern unsigned long __FLASH_segment_start__[];
\r
699 extern unsigned long __FLASH_segment_end__[];
\r
700 //const volatile unsigned long *pulStandardPeripheralRegister = ( volatile unsigned long * ) 0x400FC0C4;
\r
701 volatile unsigned long *pul;
\r
702 volatile unsigned long ulReadData;
\r
704 /* The following lines are commented out to prevent the unused variable
\r
705 compiler warnings when the tests that use the variable are also commented out.
\r
706 extern unsigned long __privileged_functions_start__[];
\r
707 const volatile unsigned long *pulSystemPeripheralRegister = ( volatile unsigned long * ) 0xe000e014; */
\r
709 ( void ) pvParameters;
\r
711 /* This task is created in User mode using the original xTaskCreate() API
\r
712 function. It should have access to all Flash and RAM except that marked
\r
713 as Privileged access only. Reading from the start and end of the non-
\r
714 privileged RAM should not cause a problem (the privileged RAM is the first
\r
715 block at the bottom of the RAM memory). */
\r
716 pul = __privileged_data_end__ + 1;
\r
718 pul = __SRAM_segment_end__ - 1;
\r
721 /* Likewise reading from the start and end of the non-privileged Flash
\r
722 should not be a problem (the privileged Flash is the first block at the
\r
723 bottom of the Flash memory). */
\r
724 pul = __privileged_functions_end__ + 1;
\r
726 pul = __FLASH_segment_end__ - 1;
\r
729 /* Standard peripherals are accessible. */
\r
730 // ulReadData = *pulStandardPeripheralRegister;
\r
732 /* System peripherals are not accessible. Uncomment the following line
\r
733 to test. Also uncomment the declaration of pulSystemPeripheralRegister
\r
734 at the top of this function. */
\r
735 /* ulReadData = *pulSystemPeripheralRegister; */
\r
737 /* Reading from anywhere inside the privileged Flash or RAM should cause a
\r
738 fault. This can be tested by uncommenting any of the following pairs of
\r
739 lines. Also uncomment the declaration of __privileged_functions_start__
\r
740 at the top of this function. */
\r
742 /* pul = __privileged_functions_start__;
\r
743 ulReadData = *pul; */
\r
745 /* pul = __privileged_functions_end__ - 1;
\r
746 ulReadData = *pul; */
\r
748 /* pul = __privileged_data_start__;
\r
749 ulReadData = *pul; */
\r
751 /* pul = __privileged_data_end__ - 1;
\r
752 ulReadData = *pul; */
\r
754 /* Must not just run off the end of a task function, so delete this task.
\r
755 Note that because this task was created using xTaskCreate() the stack was
\r
756 allocated dynamically and I have not included any code to free it again. */
\r
757 vTaskDelete( NULL );
\r
759 ( void ) ulReadData;
\r
761 /*-----------------------------------------------------------*/
\r
763 static void prvOldStylePrivilegedModeTask( void *pvParameters )
\r
765 extern unsigned long __privileged_data_start__[];
\r
766 extern unsigned long __privileged_data_end__[];
\r
767 extern unsigned long __SRAM_segment_end__[];
\r
768 extern unsigned long __privileged_functions_start__[];
\r
769 extern unsigned long __privileged_functions_end__[];
\r
770 extern unsigned long __FLASH_segment_start__[];
\r
771 extern unsigned long __FLASH_segment_end__[];
\r
772 volatile unsigned long *pul;
\r
773 volatile unsigned long ulReadData;
\r
774 const volatile unsigned long *pulSystemPeripheralRegister = ( volatile unsigned long * ) 0xe000e014; /* Systick */
\r
775 //const volatile unsigned long *pulStandardPeripheralRegister = ( volatile unsigned long * ) 0x400FC0C4;
\r
777 ( void ) pvParameters;
\r
779 /* This task is created in Privileged mode using the original xTaskCreate()
\r
780 API function. It should have access to all Flash and RAM including that
\r
781 marked as Privileged access only. So reading from the start and end of the
\r
782 non-privileged RAM should not cause a problem (the privileged RAM is the
\r
783 first block at the bottom of the RAM memory). */
\r
784 pul = __privileged_data_end__ + 1;
\r
786 pul = __SRAM_segment_end__ - 1;
\r
789 /* Likewise reading from the start and end of the non-privileged Flash
\r
790 should not be a problem (the privileged Flash is the first block at the
\r
791 bottom of the Flash memory). */
\r
792 pul = __privileged_functions_end__ + 1;
\r
794 pul = __FLASH_segment_end__ - 1;
\r
797 /* Reading from anywhere inside the privileged Flash or RAM should also
\r
798 not be a problem. */
\r
799 pul = __privileged_functions_start__;
\r
801 pul = __privileged_functions_end__ - 1;
\r
803 pul = __privileged_data_start__;
\r
805 pul = __privileged_data_end__ - 1;
\r
808 /* Finally, accessing both System and normal peripherals should both be
\r
810 ulReadData = *pulSystemPeripheralRegister;
\r
811 // ulReadData = *pulStandardPeripheralRegister;
\r
813 /* Must not just run off the end of a task function, so delete this task.
\r
814 Note that because this task was created using xTaskCreate() the stack was
\r
815 allocated dynamically and I have not included any code to free it again. */
\r
816 vTaskDelete( NULL );
\r
818 ( void ) ulReadData;
\r
820 /*-----------------------------------------------------------*/
\r
822 static void prvDeleteMe( void )
\r
824 vTaskDelete( NULL );
\r
826 /*-----------------------------------------------------------*/
\r
828 static void prvSendImAlive( QueueHandle_t xHandle, unsigned long ulTaskNumber )
\r
830 if( xHandle != NULL )
\r
832 xQueueSend( xHandle, &ulTaskNumber, mainDONT_BLOCK );
\r
835 /*-----------------------------------------------------------*/
\r
837 static void prvSetupHardware( void )
\r
840 /*-----------------------------------------------------------*/
\r
842 void vApplicationTickHook( void )
\r
844 static unsigned long ulCallCount;
\r
845 const unsigned long ulCallsBetweenSends = 5000 / portTICK_PERIOD_MS;
\r
846 const unsigned long ulMessage = mainPRINT_SYSTEM_STATUS;
\r
847 portBASE_TYPE xDummy;
\r
849 /* If configUSE_TICK_HOOK is set to 1 then this function will get called
\r
850 from each RTOS tick. It is called from the tick interrupt and therefore
\r
851 will be executing in the privileged state. */
\r
855 /* Is it time to print out the pass/fail message again? */
\r
856 if( ulCallCount >= ulCallsBetweenSends )
\r
860 /* Send a message to the check task to command it to check that all
\r
861 the tasks are still running then print out the status.
\r
863 This is running in an ISR so has to use the "FromISR" version of
\r
864 xQueueSend(). Because it is in an ISR it is running with privileges
\r
865 so can access xFileScopeCheckQueue directly. */
\r
866 xQueueSendFromISR( xFileScopeCheckQueue, &ulMessage, &xDummy );
\r
869 /*-----------------------------------------------------------*/
\r
871 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
\r
873 /* If configCHECK_FOR_STACK_OVERFLOW is set to either 1 or 2 then this
\r
874 function will automatically get called if a task overflows its stack. */
\r
876 ( void ) pcTaskName;
\r
879 /*-----------------------------------------------------------*/
\r
881 void vApplicationMallocFailedHook( void )
\r
883 /* If configUSE_MALLOC_FAILED_HOOK is set to 1 then this function will
\r
884 be called automatically if a call to pvPortMalloc() fails. pvPortMalloc()
\r
885 is called automatically when a task, queue or semaphore is created. */
\r
888 /*-----------------------------------------------------------*/
\r
890 void hard_fault_handler(unsigned int * hardfault_args)
\r
892 volatile unsigned int stacked_r0;
\r
893 volatile unsigned int stacked_r1;
\r
894 volatile unsigned int stacked_r2;
\r
895 volatile unsigned int stacked_r3;
\r
896 volatile unsigned int stacked_r12;
\r
897 volatile unsigned int stacked_lr;
\r
898 volatile unsigned int stacked_pc;
\r
899 volatile unsigned int stacked_psr;
\r
901 stacked_r0 = ((unsigned long) hardfault_args[0]);
\r
902 stacked_r1 = ((unsigned long) hardfault_args[1]);
\r
903 stacked_r2 = ((unsigned long) hardfault_args[2]);
\r
904 stacked_r3 = ((unsigned long) hardfault_args[3]);
\r
906 stacked_r12 = ((unsigned long) hardfault_args[4]);
\r
907 stacked_lr = ((unsigned long) hardfault_args[5]);
\r
908 stacked_pc = ((unsigned long) hardfault_args[6]);
\r
909 stacked_psr = ((unsigned long) hardfault_args[7]);
\r
911 /* Inspect stacked_pc to locate the offending instruction. */
\r
914 ( void ) stacked_psr;
\r
915 ( void ) stacked_pc;
\r
916 ( void ) stacked_lr;
\r
917 ( void ) stacked_r12;
\r
918 ( void ) stacked_r0;
\r
919 ( void ) stacked_r1;
\r
920 ( void ) stacked_r2;
\r
921 ( void ) stacked_r3;
\r
923 /*-----------------------------------------------------------*/
\r
925 void HardFault_Handler( void ) __attribute__((naked));
\r
926 void HardFault_Handler( void )
\r
932 " mrseq r0, msp \n"
\r
933 " mrsne r0, psp \n"
\r
934 " ldr r1, [r0, #24] \n"
\r
935 " ldr r2, handler_address_const \n"
\r
937 " handler_address_const: .word hard_fault_handler \n"
\r
940 /*-----------------------------------------------------------*/
\r
942 void MPU_Fault_ISR( void ) __attribute__((naked));
\r
943 void MemManage_Handler( void )
\r
949 " mrseq r0, msp \n"
\r
950 " mrsne r0, psp \n"
\r
951 " ldr r1, [r0, #24] \n"
\r
952 " ldr r2, handler2_address_const \n"
\r
954 " handler2_address_const: .word hard_fault_handler \n"
\r
958 /*-----------------------------------------------------------*/
\r
960 #warning Why must configSUPPORT_STATIC_ALLOCATION be set to 1 when the MPU is used?
\r
961 #warning Linker script is crippled for use with the simulator.