2 FreeRTOS V7.1.1 - Copyright (C) 2012 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 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
67 #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
71 * This file demonstrates the use of FreeRTOS-MPU. It creates tasks in both
\r
72 * User mode and Privileged mode, and using both the original xTaskCreate() and
\r
73 * the new xTaskCreateRestricted() API functions. The purpose of each created
\r
74 * task is documented in the comments above the task function prototype (in
\r
75 * this file), with the task behaviour demonstrated and documented within the
\r
76 * task function itself. In addition a queue is used to demonstrate passing
\r
77 * data between protected/restricted tasks as well as passing data between an
\r
78 * interrupt and a protected/restricted task.
\r
83 /* Library includes. */
\r
86 /* Scheduler includes. */
\r
87 #include "FreeRTOS.h"
\r
92 /* Red Suite includes. */
\r
93 #include "lcd_driver.h"
\r
97 /*-----------------------------------------------------------*/
\r
99 /* Misc constants. */
\r
100 #define mainDONT_BLOCK ( 0 )
\r
102 /* Definitions for the messages that can be sent to the check task. */
\r
103 #define mainREG_TEST_1_STILL_EXECUTING ( 0 )
\r
104 #define mainREG_TEST_2_STILL_EXECUTING ( 1 )
\r
105 #define mainPRINT_SYSTEM_STATUS ( 2 )
\r
107 /* GCC specifics. */
\r
108 #define mainALIGN_TO( x ) __attribute__((aligned(x)))
\r
110 /* Hardware specifics. The start and end address are chosen to ensure the
\r
111 required GPIO are covered while also ensuring the necessary alignment is
\r
113 #define mainGPIO_START_ADDRESS ( ( unsigned long * ) 0x2009c000 )
\r
114 #define mainGPIO_END_ADDRESS ( mainGPIO_START_ADDRESS + ( 64 * 1024 ) )
\r
117 /*-----------------------------------------------------------*/
\r
118 /* Prototypes for functions that implement tasks. -----------*/
\r
119 /*-----------------------------------------------------------*/
\r
122 * Prototype for the reg test tasks. Amongst other things, these fill the CPU
\r
123 * registers with known values before checking that the registers still contain
\r
124 * the expected values. Each of the two tasks use different values so an error
\r
125 * in the context switch mechanism can be caught. Both reg test tasks execute
\r
126 * at the idle priority so will get preempted regularly. Each task repeatedly
\r
127 * sends a message on a queue so long as it remains functioning correctly. If
\r
128 * an error is detected within the task the task is simply deleted.
\r
130 static void prvRegTest1Task( void *pvParameters );
\r
131 static void prvRegTest2Task( void *pvParameters );
\r
134 * Prototype for the check task. The check task demonstrates various features
\r
135 * of the MPU before entering a loop where it waits for messages to arrive on a
\r
138 * Two types of messages can be processes:
\r
140 * 1) "I'm Alive" messages sent from the reg test tasks, indicating that the
\r
141 * task is still operational.
\r
143 * 2) "Print Status commands" sent periodically by the tick hook function (and
\r
144 * therefore from within an interrupt) which command the check task to write
\r
145 * either pass or fail to the terminal, depending on the status of the reg
\r
148 static void prvCheckTask( void *pvParameters );
\r
151 * Prototype for a task created in User mode using the original vTaskCreate()
\r
152 * API function. The task demonstrates the characteristics of such a task,
\r
153 * before simply deleting itself.
\r
155 static void prvOldStyleUserModeTask( void *pvParameters );
\r
158 * Prototype for a task created in Privileged mode using the original
\r
159 * vTaskCreate() API function. The task demonstrates the characteristics of
\r
160 * such a task, before simply deleting itself.
\r
162 static void prvOldStylePrivilegedModeTask( void *pvParameters );
\r
165 /*-----------------------------------------------------------*/
\r
166 /* Prototypes for other misc functions. --------------------*/
\r
167 /*-----------------------------------------------------------*/
\r
170 * Just configures any clocks and IO necessary.
\r
172 static void prvSetupHardware( void );
\r
175 * Simply deletes the calling task. The function is provided only because it
\r
176 * is simpler to call from asm code than the normal vTaskDelete() API function.
\r
177 * It has the noinline attribute because it is called from asm code.
\r
179 static void prvDeleteMe( void ) __attribute__((noinline));
\r
182 * Used by both reg test tasks to send messages to the check task. The message
\r
183 * just lets the check task know that the task is still functioning correctly.
\r
184 * If a reg test task detects an error it will delete itself, and in so doing
\r
185 * prevent itself from sending any more 'I'm Alive' messages to the check task.
\r
187 static void prvSendImAlive( xQueueHandle xHandle, unsigned long ulTaskNumber );
\r
190 * The check task is created with access to three memory regions (plus its
\r
191 * stack). Each memory region is configured with different parameters and
\r
192 * prvTestMemoryRegions() demonstrates what can and cannot be accessed for each
\r
193 * region. prvTestMemoryRegions() also demonstrates a task that was created
\r
194 * as a privileged task settings its own privilege level down to that of a user
\r
197 static void prvTestMemoryRegions( void );
\r
199 /*-----------------------------------------------------------*/
\r
201 /* The handle of the queue used to communicate between tasks and between tasks
\r
202 and interrupts. Note that this is a file scope variable that falls outside of
\r
203 any MPU region. As such other techniques have to be used to allow the tasks
\r
204 to gain access to the queue. See the comments in the tasks themselves for
\r
205 further information. */
\r
206 static xQueueHandle xFileScopeCheckQueue = NULL;
\r
210 /*-----------------------------------------------------------*/
\r
211 /* Data used by the 'check' task. ---------------------------*/
\r
212 /*-----------------------------------------------------------*/
\r
214 /* Define the constants used to allocate the check task stack. Note that the
\r
215 stack size is defined in words, not bytes. */
\r
216 #define mainCHECK_TASK_STACK_SIZE_WORDS 128
\r
217 #define mainCHECK_TASK_STACK_ALIGNMENT ( mainCHECK_TASK_STACK_SIZE_WORDS * sizeof( portSTACK_TYPE ) )
\r
219 /* Declare the stack that will be used by the check task. The kernel will
\r
220 automatically create an MPU region for the stack. The stack alignment must
\r
221 match its size, so if 128 words are reserved for the stack then it must be
\r
222 aligned to ( 128 * 4 ) bytes. */
\r
223 static portSTACK_TYPE xCheckTaskStack[ mainCHECK_TASK_STACK_SIZE_WORDS ] mainALIGN_TO( mainCHECK_TASK_STACK_ALIGNMENT );
\r
225 /* Declare three arrays - an MPU region will be created for each array
\r
226 using the xTaskParameters structure below. THIS IS JUST TO DEMONSTRATE THE
\r
227 MPU FUNCTIONALITY, the data is not used by the check tasks primary function
\r
228 of monitoring the reg test tasks and printing out status information.
\r
230 Note that the arrays allocate slightly more RAM than is actually assigned to
\r
231 the MPU region. This is to permit writes off the end of the array to be
\r
232 detected even when the arrays are placed in adjacent memory locations (with no
\r
233 gaps between them). The align size must be a power of two. */
\r
234 #define mainREAD_WRITE_ARRAY_SIZE 130
\r
235 #define mainREAD_WRITE_ALIGN_SIZE 128
\r
236 char cReadWriteArray[ mainREAD_WRITE_ARRAY_SIZE ] mainALIGN_TO( mainREAD_WRITE_ALIGN_SIZE );
\r
238 #define mainREAD_ONLY_ARRAY_SIZE 260
\r
239 #define mainREAD_ONLY_ALIGN_SIZE 256
\r
240 char cReadOnlyArray[ mainREAD_ONLY_ARRAY_SIZE ] mainALIGN_TO( mainREAD_ONLY_ALIGN_SIZE );
\r
242 #define mainPRIVILEGED_ONLY_ACCESS_ARRAY_SIZE 130
\r
243 #define mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE 128
\r
244 char cPrivilegedOnlyAccessArray[ mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE ] mainALIGN_TO( mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE );
\r
246 /* Fill in a xTaskParameters structure to define the check task - this is the
\r
247 structure passed to the xTaskCreateRestricted() function. */
\r
248 static const xTaskParameters xCheckTaskParameters =
\r
250 prvCheckTask, /* pvTaskCode - the function that implements the task. */
\r
251 ( signed char * ) "Check", /* pcName */
\r
252 mainCHECK_TASK_STACK_SIZE_WORDS, /* usStackDepth - defined in words, not bytes. */
\r
253 ( void * ) 0x12121212, /* pvParameters - this value is just to test that the parameter is being passed into the task correctly. */
\r
254 ( 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
255 xCheckTaskStack, /* puxStackBuffer - the array to use as the task stack, as declared above. */
\r
257 /* xRegions - In this case the xRegions array is used to create MPU regions
\r
258 for all three of the arrays declared directly above. Each MPU region is
\r
259 created with different parameters. Again, THIS IS JUST TO DEMONSTRATE THE
\r
260 MPU FUNCTIONALITY, the data is not used by the check tasks primary function
\r
261 of monitoring the reg test tasks and printing out status information.*/
\r
263 /* Base address Length Parameters */
\r
264 { cReadWriteArray, mainREAD_WRITE_ALIGN_SIZE, portMPU_REGION_READ_WRITE },
\r
265 { cReadOnlyArray, mainREAD_ONLY_ALIGN_SIZE, portMPU_REGION_READ_ONLY },
\r
266 { cPrivilegedOnlyAccessArray, mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE, portMPU_REGION_PRIVILEGED_READ_WRITE }
\r
270 /* Three MPU regions are defined for use by the 'check' task when the task is
\r
271 created. These are only used to demonstrate the MPU features and are not
\r
272 actually necessary for the check task to fulfill its primary purpose. Instead
\r
273 the MPU regions are replaced with those defined by xAltRegions prior to the
\r
274 check task receiving any data on the queue or printing any messages to the
\r
275 debug console. The MPU region defined below covers the GPIO peripherals used
\r
276 to write to the LCD. */
\r
277 static const xMemoryRegion xAltRegions[ portNUM_CONFIGURABLE_REGIONS ] =
\r
279 /* Base address Length Parameters */
\r
280 { mainGPIO_START_ADDRESS, ( 64 * 1024 ), portMPU_REGION_READ_WRITE },
\r
287 /*-----------------------------------------------------------*/
\r
288 /* Data used by the 'reg test' tasks. -----------------------*/
\r
289 /*-----------------------------------------------------------*/
\r
291 /* Define the constants used to allocate the reg test task stacks. Note that
\r
292 that stack size is defined in words, not bytes. */
\r
293 #define mainREG_TEST_STACK_SIZE_WORDS 128
\r
294 #define mainREG_TEST_STACK_ALIGNMENT ( mainREG_TEST_STACK_SIZE_WORDS * sizeof( portSTACK_TYPE ) )
\r
296 /* Declare the stacks that will be used by the reg test tasks. The kernel will
\r
297 automatically create an MPU region for the stack. The stack alignment must
\r
298 match its size, so if 128 words are reserved for the stack then it must be
\r
299 aligned to ( 128 * 4 ) bytes. */
\r
300 static portSTACK_TYPE xRegTest1Stack[ mainREG_TEST_STACK_SIZE_WORDS ] mainALIGN_TO( mainREG_TEST_STACK_ALIGNMENT );
\r
301 static portSTACK_TYPE xRegTest2Stack[ mainREG_TEST_STACK_SIZE_WORDS ] mainALIGN_TO( mainREG_TEST_STACK_ALIGNMENT );
\r
303 /* Fill in a xTaskParameters structure per reg test task to define the tasks. */
\r
304 static const xTaskParameters xRegTest1Parameters =
\r
306 prvRegTest1Task, /* pvTaskCode - the function that implements the task. */
\r
307 ( signed char * ) "RegTest1", /* pcName */
\r
308 mainREG_TEST_STACK_SIZE_WORDS, /* usStackDepth */
\r
309 ( void * ) 0x12345678, /* pvParameters - this value is just to test that the parameter is being passed into the task correctly. */
\r
310 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
311 xRegTest1Stack, /* puxStackBuffer - the array to use as the task stack, as declared above. */
\r
312 { /* xRegions - this task does not use any non-stack data hence all members are zero. */
\r
313 /* Base address Length Parameters */
\r
314 { 0x00, 0x00, 0x00 },
\r
315 { 0x00, 0x00, 0x00 },
\r
316 { 0x00, 0x00, 0x00 }
\r
319 /*-----------------------------------------------------------*/
\r
321 static xTaskParameters xRegTest2Parameters =
\r
323 prvRegTest2Task, /* pvTaskCode - the function that implements the task. */
\r
324 ( signed char * ) "RegTest2", /* pcName */
\r
325 mainREG_TEST_STACK_SIZE_WORDS, /* usStackDepth */
\r
326 ( void * ) NULL, /* pvParameters - this task uses the parameter to pass in a queue handle, but the queue is not created yet. */
\r
327 tskIDLE_PRIORITY, /* uxPriority */
\r
328 xRegTest2Stack, /* puxStackBuffer - the array to use as the task stack, as declared above. */
\r
329 { /* xRegions - this task does not use any non-stack data hence all members are zero. */
\r
330 /* Base address Length Parameters */
\r
331 { 0x00, 0x00, 0x00 },
\r
332 { 0x00, 0x00, 0x00 },
\r
333 { 0x00, 0x00, 0x00 }
\r
337 /*-----------------------------------------------------------*/
\r
341 prvSetupHardware();
\r
343 /* Create the queue used to pass "I'm alive" messages to the check task. */
\r
344 xFileScopeCheckQueue = xQueueCreate( 1, sizeof( unsigned long ) );
\r
346 /* One check task uses the task parameter to receive the queue handle.
\r
347 This allows the file scope variable to be accessed from within the task.
\r
348 The pvParameters member of xRegTest2Parameters can only be set after the
\r
349 queue has been created so is set here. */
\r
350 xRegTest2Parameters.pvParameters = xFileScopeCheckQueue;
\r
352 /* Create the three test tasks. Handles to the created tasks are not
\r
353 required, hence the second parameter is NULL. */
\r
354 xTaskCreateRestricted( &xRegTest1Parameters, NULL );
\r
355 xTaskCreateRestricted( &xRegTest2Parameters, NULL );
\r
356 xTaskCreateRestricted( &xCheckTaskParameters, NULL );
\r
358 /* Create the tasks that are created using the original xTaskCreate() API
\r
360 xTaskCreate( prvOldStyleUserModeTask, /* The function that implements the task. */
\r
361 ( signed char * ) "Task1", /* Text name for the task. */
\r
362 100, /* Stack depth in words. */
\r
363 NULL, /* Task parameters. */
\r
364 3, /* Priority and mode (user in this case). */
\r
368 xTaskCreate( prvOldStylePrivilegedModeTask, /* The function that implements the task. */
\r
369 ( signed char * ) "Task2", /* Text name for the task. */
\r
370 100, /* Stack depth in words. */
\r
371 NULL, /* Task parameters. */
\r
372 ( 3 | portPRIVILEGE_BIT ), /* Priority and mode. */
\r
376 /* Start the scheduler. */
\r
377 vTaskStartScheduler();
\r
379 /* Will only get here if there was insufficient memory to create the idle
\r
384 /*-----------------------------------------------------------*/
\r
386 static void prvCheckTask( void *pvParameters )
\r
388 /* This task is created in privileged mode so can access the file scope
\r
389 queue variable. Take a stack copy of this before the task is set into user
\r
390 mode. Once that task is in user mode the file scope queue variable will no
\r
391 longer be accessible but the stack copy will. */
\r
392 xQueueHandle xQueue = xFileScopeCheckQueue;
\r
394 unsigned long ulStillAliveCounts[ 2 ] = { 0 };
\r
395 char *pcStatusMessage = "PASS\r\n";
\r
396 unsigned char x = 5, y = 10;
\r
398 /* Just to remove compiler warning. */
\r
399 ( void ) pvParameters;
\r
401 /* Demonstrate how the various memory regions can and can't be accessed.
\r
402 The task privilege is set down to user mode within this function. */
\r
403 prvTestMemoryRegions();
\r
405 /* Change the memory regions allocated to this task to those initially
\r
406 set up for demonstration purposes to those actually required by the task. */
\r
407 vTaskAllocateMPURegions( NULL, xAltRegions );
\r
409 /* This loop performs the main function of the task, which is blocking
\r
410 on a message queue then processing each message as it arrives. */
\r
413 /* Wait for the next message to arrive. */
\r
414 xQueueReceive( xQueue, &lMessage, portMAX_DELAY );
\r
418 case mainREG_TEST_1_STILL_EXECUTING :
\r
419 /* Message from task 1, so task 1 must still be executing. */
\r
420 ( ulStillAliveCounts[ 0 ] )++;
\r
423 case mainREG_TEST_2_STILL_EXECUTING :
\r
424 /* Message from task 2, so task 2 must still be executing. */
\r
425 ( ulStillAliveCounts[ 1 ] )++;
\r
428 case mainPRINT_SYSTEM_STATUS :
\r
429 /* Message from tick hook, time to print out the system
\r
430 status. If messages has stopped arriving from either reg
\r
431 test task then the status must be set to fail. */
\r
432 if( ( ulStillAliveCounts[ 0 ] == 0 ) || ( ulStillAliveCounts[ 1 ] == 0 ) )
\r
434 /* One or both of the test tasks are no longer sending
\r
435 'still alive' messages. */
\r
436 pcStatusMessage = "FAIL\r\n";
\r
439 /* Print a pass/fail message to the LCD - moving the
\r
440 message each time to provide feedback that the output
\r
441 is still being produced. LCD_PrintString() accesses const
\r
442 data stored in flash, which all tasks are at liberty to do,
\r
443 and GPIO for which an MPU region has been set up for it. */
\r
445 LCD_PrintString( x>>1, y>>1, pcStatusMessage, 6, COLOR_RED );
\r
449 /* Reset the count of 'still alive' messages. */
\r
450 memset( ulStillAliveCounts, 0x00, sizeof( ulStillAliveCounts ) );
\r
454 /* Something unexpected happened. Delete this task so the
\r
455 error is apparent (no output will be displayed). */
\r
461 /*-----------------------------------------------------------*/
\r
463 static void prvTestMemoryRegions( void )
\r
468 /* The check task (from which this function is called) is created in the
\r
469 Privileged mode. The privileged array can be both read from and written
\r
470 to while this task is privileged. */
\r
471 cPrivilegedOnlyAccessArray[ 0 ] = 'a';
\r
472 if( cPrivilegedOnlyAccessArray[ 0 ] != 'a' )
\r
474 /* Something unexpected happened. Delete this task so the error is
\r
475 apparent (no output will be displayed). */
\r
479 /* Writing off the end of the RAM allocated to this task will *NOT* cause a
\r
480 protection fault because the task is still executing in a privileged mode.
\r
481 Uncomment the following to test. */
\r
482 /* cPrivilegedOnlyAccessArray[ mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE ] = 'a'; */
\r
484 /* Now set the task into user mode. */
\r
485 portSWITCH_TO_USER_MODE();
\r
487 /* Accessing the privileged only array will now cause a fault. Uncomment
\r
488 the following line to test. */
\r
489 /* cPrivilegedOnlyAccessArray[ 0 ] = 'a'; */
\r
491 /* The read/write array can still be successfully read and written. */
\r
492 for( l = 0; l < mainREAD_WRITE_ALIGN_SIZE; l++ )
\r
494 cReadWriteArray[ l ] = 'a';
\r
495 if( cReadWriteArray[ l ] != 'a' )
\r
497 /* Something unexpected happened. Delete this task so the error is
\r
498 apparent (no output will be displayed). */
\r
503 /* But attempting to read or write off the end of the RAM allocated to this
\r
504 task will cause a fault. Uncomment either of the following two lines to
\r
506 /* cReadWriteArray[ 0 ] = cReadWriteArray[ -1 ]; */
\r
507 /* cReadWriteArray[ mainREAD_WRITE_ALIGN_SIZE ] = 0x00; */
\r
509 /* The read only array can be successfully read... */
\r
510 for( l = 0; l < mainREAD_ONLY_ALIGN_SIZE; l++ )
\r
512 cTemp = cReadOnlyArray[ l ];
\r
515 /* ...but cannot be written. Uncomment the following line to test. */
\r
516 /* cReadOnlyArray[ 0 ] = 'a'; */
\r
518 /* Writing to the first and last locations in the stack array should not
\r
519 cause a protection fault. Note that doing this will cause the kernel to
\r
520 detect a stack overflow if configCHECK_FOR_STACK_OVERFLOW is greater than
\r
522 xCheckTaskStack[ 0 ] = 0;
\r
523 xCheckTaskStack[ mainCHECK_TASK_STACK_SIZE_WORDS - 1 ] = 0;
\r
525 /* Writing off either end of the stack array should cause a protection
\r
526 fault, uncomment either of the following two lines to test. */
\r
527 /* xCheckTaskStack[ -1 ] = 0; */
\r
528 /* xCheckTaskStack[ mainCHECK_TASK_STACK_SIZE_WORDS ] = 0; */
\r
530 /*-----------------------------------------------------------*/
\r
532 static void prvRegTest1Task( void *pvParameters )
\r
534 /* This task is created in privileged mode so can access the file scope
\r
535 queue variable. Take a stack copy of this before the task is set into user
\r
536 mode. Once this task is in user mode the file scope queue variable will no
\r
537 longer be accessible but the stack copy will. */
\r
538 xQueueHandle xQueue = xFileScopeCheckQueue;
\r
540 /* Now the queue handle has been obtained the task can switch to user
\r
541 mode. This is just one method of passing a handle into a protected
\r
542 task, the other reg test task uses the task parameter instead. */
\r
543 portSWITCH_TO_USER_MODE();
\r
545 /* First check that the parameter value is as expected. */
\r
546 if( pvParameters != ( void * ) 0x12345678 )
\r
548 /* Error detected. Delete the task so it stops communicating with
\r
556 /* This task tests the kernel context switch mechanism by reading and
\r
557 writing directly to registers - which requires the test to be written
\r
558 in assembly code. */
\r
561 " MOV R4, #104 \n" /* Set registers to a known value. R0 to R1 are done in the loop below. */
\r
566 " MOV R10, #110 \n"
\r
567 " MOV R11, #111 \n"
\r
569 " MOV R0, #100 \n" /* Set the scratch registers to known values - done inside the loop as they get clobbered. */
\r
573 " MOV R12, #112 \n"
\r
574 " SVC #1 \n" /* Yield just to increase test coverage. */
\r
575 " CMP R0, #100 \n" /* Check all the registers still contain their expected values. */
\r
576 " BNE prvDeleteMe \n" /* Value was not as expected, delete the task so it stops communicating with the check task. */
\r
578 " BNE prvDeleteMe \n"
\r
580 " BNE prvDeleteMe \n"
\r
582 " BNE prvDeleteMe \n"
\r
583 " CMP R4, #104 \n"
\r
584 " BNE prvDeleteMe \n"
\r
586 " BNE prvDeleteMe \n"
\r
588 " BNE prvDeleteMe \n"
\r
590 " BNE prvDeleteMe \n"
\r
592 " BNE prvDeleteMe \n"
\r
593 " CMP R10, #110 \n"
\r
594 " BNE prvDeleteMe \n"
\r
595 " CMP R11, #111 \n"
\r
596 " BNE prvDeleteMe \n"
\r
597 " CMP R12, #112 \n"
\r
598 " BNE prvDeleteMe \n"
\r
599 :::"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r8", "r9", "r10", "r11", "r12"
\r
602 /* Send mainREG_TEST_1_STILL_EXECUTING to the check task to indicate that this
\r
603 task is still functioning. */
\r
604 prvSendImAlive( xQueue, mainREG_TEST_1_STILL_EXECUTING );
\r
606 /* Go back to check all the register values again. */
\r
607 __asm volatile( " B reg1loop " );
\r
610 /*-----------------------------------------------------------*/
\r
612 static void prvRegTest2Task( void *pvParameters )
\r
614 /* The queue handle is passed in as the task parameter. This is one method of
\r
615 passing data into a protected task, the other reg test task uses a different
\r
617 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
621 /* This task tests the kernel context switch mechanism by reading and
\r
622 writing directly to registers - which requires the test to be written
\r
623 in assembly code. */
\r
626 " MOV R4, #4 \n" /* Set registers to a known value. R0 to R1 are done in the loop below. */
\r
629 " MOV R8, #8 \n" /* Frame pointer is omitted as it must not be changed. */
\r
632 " MOV R11, #11 \n"
\r
634 " MOV R0, #13 \n" /* Set the scratch registers to known values - done inside the loop as they get clobbered. */
\r
639 " CMP R0, #13 \n" /* Check all the registers still contain their expected values. */
\r
640 " BNE prvDeleteMe \n" /* Value was not as expected, delete the task so it stops communicating with the check task */
\r
642 " BNE prvDeleteMe \n"
\r
644 " BNE prvDeleteMe \n"
\r
646 " BNE prvDeleteMe \n"
\r
648 " BNE prvDeleteMe \n"
\r
650 " BNE prvDeleteMe \n"
\r
652 " BNE prvDeleteMe \n"
\r
654 " BNE prvDeleteMe \n"
\r
656 " BNE prvDeleteMe \n"
\r
658 " BNE prvDeleteMe \n"
\r
660 " BNE prvDeleteMe \n"
\r
662 " BNE prvDeleteMe \n"
\r
663 :::"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r8", "r9", "r10", "r11", "r12"
\r
666 /* Send mainREG_TEST_2_STILL_EXECUTING to the check task to indicate that this
\r
667 task is still functioning. */
\r
668 prvSendImAlive( xQueue, mainREG_TEST_2_STILL_EXECUTING );
\r
670 /* Go back to check all the register values again. */
\r
671 __asm volatile( " B reg2loop " );
\r
674 /*-----------------------------------------------------------*/
\r
676 void vApplicationIdleHook( void )
\r
678 extern unsigned long __SRAM_segment_end__[];
\r
679 extern unsigned long __privileged_data_start__[];
\r
680 extern unsigned long __privileged_data_end__[];
\r
681 extern unsigned long __FLASH_segment_start__[];
\r
682 extern unsigned long __FLASH_segment_end__[];
\r
683 volatile unsigned long *pul;
\r
684 volatile unsigned long ulReadData;
\r
686 /* The idle task, and therefore this function, run in Supervisor mode and
\r
687 can therefore access all memory. Try reading from corners of flash and
\r
688 RAM to ensure a memory fault does not occur.
\r
690 Start with the edges of the privileged data area. */
\r
691 pul = __privileged_data_start__;
\r
693 pul = __privileged_data_end__ - 1;
\r
696 /* Next the standard SRAM area. */
\r
697 pul = __SRAM_segment_end__ - 1;
\r
700 /* And the standard Flash area - the start of which is marked for
\r
701 privileged access only. */
\r
702 pul = __FLASH_segment_start__;
\r
704 pul = __FLASH_segment_end__ - 1;
\r
707 /* Reading off the end of Flash or SRAM space should cause a fault.
\r
708 Uncomment one of the following two pairs of lines to test. */
\r
710 /* pul = __FLASH_segment_end__ + 4;
\r
711 ulReadData = *pul; */
\r
713 /* pul = __SRAM_segment_end__ + 1;
\r
714 ulReadData = *pul; */
\r
716 /*-----------------------------------------------------------*/
\r
718 static void prvOldStyleUserModeTask( void *pvParameters )
\r
720 extern unsigned long __privileged_data_start__[];
\r
721 extern unsigned long __privileged_data_end__[];
\r
722 extern unsigned long __SRAM_segment_end__[];
\r
723 extern unsigned long __privileged_functions_end__[];
\r
724 extern unsigned long __FLASH_segment_start__[];
\r
725 extern unsigned long __FLASH_segment_end__[];
\r
726 const volatile unsigned long *pulStandardPeripheralRegister = ( volatile unsigned long * ) 0x400FC0C4; /* PCONP */
\r
727 volatile unsigned long *pul;
\r
728 volatile unsigned long ulReadData;
\r
730 /* The following lines are commented out to prevent the unused variable
\r
731 compiler warnings when the tests that use the variable are also commented out.
\r
732 extern unsigned long __privileged_functions_start__[];
\r
733 const volatile unsigned long *pulSystemPeripheralRegister = ( volatile unsigned long * ) 0xe000e014; */
\r
735 ( void ) pvParameters;
\r
737 /* This task is created in User mode using the original xTaskCreate() API
\r
738 function. It should have access to all Flash and RAM except that marked
\r
739 as Privileged access only. Reading from the start and end of the non-
\r
740 privileged RAM should not cause a problem (the privileged RAM is the first
\r
741 block at the bottom of the RAM memory). */
\r
742 pul = __privileged_data_end__ + 1;
\r
744 pul = __SRAM_segment_end__ - 1;
\r
747 /* Likewise reading from the start and end of the non-privileged Flash
\r
748 should not be a problem (the privileged Flash is the first block at the
\r
749 bottom of the Flash memory). */
\r
750 pul = __privileged_functions_end__ + 1;
\r
752 pul = __FLASH_segment_end__ - 1;
\r
755 /* Standard peripherals are accessible. */
\r
756 ulReadData = *pulStandardPeripheralRegister;
\r
758 /* System peripherals are not accessible. Uncomment the following line
\r
759 to test. Also uncomment the declaration of pulSystemPeripheralRegister
\r
760 at the top of this function. */
\r
761 /* ulReadData = *pulSystemPeripheralRegister; */
\r
763 /* Reading from anywhere inside the privileged Flash or RAM should cause a
\r
764 fault. This can be tested by uncommenting any of the following pairs of
\r
765 lines. Also uncomment the declaration of __privileged_functions_start__
\r
766 at the top of this function. */
\r
768 /* pul = __privileged_functions_start__;
\r
769 ulReadData = *pul; */
\r
771 /* pul = __privileged_functions_end__ - 1;
\r
772 ulReadData = *pul; */
\r
774 /* pul = __privileged_data_start__;
\r
775 ulReadData = *pul; */
\r
777 /* pul = __privileged_data_end__ - 1;
\r
778 ulReadData = *pul; */
\r
780 /* Must not just run off the end of a task function, so delete this task.
\r
781 Note that because this task was created using xTaskCreate() the stack was
\r
782 allocated dynamically and I have not included any code to free it again. */
\r
783 vTaskDelete( NULL );
\r
785 /*-----------------------------------------------------------*/
\r
787 static void prvOldStylePrivilegedModeTask( void *pvParameters )
\r
789 extern unsigned long __privileged_data_start__[];
\r
790 extern unsigned long __privileged_data_end__[];
\r
791 extern unsigned long __SRAM_segment_end__[];
\r
792 extern unsigned long __privileged_functions_start__[];
\r
793 extern unsigned long __privileged_functions_end__[];
\r
794 extern unsigned long __FLASH_segment_start__[];
\r
795 extern unsigned long __FLASH_segment_end__[];
\r
796 volatile unsigned long *pul;
\r
797 volatile unsigned long ulReadData;
\r
798 const volatile unsigned long *pulSystemPeripheralRegister = ( volatile unsigned long * ) 0xe000e014; /* Systick */
\r
799 const volatile unsigned long *pulStandardPeripheralRegister = ( volatile unsigned long * ) 0x400FC0C4; /* PCONP */
\r
801 ( void ) pvParameters;
\r
803 /* This task is created in Privileged mode using the original xTaskCreate()
\r
804 API function. It should have access to all Flash and RAM including that
\r
805 marked as Privileged access only. So reading from the start and end of the
\r
806 non-privileged RAM should not cause a problem (the privileged RAM is the
\r
807 first block at the bottom of the RAM memory). */
\r
808 pul = __privileged_data_end__ + 1;
\r
810 pul = __SRAM_segment_end__ - 1;
\r
813 /* Likewise reading from the start and end of the non-privileged Flash
\r
814 should not be a problem (the privileged Flash is the first block at the
\r
815 bottom of the Flash memory). */
\r
816 pul = __privileged_functions_end__ + 1;
\r
818 pul = __FLASH_segment_end__ - 1;
\r
821 /* Reading from anywhere inside the privileged Flash or RAM should also
\r
822 not be a problem. */
\r
823 pul = __privileged_functions_start__;
\r
825 pul = __privileged_functions_end__ - 1;
\r
827 pul = __privileged_data_start__;
\r
828 ulReadData = *pul;
\r
829 pul = __privileged_data_end__ - 1;
\r
832 /* Finally, accessing both System and normal peripherals should both be
\r
834 ulReadData = *pulSystemPeripheralRegister;
\r
835 ulReadData = *pulStandardPeripheralRegister;
\r
837 /* Must not just run off the end of a task function, so delete this task.
\r
838 Note that because this task was created using xTaskCreate() the stack was
\r
839 allocated dynamically and I have not included any code to free it again. */
\r
840 vTaskDelete( NULL );
\r
842 /*-----------------------------------------------------------*/
\r
844 static void prvDeleteMe( void )
\r
846 vTaskDelete( NULL );
\r
848 /*-----------------------------------------------------------*/
\r
850 static void prvSendImAlive( xQueueHandle xHandle, unsigned long ulTaskNumber )
\r
852 if( xHandle != NULL )
\r
854 xQueueSend( xHandle, &ulTaskNumber, mainDONT_BLOCK );
\r
857 /*-----------------------------------------------------------*/
\r
859 void prvSetupHardware( void )
\r
861 /* Disable peripherals power. */
\r
864 /* Enable GPIO power. */
\r
865 SC->PCONP = PCONP_PCGPIO;
\r
867 /* Disable TPIU. */
\r
868 PINCON->PINSEL10 = 0;
\r
870 if ( SC->PLL0STAT & ( 1 << 25 ) )
\r
872 /* Enable PLL, disconnected. */
\r
874 SC->PLL0FEED = PLLFEED_FEED1;
\r
875 SC->PLL0FEED = PLLFEED_FEED2;
\r
878 /* Disable PLL, disconnected. */
\r
880 SC->PLL0FEED = PLLFEED_FEED1;
\r
881 SC->PLL0FEED = PLLFEED_FEED2;
\r
883 /* Enable main OSC. */
\r
885 while( !( SC->SCS & 0x40 ) );
\r
887 /* select main OSC, 12MHz, as the PLL clock source. */
\r
888 SC->CLKSRCSEL = 0x1;
\r
890 SC->PLL0CFG = 0x20031;
\r
891 SC->PLL0FEED = PLLFEED_FEED1;
\r
892 SC->PLL0FEED = PLLFEED_FEED2;
\r
894 /* Enable PLL, disconnected. */
\r
896 SC->PLL0FEED = PLLFEED_FEED1;
\r
897 SC->PLL0FEED = PLLFEED_FEED2;
\r
899 /* Set clock divider. */
\r
900 SC->CCLKCFG = 0x03;
\r
902 /* Configure flash accelerator. */
\r
903 SC->FLASHCFG = 0x403a;
\r
905 /* Check lock bit status. */
\r
906 while( ( ( SC->PLL0STAT & ( 1 << 26 ) ) == 0 ) );
\r
908 /* Enable and connect. */
\r
910 SC->PLL0FEED = PLLFEED_FEED1;
\r
911 SC->PLL0FEED = PLLFEED_FEED2;
\r
912 while( ( ( SC->PLL0STAT & ( 1 << 25 ) ) == 0 ) );
\r
917 /* Configure the clock for the USB. */
\r
919 if( SC->PLL1STAT & ( 1 << 9 ) )
\r
921 /* Enable PLL, disconnected. */
\r
923 SC->PLL1FEED = PLLFEED_FEED1;
\r
924 SC->PLL1FEED = PLLFEED_FEED2;
\r
927 /* Disable PLL, disconnected. */
\r
929 SC->PLL1FEED = PLLFEED_FEED1;
\r
930 SC->PLL1FEED = PLLFEED_FEED2;
\r
932 SC->PLL1CFG = 0x23;
\r
933 SC->PLL1FEED = PLLFEED_FEED1;
\r
934 SC->PLL1FEED = PLLFEED_FEED2;
\r
936 /* Enable PLL, disconnected. */
\r
938 SC->PLL1FEED = PLLFEED_FEED1;
\r
939 SC->PLL1FEED = PLLFEED_FEED2;
\r
940 while( ( ( SC->PLL1STAT & ( 1 << 10 ) ) == 0 ) );
\r
942 /* Enable and connect. */
\r
944 SC->PLL1FEED = PLLFEED_FEED1;
\r
945 SC->PLL1FEED = PLLFEED_FEED2;
\r
946 while( ( ( SC->PLL1STAT & ( 1 << 9 ) ) == 0 ) );
\r
948 /* Setup the peripheral bus to be the same as the PLL output (64 MHz). */
\r
949 SC->PCLKSEL0 = 0x05555555;
\r
951 /* Prepare the LCD. */
\r
952 LCDdriver_initialisation();
\r
953 LCD_PrintString( 5, 10, "FreeRTOS.org", 14, COLOR_GREEN);
\r
955 /*-----------------------------------------------------------*/
\r
957 void vApplicationTickHook( void )
\r
959 static unsigned long ulCallCount;
\r
960 const unsigned long ulCallsBetweenSends = 5000 / portTICK_RATE_MS;
\r
961 const unsigned long ulMessage = mainPRINT_SYSTEM_STATUS;
\r
962 portBASE_TYPE xDummy;
\r
964 /* If configUSE_TICK_HOOK is set to 1 then this function will get called
\r
965 from each RTOS tick. It is called from the tick interrupt and therefore
\r
966 will be executing in the privileged state. */
\r
970 /* Is it time to print out the pass/fail message again? */
\r
971 if( ulCallCount >= ulCallsBetweenSends )
\r
975 /* Send a message to the check task to command it to check that all
\r
976 the tasks are still running then print out the status.
\r
978 This is running in an ISR so has to use the "FromISR" version of
\r
979 xQueueSend(). Because it is in an ISR it is running with privileges
\r
980 so can access xFileScopeCheckQueue directly. */
\r
981 xQueueSendFromISR( xFileScopeCheckQueue, &ulMessage, &xDummy );
\r
984 /*-----------------------------------------------------------*/
\r
986 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )
\r
988 /* If configCHECK_FOR_STACK_OVERFLOW is set to either 1 or 2 then this
\r
989 function will automatically get called if a task overflows its stack. */
\r
991 ( void ) pcTaskName;
\r
994 /*-----------------------------------------------------------*/
\r
996 void vApplicationMallocFailedHook( void )
\r
998 /* If configUSE_MALLOC_FAILED_HOOK is set to 1 then this function will
\r
999 be called automatically if a call to pvPortMalloc() fails. pvPortMalloc()
\r
1000 is called automatically when a task, queue or semaphore is created. */
\r
1003 /*-----------------------------------------------------------*/
\r