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