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