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