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