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