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