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