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