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