2 FreeRTOS V8.2.3 - Copyright (C) 2015 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
70 #error "The batch file Demo\CORTEX_LPC1768_GCC_RedSuite\CreateProjectDirectoryStructure.bat must be executed before the first build. After executing the batch file hit F5 to refrech the Eclipse project, then delete this line."
\r
74 * This file demonstrates the use of FreeRTOS-MPU. It creates tasks in both
\r
75 * User mode and Privileged mode, and using both the original xTaskCreate() and
\r
76 * the new xTaskCreateRestricted() API functions. The purpose of each created
\r
77 * task is documented in the comments above the task function prototype (in
\r
78 * this file), with the task behaviour demonstrated and documented within the
\r
79 * task function itself. In addition a queue is used to demonstrate passing
\r
80 * data between protected/restricted tasks as well as passing data between an
\r
81 * interrupt and a protected/restricted task.
\r
86 /* Library includes. */
\r
89 /* Scheduler includes. */
\r
90 #include "FreeRTOS.h"
\r
95 /* Red Suite includes. */
\r
96 #include "lcd_driver.h"
\r
100 /*-----------------------------------------------------------*/
\r
102 /* Misc constants. */
\r
103 #define mainDONT_BLOCK ( 0 )
\r
105 /* Definitions for the messages that can be sent to the check task. */
\r
106 #define mainREG_TEST_1_STILL_EXECUTING ( 0 )
\r
107 #define mainREG_TEST_2_STILL_EXECUTING ( 1 )
\r
108 #define mainPRINT_SYSTEM_STATUS ( 2 )
\r
110 /* GCC specifics. */
\r
111 #define mainALIGN_TO( x ) __attribute__((aligned(x)))
\r
113 /* Hardware specifics. The start and end address are chosen to ensure the
\r
114 required GPIO are covered while also ensuring the necessary alignment is
\r
116 #define mainGPIO_START_ADDRESS ( ( unsigned long * ) 0x2009c000 )
\r
117 #define mainGPIO_END_ADDRESS ( mainGPIO_START_ADDRESS + ( 64 * 1024 ) )
\r
120 /*-----------------------------------------------------------*/
\r
121 /* Prototypes for functions that implement tasks. -----------*/
\r
122 /*-----------------------------------------------------------*/
\r
125 * Prototype for the reg test tasks. Amongst other things, these fill the CPU
\r
126 * registers with known values before checking that the registers still contain
\r
127 * the expected values. Each of the two tasks use different values so an error
\r
128 * in the context switch mechanism can be caught. Both reg test tasks execute
\r
129 * at the idle priority so will get preempted regularly. Each task repeatedly
\r
130 * sends a message on a queue so long as it remains functioning correctly. If
\r
131 * an error is detected within the task the task is simply deleted.
\r
133 static void prvRegTest1Task( void *pvParameters );
\r
134 static void prvRegTest2Task( void *pvParameters );
\r
137 * Prototype for the check task. The check task demonstrates various features
\r
138 * of the MPU before entering a loop where it waits for messages to arrive on a
\r
141 * Two types of messages can be processes:
\r
143 * 1) "I'm Alive" messages sent from the reg test tasks, indicating that the
\r
144 * task is still operational.
\r
146 * 2) "Print Status commands" sent periodically by the tick hook function (and
\r
147 * therefore from within an interrupt) which command the check task to write
\r
148 * either pass or fail to the terminal, depending on the status of the reg
\r
151 static void prvCheckTask( void *pvParameters );
\r
154 * Prototype for a task created in User mode using the original vTaskCreate()
\r
155 * API function. The task demonstrates the characteristics of such a task,
\r
156 * before simply deleting itself.
\r
158 static void prvOldStyleUserModeTask( void *pvParameters );
\r
161 * Prototype for a task created in Privileged mode using the original
\r
162 * vTaskCreate() API function. The task demonstrates the characteristics of
\r
163 * such a task, before simply deleting itself.
\r
165 static void prvOldStylePrivilegedModeTask( void *pvParameters );
\r
168 /*-----------------------------------------------------------*/
\r
169 /* Prototypes for other misc functions. --------------------*/
\r
170 /*-----------------------------------------------------------*/
\r
173 * Just configures any clocks and IO necessary.
\r
175 static void prvSetupHardware( void );
\r
178 * Simply deletes the calling task. The function is provided only because it
\r
179 * is simpler to call from asm code than the normal vTaskDelete() API function.
\r
180 * It has the noinline attribute because it is called from asm code.
\r
182 static void prvDeleteMe( void ) __attribute__((noinline));
\r
185 * Used by both reg test tasks to send messages to the check task. The message
\r
186 * just lets the check task know that the task is still functioning correctly.
\r
187 * If a reg test task detects an error it will delete itself, and in so doing
\r
188 * prevent itself from sending any more 'I'm Alive' messages to the check task.
\r
190 static void prvSendImAlive( QueueHandle_t xHandle, unsigned long ulTaskNumber );
\r
193 * The check task is created with access to three memory regions (plus its
\r
194 * stack). Each memory region is configured with different parameters and
\r
195 * prvTestMemoryRegions() demonstrates what can and cannot be accessed for each
\r
196 * region. prvTestMemoryRegions() also demonstrates a task that was created
\r
197 * as a privileged task settings its own privilege level down to that of a user
\r
200 static void prvTestMemoryRegions( void );
\r
202 /*-----------------------------------------------------------*/
\r
204 /* The handle of the queue used to communicate between tasks and between tasks
\r
205 and interrupts. Note that this is a file scope variable that falls outside of
\r
206 any MPU region. As such other techniques have to be used to allow the tasks
\r
207 to gain access to the queue. See the comments in the tasks themselves for
\r
208 further information. */
\r
209 static QueueHandle_t xFileScopeCheckQueue = NULL;
\r
213 /*-----------------------------------------------------------*/
\r
214 /* Data used by the 'check' task. ---------------------------*/
\r
215 /*-----------------------------------------------------------*/
\r
217 /* Define the constants used to allocate the check task stack. Note that the
\r
218 stack size is defined in words, not bytes. */
\r
219 #define mainCHECK_TASK_STACK_SIZE_WORDS 128
\r
220 #define mainCHECK_TASK_STACK_ALIGNMENT ( mainCHECK_TASK_STACK_SIZE_WORDS * sizeof( portSTACK_TYPE ) )
\r
222 /* Declare the stack that will be used by the check task. The kernel will
\r
223 automatically create an MPU region for the stack. The stack alignment must
\r
224 match its size, so if 128 words are reserved for the stack then it must be
\r
225 aligned to ( 128 * 4 ) bytes. */
\r
226 static portSTACK_TYPE xCheckTaskStack[ mainCHECK_TASK_STACK_SIZE_WORDS ] mainALIGN_TO( mainCHECK_TASK_STACK_ALIGNMENT );
\r
228 /* Declare three arrays - an MPU region will be created for each array
\r
229 using the TaskParameters_t structure below. THIS IS JUST TO DEMONSTRATE THE
\r
230 MPU FUNCTIONALITY, the data is not used by the check tasks primary function
\r
231 of monitoring the reg test tasks and printing out status information.
\r
233 Note that the arrays allocate slightly more RAM than is actually assigned to
\r
234 the MPU region. This is to permit writes off the end of the array to be
\r
235 detected even when the arrays are placed in adjacent memory locations (with no
\r
236 gaps between them). The align size must be a power of two. */
\r
237 #define mainREAD_WRITE_ARRAY_SIZE 130
\r
238 #define mainREAD_WRITE_ALIGN_SIZE 128
\r
239 char cReadWriteArray[ mainREAD_WRITE_ARRAY_SIZE ] mainALIGN_TO( mainREAD_WRITE_ALIGN_SIZE );
\r
241 #define mainREAD_ONLY_ARRAY_SIZE 260
\r
242 #define mainREAD_ONLY_ALIGN_SIZE 256
\r
243 char cReadOnlyArray[ mainREAD_ONLY_ARRAY_SIZE ] mainALIGN_TO( mainREAD_ONLY_ALIGN_SIZE );
\r
245 #define mainPRIVILEGED_ONLY_ACCESS_ARRAY_SIZE 130
\r
246 #define mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE 128
\r
247 char cPrivilegedOnlyAccessArray[ mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE ] mainALIGN_TO( mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE );
\r
249 /* Fill in a TaskParameters_t structure to define the check task - this is the
\r
250 structure passed to the xTaskCreateRestricted() function. */
\r
251 static const TaskParameters_t xCheckTaskParameters =
\r
253 prvCheckTask, /* pvTaskCode - the function that implements the task. */
\r
254 "Check", /* pcName */
\r
255 mainCHECK_TASK_STACK_SIZE_WORDS, /* usStackDepth - defined in words, not bytes. */
\r
256 ( void * ) 0x12121212, /* pvParameters - this value is just to test that the parameter is being passed into the task correctly. */
\r
257 ( 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
258 xCheckTaskStack, /* puxStackBuffer - the array to use as the task stack, as declared above. */
\r
260 /* xRegions - In this case the xRegions array is used to create MPU regions
\r
261 for all three of the arrays declared directly above. Each MPU region is
\r
262 created with different parameters. Again, THIS IS JUST TO DEMONSTRATE THE
\r
263 MPU FUNCTIONALITY, the data is not used by the check tasks primary function
\r
264 of monitoring the reg test tasks and printing out status information.*/
\r
266 /* Base address Length Parameters */
\r
267 { cReadWriteArray, mainREAD_WRITE_ALIGN_SIZE, portMPU_REGION_READ_WRITE },
\r
268 { cReadOnlyArray, mainREAD_ONLY_ALIGN_SIZE, portMPU_REGION_READ_ONLY },
\r
269 { cPrivilegedOnlyAccessArray, mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE, portMPU_REGION_PRIVILEGED_READ_WRITE }
\r
273 /* Three MPU regions are defined for use by the 'check' task when the task is
\r
274 created. These are only used to demonstrate the MPU features and are not
\r
275 actually necessary for the check task to fulfill its primary purpose. Instead
\r
276 the MPU regions are replaced with those defined by xAltRegions prior to the
\r
277 check task receiving any data on the queue or printing any messages to the
\r
278 debug console. The MPU region defined below covers the GPIO peripherals used
\r
279 to write to the LCD. */
\r
280 static const MemoryRegion_t xAltRegions[ portNUM_CONFIGURABLE_REGIONS ] =
\r
282 /* Base address Length Parameters */
\r
283 { mainGPIO_START_ADDRESS, ( 64 * 1024 ), portMPU_REGION_READ_WRITE },
\r
290 /*-----------------------------------------------------------*/
\r
291 /* Data used by the 'reg test' tasks. -----------------------*/
\r
292 /*-----------------------------------------------------------*/
\r
294 /* Define the constants used to allocate the reg test task stacks. Note that
\r
295 that stack size is defined in words, not bytes. */
\r
296 #define mainREG_TEST_STACK_SIZE_WORDS 128
\r
297 #define mainREG_TEST_STACK_ALIGNMENT ( mainREG_TEST_STACK_SIZE_WORDS * sizeof( portSTACK_TYPE ) )
\r
299 /* Declare the stacks that will be used by the reg test tasks. The kernel will
\r
300 automatically create an MPU region for the stack. The stack alignment must
\r
301 match its size, so if 128 words are reserved for the stack then it must be
\r
302 aligned to ( 128 * 4 ) bytes. */
\r
303 static portSTACK_TYPE xRegTest1Stack[ mainREG_TEST_STACK_SIZE_WORDS ] mainALIGN_TO( mainREG_TEST_STACK_ALIGNMENT );
\r
304 static portSTACK_TYPE xRegTest2Stack[ mainREG_TEST_STACK_SIZE_WORDS ] mainALIGN_TO( mainREG_TEST_STACK_ALIGNMENT );
\r
306 /* Fill in a TaskParameters_t structure per reg test task to define the tasks. */
\r
307 static const TaskParameters_t xRegTest1Parameters =
\r
309 prvRegTest1Task, /* pvTaskCode - the function that implements the task. */
\r
310 "RegTest1", /* pcName */
\r
311 mainREG_TEST_STACK_SIZE_WORDS, /* usStackDepth */
\r
312 ( void * ) 0x12345678, /* pvParameters - this value is just to test that the parameter is being passed into the task correctly. */
\r
313 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
314 xRegTest1Stack, /* puxStackBuffer - the array to use as the task stack, as declared above. */
\r
315 { /* xRegions - this task does not use any non-stack data hence all members are zero. */
\r
316 /* Base address Length Parameters */
\r
317 { 0x00, 0x00, 0x00 },
\r
318 { 0x00, 0x00, 0x00 },
\r
319 { 0x00, 0x00, 0x00 }
\r
322 /*-----------------------------------------------------------*/
\r
324 static TaskParameters_t xRegTest2Parameters =
\r
326 prvRegTest2Task, /* pvTaskCode - the function that implements the task. */
\r
327 "RegTest2", /* pcName */
\r
328 mainREG_TEST_STACK_SIZE_WORDS, /* usStackDepth */
\r
329 ( void * ) NULL, /* pvParameters - this task uses the parameter to pass in a queue handle, but the queue is not created yet. */
\r
330 tskIDLE_PRIORITY, /* uxPriority */
\r
331 xRegTest2Stack, /* puxStackBuffer - the array to use as the task stack, as declared above. */
\r
332 { /* xRegions - this task does not use any non-stack data hence all members are zero. */
\r
333 /* Base address Length Parameters */
\r
334 { 0x00, 0x00, 0x00 },
\r
335 { 0x00, 0x00, 0x00 },
\r
336 { 0x00, 0x00, 0x00 }
\r
340 /*-----------------------------------------------------------*/
\r
344 prvSetupHardware();
\r
346 /* Create the queue used to pass "I'm alive" messages to the check task. */
\r
347 xFileScopeCheckQueue = xQueueCreate( 1, sizeof( unsigned long ) );
\r
349 /* One check task uses the task parameter to receive the queue handle.
\r
350 This allows the file scope variable to be accessed from within the task.
\r
351 The pvParameters member of xRegTest2Parameters can only be set after the
\r
352 queue has been created so is set here. */
\r
353 xRegTest2Parameters.pvParameters = xFileScopeCheckQueue;
\r
355 /* Create the three test tasks. Handles to the created tasks are not
\r
356 required, hence the second parameter is NULL. */
\r
357 xTaskCreateRestricted( &xRegTest1Parameters, NULL );
\r
358 xTaskCreateRestricted( &xRegTest2Parameters, NULL );
\r
359 xTaskCreateRestricted( &xCheckTaskParameters, NULL );
\r
361 /* Create the tasks that are created using the original xTaskCreate() API
\r
363 xTaskCreate( prvOldStyleUserModeTask, /* The function that implements the task. */
\r
364 "Task1", /* Text name for the task. */
\r
365 100, /* Stack depth in words. */
\r
366 NULL, /* Task parameters. */
\r
367 3, /* Priority and mode (user in this case). */
\r
371 xTaskCreate( prvOldStylePrivilegedModeTask, /* The function that implements the task. */
\r
372 "Task2", /* Text name for the task. */
\r
373 100, /* Stack depth in words. */
\r
374 NULL, /* Task parameters. */
\r
375 ( 3 | portPRIVILEGE_BIT ), /* Priority and mode. */
\r
379 /* Start the scheduler. */
\r
380 vTaskStartScheduler();
\r
382 /* Will only get here if there was insufficient memory to create the idle
\r
387 /*-----------------------------------------------------------*/
\r
389 static void prvCheckTask( void *pvParameters )
\r
391 /* This task is created in privileged mode so can access the file scope
\r
392 queue variable. Take a stack copy of this before the task is set into user
\r
393 mode. Once that task is in user mode the file scope queue variable will no
\r
394 longer be accessible but the stack copy will. */
\r
395 QueueHandle_t xQueue = xFileScopeCheckQueue;
\r
397 unsigned long ulStillAliveCounts[ 2 ] = { 0 };
\r
398 char *pcStatusMessage = "PASS\r\n";
\r
399 unsigned char x = 5, y = 10;
\r
401 /* Just to remove compiler warning. */
\r
402 ( void ) pvParameters;
\r
404 /* Demonstrate how the various memory regions can and can't be accessed.
\r
405 The task privilege is set down to user mode within this function. */
\r
406 prvTestMemoryRegions();
\r
408 /* Change the memory regions allocated to this task to those initially
\r
409 set up for demonstration purposes to those actually required by the task. */
\r
410 vTaskAllocateMPURegions( NULL, xAltRegions );
\r
412 /* This loop performs the main function of the task, which is blocking
\r
413 on a message queue then processing each message as it arrives. */
\r
416 /* Wait for the next message to arrive. */
\r
417 xQueueReceive( xQueue, &lMessage, portMAX_DELAY );
\r
421 case mainREG_TEST_1_STILL_EXECUTING :
\r
422 /* Message from task 1, so task 1 must still be executing. */
\r
423 ( ulStillAliveCounts[ 0 ] )++;
\r
426 case mainREG_TEST_2_STILL_EXECUTING :
\r
427 /* Message from task 2, so task 2 must still be executing. */
\r
428 ( ulStillAliveCounts[ 1 ] )++;
\r
431 case mainPRINT_SYSTEM_STATUS :
\r
432 /* Message from tick hook, time to print out the system
\r
433 status. If messages has stopped arriving from either reg
\r
434 test task then the status must be set to fail. */
\r
435 if( ( ulStillAliveCounts[ 0 ] == 0 ) || ( ulStillAliveCounts[ 1 ] == 0 ) )
\r
437 /* One or both of the test tasks are no longer sending
\r
438 'still alive' messages. */
\r
439 pcStatusMessage = "FAIL\r\n";
\r
442 /* Print a pass/fail message to the LCD - moving the
\r
443 message each time to provide feedback that the output
\r
444 is still being produced. LCD_PrintString() accesses const
\r
445 data stored in flash, which all tasks are at liberty to do,
\r
446 and GPIO for which an MPU region has been set up for it. */
\r
448 LCD_PrintString( x>>1, y>>1, pcStatusMessage, 6, COLOR_RED );
\r
452 /* Reset the count of 'still alive' messages. */
\r
453 memset( ulStillAliveCounts, 0x00, sizeof( ulStillAliveCounts ) );
\r
457 /* Something unexpected happened. Delete this task so the
\r
458 error is apparent (no output will be displayed). */
\r
464 /*-----------------------------------------------------------*/
\r
466 static void prvTestMemoryRegions( void )
\r
471 /* The check task (from which this function is called) is created in the
\r
472 Privileged mode. The privileged array can be both read from and written
\r
473 to while this task is privileged. */
\r
474 cPrivilegedOnlyAccessArray[ 0 ] = 'a';
\r
475 if( cPrivilegedOnlyAccessArray[ 0 ] != 'a' )
\r
477 /* Something unexpected happened. Delete this task so the error is
\r
478 apparent (no output will be displayed). */
\r
482 /* Writing off the end of the RAM allocated to this task will *NOT* cause a
\r
483 protection fault because the task is still executing in a privileged mode.
\r
484 Uncomment the following to test. */
\r
485 /* cPrivilegedOnlyAccessArray[ mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE ] = 'a'; */
\r
487 /* Now set the task into user mode. */
\r
488 portSWITCH_TO_USER_MODE();
\r
490 /* Accessing the privileged only array will now cause a fault. Uncomment
\r
491 the following line to test. */
\r
492 /* cPrivilegedOnlyAccessArray[ 0 ] = 'a'; */
\r
494 /* The read/write array can still be successfully read and written. */
\r
495 for( l = 0; l < mainREAD_WRITE_ALIGN_SIZE; l++ )
\r
497 cReadWriteArray[ l ] = 'a';
\r
498 if( cReadWriteArray[ l ] != 'a' )
\r
500 /* Something unexpected happened. Delete this task so the error is
\r
501 apparent (no output will be displayed). */
\r
506 /* But attempting to read or write off the end of the RAM allocated to this
\r
507 task will cause a fault. Uncomment either of the following two lines to
\r
509 /* cReadWriteArray[ 0 ] = cReadWriteArray[ -1 ]; */
\r
510 /* cReadWriteArray[ mainREAD_WRITE_ALIGN_SIZE ] = 0x00; */
\r
512 /* The read only array can be successfully read... */
\r
513 for( l = 0; l < mainREAD_ONLY_ALIGN_SIZE; l++ )
\r
515 cTemp = cReadOnlyArray[ l ];
\r
518 /* ...but cannot be written. Uncomment the following line to test. */
\r
519 /* cReadOnlyArray[ 0 ] = 'a'; */
\r
521 /* Writing to the first and last locations in the stack array should not
\r
522 cause a protection fault. Note that doing this will cause the kernel to
\r
523 detect a stack overflow if configCHECK_FOR_STACK_OVERFLOW is greater than
\r
525 xCheckTaskStack[ 0 ] = 0;
\r
526 xCheckTaskStack[ mainCHECK_TASK_STACK_SIZE_WORDS - 1 ] = 0;
\r
528 /* Writing off either end of the stack array should cause a protection
\r
529 fault, uncomment either of the following two lines to test. */
\r
530 /* xCheckTaskStack[ -1 ] = 0; */
\r
531 /* xCheckTaskStack[ mainCHECK_TASK_STACK_SIZE_WORDS ] = 0; */
\r
533 /*-----------------------------------------------------------*/
\r
535 static void prvRegTest1Task( void *pvParameters )
\r
537 /* This task is created in privileged mode so can access the file scope
\r
538 queue variable. Take a stack copy of this before the task is set into user
\r
539 mode. Once this task is in user mode the file scope queue variable will no
\r
540 longer be accessible but the stack copy will. */
\r
541 QueueHandle_t xQueue = xFileScopeCheckQueue;
\r
543 /* Now the queue handle has been obtained the task can switch to user
\r
544 mode. This is just one method of passing a handle into a protected
\r
545 task, the other reg test task uses the task parameter instead. */
\r
546 portSWITCH_TO_USER_MODE();
\r
548 /* First check that the parameter value is as expected. */
\r
549 if( pvParameters != ( void * ) 0x12345678 )
\r
551 /* Error detected. Delete the task so it stops communicating with
\r
559 /* This task tests the kernel context switch mechanism by reading and
\r
560 writing directly to registers - which requires the test to be written
\r
561 in assembly code. */
\r
564 " MOV R4, #104 \n" /* Set registers to a known value. R0 to R1 are done in the loop below. */
\r
569 " MOV R10, #110 \n"
\r
570 " MOV R11, #111 \n"
\r
572 " MOV R0, #100 \n" /* Set the scratch registers to known values - done inside the loop as they get clobbered. */
\r
576 " MOV R12, #112 \n"
\r
577 " SVC #1 \n" /* Yield just to increase test coverage. */
\r
578 " CMP R0, #100 \n" /* Check all the registers still contain their expected values. */
\r
579 " BNE prvDeleteMe \n" /* Value was not as expected, delete the task so it stops communicating with the check task. */
\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
596 " CMP R10, #110 \n"
\r
597 " BNE prvDeleteMe \n"
\r
598 " CMP R11, #111 \n"
\r
599 " BNE prvDeleteMe \n"
\r
600 " CMP R12, #112 \n"
\r
601 " BNE prvDeleteMe \n"
\r
602 :::"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r8", "r9", "r10", "r11", "r12"
\r
605 /* Send mainREG_TEST_1_STILL_EXECUTING to the check task to indicate that this
\r
606 task is still functioning. */
\r
607 prvSendImAlive( xQueue, mainREG_TEST_1_STILL_EXECUTING );
\r
609 /* Go back to check all the register values again. */
\r
610 __asm volatile( " B reg1loop " );
\r
613 /*-----------------------------------------------------------*/
\r
615 static void prvRegTest2Task( void *pvParameters )
\r
617 /* The queue handle is passed in as the task parameter. This is one method of
\r
618 passing data into a protected task, the other reg test task uses a different
\r
620 QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;
\r
624 /* This task tests the kernel context switch mechanism by reading and
\r
625 writing directly to registers - which requires the test to be written
\r
626 in assembly code. */
\r
629 " MOV R4, #4 \n" /* Set registers to a known value. R0 to R1 are done in the loop below. */
\r
632 " MOV R8, #8 \n" /* Frame pointer is omitted as it must not be changed. */
\r
637 " MOV R0, #13 \n" /* Set the scratch registers to known values - done inside the loop as they get clobbered. */
\r
642 " CMP R0, #13 \n" /* Check all the registers still contain their expected values. */
\r
643 " BNE prvDeleteMe \n" /* Value was not as expected, delete the task so it stops communicating with the check task */
\r
645 " BNE prvDeleteMe \n"
\r
647 " BNE prvDeleteMe \n"
\r
649 " BNE prvDeleteMe \n"
\r
651 " BNE prvDeleteMe \n"
\r
653 " BNE prvDeleteMe \n"
\r
655 " BNE prvDeleteMe \n"
\r
657 " BNE prvDeleteMe \n"
\r
659 " BNE prvDeleteMe \n"
\r
661 " BNE prvDeleteMe \n"
\r
663 " BNE prvDeleteMe \n"
\r
665 " BNE prvDeleteMe \n"
\r
666 :::"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r8", "r9", "r10", "r11", "r12"
\r
669 /* Send mainREG_TEST_2_STILL_EXECUTING to the check task to indicate that this
\r
670 task is still functioning. */
\r
671 prvSendImAlive( xQueue, mainREG_TEST_2_STILL_EXECUTING );
\r
673 /* Go back to check all the register values again. */
\r
674 __asm volatile( " B reg2loop " );
\r
677 /*-----------------------------------------------------------*/
\r
679 void vApplicationIdleHook( void )
\r
681 extern unsigned long __SRAM_segment_end__[];
\r
682 extern unsigned long __privileged_data_start__[];
\r
683 extern unsigned long __privileged_data_end__[];
\r
684 extern unsigned long __FLASH_segment_start__[];
\r
685 extern unsigned long __FLASH_segment_end__[];
\r
686 volatile unsigned long *pul;
\r
687 volatile unsigned long ulReadData;
\r
689 /* The idle task, and therefore this function, run in Supervisor mode and
\r
690 can therefore access all memory. Try reading from corners of flash and
\r
691 RAM to ensure a memory fault does not occur.
\r
693 Start with the edges of the privileged data area. */
\r
694 pul = __privileged_data_start__;
\r
696 pul = __privileged_data_end__ - 1;
\r
699 /* Next the standard SRAM area. */
\r
700 pul = __SRAM_segment_end__ - 1;
\r
703 /* And the standard Flash area - the start of which is marked for
\r
704 privileged access only. */
\r
705 pul = __FLASH_segment_start__;
\r
707 pul = __FLASH_segment_end__ - 1;
\r
710 /* Reading off the end of Flash or SRAM space should cause a fault.
\r
711 Uncomment one of the following two pairs of lines to test. */
\r
713 /* pul = __FLASH_segment_end__ + 4;
\r
714 ulReadData = *pul; */
\r
716 /* pul = __SRAM_segment_end__ + 1;
\r
717 ulReadData = *pul; */
\r
719 /*-----------------------------------------------------------*/
\r
721 static void prvOldStyleUserModeTask( void *pvParameters )
\r
723 extern unsigned long __privileged_data_start__[];
\r
724 extern unsigned long __privileged_data_end__[];
\r
725 extern unsigned long __SRAM_segment_end__[];
\r
726 extern unsigned long __privileged_functions_end__[];
\r
727 extern unsigned long __FLASH_segment_start__[];
\r
728 extern unsigned long __FLASH_segment_end__[];
\r
729 const volatile unsigned long *pulStandardPeripheralRegister = ( volatile unsigned long * ) 0x400FC0C4; /* PCONP */
\r
730 volatile unsigned long *pul;
\r
731 volatile unsigned long ulReadData;
\r
733 /* The following lines are commented out to prevent the unused variable
\r
734 compiler warnings when the tests that use the variable are also commented out.
\r
735 extern unsigned long __privileged_functions_start__[];
\r
736 const volatile unsigned long *pulSystemPeripheralRegister = ( volatile unsigned long * ) 0xe000e014; */
\r
738 ( void ) pvParameters;
\r
740 /* This task is created in User mode using the original xTaskCreate() API
\r
741 function. It should have access to all Flash and RAM except that marked
\r
742 as Privileged access only. Reading from the start and end of the non-
\r
743 privileged RAM should not cause a problem (the privileged RAM is the first
\r
744 block at the bottom of the RAM memory). */
\r
745 pul = __privileged_data_end__ + 1;
\r
747 pul = __SRAM_segment_end__ - 1;
\r
750 /* Likewise reading from the start and end of the non-privileged Flash
\r
751 should not be a problem (the privileged Flash is the first block at the
\r
752 bottom of the Flash memory). */
\r
753 pul = __privileged_functions_end__ + 1;
\r
755 pul = __FLASH_segment_end__ - 1;
\r
758 /* Standard peripherals are accessible. */
\r
759 ulReadData = *pulStandardPeripheralRegister;
\r
761 /* System peripherals are not accessible. Uncomment the following line
\r
762 to test. Also uncomment the declaration of pulSystemPeripheralRegister
\r
763 at the top of this function. */
\r
764 /* ulReadData = *pulSystemPeripheralRegister; */
\r
766 /* Reading from anywhere inside the privileged Flash or RAM should cause a
\r
767 fault. This can be tested by uncommenting any of the following pairs of
\r
768 lines. Also uncomment the declaration of __privileged_functions_start__
\r
769 at the top of this function. */
\r
771 /* pul = __privileged_functions_start__;
\r
772 ulReadData = *pul; */
\r
774 /* pul = __privileged_functions_end__ - 1;
\r
775 ulReadData = *pul; */
\r
777 /* pul = __privileged_data_start__;
\r
778 ulReadData = *pul; */
\r
780 /* pul = __privileged_data_end__ - 1;
\r
781 ulReadData = *pul; */
\r
783 /* Must not just run off the end of a task function, so delete this task.
\r
784 Note that because this task was created using xTaskCreate() the stack was
\r
785 allocated dynamically and I have not included any code to free it again. */
\r
786 vTaskDelete( NULL );
\r
788 /*-----------------------------------------------------------*/
\r
790 static void prvOldStylePrivilegedModeTask( void *pvParameters )
\r
792 extern unsigned long __privileged_data_start__[];
\r
793 extern unsigned long __privileged_data_end__[];
\r
794 extern unsigned long __SRAM_segment_end__[];
\r
795 extern unsigned long __privileged_functions_start__[];
\r
796 extern unsigned long __privileged_functions_end__[];
\r
797 extern unsigned long __FLASH_segment_start__[];
\r
798 extern unsigned long __FLASH_segment_end__[];
\r
799 volatile unsigned long *pul;
\r
800 volatile unsigned long ulReadData;
\r
801 const volatile unsigned long *pulSystemPeripheralRegister = ( volatile unsigned long * ) 0xe000e014; /* Systick */
\r
802 const volatile unsigned long *pulStandardPeripheralRegister = ( volatile unsigned long * ) 0x400FC0C4; /* PCONP */
\r
804 ( void ) pvParameters;
\r
806 /* This task is created in Privileged mode using the original xTaskCreate()
\r
807 API function. It should have access to all Flash and RAM including that
\r
808 marked as Privileged access only. So reading from the start and end of the
\r
809 non-privileged RAM should not cause a problem (the privileged RAM is the
\r
810 first block at the bottom of the RAM memory). */
\r
811 pul = __privileged_data_end__ + 1;
\r
813 pul = __SRAM_segment_end__ - 1;
\r
816 /* Likewise reading from the start and end of the non-privileged Flash
\r
817 should not be a problem (the privileged Flash is the first block at the
\r
818 bottom of the Flash memory). */
\r
819 pul = __privileged_functions_end__ + 1;
\r
821 pul = __FLASH_segment_end__ - 1;
\r
824 /* Reading from anywhere inside the privileged Flash or RAM should also
\r
825 not be a problem. */
\r
826 pul = __privileged_functions_start__;
\r
828 pul = __privileged_functions_end__ - 1;
\r
830 pul = __privileged_data_start__;
\r
832 pul = __privileged_data_end__ - 1;
\r
835 /* Finally, accessing both System and normal peripherals should both be
\r
837 ulReadData = *pulSystemPeripheralRegister;
\r
838 ulReadData = *pulStandardPeripheralRegister;
\r
840 /* Must not just run off the end of a task function, so delete this task.
\r
841 Note that because this task was created using xTaskCreate() the stack was
\r
842 allocated dynamically and I have not included any code to free it again. */
\r
843 vTaskDelete( NULL );
\r
845 /*-----------------------------------------------------------*/
\r
847 static void prvDeleteMe( void )
\r
849 vTaskDelete( NULL );
\r
851 /*-----------------------------------------------------------*/
\r
853 static void prvSendImAlive( QueueHandle_t xHandle, unsigned long ulTaskNumber )
\r
855 if( xHandle != NULL )
\r
857 xQueueSend( xHandle, &ulTaskNumber, mainDONT_BLOCK );
\r
860 /*-----------------------------------------------------------*/
\r
862 void prvSetupHardware( void )
\r
864 /* Disable peripherals power. */
\r
867 /* Enable GPIO power. */
\r
868 SC->PCONP = PCONP_PCGPIO;
\r
870 /* Disable TPIU. */
\r
871 PINCON->PINSEL10 = 0;
\r
873 if ( SC->PLL0STAT & ( 1 << 25 ) )
\r
875 /* Enable PLL, disconnected. */
\r
877 SC->PLL0FEED = PLLFEED_FEED1;
\r
878 SC->PLL0FEED = PLLFEED_FEED2;
\r
881 /* Disable PLL, disconnected. */
\r
883 SC->PLL0FEED = PLLFEED_FEED1;
\r
884 SC->PLL0FEED = PLLFEED_FEED2;
\r
886 /* Enable main OSC. */
\r
888 while( !( SC->SCS & 0x40 ) );
\r
890 /* select main OSC, 12MHz, as the PLL clock source. */
\r
891 SC->CLKSRCSEL = 0x1;
\r
893 SC->PLL0CFG = 0x20031;
\r
894 SC->PLL0FEED = PLLFEED_FEED1;
\r
895 SC->PLL0FEED = PLLFEED_FEED2;
\r
897 /* Enable PLL, disconnected. */
\r
899 SC->PLL0FEED = PLLFEED_FEED1;
\r
900 SC->PLL0FEED = PLLFEED_FEED2;
\r
902 /* Set clock divider. */
\r
903 SC->CCLKCFG = 0x03;
\r
905 /* Configure flash accelerator. */
\r
906 SC->FLASHCFG = 0x403a;
\r
908 /* Check lock bit status. */
\r
909 while( ( ( SC->PLL0STAT & ( 1 << 26 ) ) == 0 ) );
\r
911 /* Enable and connect. */
\r
913 SC->PLL0FEED = PLLFEED_FEED1;
\r
914 SC->PLL0FEED = PLLFEED_FEED2;
\r
915 while( ( ( SC->PLL0STAT & ( 1 << 25 ) ) == 0 ) );
\r
920 /* Configure the clock for the USB. */
\r
922 if( SC->PLL1STAT & ( 1 << 9 ) )
\r
924 /* Enable PLL, disconnected. */
\r
926 SC->PLL1FEED = PLLFEED_FEED1;
\r
927 SC->PLL1FEED = PLLFEED_FEED2;
\r
930 /* Disable PLL, disconnected. */
\r
932 SC->PLL1FEED = PLLFEED_FEED1;
\r
933 SC->PLL1FEED = PLLFEED_FEED2;
\r
935 SC->PLL1CFG = 0x23;
\r
936 SC->PLL1FEED = PLLFEED_FEED1;
\r
937 SC->PLL1FEED = PLLFEED_FEED2;
\r
939 /* Enable PLL, disconnected. */
\r
941 SC->PLL1FEED = PLLFEED_FEED1;
\r
942 SC->PLL1FEED = PLLFEED_FEED2;
\r
943 while( ( ( SC->PLL1STAT & ( 1 << 10 ) ) == 0 ) );
\r
945 /* Enable and connect. */
\r
947 SC->PLL1FEED = PLLFEED_FEED1;
\r
948 SC->PLL1FEED = PLLFEED_FEED2;
\r
949 while( ( ( SC->PLL1STAT & ( 1 << 9 ) ) == 0 ) );
\r
951 /* Setup the peripheral bus to be the same as the PLL output (64 MHz). */
\r
952 SC->PCLKSEL0 = 0x05555555;
\r
954 /* Prepare the LCD. */
\r
955 LCDdriver_initialisation();
\r
956 LCD_PrintString( 5, 10, "FreeRTOS.org", 14, COLOR_GREEN);
\r
958 /*-----------------------------------------------------------*/
\r
960 void vApplicationTickHook( void )
\r
962 static unsigned long ulCallCount;
\r
963 const unsigned long ulCallsBetweenSends = 5000 / portTICK_PERIOD_MS;
\r
964 const unsigned long ulMessage = mainPRINT_SYSTEM_STATUS;
\r
965 portBASE_TYPE xDummy;
\r
967 /* If configUSE_TICK_HOOK is set to 1 then this function will get called
\r
968 from each RTOS tick. It is called from the tick interrupt and therefore
\r
969 will be executing in the privileged state. */
\r
973 /* Is it time to print out the pass/fail message again? */
\r
974 if( ulCallCount >= ulCallsBetweenSends )
\r
978 /* Send a message to the check task to command it to check that all
\r
979 the tasks are still running then print out the status.
\r
981 This is running in an ISR so has to use the "FromISR" version of
\r
982 xQueueSend(). Because it is in an ISR it is running with privileges
\r
983 so can access xFileScopeCheckQueue directly. */
\r
984 xQueueSendFromISR( xFileScopeCheckQueue, &ulMessage, &xDummy );
\r
987 /*-----------------------------------------------------------*/
\r
989 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
\r
991 /* If configCHECK_FOR_STACK_OVERFLOW is set to either 1 or 2 then this
\r
992 function will automatically get called if a task overflows its stack. */
\r
994 ( void ) pcTaskName;
\r
997 /*-----------------------------------------------------------*/
\r
999 void vApplicationMallocFailedHook( void )
\r
1001 /* If configUSE_MALLOC_FAILED_HOOK is set to 1 then this function will
\r
1002 be called automatically if a call to pvPortMalloc() fails. pvPortMalloc()
\r
1003 is called automatically when a task, queue or semaphore is created. */
\r
1006 /*-----------------------------------------------------------*/
\r