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