2 FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
32 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
33 distribute a combined work that includes FreeRTOS without being obliged to
\r
34 provide the source code for proprietary components outside of the FreeRTOS
\r
35 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
36 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
37 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
38 more details. You should have received a copy of the GNU General Public
\r
39 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
40 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
41 by writing to Richard Barry, contact details for whom are available on the
\r
46 ***************************************************************************
\r
48 * Having a problem? Start by reading the FAQ "My application does *
\r
49 * not run, what could be wrong?" *
\r
51 * http://www.FreeRTOS.org/FAQHelp.html *
\r
53 ***************************************************************************
\r
56 http://www.FreeRTOS.org - Documentation, training, latest versions, license
\r
57 and contact details.
\r
59 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
60 including FreeRTOS+Trace - an indispensable productivity tool.
\r
62 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
63 the code with commercial support, indemnification, and middleware, under
\r
64 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
65 provide a safety engineered and independently SIL3 certified version under
\r
66 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
69 #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
73 * This file demonstrates the use of FreeRTOS-MPU. It creates tasks in both
\r
74 * User mode and Privileged mode, and using both the original xTaskCreate() and
\r
75 * the new xTaskCreateRestricted() API functions. The purpose of each created
\r
76 * task is documented in the comments above the task function prototype (in
\r
77 * this file), with the task behaviour demonstrated and documented within the
\r
78 * task function itself. In addition a queue is used to demonstrate passing
\r
79 * data between protected/restricted tasks as well as passing data between an
\r
80 * interrupt and a protected/restricted task.
\r
85 /* Library includes. */
\r
88 /* Scheduler includes. */
\r
89 #include "FreeRTOS.h"
\r
94 /* Red Suite includes. */
\r
95 #include "lcd_driver.h"
\r
99 /*-----------------------------------------------------------*/
\r
101 /* Misc constants. */
\r
102 #define mainDONT_BLOCK ( 0 )
\r
104 /* Definitions for the messages that can be sent to the check task. */
\r
105 #define mainREG_TEST_1_STILL_EXECUTING ( 0 )
\r
106 #define mainREG_TEST_2_STILL_EXECUTING ( 1 )
\r
107 #define mainPRINT_SYSTEM_STATUS ( 2 )
\r
109 /* GCC specifics. */
\r
110 #define mainALIGN_TO( x ) __attribute__((aligned(x)))
\r
112 /* Hardware specifics. The start and end address are chosen to ensure the
\r
113 required GPIO are covered while also ensuring the necessary alignment is
\r
115 #define mainGPIO_START_ADDRESS ( ( unsigned long * ) 0x2009c000 )
\r
116 #define mainGPIO_END_ADDRESS ( mainGPIO_START_ADDRESS + ( 64 * 1024 ) )
\r
119 /*-----------------------------------------------------------*/
\r
120 /* Prototypes for functions that implement tasks. -----------*/
\r
121 /*-----------------------------------------------------------*/
\r
124 * Prototype for the reg test tasks. Amongst other things, these fill the CPU
\r
125 * registers with known values before checking that the registers still contain
\r
126 * the expected values. Each of the two tasks use different values so an error
\r
127 * in the context switch mechanism can be caught. Both reg test tasks execute
\r
128 * at the idle priority so will get preempted regularly. Each task repeatedly
\r
129 * sends a message on a queue so long as it remains functioning correctly. If
\r
130 * an error is detected within the task the task is simply deleted.
\r
132 static void prvRegTest1Task( void *pvParameters );
\r
133 static void prvRegTest2Task( void *pvParameters );
\r
136 * Prototype for the check task. The check task demonstrates various features
\r
137 * of the MPU before entering a loop where it waits for messages to arrive on a
\r
140 * Two types of messages can be processes:
\r
142 * 1) "I'm Alive" messages sent from the reg test tasks, indicating that the
\r
143 * task is still operational.
\r
145 * 2) "Print Status commands" sent periodically by the tick hook function (and
\r
146 * therefore from within an interrupt) which command the check task to write
\r
147 * either pass or fail to the terminal, depending on the status of the reg
\r
150 static void prvCheckTask( void *pvParameters );
\r
153 * Prototype for a task created in User mode using the original vTaskCreate()
\r
154 * API function. The task demonstrates the characteristics of such a task,
\r
155 * before simply deleting itself.
\r
157 static void prvOldStyleUserModeTask( void *pvParameters );
\r
160 * Prototype for a task created in Privileged mode using the original
\r
161 * vTaskCreate() API function. The task demonstrates the characteristics of
\r
162 * such a task, before simply deleting itself.
\r
164 static void prvOldStylePrivilegedModeTask( void *pvParameters );
\r
167 /*-----------------------------------------------------------*/
\r
168 /* Prototypes for other misc functions. --------------------*/
\r
169 /*-----------------------------------------------------------*/
\r
172 * Just configures any clocks and IO necessary.
\r
174 static void prvSetupHardware( void );
\r
177 * Simply deletes the calling task. The function is provided only because it
\r
178 * is simpler to call from asm code than the normal vTaskDelete() API function.
\r
179 * It has the noinline attribute because it is called from asm code.
\r
181 static void prvDeleteMe( void ) __attribute__((noinline));
\r
184 * Used by both reg test tasks to send messages to the check task. The message
\r
185 * just lets the check task know that the task is still functioning correctly.
\r
186 * If a reg test task detects an error it will delete itself, and in so doing
\r
187 * prevent itself from sending any more 'I'm Alive' messages to the check task.
\r
189 static void prvSendImAlive( xQueueHandle xHandle, unsigned long ulTaskNumber );
\r
192 * The check task is created with access to three memory regions (plus its
\r
193 * stack). Each memory region is configured with different parameters and
\r
194 * prvTestMemoryRegions() demonstrates what can and cannot be accessed for each
\r
195 * region. prvTestMemoryRegions() also demonstrates a task that was created
\r
196 * as a privileged task settings its own privilege level down to that of a user
\r
199 static void prvTestMemoryRegions( void );
\r
201 /*-----------------------------------------------------------*/
\r
203 /* The handle of the queue used to communicate between tasks and between tasks
\r
204 and interrupts. Note that this is a file scope variable that falls outside of
\r
205 any MPU region. As such other techniques have to be used to allow the tasks
\r
206 to gain access to the queue. See the comments in the tasks themselves for
\r
207 further information. */
\r
208 static xQueueHandle xFileScopeCheckQueue = NULL;
\r
212 /*-----------------------------------------------------------*/
\r
213 /* Data used by the 'check' task. ---------------------------*/
\r
214 /*-----------------------------------------------------------*/
\r
216 /* Define the constants used to allocate the check task stack. Note that the
\r
217 stack size is defined in words, not bytes. */
\r
218 #define mainCHECK_TASK_STACK_SIZE_WORDS 128
\r
219 #define mainCHECK_TASK_STACK_ALIGNMENT ( mainCHECK_TASK_STACK_SIZE_WORDS * sizeof( portSTACK_TYPE ) )
\r
221 /* Declare the stack that will be used by the check task. The kernel will
\r
222 automatically create an MPU region for the stack. The stack alignment must
\r
223 match its size, so if 128 words are reserved for the stack then it must be
\r
224 aligned to ( 128 * 4 ) bytes. */
\r
225 static portSTACK_TYPE xCheckTaskStack[ mainCHECK_TASK_STACK_SIZE_WORDS ] mainALIGN_TO( mainCHECK_TASK_STACK_ALIGNMENT );
\r
227 /* Declare three arrays - an MPU region will be created for each array
\r
228 using the xTaskParameters structure below. THIS IS JUST TO DEMONSTRATE THE
\r
229 MPU FUNCTIONALITY, the data is not used by the check tasks primary function
\r
230 of monitoring the reg test tasks and printing out status information.
\r
232 Note that the arrays allocate slightly more RAM than is actually assigned to
\r
233 the MPU region. This is to permit writes off the end of the array to be
\r
234 detected even when the arrays are placed in adjacent memory locations (with no
\r
235 gaps between them). The align size must be a power of two. */
\r
236 #define mainREAD_WRITE_ARRAY_SIZE 130
\r
237 #define mainREAD_WRITE_ALIGN_SIZE 128
\r
238 char cReadWriteArray[ mainREAD_WRITE_ARRAY_SIZE ] mainALIGN_TO( mainREAD_WRITE_ALIGN_SIZE );
\r
240 #define mainREAD_ONLY_ARRAY_SIZE 260
\r
241 #define mainREAD_ONLY_ALIGN_SIZE 256
\r
242 char cReadOnlyArray[ mainREAD_ONLY_ARRAY_SIZE ] mainALIGN_TO( mainREAD_ONLY_ALIGN_SIZE );
\r
244 #define mainPRIVILEGED_ONLY_ACCESS_ARRAY_SIZE 130
\r
245 #define mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE 128
\r
246 char cPrivilegedOnlyAccessArray[ mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE ] mainALIGN_TO( mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE );
\r
248 /* Fill in a xTaskParameters structure to define the check task - this is the
\r
249 structure passed to the xTaskCreateRestricted() function. */
\r
250 static const xTaskParameters xCheckTaskParameters =
\r
252 prvCheckTask, /* pvTaskCode - the function that implements the task. */
\r
253 ( signed char * ) "Check", /* pcName */
\r
254 mainCHECK_TASK_STACK_SIZE_WORDS, /* usStackDepth - defined in words, not bytes. */
\r
255 ( void * ) 0x12121212, /* pvParameters - this value is just to test that the parameter is being passed into the task correctly. */
\r
256 ( 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
257 xCheckTaskStack, /* puxStackBuffer - the array to use as the task stack, as declared above. */
\r
259 /* xRegions - In this case the xRegions array is used to create MPU regions
\r
260 for all three of the arrays declared directly above. Each MPU region is
\r
261 created with different parameters. Again, THIS IS JUST TO DEMONSTRATE THE
\r
262 MPU FUNCTIONALITY, the data is not used by the check tasks primary function
\r
263 of monitoring the reg test tasks and printing out status information.*/
\r
265 /* Base address Length Parameters */
\r
266 { cReadWriteArray, mainREAD_WRITE_ALIGN_SIZE, portMPU_REGION_READ_WRITE },
\r
267 { cReadOnlyArray, mainREAD_ONLY_ALIGN_SIZE, portMPU_REGION_READ_ONLY },
\r
268 { cPrivilegedOnlyAccessArray, mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE, portMPU_REGION_PRIVILEGED_READ_WRITE }
\r
272 /* Three MPU regions are defined for use by the 'check' task when the task is
\r
273 created. These are only used to demonstrate the MPU features and are not
\r
274 actually necessary for the check task to fulfill its primary purpose. Instead
\r
275 the MPU regions are replaced with those defined by xAltRegions prior to the
\r
276 check task receiving any data on the queue or printing any messages to the
\r
277 debug console. The MPU region defined below covers the GPIO peripherals used
\r
278 to write to the LCD. */
\r
279 static const xMemoryRegion xAltRegions[ portNUM_CONFIGURABLE_REGIONS ] =
\r
281 /* Base address Length Parameters */
\r
282 { mainGPIO_START_ADDRESS, ( 64 * 1024 ), portMPU_REGION_READ_WRITE },
\r
289 /*-----------------------------------------------------------*/
\r
290 /* Data used by the 'reg test' tasks. -----------------------*/
\r
291 /*-----------------------------------------------------------*/
\r
293 /* Define the constants used to allocate the reg test task stacks. Note that
\r
294 that stack size is defined in words, not bytes. */
\r
295 #define mainREG_TEST_STACK_SIZE_WORDS 128
\r
296 #define mainREG_TEST_STACK_ALIGNMENT ( mainREG_TEST_STACK_SIZE_WORDS * sizeof( portSTACK_TYPE ) )
\r
298 /* Declare the stacks that will be used by the reg test tasks. The kernel will
\r
299 automatically create an MPU region for the stack. The stack alignment must
\r
300 match its size, so if 128 words are reserved for the stack then it must be
\r
301 aligned to ( 128 * 4 ) bytes. */
\r
302 static portSTACK_TYPE xRegTest1Stack[ mainREG_TEST_STACK_SIZE_WORDS ] mainALIGN_TO( mainREG_TEST_STACK_ALIGNMENT );
\r
303 static portSTACK_TYPE xRegTest2Stack[ mainREG_TEST_STACK_SIZE_WORDS ] mainALIGN_TO( mainREG_TEST_STACK_ALIGNMENT );
\r
305 /* Fill in a xTaskParameters structure per reg test task to define the tasks. */
\r
306 static const xTaskParameters xRegTest1Parameters =
\r
308 prvRegTest1Task, /* pvTaskCode - the function that implements the task. */
\r
309 ( signed char * ) "RegTest1", /* pcName */
\r
310 mainREG_TEST_STACK_SIZE_WORDS, /* usStackDepth */
\r
311 ( void * ) 0x12345678, /* pvParameters - this value is just to test that the parameter is being passed into the task correctly. */
\r
312 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
313 xRegTest1Stack, /* puxStackBuffer - the array to use as the task stack, as declared above. */
\r
314 { /* xRegions - this task does not use any non-stack data hence all members are zero. */
\r
315 /* Base address Length Parameters */
\r
316 { 0x00, 0x00, 0x00 },
\r
317 { 0x00, 0x00, 0x00 },
\r
318 { 0x00, 0x00, 0x00 }
\r
321 /*-----------------------------------------------------------*/
\r
323 static xTaskParameters xRegTest2Parameters =
\r
325 prvRegTest2Task, /* pvTaskCode - the function that implements the task. */
\r
326 ( signed char * ) "RegTest2", /* pcName */
\r
327 mainREG_TEST_STACK_SIZE_WORDS, /* usStackDepth */
\r
328 ( void * ) NULL, /* pvParameters - this task uses the parameter to pass in a queue handle, but the queue is not created yet. */
\r
329 tskIDLE_PRIORITY, /* uxPriority */
\r
330 xRegTest2Stack, /* puxStackBuffer - the array to use as the task stack, as declared above. */
\r
331 { /* xRegions - this task does not use any non-stack data hence all members are zero. */
\r
332 /* Base address Length Parameters */
\r
333 { 0x00, 0x00, 0x00 },
\r
334 { 0x00, 0x00, 0x00 },
\r
335 { 0x00, 0x00, 0x00 }
\r
339 /*-----------------------------------------------------------*/
\r
343 prvSetupHardware();
\r
345 /* Create the queue used to pass "I'm alive" messages to the check task. */
\r
346 xFileScopeCheckQueue = xQueueCreate( 1, sizeof( unsigned long ) );
\r
348 /* One check task uses the task parameter to receive the queue handle.
\r
349 This allows the file scope variable to be accessed from within the task.
\r
350 The pvParameters member of xRegTest2Parameters can only be set after the
\r
351 queue has been created so is set here. */
\r
352 xRegTest2Parameters.pvParameters = xFileScopeCheckQueue;
\r
354 /* Create the three test tasks. Handles to the created tasks are not
\r
355 required, hence the second parameter is NULL. */
\r
356 xTaskCreateRestricted( &xRegTest1Parameters, NULL );
\r
357 xTaskCreateRestricted( &xRegTest2Parameters, NULL );
\r
358 xTaskCreateRestricted( &xCheckTaskParameters, NULL );
\r
360 /* Create the tasks that are created using the original xTaskCreate() API
\r
362 xTaskCreate( prvOldStyleUserModeTask, /* The function that implements the task. */
\r
363 ( signed char * ) "Task1", /* Text name for the task. */
\r
364 100, /* Stack depth in words. */
\r
365 NULL, /* Task parameters. */
\r
366 3, /* Priority and mode (user in this case). */
\r
370 xTaskCreate( prvOldStylePrivilegedModeTask, /* The function that implements the task. */
\r
371 ( signed char * ) "Task2", /* Text name for the task. */
\r
372 100, /* Stack depth in words. */
\r
373 NULL, /* Task parameters. */
\r
374 ( 3 | portPRIVILEGE_BIT ), /* Priority and mode. */
\r
378 /* Start the scheduler. */
\r
379 vTaskStartScheduler();
\r
381 /* Will only get here if there was insufficient memory to create the idle
\r
386 /*-----------------------------------------------------------*/
\r
388 static void prvCheckTask( void *pvParameters )
\r
390 /* This task is created in privileged mode so can access the file scope
\r
391 queue variable. Take a stack copy of this before the task is set into user
\r
392 mode. Once that task is in user mode the file scope queue variable will no
\r
393 longer be accessible but the stack copy will. */
\r
394 xQueueHandle xQueue = xFileScopeCheckQueue;
\r
396 unsigned long ulStillAliveCounts[ 2 ] = { 0 };
\r
397 char *pcStatusMessage = "PASS\r\n";
\r
398 unsigned char x = 5, y = 10;
\r
400 /* Just to remove compiler warning. */
\r
401 ( void ) pvParameters;
\r
403 /* Demonstrate how the various memory regions can and can't be accessed.
\r
404 The task privilege is set down to user mode within this function. */
\r
405 prvTestMemoryRegions();
\r
407 /* Change the memory regions allocated to this task to those initially
\r
408 set up for demonstration purposes to those actually required by the task. */
\r
409 vTaskAllocateMPURegions( NULL, xAltRegions );
\r
411 /* This loop performs the main function of the task, which is blocking
\r
412 on a message queue then processing each message as it arrives. */
\r
415 /* Wait for the next message to arrive. */
\r
416 xQueueReceive( xQueue, &lMessage, portMAX_DELAY );
\r
420 case mainREG_TEST_1_STILL_EXECUTING :
\r
421 /* Message from task 1, so task 1 must still be executing. */
\r
422 ( ulStillAliveCounts[ 0 ] )++;
\r
425 case mainREG_TEST_2_STILL_EXECUTING :
\r
426 /* Message from task 2, so task 2 must still be executing. */
\r
427 ( ulStillAliveCounts[ 1 ] )++;
\r
430 case mainPRINT_SYSTEM_STATUS :
\r
431 /* Message from tick hook, time to print out the system
\r
432 status. If messages has stopped arriving from either reg
\r
433 test task then the status must be set to fail. */
\r
434 if( ( ulStillAliveCounts[ 0 ] == 0 ) || ( ulStillAliveCounts[ 1 ] == 0 ) )
\r
436 /* One or both of the test tasks are no longer sending
\r
437 'still alive' messages. */
\r
438 pcStatusMessage = "FAIL\r\n";
\r
441 /* Print a pass/fail message to the LCD - moving the
\r
442 message each time to provide feedback that the output
\r
443 is still being produced. LCD_PrintString() accesses const
\r
444 data stored in flash, which all tasks are at liberty to do,
\r
445 and GPIO for which an MPU region has been set up for it. */
\r
447 LCD_PrintString( x>>1, y>>1, pcStatusMessage, 6, COLOR_RED );
\r
451 /* Reset the count of 'still alive' messages. */
\r
452 memset( ulStillAliveCounts, 0x00, sizeof( ulStillAliveCounts ) );
\r
456 /* Something unexpected happened. Delete this task so the
\r
457 error is apparent (no output will be displayed). */
\r
463 /*-----------------------------------------------------------*/
\r
465 static void prvTestMemoryRegions( void )
\r
470 /* The check task (from which this function is called) is created in the
\r
471 Privileged mode. The privileged array can be both read from and written
\r
472 to while this task is privileged. */
\r
473 cPrivilegedOnlyAccessArray[ 0 ] = 'a';
\r
474 if( cPrivilegedOnlyAccessArray[ 0 ] != 'a' )
\r
476 /* Something unexpected happened. Delete this task so the error is
\r
477 apparent (no output will be displayed). */
\r
481 /* Writing off the end of the RAM allocated to this task will *NOT* cause a
\r
482 protection fault because the task is still executing in a privileged mode.
\r
483 Uncomment the following to test. */
\r
484 /* cPrivilegedOnlyAccessArray[ mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE ] = 'a'; */
\r
486 /* Now set the task into user mode. */
\r
487 portSWITCH_TO_USER_MODE();
\r
489 /* Accessing the privileged only array will now cause a fault. Uncomment
\r
490 the following line to test. */
\r
491 /* cPrivilegedOnlyAccessArray[ 0 ] = 'a'; */
\r
493 /* The read/write array can still be successfully read and written. */
\r
494 for( l = 0; l < mainREAD_WRITE_ALIGN_SIZE; l++ )
\r
496 cReadWriteArray[ l ] = 'a';
\r
497 if( cReadWriteArray[ l ] != 'a' )
\r
499 /* Something unexpected happened. Delete this task so the error is
\r
500 apparent (no output will be displayed). */
\r
505 /* But attempting to read or write off the end of the RAM allocated to this
\r
506 task will cause a fault. Uncomment either of the following two lines to
\r
508 /* cReadWriteArray[ 0 ] = cReadWriteArray[ -1 ]; */
\r
509 /* cReadWriteArray[ mainREAD_WRITE_ALIGN_SIZE ] = 0x00; */
\r
511 /* The read only array can be successfully read... */
\r
512 for( l = 0; l < mainREAD_ONLY_ALIGN_SIZE; l++ )
\r
514 cTemp = cReadOnlyArray[ l ];
\r
517 /* ...but cannot be written. Uncomment the following line to test. */
\r
518 /* cReadOnlyArray[ 0 ] = 'a'; */
\r
520 /* Writing to the first and last locations in the stack array should not
\r
521 cause a protection fault. Note that doing this will cause the kernel to
\r
522 detect a stack overflow if configCHECK_FOR_STACK_OVERFLOW is greater than
\r
524 xCheckTaskStack[ 0 ] = 0;
\r
525 xCheckTaskStack[ mainCHECK_TASK_STACK_SIZE_WORDS - 1 ] = 0;
\r
527 /* Writing off either end of the stack array should cause a protection
\r
528 fault, uncomment either of the following two lines to test. */
\r
529 /* xCheckTaskStack[ -1 ] = 0; */
\r
530 /* xCheckTaskStack[ mainCHECK_TASK_STACK_SIZE_WORDS ] = 0; */
\r
532 /*-----------------------------------------------------------*/
\r
534 static void prvRegTest1Task( void *pvParameters )
\r
536 /* This task is created in privileged mode so can access the file scope
\r
537 queue variable. Take a stack copy of this before the task is set into user
\r
538 mode. Once this task is in user mode the file scope queue variable will no
\r
539 longer be accessible but the stack copy will. */
\r
540 xQueueHandle xQueue = xFileScopeCheckQueue;
\r
542 /* Now the queue handle has been obtained the task can switch to user
\r
543 mode. This is just one method of passing a handle into a protected
\r
544 task, the other reg test task uses the task parameter instead. */
\r
545 portSWITCH_TO_USER_MODE();
\r
547 /* First check that the parameter value is as expected. */
\r
548 if( pvParameters != ( void * ) 0x12345678 )
\r
550 /* Error detected. Delete the task so it stops communicating with
\r
558 /* This task tests the kernel context switch mechanism by reading and
\r
559 writing directly to registers - which requires the test to be written
\r
560 in assembly code. */
\r
563 " MOV R4, #104 \n" /* Set registers to a known value. R0 to R1 are done in the loop below. */
\r
568 " MOV R10, #110 \n"
\r
569 " MOV R11, #111 \n"
\r
571 " MOV R0, #100 \n" /* Set the scratch registers to known values - done inside the loop as they get clobbered. */
\r
575 " MOV R12, #112 \n"
\r
576 " SVC #1 \n" /* Yield just to increase test coverage. */
\r
577 " CMP R0, #100 \n" /* Check all the registers still contain their expected values. */
\r
578 " BNE prvDeleteMe \n" /* Value was not as expected, delete the task so it stops communicating with the check task. */
\r
580 " BNE prvDeleteMe \n"
\r
582 " BNE prvDeleteMe \n"
\r
584 " BNE prvDeleteMe \n"
\r
585 " CMP R4, #104 \n"
\r
586 " BNE prvDeleteMe \n"
\r
588 " BNE prvDeleteMe \n"
\r
590 " BNE prvDeleteMe \n"
\r
592 " BNE prvDeleteMe \n"
\r
594 " BNE prvDeleteMe \n"
\r
595 " CMP R10, #110 \n"
\r
596 " BNE prvDeleteMe \n"
\r
597 " CMP R11, #111 \n"
\r
598 " BNE prvDeleteMe \n"
\r
599 " CMP R12, #112 \n"
\r
600 " BNE prvDeleteMe \n"
\r
601 :::"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r8", "r9", "r10", "r11", "r12"
\r
604 /* Send mainREG_TEST_1_STILL_EXECUTING to the check task to indicate that this
\r
605 task is still functioning. */
\r
606 prvSendImAlive( xQueue, mainREG_TEST_1_STILL_EXECUTING );
\r
608 /* Go back to check all the register values again. */
\r
609 __asm volatile( " B reg1loop " );
\r
612 /*-----------------------------------------------------------*/
\r
614 static void prvRegTest2Task( void *pvParameters )
\r
616 /* The queue handle is passed in as the task parameter. This is one method of
\r
617 passing data into a protected task, the other reg test task uses a different
\r
619 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
623 /* This task tests the kernel context switch mechanism by reading and
\r
624 writing directly to registers - which requires the test to be written
\r
625 in assembly code. */
\r
628 " MOV R4, #4 \n" /* Set registers to a known value. R0 to R1 are done in the loop below. */
\r
631 " MOV R8, #8 \n" /* Frame pointer is omitted as it must not be changed. */
\r
634 " MOV R11, #11 \n"
\r
636 " MOV R0, #13 \n" /* Set the scratch registers to known values - done inside the loop as they get clobbered. */
\r
641 " CMP R0, #13 \n" /* Check all the registers still contain their expected values. */
\r
642 " BNE prvDeleteMe \n" /* Value was not as expected, delete the task so it stops communicating with the check task */
\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
664 " BNE prvDeleteMe \n"
\r
665 :::"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r8", "r9", "r10", "r11", "r12"
\r
668 /* Send mainREG_TEST_2_STILL_EXECUTING to the check task to indicate that this
\r
669 task is still functioning. */
\r
670 prvSendImAlive( xQueue, mainREG_TEST_2_STILL_EXECUTING );
\r
672 /* Go back to check all the register values again. */
\r
673 __asm volatile( " B reg2loop " );
\r
676 /*-----------------------------------------------------------*/
\r
678 void vApplicationIdleHook( void )
\r
680 extern unsigned long __SRAM_segment_end__[];
\r
681 extern unsigned long __privileged_data_start__[];
\r
682 extern unsigned long __privileged_data_end__[];
\r
683 extern unsigned long __FLASH_segment_start__[];
\r
684 extern unsigned long __FLASH_segment_end__[];
\r
685 volatile unsigned long *pul;
\r
686 volatile unsigned long ulReadData;
\r
688 /* The idle task, and therefore this function, run in Supervisor mode and
\r
689 can therefore access all memory. Try reading from corners of flash and
\r
690 RAM to ensure a memory fault does not occur.
\r
692 Start with the edges of the privileged data area. */
\r
693 pul = __privileged_data_start__;
\r
695 pul = __privileged_data_end__ - 1;
\r
698 /* Next the standard SRAM area. */
\r
699 pul = __SRAM_segment_end__ - 1;
\r
702 /* And the standard Flash area - the start of which is marked for
\r
703 privileged access only. */
\r
704 pul = __FLASH_segment_start__;
\r
706 pul = __FLASH_segment_end__ - 1;
\r
709 /* Reading off the end of Flash or SRAM space should cause a fault.
\r
710 Uncomment one of the following two pairs of lines to test. */
\r
712 /* pul = __FLASH_segment_end__ + 4;
\r
713 ulReadData = *pul; */
\r
715 /* pul = __SRAM_segment_end__ + 1;
\r
716 ulReadData = *pul; */
\r
718 /*-----------------------------------------------------------*/
\r
720 static void prvOldStyleUserModeTask( void *pvParameters )
\r
722 extern unsigned long __privileged_data_start__[];
\r
723 extern unsigned long __privileged_data_end__[];
\r
724 extern unsigned long __SRAM_segment_end__[];
\r
725 extern unsigned long __privileged_functions_end__[];
\r
726 extern unsigned long __FLASH_segment_start__[];
\r
727 extern unsigned long __FLASH_segment_end__[];
\r
728 const volatile unsigned long *pulStandardPeripheralRegister = ( volatile unsigned long * ) 0x400FC0C4; /* PCONP */
\r
729 volatile unsigned long *pul;
\r
730 volatile unsigned long ulReadData;
\r
732 /* The following lines are commented out to prevent the unused variable
\r
733 compiler warnings when the tests that use the variable are also commented out.
\r
734 extern unsigned long __privileged_functions_start__[];
\r
735 const volatile unsigned long *pulSystemPeripheralRegister = ( volatile unsigned long * ) 0xe000e014; */
\r
737 ( void ) pvParameters;
\r
739 /* This task is created in User mode using the original xTaskCreate() API
\r
740 function. It should have access to all Flash and RAM except that marked
\r
741 as Privileged access only. Reading from the start and end of the non-
\r
742 privileged RAM should not cause a problem (the privileged RAM is the first
\r
743 block at the bottom of the RAM memory). */
\r
744 pul = __privileged_data_end__ + 1;
\r
746 pul = __SRAM_segment_end__ - 1;
\r
749 /* Likewise reading from the start and end of the non-privileged Flash
\r
750 should not be a problem (the privileged Flash is the first block at the
\r
751 bottom of the Flash memory). */
\r
752 pul = __privileged_functions_end__ + 1;
\r
754 pul = __FLASH_segment_end__ - 1;
\r
757 /* Standard peripherals are accessible. */
\r
758 ulReadData = *pulStandardPeripheralRegister;
\r
760 /* System peripherals are not accessible. Uncomment the following line
\r
761 to test. Also uncomment the declaration of pulSystemPeripheralRegister
\r
762 at the top of this function. */
\r
763 /* ulReadData = *pulSystemPeripheralRegister; */
\r
765 /* Reading from anywhere inside the privileged Flash or RAM should cause a
\r
766 fault. This can be tested by uncommenting any of the following pairs of
\r
767 lines. Also uncomment the declaration of __privileged_functions_start__
\r
768 at the top of this function. */
\r
770 /* pul = __privileged_functions_start__;
\r
771 ulReadData = *pul; */
\r
773 /* pul = __privileged_functions_end__ - 1;
\r
774 ulReadData = *pul; */
\r
776 /* pul = __privileged_data_start__;
\r
777 ulReadData = *pul; */
\r
779 /* pul = __privileged_data_end__ - 1;
\r
780 ulReadData = *pul; */
\r
782 /* Must not just run off the end of a task function, so delete this task.
\r
783 Note that because this task was created using xTaskCreate() the stack was
\r
784 allocated dynamically and I have not included any code to free it again. */
\r
785 vTaskDelete( NULL );
\r
787 /*-----------------------------------------------------------*/
\r
789 static void prvOldStylePrivilegedModeTask( void *pvParameters )
\r
791 extern unsigned long __privileged_data_start__[];
\r
792 extern unsigned long __privileged_data_end__[];
\r
793 extern unsigned long __SRAM_segment_end__[];
\r
794 extern unsigned long __privileged_functions_start__[];
\r
795 extern unsigned long __privileged_functions_end__[];
\r
796 extern unsigned long __FLASH_segment_start__[];
\r
797 extern unsigned long __FLASH_segment_end__[];
\r
798 volatile unsigned long *pul;
\r
799 volatile unsigned long ulReadData;
\r
800 const volatile unsigned long *pulSystemPeripheralRegister = ( volatile unsigned long * ) 0xe000e014; /* Systick */
\r
801 const volatile unsigned long *pulStandardPeripheralRegister = ( volatile unsigned long * ) 0x400FC0C4; /* PCONP */
\r
803 ( void ) pvParameters;
\r
805 /* This task is created in Privileged mode using the original xTaskCreate()
\r
806 API function. It should have access to all Flash and RAM including that
\r
807 marked as Privileged access only. So reading from the start and end of the
\r
808 non-privileged RAM should not cause a problem (the privileged RAM is the
\r
809 first block at the bottom of the RAM memory). */
\r
810 pul = __privileged_data_end__ + 1;
\r
812 pul = __SRAM_segment_end__ - 1;
\r
815 /* Likewise reading from the start and end of the non-privileged Flash
\r
816 should not be a problem (the privileged Flash is the first block at the
\r
817 bottom of the Flash memory). */
\r
818 pul = __privileged_functions_end__ + 1;
\r
820 pul = __FLASH_segment_end__ - 1;
\r
823 /* Reading from anywhere inside the privileged Flash or RAM should also
\r
824 not be a problem. */
\r
825 pul = __privileged_functions_start__;
\r
827 pul = __privileged_functions_end__ - 1;
\r
829 pul = __privileged_data_start__;
\r
830 ulReadData = *pul;
\r
831 pul = __privileged_data_end__ - 1;
\r
834 /* Finally, accessing both System and normal peripherals should both be
\r
836 ulReadData = *pulSystemPeripheralRegister;
\r
837 ulReadData = *pulStandardPeripheralRegister;
\r
839 /* Must not just run off the end of a task function, so delete this task.
\r
840 Note that because this task was created using xTaskCreate() the stack was
\r
841 allocated dynamically and I have not included any code to free it again. */
\r
842 vTaskDelete( NULL );
\r
844 /*-----------------------------------------------------------*/
\r
846 static void prvDeleteMe( void )
\r
848 vTaskDelete( NULL );
\r
850 /*-----------------------------------------------------------*/
\r
852 static void prvSendImAlive( xQueueHandle xHandle, unsigned long ulTaskNumber )
\r
854 if( xHandle != NULL )
\r
856 xQueueSend( xHandle, &ulTaskNumber, mainDONT_BLOCK );
\r
859 /*-----------------------------------------------------------*/
\r
861 void prvSetupHardware( void )
\r
863 /* Disable peripherals power. */
\r
866 /* Enable GPIO power. */
\r
867 SC->PCONP = PCONP_PCGPIO;
\r
869 /* Disable TPIU. */
\r
870 PINCON->PINSEL10 = 0;
\r
872 if ( SC->PLL0STAT & ( 1 << 25 ) )
\r
874 /* Enable PLL, disconnected. */
\r
876 SC->PLL0FEED = PLLFEED_FEED1;
\r
877 SC->PLL0FEED = PLLFEED_FEED2;
\r
880 /* Disable PLL, disconnected. */
\r
882 SC->PLL0FEED = PLLFEED_FEED1;
\r
883 SC->PLL0FEED = PLLFEED_FEED2;
\r
885 /* Enable main OSC. */
\r
887 while( !( SC->SCS & 0x40 ) );
\r
889 /* select main OSC, 12MHz, as the PLL clock source. */
\r
890 SC->CLKSRCSEL = 0x1;
\r
892 SC->PLL0CFG = 0x20031;
\r
893 SC->PLL0FEED = PLLFEED_FEED1;
\r
894 SC->PLL0FEED = PLLFEED_FEED2;
\r
896 /* Enable PLL, disconnected. */
\r
898 SC->PLL0FEED = PLLFEED_FEED1;
\r
899 SC->PLL0FEED = PLLFEED_FEED2;
\r
901 /* Set clock divider. */
\r
902 SC->CCLKCFG = 0x03;
\r
904 /* Configure flash accelerator. */
\r
905 SC->FLASHCFG = 0x403a;
\r
907 /* Check lock bit status. */
\r
908 while( ( ( SC->PLL0STAT & ( 1 << 26 ) ) == 0 ) );
\r
910 /* Enable and connect. */
\r
912 SC->PLL0FEED = PLLFEED_FEED1;
\r
913 SC->PLL0FEED = PLLFEED_FEED2;
\r
914 while( ( ( SC->PLL0STAT & ( 1 << 25 ) ) == 0 ) );
\r
919 /* Configure the clock for the USB. */
\r
921 if( SC->PLL1STAT & ( 1 << 9 ) )
\r
923 /* Enable PLL, disconnected. */
\r
925 SC->PLL1FEED = PLLFEED_FEED1;
\r
926 SC->PLL1FEED = PLLFEED_FEED2;
\r
929 /* Disable PLL, disconnected. */
\r
931 SC->PLL1FEED = PLLFEED_FEED1;
\r
932 SC->PLL1FEED = PLLFEED_FEED2;
\r
934 SC->PLL1CFG = 0x23;
\r
935 SC->PLL1FEED = PLLFEED_FEED1;
\r
936 SC->PLL1FEED = PLLFEED_FEED2;
\r
938 /* Enable PLL, disconnected. */
\r
940 SC->PLL1FEED = PLLFEED_FEED1;
\r
941 SC->PLL1FEED = PLLFEED_FEED2;
\r
942 while( ( ( SC->PLL1STAT & ( 1 << 10 ) ) == 0 ) );
\r
944 /* Enable and connect. */
\r
946 SC->PLL1FEED = PLLFEED_FEED1;
\r
947 SC->PLL1FEED = PLLFEED_FEED2;
\r
948 while( ( ( SC->PLL1STAT & ( 1 << 9 ) ) == 0 ) );
\r
950 /* Setup the peripheral bus to be the same as the PLL output (64 MHz). */
\r
951 SC->PCLKSEL0 = 0x05555555;
\r
953 /* Prepare the LCD. */
\r
954 LCDdriver_initialisation();
\r
955 LCD_PrintString( 5, 10, "FreeRTOS.org", 14, COLOR_GREEN);
\r
957 /*-----------------------------------------------------------*/
\r
959 void vApplicationTickHook( void )
\r
961 static unsigned long ulCallCount;
\r
962 const unsigned long ulCallsBetweenSends = 5000 / portTICK_RATE_MS;
\r
963 const unsigned long ulMessage = mainPRINT_SYSTEM_STATUS;
\r
964 portBASE_TYPE xDummy;
\r
966 /* If configUSE_TICK_HOOK is set to 1 then this function will get called
\r
967 from each RTOS tick. It is called from the tick interrupt and therefore
\r
968 will be executing in the privileged state. */
\r
972 /* Is it time to print out the pass/fail message again? */
\r
973 if( ulCallCount >= ulCallsBetweenSends )
\r
977 /* Send a message to the check task to command it to check that all
\r
978 the tasks are still running then print out the status.
\r
980 This is running in an ISR so has to use the "FromISR" version of
\r
981 xQueueSend(). Because it is in an ISR it is running with privileges
\r
982 so can access xFileScopeCheckQueue directly. */
\r
983 xQueueSendFromISR( xFileScopeCheckQueue, &ulMessage, &xDummy );
\r
986 /*-----------------------------------------------------------*/
\r
988 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )
\r
990 /* If configCHECK_FOR_STACK_OVERFLOW is set to either 1 or 2 then this
\r
991 function will automatically get called if a task overflows its stack. */
\r
993 ( void ) pcTaskName;
\r
996 /*-----------------------------------------------------------*/
\r
998 void vApplicationMallocFailedHook( void )
\r
1000 /* If configUSE_MALLOC_FAILED_HOOK is set to 1 then this function will
\r
1001 be called automatically if a call to pvPortMalloc() fails. pvPortMalloc()
\r
1002 is called automatically when a task, queue or semaphore is created. */
\r
1005 /*-----------------------------------------------------------*/
\r