]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_MPU_LM3Sxxxx_Rowley/main.c
Add clobber list to asm sections to allow use with higher optimisation.
[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 /* Standard includes. */\r
50 #include <string.h>\r
51 #include <__cross_studio_io.h>\r
52 \r
53 /* Scheduler includes. */\r
54 #include "FreeRTOS.h"\r
55 #include "task.h"\r
56 #include "queue.h"\r
57 #include "semphr.h"\r
58 \r
59 /* Hardware library includes. */\r
60 #include "hw_types.h"\r
61 #include "hw_sysctl.h"\r
62 #include "sysctl.h"\r
63 \r
64 /*\r
65  * This file demonstrates the use of MPU using just three tasks - two 'reg test'\r
66  * tasks and one 'check' task.  Read the comments above the\r
67  * function prototypes for more information.\r
68  */\r
69 \r
70 /*-----------------------------------------------------------*/\r
71 \r
72 /* Misc constants. */\r
73 #define mainDONT_BLOCK                                  ( 0 )\r
74 \r
75 /* Definitions for the messages that can be sent to the check task. */\r
76 #define mainREG_TEST_1_STILL_EXECUTING  ( 0 )\r
77 #define mainREG_TEST_2_STILL_EXECUTING  ( 1 )\r
78 #define mainPRINT_SYSTEM_STATUS                 ( 2 )\r
79 \r
80 /* GCC specifics. */\r
81 #define mainALIGN_TO( x )                               __attribute__((aligned(x)))\r
82 \r
83 \r
84 /*-----------------------------------------------------------*/\r
85 /* Prototypes for functions that implement tasks. -----------*/\r
86 /*-----------------------------------------------------------*/\r
87 \r
88 /* \r
89  * Prototype for the reg test tasks.  Amongst other things, these fill the CPU\r
90  * registers with known values before checking that the registers still contain\r
91  * the expected values.  Each of the two tasks use different values so an error\r
92  * in the context switch mechanism can be caught.  Both reg test tasks execute\r
93  * at the idle priority so will get preempted regularly.\r
94  */\r
95 static void prvRegTest1Task( void *pvParameters );\r
96 static void prvRegTest2Task( void *pvParameters );\r
97 \r
98 /*\r
99  * Prototype for the check task.  The check task demonstrates various features\r
100  * of the MPU before entering a loop where it waits for commands to arrive on a\r
101  * queue.\r
102  *\r
103  * The check task will periodically be commanded to print out a status message.\r
104  * If both the reg tests tasks are executing as expected the check task will\r
105  * print "PASS" to the debug port, otherwise it will print 'FAIL'.  Debug port\r
106  * messages can be viewed within the CrossWorks IDE.\r
107  */\r
108 static void prvCheckTask( void *pvParameters );\r
109 \r
110 \r
111 \r
112 /*-----------------------------------------------------------*/\r
113 /* Prototypes for other misc functions.  --------------------*/\r
114 /*-----------------------------------------------------------*/\r
115 \r
116 /*\r
117  * Just configures any clocks and IO necessary.\r
118  */\r
119 static void prvSetupHardware( void );\r
120 \r
121 /*\r
122  * Simply deletes the calling task.  The function is provided only because it\r
123  * is simpler to call from asm code than the normal vTaskDelete() API function.\r
124  * It has the noinline attribute because it is called from asm code.\r
125  */\r
126 static void prvDeleteMe( void ) __attribute__((noinline));\r
127 \r
128 /*\r
129  * Used by both reg test tasks to send messages to the check task.  The message\r
130  * just lets the check task know that the sending is still functioning correctly.\r
131  * If a reg test task detects an error it will delete itself, and in so doing\r
132  * prevent itself from sending any more 'I'm Alive' messages to the check task.\r
133  */\r
134 static void prvSendImAlive( xQueueHandle xHandle, unsigned long ulTaskNumber );\r
135 \r
136 /*\r
137  * The check task is created with access to three memory regions (plus its\r
138  * stack).  Each memory region is configured with different parameters and\r
139  * prvTestMemoryRegions() demonstrates what can and cannot be accessed for each\r
140  * region.  prvTestMemoryRegions() also demonstrates a task that was created\r
141  * as a privileged task settings its own privilege level down to that of a user\r
142  * task.\r
143  */\r
144 static void prvTestMemoryRegions( void );\r
145 \r
146 /*-----------------------------------------------------------*/\r
147 \r
148 /* The handle of the queue used to communicate between tasks and between tasks\r
149 and interrupts.  Note that this is a file scope variable that falls outside of\r
150 any MPU region.  As such other techniques have to be used to allow the tasks\r
151 to gain access to the queue.  See the comments in the tasks themselves for \r
152 further information. */\r
153 static xQueueHandle xFileScopeCheckQueue = NULL;\r
154 \r
155 \r
156 \r
157 /*-----------------------------------------------------------*/\r
158 /* Data used by the 'check' task. ---------------------------*/\r
159 /*-----------------------------------------------------------*/\r
160 \r
161 /* Define the constants used to allocate the check task stack.  Note that the\r
162 stack size is defined in words, not bytes. */\r
163 #define mainCHECK_TASK_STACK_SIZE_WORDS 128\r
164 #define mainCHECK_TASK_STACK_ALIGNMENT ( mainCHECK_TASK_STACK_SIZE_WORDS * sizeof( portSTACK_TYPE ) )\r
165 \r
166 /* Declare the stack that will be used by the check task.  The kernel will\r
167  automatically create an MPU region for the stack.  The stack alignment must \r
168  match its size, so if 128 words are reserved for the stack then it must be \r
169  aligned to ( 128 * 4 ) bytes. */\r
170 static portSTACK_TYPE xCheckTaskStack[ mainCHECK_TASK_STACK_SIZE_WORDS ] mainALIGN_TO( mainCHECK_TASK_STACK_ALIGNMENT );\r
171 \r
172 /* Declare three arrays - an MPU region will be created for each array\r
173  using the xTaskParameters structure below.  Note that the arrays allocate \r
174 slightly more RAM than is actually assigned to the MPU region.  This is to \r
175 permit writes off the end of the array to be detected even when the arrays are \r
176 placed in adjacent memory locations (with no gaps between them).  The align \r
177 size must be a power of two. */\r
178 #define mainREAD_WRITE_ARRAY_SIZE 130\r
179 #define mainREAD_WRITE_ALIGN_SIZE 128\r
180 char cReadWriteArray[ mainREAD_WRITE_ARRAY_SIZE ] mainALIGN_TO( mainREAD_WRITE_ALIGN_SIZE );\r
181 \r
182 #define mainREAD_ONLY_ARRAY_SIZE 260\r
183 #define mainREAD_ONLY_ALIGN_SIZE 256\r
184 char cReadOnlyArray[ mainREAD_ONLY_ARRAY_SIZE ] mainALIGN_TO( mainREAD_ONLY_ALIGN_SIZE );\r
185 \r
186 #define mainPRIVILEGED_ONLY_ACCESS_ARRAY_SIZE 130\r
187 #define mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE 128\r
188 char cPrivilegedOnlyAccessArray[ mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE ] mainALIGN_TO( mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE );\r
189 \r
190 /* Fill in a xTaskParameters structure to define the check task. */\r
191 static const xTaskParameters xCheckTaskParameters =\r
192 {\r
193         prvCheckTask,                                                           /* pvTaskCode - the function that implements the task. */\r
194         ( signed char * ) "Check",                                      /* pcName                       */\r
195         mainCHECK_TASK_STACK_SIZE_WORDS,                        /* usStackDepth - defined in words, not bytes. */\r
196         ( void * ) 0x12121212,                                          /* pvParameters - this value is just to test that the parameter is being passed into the task correctly. */\r
197         ( 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
198         xCheckTaskStack,                                                        /* puxStackBuffer - the array to use as the task stack, as declared above. */\r
199 \r
200         /* xRegions - In this case the xRegions array is used to create MPU regions\r
201         for all three of the arrays declared directly above.  Each MPU region is\r
202         created with different parameters. */\r
203         {                                                                                       \r
204                 /* Base address                                 Length                                                                  Parameters */\r
205         { cReadWriteArray,                              mainREAD_WRITE_ALIGN_SIZE,                              portMPU_REGION_READ_WRITE },\r
206         { cReadOnlyArray,                               mainREAD_ONLY_ALIGN_SIZE,                               portMPU_REGION_READ_ONLY },\r
207         { cPrivilegedOnlyAccessArray,   mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE,  portMPU_REGION_PRIVILEGED_READ_WRITE }\r
208         }\r
209 };\r
210 \r
211 /* Three MPU regions are defined for use by the 'check' task when the task is \r
212 created.  These are only used to demonstrate the MPU features and are not\r
213 actually necessary for the check task to fulfill its primary purpose.  Instead\r
214 the MPU regions are replaced with those defined by xAltRegions prior to the \r
215 check task receiving any data on the queue or printing any messages to the\r
216 debug console.  The region configured by xAltRegions just gives the check task\r
217 access to the debug variables that form part of the Rowley library, and are\r
218 accessed within the debug_printf() function. */\r
219 extern unsigned long dbgCntrlWord_mempoll;\r
220 static const xMemoryRegion xAltRegions[ portNUM_CONFIGURABLE_REGIONS ] =\r
221 {                                                                                       \r
222         /* Base address                                         Length          Parameters */\r
223         { ( void * ) &dbgCntrlWord_mempoll,     32,                     portMPU_REGION_READ_WRITE },\r
224         { 0,                                                            0,                      0 },\r
225         { 0,                                                            0,                      0 }\r
226 };\r
227 \r
228 \r
229 \r
230 /*-----------------------------------------------------------*/\r
231 /* Data used by the 'reg test' tasks. -----------------------*/\r
232 /*-----------------------------------------------------------*/\r
233 \r
234 /* Define the constants used to allocate the reg test task stacks.  Note that\r
235 that stack size is defined in words, not bytes. */\r
236 #define mainREG_TEST_STACK_SIZE_WORDS   128\r
237 #define mainREG_TEST_STACK_ALIGNMENT    ( mainREG_TEST_STACK_SIZE_WORDS * sizeof( portSTACK_TYPE ) )\r
238 \r
239 /* Declare the stacks that will be used by the reg test tasks.  The kernel will\r
240 automatically create an MPU region for the stack.  The stack alignment must \r
241 match its size, so if 128 words are reserved for the stack then it must be \r
242 aligned to ( 128 * 4 ) bytes. */\r
243 static portSTACK_TYPE xRegTest1Stack[ mainREG_TEST_STACK_SIZE_WORDS ] mainALIGN_TO( mainREG_TEST_STACK_ALIGNMENT );\r
244 static portSTACK_TYPE xRegTest2Stack[ mainREG_TEST_STACK_SIZE_WORDS ] mainALIGN_TO( mainREG_TEST_STACK_ALIGNMENT );\r
245 \r
246 /* Fill in a xTaskParameters structure per reg test task to define the tasks. */\r
247 static const xTaskParameters xRegTest1Parameters =\r
248 {\r
249         prvRegTest1Task,                                                /* pvTaskCode - the function that implements the task. */\r
250         ( signed char * ) "RegTest1",                   /* pcName                       */\r
251         mainREG_TEST_STACK_SIZE_WORDS,                  /* usStackDepth         */\r
252         ( void * ) 0x12345678,                                  /* pvParameters - this value is just to test that the parameter is being passed into the task correctly. */\r
253         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
254         xRegTest1Stack,                                                 /* puxStackBuffer - the array to use as the task stack, as declared above. */\r
255         {                                                                               /* xRegions - this task does not use any non-stack data. */\r
256                 /* Base address         Length          Parameters */\r
257         { 0x00,                         0x00,                   0x00 },\r
258         { 0x00,                         0x00,                   0x00 },\r
259         { 0x00,                         0x00,                   0x00 }\r
260         }\r
261 };\r
262 /*-----------------------------------------------------------*/\r
263 \r
264 static xTaskParameters xRegTest2Parameters =\r
265 {\r
266         prvRegTest2Task,                                /* pvTaskCode - the function that implements the task. */\r
267         ( signed char * ) "RegTest2",   /* pcName                       */\r
268         mainREG_TEST_STACK_SIZE_WORDS,  /* usStackDepth         */\r
269         ( void * ) NULL,                                /* pvParameters - this task uses the parameter to pass in a queue handle, but the queue is not created yet. */\r
270         tskIDLE_PRIORITY,                               /* uxPriority           */\r
271         xRegTest2Stack,                                 /* puxStackBuffer - the array to use as the task stack, as declared above. */\r
272         {                                                               /* xRegions - this task does not use any non-stack data. */\r
273                 /* Base address         Length          Parameters */\r
274         { 0x00,                         0x00,                   0x00 },\r
275         { 0x00,                         0x00,                   0x00 },\r
276         { 0x00,                         0x00,                   0x00 }\r
277         }\r
278 };\r
279 \r
280 /*-----------------------------------------------------------*/\r
281 \r
282 int main( void )\r
283 {\r
284         prvSetupHardware();\r
285 \r
286         /* Create the queue used to pass "I'm alive" messages to the check task. */\r
287         xFileScopeCheckQueue = xQueueCreate( 1, sizeof( unsigned long ) );\r
288 \r
289         /* One check task uses the task parameter to receive the queue handle.\r
290         This allows the file scope variable to be accessed from within the task.\r
291         The pvParameters member of xRegTest2Parameters can only be set after the\r
292         queue has been created. */\r
293         xRegTest2Parameters.pvParameters = xFileScopeCheckQueue;\r
294 \r
295         /* Create the three test tasks.  Handles to the created tasks are not\r
296         required, hence the second parameter is NULL. */\r
297         xTaskCreateRestricted( &xRegTest1Parameters, NULL );\r
298     xTaskCreateRestricted( &xRegTest2Parameters, NULL );\r
299         xTaskCreateRestricted( &xCheckTaskParameters, NULL );\r
300 \r
301         /* Print out the amount of free heap space so configTOTAL_HEAP_SIZE can be\r
302         tuned.  The heap size is set to be very small in this example and will need\r
303         to be increased before many more tasks, queues or semaphores can be \r
304         created. */\r
305         debug_printf( "There are %d bytes of unused heap space, although the idle task is yet to be created.\r\n", xPortGetFreeHeapSize() );\r
306 \r
307         /* Start the scheduler. */\r
308         vTaskStartScheduler();\r
309 \r
310         /* Will only get here if there was insufficient memory to create the idle\r
311         task. */\r
312         for( ;; );\r
313         return 0;\r
314 }\r
315 /*-----------------------------------------------------------*/\r
316 \r
317 static void prvCheckTask( void *pvParameters )\r
318 {\r
319 /* This task is created in privileged mode so can access the file scope\r
320 queue variable.  Take a stack copy of this before the task is set into user\r
321 mode.  Once that task is in user mode the file scope queue variable will no\r
322 longer be accessible but the stack copy will. */\r
323 xQueueHandle xQueue = xFileScopeCheckQueue;\r
324 long lMessage;\r
325 unsigned long ulStillAliveCounts[ 2 ] = { 0 };\r
326 const char *pcStatusMessage = "PASS\r\n";\r
327 \r
328         /* Just to remove compiler warning. */\r
329         ( void ) pvParameters;\r
330 \r
331         /* Demonstrate how the various memory regions can and can't be accessed. \r
332         The task privilege is set down to user mode within this function. */\r
333         prvTestMemoryRegions();\r
334 \r
335         /* Change the memory regions allocated to this task to those initially\r
336         set up for demonstration purposes to those actually required by the task. */\r
337         vTaskAllocateMPURegions( NULL, xAltRegions );\r
338 \r
339         /* This loop performs the main function of the task, which is blocking\r
340         on a message queue then processing each message as it arrives. */\r
341         for( ;; )\r
342         {\r
343                 /* Wait for the next message to arrive. */\r
344                 xQueueReceive( xQueue, &lMessage, portMAX_DELAY );\r
345                 \r
346                 switch( lMessage )\r
347                 {\r
348                         case mainREG_TEST_1_STILL_EXECUTING     :       \r
349                                         /* Message from task 1, so task 1 must still be executing. */\r
350                                         ( ulStillAliveCounts[ 0 ] )++;\r
351                                         break;\r
352 \r
353                         case mainREG_TEST_2_STILL_EXECUTING     :                                               \r
354                                         /* Message from task 2, so task 2 must still be executing. */\r
355                                         ( ulStillAliveCounts[ 1 ] )++;\r
356                                         break;\r
357 \r
358                         case mainPRINT_SYSTEM_STATUS            :       \r
359                                         /* Message from tick hook, time to print out the system\r
360                                         status.  If messages has stopped arriving from either reg\r
361                                         test task then the status must be set to fail. */\r
362                                         if( ( ulStillAliveCounts[ 0 ] == 0 ) || ( ulStillAliveCounts[ 1 ] == 0 )  )\r
363                                         {\r
364                                                 /* One or both of the test tasks are no longer sending \r
365                                                 'still alive' messages. */\r
366                                                 pcStatusMessage = "FAIL\r\n";\r
367                                         }\r
368 \r
369                                         /* Print a pass/fail message to the terminal.  This will be\r
370                                         visible in the CrossWorks IDE. */\r
371                                         debug_printf( pcStatusMessage );\r
372 \r
373                                         /* Reset the count of 'still alive' messages. */\r
374                                         memset( ulStillAliveCounts, 0x00, sizeof( ulStillAliveCounts ) );\r
375                                         break;\r
376 \r
377                 default :\r
378                                         /* Something unexpected happened.  Delete this task so the \r
379                                         error is apparent (no output will be displayed). */\r
380                                         prvDeleteMe();\r
381                                         break;\r
382                 }\r
383         }\r
384 }\r
385 /*-----------------------------------------------------------*/\r
386 \r
387 static void prvTestMemoryRegions( void )\r
388 {\r
389 long l;\r
390 char cTemp;\r
391 \r
392         /* The check task is created in the privileged mode.  The privileged array \r
393         can be both read from and written to while this task is privileged. */\r
394         cPrivilegedOnlyAccessArray[ 0 ] = 'a';\r
395         if( cPrivilegedOnlyAccessArray[ 0 ] != 'a' )\r
396         {\r
397                 /* Something unexpected happened.  Delete this task so the error is\r
398                 apparent (no output will be displayed). */\r
399                 prvDeleteMe();\r
400         }\r
401 \r
402         /* Writing off the end of the RAM allocated to this task will *NOT* cause a\r
403         protection fault because the task is still executing in a privileged mode.  \r
404         Uncomment the following to test. */\r
405         /*cPrivilegedOnlyAccessArray[ mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE ] = 'a';*/\r
406 \r
407         /* Now set the task into user mode. */\r
408         portSWITCH_TO_USER_MODE();\r
409          \r
410         /* Accessing the privileged only array will now cause a fault.  Uncomment \r
411         the following line to test. */    \r
412         /*cPrivilegedOnlyAccessArray[ 0 ] = 'a';*/\r
413 \r
414         /* The read/write array can still be successfully read and written. */\r
415         for( l = 0; l < mainREAD_WRITE_ALIGN_SIZE; l++ )\r
416         {\r
417                 cReadWriteArray[ l ] = 'a';\r
418                 if( cReadWriteArray[ l ] != 'a' )\r
419                 {\r
420                         /* Something unexpected happened.  Delete this task so the error is\r
421                         apparent (no output will be displayed). */\r
422                         prvDeleteMe();\r
423                 }\r
424         }\r
425 \r
426         /* But attempting to read or write off the end of the RAM allocated to this\r
427         task will cause a fault.  Uncomment either of the following two lines to \r
428         test. */\r
429         /* cReadWriteArray[ 0 ] = cReadWriteArray[ -1 ]; */\r
430         /* cReadWriteArray[ mainREAD_WRITE_ALIGN_SIZE ] = 0x00; */\r
431 \r
432         /* The read only array can be successfully read... */\r
433         for( l = 0; l < mainREAD_ONLY_ALIGN_SIZE; l++ )\r
434         {\r
435                 cTemp = cReadOnlyArray[ l ];\r
436         }\r
437 \r
438         /* ...but cannot be written.  Uncomment the following line to test. */\r
439         /* cReadOnlyArray[ 0 ] = 'a'; */\r
440 \r
441         /* Writing to the first and last locations in the stack array should not \r
442         cause a protection fault.  Note that doing this will cause the kernel to\r
443         detect a stack overflow if configCHECK_FOR_STACK_OVERFLOW is greater than \r
444         1. */\r
445     xCheckTaskStack[ 0 ] = 0;\r
446     xCheckTaskStack[ mainCHECK_TASK_STACK_SIZE_WORDS - 1 ] = 0;\r
447 \r
448         /* Writing off either end of the stack array should cause a protection \r
449         fault, uncomment either of the following two lines to test. */\r
450         /* xCheckTaskStack[ -1 ] = 0; */\r
451     /* xCheckTaskStack[ mainCHECK_TASK_STACK_SIZE_WORDS ] = 0; */\r
452 }\r
453 /*-----------------------------------------------------------*/\r
454 \r
455 static void prvRegTest1Task( void *pvParameters )\r
456 {\r
457 /* This task is created in privileged mode so can access the file scope\r
458 queue variable.  Take a stack copy of this before the task is set into user\r
459 mode.  Once that task is in user mode the file scope queue variable will no\r
460 longer be accessible but the stack copy will. */\r
461 xQueueHandle xQueue = xFileScopeCheckQueue;\r
462 \r
463         /* Now the queue handle has been obtained the task can switch to user \r
464         mode.  This is just one method of passing a handle into a protected\r
465         task, the other reg test task uses the task parameter instead. */\r
466     portSWITCH_TO_USER_MODE();\r
467 \r
468         /* First check that the parameter value is as expected. */\r
469         if( pvParameters != ( void * ) 0x12345678 )\r
470         {\r
471                 /* Error detected.  Delete the task so it stops communicating with\r
472                 the check task. */\r
473                 prvDeleteMe();\r
474         }\r
475 \r
476 \r
477         for( ;; )\r
478         {               \r
479                 /* This task tests the kernel context switch mechanism by reading and\r
480                 writing directly to registers - which requires the test to be written\r
481                 in assembly code. */\r
482                 __asm volatile \r
483                 (       \r
484                         "               MOV     R4, #104                        \n" /* Set registers to a known value.  R0 to R1 are done in the loop below. */\r
485                         "               MOV     R5, #105                        \n"\r
486                         "               MOV     R6, #106                        \n"\r
487                         "               MOV     R8, #108                        \n"\r
488                         "               MOV     R9, #109                        \n"\r
489                         "               MOV     R10, #110                       \n"\r
490                         "               MOV     R11, #111                       \n"\r
491                         "reg1loop:                                              \n"\r
492                         "               MOV     R0, #100                        \n" /* Set the scratch registers to known values - done inside the loop as they get clobbered. */\r
493                         "               MOV     R1, #101                        \n"\r
494                         "               MOV     R2, #102                        \n"\r
495                         "               MOV R3, #103                    \n"\r
496                         "               MOV     R12, #112                       \n"\r
497                         "               SVC #1                                  \n" /* Yield just to increase test coverage. */\r
498                         "               CMP     R0, #100                        \n" /* Check all the registers still contain their expected values. */\r
499                         "               BNE     prvDeleteMe                     \n" /* Value was not as expected, delete the task so it stops communicating with the check task. */\r
500                         "               CMP     R1, #101                        \n"\r
501                         "               BNE     prvDeleteMe                     \n"\r
502                         "               CMP     R2, #102                        \n"\r
503                         "               BNE     prvDeleteMe                     \n"\r
504                         "               CMP R3, #103                    \n"\r
505                         "               BNE     prvDeleteMe                     \n"\r
506                         "               CMP     R4, #104                        \n" \r
507                         "               BNE     prvDeleteMe                     \n" \r
508                         "               CMP     R5, #105                        \n"\r
509                         "               BNE     prvDeleteMe                     \n"\r
510                         "               CMP     R6, #106                        \n"\r
511                         "               BNE     prvDeleteMe                     \n"\r
512                         "               CMP     R8, #108                        \n"\r
513                         "               BNE     prvDeleteMe                     \n"\r
514                         "               CMP     R9, #109                        \n"\r
515                         "               BNE     prvDeleteMe                     \n"\r
516                         "               CMP     R10, #110                       \n"\r
517                         "               BNE     prvDeleteMe                     \n"\r
518                         "               CMP     R11, #111                       \n"\r
519                         "               BNE     prvDeleteMe                     \n"\r
520                         "               CMP     R12, #112                       \n"\r
521                         "               BNE     prvDeleteMe                     \n"\r
522                         :::"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r8", "r9", "r10", "r11", "r12"\r
523                 );\r
524 \r
525                 /* Send mainREG_TEST_1_STILL_EXECUTING to the check task to indicate that this \r
526                 task is still functioning. */\r
527                 prvSendImAlive( xQueue, mainREG_TEST_1_STILL_EXECUTING );\r
528 \r
529                 /* Go back to check all the register values again. */\r
530                 __asm volatile( "               B reg1loop      " );\r
531         }\r
532 }\r
533 /*-----------------------------------------------------------*/\r
534 \r
535 static void prvRegTest2Task( void *pvParameters )\r
536 {\r
537 /* The queue handle is passed in as the task parameter.  This is one method of\r
538 passing data into a protected task, the other check task uses a different \r
539 method. */\r
540 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;\r
541 \r
542         for( ;; )\r
543         {\r
544                 /* This task tests the kernel context switch mechanism by reading and\r
545                 writing directly to registers - which requires the test to be written\r
546                 in assembly code. */\r
547                 __asm volatile \r
548                 (       \r
549                         "               MOV     R4, #4                          \n" /* Set registers to a known value.  R0 to R1 are done in the loop below. */\r
550                         "               MOV     R5, #5                          \n"\r
551                         "               MOV     R6, #6                          \n"\r
552                         "               MOV     R8, #8                          \n" /* Frame pointer is omitted as it must not be changed. */\r
553                         "               MOV     R9, #9                          \n"\r
554                         "               MOV     R10, 10                         \n"\r
555                         "               MOV     R11, #11                        \n"                                             \r
556                         "reg2loop:                                              \n"\r
557                         "               MOV     R0, #13                         \n" /* Set the scratch registers to known values - done inside the loop as they get clobbered. */\r
558                         "               MOV     R1, #1                          \n"\r
559                         "               MOV     R2, #2                          \n"\r
560                         "               MOV R3, #3                              \n"\r
561                         "               MOV     R12, #12                        \n"\r
562                         "               CMP     R0, #13                         \n" /* Check all the registers still contain their expected values. */\r
563                         "               BNE     prvDeleteMe                     \n" /* Value was not as expected, delete the task so it stops communicating with the check task */\r
564                         "               CMP     R1, #1                          \n"\r
565                         "               BNE     prvDeleteMe                     \n"\r
566                         "               CMP     R2, #2                          \n"\r
567                         "               BNE     prvDeleteMe                     \n"\r
568                         "               CMP R3, #3                              \n"\r
569                         "               BNE     prvDeleteMe                     \n"\r
570                         "               CMP     R4, #4                          \n"\r
571                         "               BNE     prvDeleteMe                     \n"\r
572                         "               CMP     R5, #5                          \n"\r
573                         "               BNE     prvDeleteMe                     \n"\r
574                         "               CMP     R6, #6                          \n"\r
575                         "               BNE     prvDeleteMe                     \n"\r
576                         "               CMP     R8, #8                          \n"\r
577                         "               BNE     prvDeleteMe                     \n"\r
578                         "               CMP     R9, #9                          \n"\r
579                         "               BNE     prvDeleteMe                     \n"\r
580                         "               CMP     R10, #10                        \n"\r
581                         "               BNE     prvDeleteMe                     \n"\r
582                         "               CMP     R11, #11                        \n"\r
583                         "               BNE     prvDeleteMe                     \n"\r
584                         "               CMP     R12, #12                        \n"\r
585                         "               BNE     prvDeleteMe                     \n"\r
586             :::"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r8", "r9", "r10", "r11", "r12"\r
587                 );\r
588 \r
589                 /* Send mainREG_TEST_2_STILL_EXECUTING to the check task to indicate that this \r
590                 task is still functioning. */\r
591                 prvSendImAlive( xQueue, mainREG_TEST_2_STILL_EXECUTING );\r
592 \r
593                 /* Go back to check all the register values again. */\r
594                 __asm volatile( "               B reg2loop      " );\r
595         }\r
596 }\r
597 /*-----------------------------------------------------------*/\r
598 \r
599 static void prvDeleteMe( void )\r
600 {\r
601         vTaskDelete( NULL );\r
602 }\r
603 /*-----------------------------------------------------------*/\r
604 \r
605 static void prvSendImAlive( xQueueHandle xHandle, unsigned long ulTaskNumber )\r
606 {\r
607         if( xHandle != NULL )\r
608         {\r
609                 xQueueSend( xHandle, &ulTaskNumber, mainDONT_BLOCK );\r
610         }\r
611 }\r
612 /*-----------------------------------------------------------*/\r
613 \r
614 static void prvSetupHardware( void )\r
615 {\r
616         /* If running on Rev A2 silicon, turn the LDO voltage up to 2.75V.  This is\r
617         a workaround to allow the PLL to operate reliably. */\r
618         if( DEVICE_IS_REVA2 )\r
619         {\r
620                 SysCtlLDOSet( SYSCTL_LDO_2_75V );\r
621         }\r
622 \r
623         /* Set the clocking to run from the PLL at 50 MHz */\r
624         SysCtlClockSet( SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_8MHZ );\r
625 }\r
626 /*-----------------------------------------------------------*/\r
627 \r
628 void vApplicationTickHook( void )\r
629 {\r
630 static unsigned long ulCallCount;\r
631 const unsigned long ulCallsBetweenSends = 5000 / portTICK_RATE_MS;\r
632 const unsigned long ulMessage = mainPRINT_SYSTEM_STATUS;\r
633 portBASE_TYPE xDummy;\r
634 \r
635         /* If configUSE_TICK_HOOK is set to 1 then this function will get called\r
636         from each RTOS tick.  It is called from the tick interrupt and therefore\r
637         will be executing in the privileged state. */\r
638 \r
639         ulCallCount++;\r
640 \r
641         /* Is it time to print out the pass/fail message again? */\r
642         if( ulCallCount >= ulCallsBetweenSends )\r
643         {\r
644                 ulCallCount = 0;\r
645 \r
646                 /* Send a message to the check task to command it to check that all\r
647                 the tasks are still running then print out the status. \r
648                 \r
649                 This is running in an ISR so has to use the "FromISR" version of\r
650                 xQueueSend().  Because it is in an ISR it is running with privileges\r
651                 so can access xFileScopeCheckQueue directly. */\r
652                 xQueueSendFromISR( xFileScopeCheckQueue, &ulMessage, &xDummy );\r
653         }\r
654 }\r
655 /*-----------------------------------------------------------*/\r
656 \r
657 void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )\r
658 {\r
659         /* If configCHECK_FOR_STACK_OVERFLOW is set to either 1 or 2 then this \r
660         function will automatically get called if a task overflows its stack. */\r
661         ( void ) pxTask;\r
662         ( void ) pcTaskName;\r
663         for( ;; );\r
664 }\r
665 /*-----------------------------------------------------------*/\r
666 \r
667 void vApplicationMallocFailedHook( void )\r
668 {\r
669         /* If configUSE_MALLOC_FAILED_HOOK is set to 1 then this function will\r
670         be called automatically if a call to pvPortMalloc() fails.  pvPortMalloc()\r
671         is called automatically when a task, queue or semaphore is created. */\r
672         for( ;; );\r
673 }\r
674 /*-----------------------------------------------------------*/\r
675 \r
676 /* Just to keep the linker happy. */\r
677 void __error__( char *pcFilename, unsigned long ulLine )\r
678 {\r
679         ( void ) pcFilename;\r
680         ( void ) ulLine;\r
681         for( ;; );\r
682 }\r
683 /*-----------------------------------------------------------*/\r
684 \r
685 /* Just to keep the linker happy. */\r
686 int uipprintf( const char *fmt, ... )\r
687 {\r
688         ( void ) fmt;\r
689         return( 0 );\r
690 }\r
691 /*-----------------------------------------------------------*/\r
692 \r
693 void hard_fault_handler(unsigned int * hardfault_args)\r
694 {\r
695 volatile unsigned int stacked_r0;\r
696 volatile unsigned int stacked_r1;\r
697 volatile unsigned int stacked_r2;\r
698 volatile unsigned int stacked_r3;\r
699 volatile unsigned int stacked_r12;\r
700 volatile unsigned int stacked_lr;\r
701 volatile unsigned int stacked_pc;\r
702 volatile unsigned int stacked_psr;\r
703 \r
704         stacked_r0 = ((unsigned long) hardfault_args[0]);\r
705         stacked_r1 = ((unsigned long) hardfault_args[1]);\r
706         stacked_r2 = ((unsigned long) hardfault_args[2]);\r
707         stacked_r3 = ((unsigned long) hardfault_args[3]);\r
708 \r
709         stacked_r12 = ((unsigned long) hardfault_args[4]);\r
710         stacked_lr = ((unsigned long) hardfault_args[5]);\r
711         stacked_pc = ((unsigned long) hardfault_args[6]);\r
712         stacked_psr = ((unsigned long) hardfault_args[7]);\r
713 \r
714         /* Inspect stacked_pc to locate the offending instruction. */\r
715         for( ;; );\r
716 }\r
717 /*-----------------------------------------------------------*/\r
718 \r
719 void Fault_ISR( void ) __attribute__((naked));\r
720 void Fault_ISR( void )\r
721 {\r
722         __asm volatile\r
723         (\r
724                 " tst lr, #4                                                                            \n"\r
725                 " ite eq                                                                                        \n"\r
726                 " mrseq r0, msp                                                                         \n"\r
727                 " mrsne r0, psp                                                                         \n"\r
728                 " ldr r1, [r0, #24]                                                                     \n"\r
729                 " ldr r2, handler_address_const                                         \n"\r
730                 " bx r2                                                                                         \n"\r
731                 " handler_address_const: .word hard_fault_handler       \n"\r
732         );\r
733 }\r
734 /*-----------------------------------------------------------*/\r
735 \r
736 void MPU_Fault_ISR( void ) __attribute__((naked));\r
737 void MPU_Fault_ISR( void )\r
738 {\r
739         __asm volatile\r
740         (\r
741                 " tst lr, #4                                                                            \n"\r
742                 " ite eq                                                                                        \n"\r
743                 " mrseq r0, msp                                                                         \n"\r
744                 " mrsne r0, psp                                                                         \n"\r
745                 " ldr r1, [r0, #24]                                                                     \n"\r
746                 " ldr r2, handler_address_const                                         \n"\r
747                 " bx r2                                                                                         \n"\r
748                 " handler2_address_const: .word hard_fault_handler      \n"\r
749         );\r
750 }\r
751 /*-----------------------------------------------------------*/