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