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