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