]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_MPU_LM3Sxxxx_Rowley/main.c
Add a few more tests and demos.
[freertos] / Demo / CORTEX_MPU_LM3Sxxxx_Rowley / main.c
1 /*\r
2     FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4     This file is part of the FreeRTOS distribution.\r
5 \r
6     FreeRTOS is free software; you can redistribute it and/or modify it    under\r
7     the terms of the GNU General Public License (version 2) as published by the\r
8     Free Software Foundation and modified by the FreeRTOS exception.\r
9     **NOTE** The exception to the GPL is included to allow you to distribute a\r
10     combined work that includes FreeRTOS without being obliged to provide the\r
11     source code for proprietary components outside of the FreeRTOS kernel.\r
12     Alternative commercial license and support terms are also available upon\r
13     request.  See the licensing section of http://www.FreeRTOS.org for full\r
14     license details.\r
15 \r
16     FreeRTOS is distributed in the hope that it will be useful,    but WITHOUT\r
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
19     more details.\r
20 \r
21     You should have received a copy of the GNU General Public License along\r
22     with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59\r
23     Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
24 \r
25 \r
26     ***************************************************************************\r
27     *                                                                         *\r
28     * The FreeRTOS eBook and reference manual are available to purchase for a *\r
29     * small fee. Help yourself get started quickly while also helping the     *\r
30     * FreeRTOS project! See http://www.FreeRTOS.org/Documentation for details *\r
31     *                                                                         *\r
32     ***************************************************************************\r
33 \r
34     1 tab == 4 spaces!\r
35 \r
36     Please ensure to read the configuration and relevant port sections of the\r
37     online documentation.\r
38 \r
39     http://www.FreeRTOS.org - Documentation, latest information, license and\r
40     contact details.\r
41 \r
42     http://www.SafeRTOS.com - A version that is certified for use in safety\r
43     critical systems.\r
44 \r
45     http://www.OpenRTOS.com - Commercial support, development, porting,\r
46     licensing and training services.\r
47 */\r
48 \r
49 \r
50 /*\r
51  * This file demonstrates the use of FreeRTOS-MPU.  It creates tasks in both\r
52  * User mode and Privileged mode, and using both the original xTaskCreate() and\r
53  * the new xTaskCreateRestricted() API functions.  The purpose of each created\r
54  * task is documented in the comments above the task function prototype (in\r
55  * this file), with the task behaviour demonstrated and documented within the \r
56  * task function itself.  In addition a queue is used to demonstrate passing\r
57  * data between protected/restricted tasks as well as passing data between an\r
58  * interrupt and a protected/restricted task.\r
59  */\r
60 \r
61 \r
62 \r
63 /* Standard includes. */\r
64 #include <string.h>\r
65 #include <__cross_studio_io.h>\r
66 \r
67 /* Scheduler includes. */\r
68 #include "FreeRTOS.h"\r
69 #include "task.h"\r
70 #include "queue.h"\r
71 #include "semphr.h"\r
72 \r
73 /* Hardware library includes. */\r
74 #include "hw_types.h"\r
75 #include "hw_sysctl.h"\r
76 #include "sysctl.h"\r
77 \r
78 /*-----------------------------------------------------------*/\r
79 \r
80 /* Misc constants. */\r
81 #define mainDONT_BLOCK                                  ( 0 )\r
82 \r
83 /* Definitions for the messages that can be sent to the check task. */\r
84 #define mainREG_TEST_1_STILL_EXECUTING  ( 0 )\r
85 #define mainREG_TEST_2_STILL_EXECUTING  ( 1 )\r
86 #define mainPRINT_SYSTEM_STATUS                 ( 2 )\r
87 \r
88 /* GCC specifics. */\r
89 #define mainALIGN_TO( x )                               __attribute__((aligned(x)))\r
90 \r
91 \r
92 /*-----------------------------------------------------------*/\r
93 /* Prototypes for functions that implement tasks. -----------*/\r
94 /*-----------------------------------------------------------*/\r
95 \r
96 /* \r
97  * Prototype for the reg test tasks.  Amongst other things, these fill the CPU\r
98  * registers with known values before checking that the registers still contain\r
99  * the expected values.  Each of the two tasks use different values so an error\r
100  * in the context switch mechanism can be caught.  Both reg test tasks execute\r
101  * at the idle priority so will get preempted regularly.  Each task repeatedly\r
102  * sends a message on a queue so long as it remains functioning correctly.  If\r
103  * an error is detected within the task the task is simply deleted.\r
104  */\r
105 static void prvRegTest1Task( void *pvParameters );\r
106 static void prvRegTest2Task( void *pvParameters );\r
107 \r
108 /*\r
109  * Prototype for the check task.  The check task demonstrates various features\r
110  * of the MPU before entering a loop where it waits for messages to arrive on a\r
111  * queue.\r
112  *\r
113  * Two types of messages can be processes:\r
114  *\r
115  * 1) "I'm Alive" messages sent from the reg test tasks, indicating that the\r
116  *    task is still operational.\r
117  *\r
118  * 2) "Print Status commands" sent periodically by the tick hook function (and\r
119  *    therefore from within an interrupt) which command the check task to write\r
120  *    either pass or fail to the terminal, depending on the status of the reg\r
121  *    test tasks.\r
122  */\r
123 static void prvCheckTask( void *pvParameters );\r
124 \r
125 /*\r
126  * Prototype for a task created in User mode using the original vTaskCreate() \r
127  * API function.  The task demonstrates the characteristics of such a task,\r
128  * before simply deleting itself.\r
129  */\r
130 static void prvOldStyleUserModeTask( void *pvParameters );\r
131 \r
132 /*\r
133  * Prototype for a task created in Privileged mode using the original \r
134  * vTaskCreate() API function.  The task demonstrates the characteristics of \r
135  * such a task, before simply deleting itself.\r
136  */\r
137 static void prvOldStylePrivilegedModeTask( void *pvParameters );\r
138 \r
139 \r
140 /*-----------------------------------------------------------*/\r
141 /* Prototypes for other misc functions.  --------------------*/\r
142 /*-----------------------------------------------------------*/\r
143 \r
144 /*\r
145  * Just configures any clocks and IO necessary.\r
146  */\r
147 static void prvSetupHardware( void );\r
148 \r
149 /*\r
150  * Simply deletes the calling task.  The function is provided only because it\r
151  * is simpler to call from asm code than the normal vTaskDelete() API function.\r
152  * It has the noinline attribute because it is called from asm code.\r
153  */\r
154 static void prvDeleteMe( void ) __attribute__((noinline));\r
155 \r
156 /*\r
157  * Used by both reg test tasks to send messages to the check task.  The message\r
158  * just lets the check task know that the task is still functioning correctly.\r
159  * If a reg test task detects an error it will delete itself, and in so doing\r
160  * prevent itself from sending any more 'I'm Alive' messages to the check task.\r
161  */\r
162 static void prvSendImAlive( xQueueHandle xHandle, unsigned long ulTaskNumber );\r
163 \r
164 /*\r
165  * The check task is created with access to three memory regions (plus its\r
166  * stack).  Each memory region is configured with different parameters and\r
167  * prvTestMemoryRegions() demonstrates what can and cannot be accessed for each\r
168  * region.  prvTestMemoryRegions() also demonstrates a task that was created\r
169  * as a privileged task settings its own privilege level down to that of a user\r
170  * task.\r
171  */\r
172 static void prvTestMemoryRegions( void );\r
173 \r
174 /*-----------------------------------------------------------*/\r
175 \r
176 /* The handle of the queue used to communicate between tasks and between tasks\r
177 and interrupts.  Note that this is a file scope variable that falls outside of\r
178 any MPU region.  As such other techniques have to be used to allow the tasks\r
179 to gain access to the queue.  See the comments in the tasks themselves for \r
180 further information. */\r
181 static xQueueHandle xFileScopeCheckQueue = NULL;\r
182 \r
183 \r
184 /*-----------------------------------------------------------*/\r
185 /* Data used by the 'check' task. ---------------------------*/\r
186 /*-----------------------------------------------------------*/\r
187 \r
188 /* Define the constants used to allocate the check task stack.  Note that the\r
189 stack size is defined in words, not bytes. */\r
190 #define mainCHECK_TASK_STACK_SIZE_WORDS 128\r
191 #define mainCHECK_TASK_STACK_ALIGNMENT ( mainCHECK_TASK_STACK_SIZE_WORDS * sizeof( portSTACK_TYPE ) )\r
192 \r
193 /* Declare the stack that will be used by the check task.  The kernel will\r
194  automatically create an MPU region for the stack.  The stack alignment must \r
195  match its size, so if 128 words are reserved for the stack then it must be \r
196  aligned to ( 128 * 4 ) bytes. */\r
197 static portSTACK_TYPE xCheckTaskStack[ mainCHECK_TASK_STACK_SIZE_WORDS ] mainALIGN_TO( mainCHECK_TASK_STACK_ALIGNMENT );\r
198 \r
199 /* Declare three arrays - an MPU region will be created for each array\r
200 using the xTaskParameters structure below.  THIS IS JUST TO DEMONSTRATE THE\r
201 MPU FUNCTIONALITY, the data is not used by the check tasks primary function\r
202 of monitoring the reg test tasks and printing out status information.\r
203 \r
204 Note that the arrays allocate slightly more RAM than is actually assigned to \r
205 the MPU region.  This is to permit writes off the end of the array to be \r
206 detected even when the arrays are placed in adjacent memory locations (with no \r
207 gaps between them).  The align size must be a power of two. */\r
208 #define mainREAD_WRITE_ARRAY_SIZE 130\r
209 #define mainREAD_WRITE_ALIGN_SIZE 128\r
210 char cReadWriteArray[ mainREAD_WRITE_ARRAY_SIZE ] mainALIGN_TO( mainREAD_WRITE_ALIGN_SIZE );\r
211 \r
212 #define mainREAD_ONLY_ARRAY_SIZE 260\r
213 #define mainREAD_ONLY_ALIGN_SIZE 256\r
214 char cReadOnlyArray[ mainREAD_ONLY_ARRAY_SIZE ] mainALIGN_TO( mainREAD_ONLY_ALIGN_SIZE );\r
215 \r
216 #define mainPRIVILEGED_ONLY_ACCESS_ARRAY_SIZE 130\r
217 #define mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE 128\r
218 char cPrivilegedOnlyAccessArray[ mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE ] mainALIGN_TO( mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE );\r
219 \r
220 /* Fill in a xTaskParameters structure to define the check task - this is the\r
221 structure passed to the xTaskCreateRestricted() function. */\r
222 static const xTaskParameters xCheckTaskParameters =\r
223 {\r
224         prvCheckTask,                                                           /* pvTaskCode - the function that implements the task. */\r
225         ( signed char * ) "Check",                                      /* pcName                       */\r
226         mainCHECK_TASK_STACK_SIZE_WORDS,                        /* usStackDepth - defined in words, not bytes. */\r
227         ( void * ) 0x12121212,                                          /* pvParameters - this value is just to test that the parameter is being passed into the task correctly. */\r
228         ( 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
229         xCheckTaskStack,                                                        /* puxStackBuffer - the array to use as the task stack, as declared above. */\r
230 \r
231         /* xRegions - In this case the xRegions array is used to create MPU regions\r
232         for all three of the arrays declared directly above.  Each MPU region is\r
233         created with different parameters.  Again, THIS IS JUST TO DEMONSTRATE THE\r
234         MPU FUNCTIONALITY, the data is not used by the check tasks primary function\r
235         of monitoring the reg test tasks and printing out status information.*/\r
236         {                                                                                       \r
237                 /* Base address                                 Length                                                                  Parameters */\r
238         { cReadWriteArray,                              mainREAD_WRITE_ALIGN_SIZE,                              portMPU_REGION_READ_WRITE },\r
239         { cReadOnlyArray,                               mainREAD_ONLY_ALIGN_SIZE,                               portMPU_REGION_READ_ONLY },\r
240         { cPrivilegedOnlyAccessArray,   mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE,  portMPU_REGION_PRIVILEGED_READ_WRITE }\r
241         }\r
242 };\r
243 \r
244 /* Three MPU regions are defined for use by the 'check' task when the task is \r
245 created.  These are only used to demonstrate the MPU features and are not\r
246 actually necessary for the check task to fulfill its primary purpose.  Instead\r
247 the MPU regions are replaced with those defined by xAltRegions prior to the \r
248 check task receiving any data on the queue or printing any messages to the\r
249 debug console.  The region configured by xAltRegions just gives the check task\r
250 access to the debug variables that form part of the Rowley library, and are\r
251 accessed within the debug_printf() function. */\r
252 extern unsigned long dbgCntrlWord_mempoll;\r
253 static const xMemoryRegion xAltRegions[ portNUM_CONFIGURABLE_REGIONS ] =\r
254 {                                                                                       \r
255         /* Base address                                         Length          Parameters */\r
256         { ( void * ) &dbgCntrlWord_mempoll,     32,                     portMPU_REGION_READ_WRITE },\r
257         { 0,                                                            0,                      0 },\r
258         { 0,                                                            0,                      0 }\r
259 };\r
260 \r
261 \r
262 \r
263 /*-----------------------------------------------------------*/\r
264 /* Data used by the 'reg test' tasks. -----------------------*/\r
265 /*-----------------------------------------------------------*/\r
266 \r
267 /* Define the constants used to allocate the reg test task stacks.  Note that\r
268 that stack size is defined in words, not bytes. */\r
269 #define mainREG_TEST_STACK_SIZE_WORDS   128\r
270 #define mainREG_TEST_STACK_ALIGNMENT    ( mainREG_TEST_STACK_SIZE_WORDS * sizeof( portSTACK_TYPE ) )\r
271 \r
272 /* Declare the stacks that will be used by the reg test tasks.  The kernel will\r
273 automatically create an MPU region for the stack.  The stack alignment must \r
274 match its size, so if 128 words are reserved for the stack then it must be \r
275 aligned to ( 128 * 4 ) bytes. */\r
276 static portSTACK_TYPE xRegTest1Stack[ mainREG_TEST_STACK_SIZE_WORDS ] mainALIGN_TO( mainREG_TEST_STACK_ALIGNMENT );\r
277 static portSTACK_TYPE xRegTest2Stack[ mainREG_TEST_STACK_SIZE_WORDS ] mainALIGN_TO( mainREG_TEST_STACK_ALIGNMENT );\r
278 \r
279 /* Fill in a xTaskParameters structure per reg test task to define the tasks. */\r
280 static const xTaskParameters xRegTest1Parameters =\r
281 {\r
282         prvRegTest1Task,                                                /* pvTaskCode - the function that implements the task. */\r
283         ( signed char * ) "RegTest1",                   /* pcName                       */\r
284         mainREG_TEST_STACK_SIZE_WORDS,                  /* usStackDepth         */\r
285         ( void * ) 0x12345678,                                  /* pvParameters - this value is just to test that the parameter is being passed into the task correctly. */\r
286         tskIDLE_PRIORITY | portPRIVILEGE_BIT,   /* uxPriority - note that this task is created with privileges to demonstrate one method of passing a queue handle into the task. */\r
287         xRegTest1Stack,                                                 /* puxStackBuffer - the array to use as the task stack, as declared above. */\r
288         {                                                                               /* xRegions - this task does not use any non-stack data hence all members are zero. */\r
289                 /* Base address         Length          Parameters */\r
290         { 0x00,                         0x00,                   0x00 },\r
291         { 0x00,                         0x00,                   0x00 },\r
292         { 0x00,                         0x00,                   0x00 }\r
293         }\r
294 };\r
295 /*-----------------------------------------------------------*/\r
296 \r
297 static xTaskParameters xRegTest2Parameters =\r
298 {\r
299         prvRegTest2Task,                                /* pvTaskCode - the function that implements the task. */\r
300         ( signed char * ) "RegTest2",   /* pcName                       */\r
301         mainREG_TEST_STACK_SIZE_WORDS,  /* usStackDepth         */\r
302         ( void * ) NULL,                                /* pvParameters - this task uses the parameter to pass in a queue handle, but the queue is not created yet. */\r
303         tskIDLE_PRIORITY,                               /* uxPriority           */\r
304         xRegTest2Stack,                                 /* puxStackBuffer - the array to use as the task stack, as declared above. */\r
305         {                                                               /* xRegions - this task does not use any non-stack data hence all members are zero. */\r
306                 /* Base address         Length          Parameters */\r
307         { 0x00,                         0x00,                   0x00 },\r
308         { 0x00,                         0x00,                   0x00 },\r
309         { 0x00,                         0x00,                   0x00 }\r
310         }\r
311 };\r
312 \r
313 /*-----------------------------------------------------------*/\r
314 \r
315 int main( void )\r
316 {\r
317         prvSetupHardware();\r
318 \r
319         /* Create the queue used to pass "I'm alive" messages to the check task. */\r
320         xFileScopeCheckQueue = xQueueCreate( 1, sizeof( unsigned long ) );\r
321 \r
322         /* One check task uses the task parameter to receive the queue handle.\r
323         This allows the file scope variable to be accessed from within the task.\r
324         The pvParameters member of xRegTest2Parameters can only be set after the\r
325         queue has been created so is set here. */\r
326         xRegTest2Parameters.pvParameters = xFileScopeCheckQueue;\r
327 \r
328         /* Create the three test tasks.  Handles to the created tasks are not\r
329         required, hence the second parameter is NULL. */\r
330         xTaskCreateRestricted( &xRegTest1Parameters, NULL );\r
331     xTaskCreateRestricted( &xRegTest2Parameters, NULL );\r
332         xTaskCreateRestricted( &xCheckTaskParameters, NULL );\r
333 \r
334         /* Create the tasks that are created using the original xTaskCreate() API\r
335         function. */\r
336         xTaskCreate(    prvOldStyleUserModeTask,        /* The function that implements the task. */\r
337                                         ( signed char * ) "Task1",      /* Text name for the task. */\r
338                                         100,                                            /* Stack depth in words. */\r
339                                         NULL,                                           /* Task parameters. */\r
340                                         3,                                                      /* Priority and mode (user in this case). */\r
341                                         NULL                                            /* Handle. */\r
342                                 );\r
343 \r
344         xTaskCreate(    prvOldStylePrivilegedModeTask,  /* The function that implements the task. */\r
345                                         ( signed char * ) "Task2",              /* Text name for the task. */\r
346                                         100,                                                    /* Stack depth in words. */\r
347                                         NULL,                                                   /* Task parameters. */\r
348                                         ( 3 | portPRIVILEGE_BIT ),              /* Priority and mode. */\r
349                                         NULL                                                    /* Handle. */\r
350                                 );\r
351 \r
352         /* Start the scheduler. */\r
353         vTaskStartScheduler();\r
354 \r
355         /* Will only get here if there was insufficient memory to create the idle\r
356         task. */\r
357         for( ;; );\r
358         return 0;\r
359 }\r
360 /*-----------------------------------------------------------*/\r
361 \r
362 static void prvCheckTask( void *pvParameters )\r
363 {\r
364 /* This task is created in privileged mode so can access the file scope\r
365 queue variable.  Take a stack copy of this before the task is set into user\r
366 mode.  Once that task is in user mode the file scope queue variable will no\r
367 longer be accessible but the stack copy will. */\r
368 xQueueHandle xQueue = xFileScopeCheckQueue;\r
369 long lMessage;\r
370 unsigned long ulStillAliveCounts[ 2 ] = { 0 };\r
371 const char *pcStatusMessage = "PASS\r\n";\r
372 \r
373         /* Just to remove compiler warning. */\r
374         ( void ) pvParameters;\r
375 \r
376         /* Print out the amount of free heap space so configTOTAL_HEAP_SIZE can be\r
377         tuned.  The heap size is set to be very small in this example and will need\r
378         to be increased before many more tasks, queues or semaphores can be \r
379         created. */\r
380         debug_printf( "There are %d bytes of unused heap space.\r\n", xPortGetFreeHeapSize() );\r
381 \r
382         /* Demonstrate how the various memory regions can and can't be accessed. \r
383         The task privilege level is set down to user mode within this function. */\r
384         prvTestMemoryRegions();\r
385 \r
386         /* Change the memory regions allocated to this task to those initially\r
387         set up for demonstration purposes to those actually required by the task. */\r
388         vTaskAllocateMPURegions( NULL, xAltRegions );\r
389 \r
390         /* This loop performs the main function of the task, which is blocking\r
391         on a message queue then processing each message as it arrives. */\r
392         for( ;; )\r
393         {\r
394                 /* Wait for the next message to arrive. */\r
395                 xQueueReceive( xQueue, &lMessage, portMAX_DELAY );\r
396                 \r
397                 switch( lMessage )\r
398                 {\r
399                         case mainREG_TEST_1_STILL_EXECUTING     :       \r
400                                         /* Message from task 1, so task 1 must still be executing. */\r
401                                         ( ulStillAliveCounts[ 0 ] )++;\r
402                                         break;\r
403 \r
404                         case mainREG_TEST_2_STILL_EXECUTING     :                                               \r
405                                         /* Message from task 2, so task 2 must still be executing. */\r
406                                         ( ulStillAliveCounts[ 1 ] )++;\r
407                                         break;\r
408 \r
409                         case mainPRINT_SYSTEM_STATUS            :       \r
410                                         /* Message from tick hook, time to print out the system\r
411                                         status.  If messages has stopped arriving from either reg\r
412                                         test task then the status must be set to fail. */\r
413                                         if( ( ulStillAliveCounts[ 0 ] == 0 ) || ( ulStillAliveCounts[ 1 ] == 0 )  )\r
414                                         {\r
415                                                 /* One or both of the test tasks are no longer sending \r
416                                                 'still alive' messages. */\r
417                                                 pcStatusMessage = "FAIL\r\n";\r
418                                         }\r
419 \r
420                                         /* Print a pass/fail message to the terminal.  This will be\r
421                                         visible in the CrossWorks IDE. */\r
422                                         debug_printf( pcStatusMessage );\r
423 \r
424                                         /* Reset the count of 'still alive' messages. */\r
425                                         memset( ulStillAliveCounts, 0x00, sizeof( ulStillAliveCounts ) );\r
426                                         break;\r
427 \r
428                 default :\r
429                                         /* Something unexpected happened.  Delete this task so the \r
430                                         error is apparent (no output will be displayed). */\r
431                                         prvDeleteMe();\r
432                                         break;\r
433                 }\r
434         }\r
435 }\r
436 /*-----------------------------------------------------------*/\r
437 \r
438 static void prvTestMemoryRegions( void )\r
439 {\r
440 long l;\r
441 char cTemp;\r
442 \r
443         /* The check task (from which this function is called) is created in the \r
444         Privileged mode.  The privileged array can be both read from and written \r
445         to while this task is privileged. */\r
446         cPrivilegedOnlyAccessArray[ 0 ] = 'a';\r
447         if( cPrivilegedOnlyAccessArray[ 0 ] != 'a' )\r
448         {\r
449                 /* Something unexpected happened.  Delete this task so the error is\r
450                 apparent (no output will be displayed). */\r
451                 prvDeleteMe();\r
452         }\r
453 \r
454         /* Writing off the end of the RAM allocated to this task will *NOT* cause a\r
455         protection fault because the task is still executing in a privileged mode.  \r
456         Uncomment the following to test. */\r
457         /*cPrivilegedOnlyAccessArray[ mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE ] = 'a';*/\r
458 \r
459         /* Now set the task into user mode. */\r
460         portSWITCH_TO_USER_MODE();\r
461          \r
462         /* Accessing the privileged only array will now cause a fault.  Uncomment \r
463         the following line to test. */    \r
464         /*cPrivilegedOnlyAccessArray[ 0 ] = 'a';*/\r
465 \r
466         /* The read/write array can still be successfully read and written. */\r
467         for( l = 0; l < mainREAD_WRITE_ALIGN_SIZE; l++ )\r
468         {\r
469                 cReadWriteArray[ l ] = 'a';\r
470                 if( cReadWriteArray[ l ] != 'a' )\r
471                 {\r
472                         /* Something unexpected happened.  Delete this task so the error is\r
473                         apparent (no output will be displayed). */\r
474                         prvDeleteMe();\r
475                 }\r
476         }\r
477 \r
478         /* But attempting to read or write off the end of the RAM allocated to this\r
479         task will cause a fault.  Uncomment either of the following two lines to \r
480         test. */\r
481         /* cReadWriteArray[ 0 ] = cReadWriteArray[ -1 ]; */\r
482         /* cReadWriteArray[ mainREAD_WRITE_ALIGN_SIZE ] = 0x00; */\r
483 \r
484         /* The read only array can be successfully read... */\r
485         for( l = 0; l < mainREAD_ONLY_ALIGN_SIZE; l++ )\r
486         {\r
487                 cTemp = cReadOnlyArray[ l ];\r
488         }\r
489 \r
490         /* ...but cannot be written.  Uncomment the following line to test. */\r
491         /* cReadOnlyArray[ 0 ] = 'a'; */\r
492 \r
493         /* Writing to the first and last locations in the stack array should not \r
494         cause a protection fault.  Note that doing this will cause the kernel to\r
495         detect a stack overflow if configCHECK_FOR_STACK_OVERFLOW is greater than \r
496         1. */\r
497     xCheckTaskStack[ 0 ] = 0;\r
498     xCheckTaskStack[ mainCHECK_TASK_STACK_SIZE_WORDS - 1 ] = 0;\r
499 \r
500         /* Writing off either end of the stack array should cause a protection \r
501         fault, uncomment either of the following two lines to test. */\r
502         /* xCheckTaskStack[ -1 ] = 0; */\r
503     /* xCheckTaskStack[ mainCHECK_TASK_STACK_SIZE_WORDS ] = 0; */\r
504 }\r
505 /*-----------------------------------------------------------*/\r
506 \r
507 static void prvRegTest1Task( void *pvParameters )\r
508 {\r
509 /* This task is created in privileged mode so can access the file scope\r
510 queue variable.  Take a stack copy of this before the task is set into user\r
511 mode.  Once this task is in user mode the file scope queue variable will no\r
512 longer be accessible but the stack copy will. */\r
513 xQueueHandle xQueue = xFileScopeCheckQueue;\r
514 \r
515         /* Now the queue handle has been obtained the task can switch to user \r
516         mode.  This is just one method of passing a handle into a protected\r
517         task, the other reg test task uses the task parameter instead. */\r
518     portSWITCH_TO_USER_MODE();\r
519 \r
520         /* First check that the parameter value is as expected. */\r
521         if( pvParameters != ( void * ) 0x12345678 )\r
522         {\r
523                 /* Error detected.  Delete the task so it stops communicating with\r
524                 the check task. */\r
525                 prvDeleteMe();\r
526         }\r
527 \r
528 \r
529         for( ;; )\r
530         {               \r
531                 /* This task tests the kernel context switch mechanism by reading and\r
532                 writing directly to registers - which requires the test to be written\r
533                 in assembly code. */\r
534                 __asm volatile \r
535                 (       \r
536                         "               MOV     R4, #104                        \n" /* Set registers to a known value.  R0 to R1 are done in the loop below. */\r
537                         "               MOV     R5, #105                        \n"\r
538                         "               MOV     R6, #106                        \n"\r
539                         "               MOV     R8, #108                        \n"\r
540                         "               MOV     R9, #109                        \n"\r
541                         "               MOV     R10, #110                       \n"\r
542                         "               MOV     R11, #111                       \n"\r
543                         "reg1loop:                                              \n"\r
544                         "               MOV     R0, #100                        \n" /* Set the scratch registers to known values - done inside the loop as they get clobbered. */\r
545                         "               MOV     R1, #101                        \n"\r
546                         "               MOV     R2, #102                        \n"\r
547                         "               MOV R3, #103                    \n"\r
548                         "               MOV     R12, #112                       \n"\r
549                         "               SVC #1                                  \n" /* Yield just to increase test coverage. */\r
550                         "               CMP     R0, #100                        \n" /* Check all the registers still contain their expected values. */\r
551                         "               BNE     prvDeleteMe                     \n" /* Value was not as expected, delete the task so it stops communicating with the check task. */\r
552                         "               CMP     R1, #101                        \n"\r
553                         "               BNE     prvDeleteMe                     \n"\r
554                         "               CMP     R2, #102                        \n"\r
555                         "               BNE     prvDeleteMe                     \n"\r
556                         "               CMP R3, #103                    \n"\r
557                         "               BNE     prvDeleteMe                     \n"\r
558                         "               CMP     R4, #104                        \n" \r
559                         "               BNE     prvDeleteMe                     \n" \r
560                         "               CMP     R5, #105                        \n"\r
561                         "               BNE     prvDeleteMe                     \n"\r
562                         "               CMP     R6, #106                        \n"\r
563                         "               BNE     prvDeleteMe                     \n"\r
564                         "               CMP     R8, #108                        \n"\r
565                         "               BNE     prvDeleteMe                     \n"\r
566                         "               CMP     R9, #109                        \n"\r
567                         "               BNE     prvDeleteMe                     \n"\r
568                         "               CMP     R10, #110                       \n"\r
569                         "               BNE     prvDeleteMe                     \n"\r
570                         "               CMP     R11, #111                       \n"\r
571                         "               BNE     prvDeleteMe                     \n"\r
572                         "               CMP     R12, #112                       \n"\r
573                         "               BNE     prvDeleteMe                     \n"\r
574                         :::"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r8", "r9", "r10", "r11", "r12"\r
575                 );\r
576 \r
577                 /* Send mainREG_TEST_1_STILL_EXECUTING to the check task to indicate that this \r
578                 task is still functioning. */\r
579                 prvSendImAlive( xQueue, mainREG_TEST_1_STILL_EXECUTING );\r
580 \r
581                 /* Go back to check all the register values again. */\r
582                 __asm volatile( "               B reg1loop      " );\r
583         }\r
584 }\r
585 /*-----------------------------------------------------------*/\r
586 \r
587 static void prvRegTest2Task( void *pvParameters )\r
588 {\r
589 /* The queue handle is passed in as the task parameter.  This is one method of\r
590 passing data into a protected task, the other reg test task uses a different \r
591 method. */\r
592 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;\r
593 \r
594         for( ;; )\r
595         {\r
596                 /* This task tests the kernel context switch mechanism by reading and\r
597                 writing directly to registers - which requires the test to be written\r
598                 in assembly code. */\r
599                 __asm volatile \r
600                 (       \r
601                         "               MOV     R4, #4                          \n" /* Set registers to a known value.  R0 to R1 are done in the loop below. */\r
602                         "               MOV     R5, #5                          \n"\r
603                         "               MOV     R6, #6                          \n"\r
604                         "               MOV     R8, #8                          \n" /* Frame pointer is omitted as it must not be changed. */\r
605                         "               MOV     R9, #9                          \n"\r
606                         "               MOV     R10, 10                         \n"\r
607                         "               MOV     R11, #11                        \n"                                             \r
608                         "reg2loop:                                              \n"\r
609                         "               MOV     R0, #13                         \n" /* Set the scratch registers to known values - done inside the loop as they get clobbered. */\r
610                         "               MOV     R1, #1                          \n"\r
611                         "               MOV     R2, #2                          \n"\r
612                         "               MOV R3, #3                              \n"\r
613                         "               MOV     R12, #12                        \n"\r
614                         "               CMP     R0, #13                         \n" /* Check all the registers still contain their expected values. */\r
615                         "               BNE     prvDeleteMe                     \n" /* Value was not as expected, delete the task so it stops communicating with the check task */\r
616                         "               CMP     R1, #1                          \n"\r
617                         "               BNE     prvDeleteMe                     \n"\r
618                         "               CMP     R2, #2                          \n"\r
619                         "               BNE     prvDeleteMe                     \n"\r
620                         "               CMP R3, #3                              \n"\r
621                         "               BNE     prvDeleteMe                     \n"\r
622                         "               CMP     R4, #4                          \n"\r
623                         "               BNE     prvDeleteMe                     \n"\r
624                         "               CMP     R5, #5                          \n"\r
625                         "               BNE     prvDeleteMe                     \n"\r
626                         "               CMP     R6, #6                          \n"\r
627                         "               BNE     prvDeleteMe                     \n"\r
628                         "               CMP     R8, #8                          \n"\r
629                         "               BNE     prvDeleteMe                     \n"\r
630                         "               CMP     R9, #9                          \n"\r
631                         "               BNE     prvDeleteMe                     \n"\r
632                         "               CMP     R10, #10                        \n"\r
633                         "               BNE     prvDeleteMe                     \n"\r
634                         "               CMP     R11, #11                        \n"\r
635                         "               BNE     prvDeleteMe                     \n"\r
636                         "               CMP     R12, #12                        \n"\r
637                         "               BNE     prvDeleteMe                     \n"\r
638             :::"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r8", "r9", "r10", "r11", "r12"\r
639                 );\r
640 \r
641                 /* Send mainREG_TEST_2_STILL_EXECUTING to the check task to indicate that this \r
642                 task is still functioning. */\r
643                 prvSendImAlive( xQueue, mainREG_TEST_2_STILL_EXECUTING );\r
644 \r
645                 /* Go back to check all the register values again. */\r
646                 __asm volatile( "               B reg2loop      " );\r
647         }\r
648 }\r
649 /*-----------------------------------------------------------*/\r
650 \r
651 void vApplicationIdleHook( void )\r
652 {\r
653 extern unsigned long __SRAM_segment_end__[];\r
654 extern unsigned long __privileged_data_start__[];\r
655 extern unsigned long __privileged_data_end__[];\r
656 extern unsigned long __FLASH_segment_start__[];\r
657 extern unsigned long __FLASH_segment_end__[];\r
658 volatile unsigned long *pul;\r
659 volatile unsigned long ulReadData;\r
660 \r
661         /* The idle task, and therefore this function, run in Supervisor mode and\r
662         can therefore access all memory.  Try reading from corners of flash and\r
663         RAM to ensure a memory fault does not occur. \r
664         \r
665         Start with the edges of the privileged data area. */\r
666         pul = __privileged_data_start__;\r
667         ulReadData = *pul;\r
668         pul = __privileged_data_end__ - 1;\r
669         ulReadData = *pul;\r
670 \r
671         /* Next the standard SRAM area. */\r
672         pul = __SRAM_segment_end__ - 1;\r
673         ulReadData = *pul;\r
674 \r
675         /* And the standard Flash area - the start of which is marked for\r
676         privileged access only. */\r
677         pul = __FLASH_segment_start__;\r
678         ulReadData = *pul;\r
679         pul = __FLASH_segment_end__ - 1;\r
680         ulReadData = *pul;\r
681 \r
682         /* Reading off the end of Flash or SRAM space should cause a fault.  \r
683         Uncomment one of the following two pairs of lines to test. */\r
684         \r
685         /* pul = __FLASH_segment_end__ + 4;\r
686         ulReadData = *pul; */\r
687 \r
688         /* pul = __SRAM_segment_end__ + 1;\r
689         ulReadData = *pul; */\r
690 }\r
691 /*-----------------------------------------------------------*/\r
692 \r
693 static void prvOldStyleUserModeTask( void *pvParameters )\r
694 {\r
695 extern unsigned long __privileged_data_start__[];\r
696 extern unsigned long __privileged_data_end__[];\r
697 extern unsigned long __SRAM_segment_end__[];\r
698 extern unsigned long __privileged_functions_end__[];\r
699 extern unsigned long __FLASH_segment_start__[];\r
700 extern unsigned long __FLASH_segment_end__[];\r
701 const volatile unsigned long *pulStandardPeripheralRegister = ( volatile unsigned long * ) 0x400FC0C4; /* PCONP */\r
702 volatile unsigned long *pul;\r
703 volatile unsigned long ulReadData;\r
704 \r
705 /* The following lines are commented out to prevent the unused variable \r
706 compiler warnings when the tests that use the variable are also commented out.\r
707 extern unsigned long __privileged_functions_start__[];\r
708 const volatile unsigned long *pulSystemPeripheralRegister = ( volatile unsigned long * ) 0xe000e014; */\r
709 \r
710         ( void ) pvParameters;\r
711 \r
712         /* This task is created in User mode using the original xTaskCreate() API\r
713         function.  It should have access to all Flash and RAM except that marked\r
714         as Privileged access only.  Reading from the start and end of the non-\r
715         privileged RAM should not cause a problem (the privileged RAM is the first\r
716         block at the bottom of the RAM memory). */\r
717         pul = __privileged_data_end__ + 1;\r
718         ulReadData = *pul;\r
719         pul = __SRAM_segment_end__ - 1;\r
720         ulReadData = *pul;\r
721 \r
722         /* Likewise reading from the start and end of the non-privileged Flash\r
723         should not be a problem (the privileged Flash is the first block at the\r
724         bottom of the Flash memory). */\r
725         pul = __privileged_functions_end__ + 1;\r
726         ulReadData = *pul;\r
727         pul = __FLASH_segment_end__ - 1;\r
728         ulReadData = *pul;\r
729 \r
730         /* Standard peripherals are accessible. */\r
731         ulReadData = *pulStandardPeripheralRegister;\r
732 \r
733         /* System peripherals are not accessible.  Uncomment the following line\r
734         to test.  Also uncomment the declaration of pulSystemPeripheralRegister\r
735         at the top of this function. */\r
736     /* ulReadData = *pulSystemPeripheralRegister; */\r
737 \r
738         /* Reading from anywhere inside the privileged Flash or RAM should cause a\r
739         fault.  This can be tested by uncommenting any of the following pairs of\r
740         lines.  Also uncomment the declaration of __privileged_functions_start__\r
741         at the top of this function. */\r
742 \r
743         /* pul = __privileged_functions_start__;\r
744         ulReadData = *pul; */\r
745         \r
746         /* pul = __privileged_functions_end__ - 1;\r
747         ulReadData = *pul; */\r
748 \r
749         /* pul = __privileged_data_start__;\r
750         ulReadData = *pul; */\r
751         \r
752         /* pul = __privileged_data_end__ - 1;\r
753         ulReadData = *pul; */\r
754 \r
755         /* Must not just run off the end of a task function, so delete this task. \r
756         Note that because this task was created using xTaskCreate() the stack was\r
757         allocated dynamically and I have not included any code to free it again. */\r
758         vTaskDelete( NULL );\r
759 }\r
760 /*-----------------------------------------------------------*/\r
761 \r
762 static void prvOldStylePrivilegedModeTask( void *pvParameters )\r
763 {\r
764 extern unsigned long __privileged_data_start__[];\r
765 extern unsigned long __privileged_data_end__[];\r
766 extern unsigned long __SRAM_segment_end__[];\r
767 extern unsigned long __privileged_functions_start__[];\r
768 extern unsigned long __privileged_functions_end__[];\r
769 extern unsigned long __FLASH_segment_start__[];\r
770 extern unsigned long __FLASH_segment_end__[];\r
771 volatile unsigned long *pul;\r
772 volatile unsigned long ulReadData;\r
773 const volatile unsigned long *pulSystemPeripheralRegister = ( volatile unsigned long * ) 0xe000e014; /* Systick */\r
774 const volatile unsigned long *pulStandardPeripheralRegister = ( volatile unsigned long * ) 0x400FC0C4; /* PCONP */\r
775 \r
776         ( void ) pvParameters;\r
777 \r
778         /* This task is created in Privileged mode using the original xTaskCreate() \r
779         API     function.  It should have access to all Flash and RAM including that \r
780         marked as Privileged access only.  So reading from the start and end of the \r
781         non-privileged RAM should not cause a problem (the privileged RAM is the \r
782         first block at the bottom of the RAM memory). */\r
783         pul = __privileged_data_end__ + 1;\r
784         ulReadData = *pul;\r
785         pul = __SRAM_segment_end__ - 1;\r
786         ulReadData = *pul;\r
787 \r
788         /* Likewise reading from the start and end of the non-privileged Flash\r
789         should not be a problem (the privileged Flash is the first block at the\r
790         bottom of the Flash memory). */\r
791         pul = __privileged_functions_end__ + 1;\r
792         ulReadData = *pul;\r
793         pul = __FLASH_segment_end__ - 1;\r
794         ulReadData = *pul;\r
795 \r
796         /* Reading from anywhere inside the privileged Flash or RAM should also\r
797         not be a problem. */\r
798         pul = __privileged_functions_start__;\r
799         ulReadData = *pul;\r
800         pul = __privileged_functions_end__ - 1;\r
801         ulReadData = *pul;\r
802         pul = __privileged_data_start__;\r
803         ulReadData = *pul;      \r
804         pul = __privileged_data_end__ - 1;\r
805         ulReadData = *pul;\r
806 \r
807         /* Finally, accessing both System and normal peripherals should both be\r
808         possible. */\r
809     ulReadData = *pulSystemPeripheralRegister;\r
810         ulReadData = *pulStandardPeripheralRegister;\r
811 \r
812         /* Must not just run off the end of a task function, so delete this task. \r
813         Note that because this task was created using xTaskCreate() the stack was\r
814         allocated dynamically and I have not included any code to free it again. */\r
815         vTaskDelete( NULL );\r
816 }\r
817 /*-----------------------------------------------------------*/\r
818 \r
819 static void prvDeleteMe( void )\r
820 {\r
821         vTaskDelete( NULL );\r
822 }\r
823 /*-----------------------------------------------------------*/\r
824 \r
825 static void prvSendImAlive( xQueueHandle xHandle, unsigned long ulTaskNumber )\r
826 {\r
827         if( xHandle != NULL )\r
828         {\r
829                 xQueueSend( xHandle, &ulTaskNumber, mainDONT_BLOCK );\r
830         }\r
831 }\r
832 /*-----------------------------------------------------------*/\r
833 \r
834 static void prvSetupHardware( void )\r
835 {\r
836         /* If running on Rev A2 silicon, turn the LDO voltage up to 2.75V.  This is\r
837         a workaround to allow the PLL to operate reliably. */\r
838         if( DEVICE_IS_REVA2 )\r
839         {\r
840                 SysCtlLDOSet( SYSCTL_LDO_2_75V );\r
841         }\r
842 \r
843         /* Set the clocking to run from the PLL at 50 MHz */\r
844         SysCtlClockSet( SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_8MHZ );\r
845 }\r
846 /*-----------------------------------------------------------*/\r
847 \r
848 void vApplicationTickHook( void )\r
849 {\r
850 static unsigned long ulCallCount;\r
851 const unsigned long ulCallsBetweenSends = 5000 / portTICK_RATE_MS;\r
852 const unsigned long ulMessage = mainPRINT_SYSTEM_STATUS;\r
853 portBASE_TYPE xDummy;\r
854 \r
855         /* If configUSE_TICK_HOOK is set to 1 then this function will get called\r
856         from each RTOS tick.  It is called from the tick interrupt and therefore\r
857         will be executing in the privileged state. */\r
858 \r
859         ulCallCount++;\r
860 \r
861         /* Is it time to print out the pass/fail message again? */\r
862         if( ulCallCount >= ulCallsBetweenSends )\r
863         {\r
864                 ulCallCount = 0;\r
865 \r
866                 /* Send a message to the check task to command it to check that all\r
867                 the tasks are still running then print out the status. \r
868                 \r
869                 This is running in an ISR so has to use the "FromISR" version of\r
870                 xQueueSend().  Because it is in an ISR it is running with privileges\r
871                 so can access xFileScopeCheckQueue directly. */\r
872                 xQueueSendFromISR( xFileScopeCheckQueue, &ulMessage, &xDummy );\r
873         }\r
874 }\r
875 /*-----------------------------------------------------------*/\r
876 \r
877 void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )\r
878 {\r
879         /* If configCHECK_FOR_STACK_OVERFLOW is set to either 1 or 2 then this \r
880         function will automatically get called if a task overflows its stack. */\r
881         ( void ) pxTask;\r
882         ( void ) pcTaskName;\r
883         for( ;; );\r
884 }\r
885 /*-----------------------------------------------------------*/\r
886 \r
887 void vApplicationMallocFailedHook( void )\r
888 {\r
889         /* If configUSE_MALLOC_FAILED_HOOK is set to 1 then this function will\r
890         be called automatically if a call to pvPortMalloc() fails.  pvPortMalloc()\r
891         is called automatically when a task, queue or semaphore is created. */\r
892         for( ;; );\r
893 }\r
894 /*-----------------------------------------------------------*/\r
895 \r
896 /* Just to keep the linker happy. */\r
897 void __error__( char *pcFilename, unsigned long ulLine )\r
898 {\r
899         ( void ) pcFilename;\r
900         ( void ) ulLine;\r
901         for( ;; );\r
902 }\r
903 /*-----------------------------------------------------------*/\r
904 \r
905 /* Just to keep the linker happy. */\r
906 int uipprintf( const char *fmt, ... )\r
907 {\r
908         ( void ) fmt;\r
909         return( 0 );\r
910 }\r
911 /*-----------------------------------------------------------*/\r
912 \r
913 void hard_fault_handler(unsigned int * hardfault_args)\r
914 {\r
915 volatile unsigned int stacked_r0;\r
916 volatile unsigned int stacked_r1;\r
917 volatile unsigned int stacked_r2;\r
918 volatile unsigned int stacked_r3;\r
919 volatile unsigned int stacked_r12;\r
920 volatile unsigned int stacked_lr;\r
921 volatile unsigned int stacked_pc;\r
922 volatile unsigned int stacked_psr;\r
923 \r
924         stacked_r0 = ((unsigned long) hardfault_args[0]);\r
925         stacked_r1 = ((unsigned long) hardfault_args[1]);\r
926         stacked_r2 = ((unsigned long) hardfault_args[2]);\r
927         stacked_r3 = ((unsigned long) hardfault_args[3]);\r
928 \r
929         stacked_r12 = ((unsigned long) hardfault_args[4]);\r
930         stacked_lr = ((unsigned long) hardfault_args[5]);\r
931         stacked_pc = ((unsigned long) hardfault_args[6]);\r
932         stacked_psr = ((unsigned long) hardfault_args[7]);\r
933 \r
934         /* Inspect stacked_pc to locate the offending instruction. */\r
935         for( ;; );\r
936 }\r
937 /*-----------------------------------------------------------*/\r
938 \r
939 void Fault_ISR( void ) __attribute__((naked));\r
940 void Fault_ISR( void )\r
941 {\r
942         __asm volatile\r
943         (\r
944                 " tst lr, #4                                                                            \n"\r
945                 " ite eq                                                                                        \n"\r
946                 " mrseq r0, msp                                                                         \n"\r
947                 " mrsne r0, psp                                                                         \n"\r
948                 " ldr r1, [r0, #24]                                                                     \n"\r
949                 " ldr r2, handler_address_const                                         \n"\r
950                 " bx r2                                                                                         \n"\r
951                 " handler_address_const: .word hard_fault_handler       \n"\r
952         );\r
953 }\r
954 /*-----------------------------------------------------------*/\r
955 \r
956 void MPU_Fault_ISR( void ) __attribute__((naked));\r
957 void MPU_Fault_ISR( void )\r
958 {\r
959         __asm volatile\r
960         (\r
961                 " tst lr, #4                                                                            \n"\r
962                 " ite eq                                                                                        \n"\r
963                 " mrseq r0, msp                                                                         \n"\r
964                 " mrsne r0, psp                                                                         \n"\r
965                 " ldr r1, [r0, #24]                                                                     \n"\r
966                 " ldr r2, handler_address_const                                         \n"\r
967                 " bx r2                                                                                         \n"\r
968                 " handler2_address_const: .word hard_fault_handler      \n"\r
969         );\r
970 }\r
971 /*-----------------------------------------------------------*/