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