]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_MPU_LM3Sxxxx_Rowley/main.c
Added CM3 MPU demo.
[freertos] / Demo / CORTEX_MPU_LM3Sxxxx_Rowley / main.c
1 /*\r
2         FreeRTOS V5.4.2 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4         This file is part of the FreeRTOS distribution.\r
5 \r
6         FreeRTOS is free software; you can redistribute it and/or modify it     under\r
7         the terms of the GNU General Public License (version 2) as published by the\r
8         Free Software Foundation and modified by the FreeRTOS exception.\r
9         **NOTE** The exception to the GPL is included to allow you to distribute a\r
10         combined work that includes FreeRTOS without being obliged to provide the\r
11         source code for proprietary components outside of the FreeRTOS kernel.\r
12         Alternative commercial license and support terms are also available upon\r
13         request.  See the licensing section of http://www.FreeRTOS.org for full\r
14         license details.\r
15 \r
16         FreeRTOS is distributed in the hope that it will be useful,     but WITHOUT\r
17         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
18         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
19         more details.\r
20 \r
21         You should have received a copy of the GNU General Public License along\r
22         with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59\r
23         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
24 \r
25 \r
26         ***************************************************************************\r
27         *                                                                                                                                                *\r
28         * Looking for a quick start?  Then check out the FreeRTOS eBook!                  *\r
29         * See http://www.FreeRTOS.org/Documentation for details                            *\r
30         *                                                                                                                                                *\r
31         ***************************************************************************\r
32 \r
33         1 tab == 4 spaces!\r
34 \r
35         Please ensure to read the configuration and relevant port sections of the\r
36         online documentation.\r
37 \r
38         http://www.FreeRTOS.org - Documentation, latest information, license and\r
39         contact details.\r
40 \r
41         http://www.SafeRTOS.com - A version that is certified for use in safety\r
42         critical systems.\r
43 \r
44         http://www.OpenRTOS.com - Commercial support, development, porting,\r
45         licensing and training services.\r
46 */\r
47 \r
48 /* Standard includes. */\r
49 #include <string.h>\r
50 #include <__cross_studio_io.h>\r
51 \r
52 /* Scheduler includes. */\r
53 #include "FreeRTOS.h"\r
54 #include "task.h"\r
55 #include "queue.h"\r
56 #include "semphr.h"\r
57 \r
58 /* Hardware library includes. */\r
59 #include "hw_types.h"\r
60 #include "hw_sysctl.h"\r
61 #include "sysctl.h"\r
62 \r
63 /*\r
64  * This file demonstrates the use of MPU using just three tasks - two 'reg test'\r
65  * tasks and one 'check' task.  Read the comments above the\r
66  * function prototypes for more information.\r
67  */\r
68 \r
69 /*-----------------------------------------------------------*/\r
70 \r
71 /* Misc constants. */\r
72 #define mainDONT_BLOCK                                  ( 0 )\r
73 \r
74 /* Definitions for the messages that can be sent to the check task. */\r
75 #define mainREG_TEST_1_STILL_EXECUTING  ( 0 )\r
76 #define mainREG_TEST_2_STILL_EXECUTING  ( 1 )\r
77 #define mainPRINT_SYSTEM_STATUS                 ( 2 )\r
78 \r
79 /* GCC specifics. */\r
80 #define mainALIGN_TO( x )                               __attribute__((aligned(x)))\r
81 \r
82 \r
83 /*-----------------------------------------------------------*/\r
84 /* Prototypes for functions that implement tasks. -----------*/\r
85 /*-----------------------------------------------------------*/\r
86 \r
87 /* \r
88  * Prototype for the reg test tasks.  Amongst other things, these fill the CPU\r
89  * registers with known values before checking that the registers still contain\r
90  * the expected values.  Each of the two tasks use different values so an error\r
91  * in the context switch mechanism can be caught.  Both reg test tasks execute\r
92  * at the idle priority so will get preempted regularly.\r
93  */\r
94 static void prvRegTest1Task( void *pvParameters );\r
95 static void prvRegTest2Task( void *pvParameters );\r
96 \r
97 /*\r
98  * Prototype for the check task.  The check task demonstrates various features\r
99  * of the MPU before entering a loop where it waits for commands to arrive on a\r
100  * queue.\r
101  *\r
102  * The check task will periodically be commanded to print out a status message.\r
103  * If both the reg tests tasks are executing as expected the check task will\r
104  * print "PASS" to the debug port, otherwise it will print 'FAIL'.  Debug port\r
105  * messages can be viewed within the CrossWorks IDE.\r
106  */\r
107 static void prvCheckTask( void *pvParameters );\r
108 \r
109 \r
110 \r
111 /*-----------------------------------------------------------*/\r
112 /* Prototypes for other misc functions.  --------------------*/\r
113 /*-----------------------------------------------------------*/\r
114 \r
115 /*\r
116  * Just configures any clocks and IO necessary.\r
117  */\r
118 static void prvSetupHardware( void );\r
119 \r
120 /*\r
121  * Simply deletes the calling task.  The function is provided only because it\r
122  * is simpler to call from asm code than the normal vTaskDelete() API function.\r
123  * It has the noinline attribute because it is called from asm code.\r
124  */\r
125 static void prvDeleteMe( void ) __attribute__((noinline));\r
126 \r
127 /*\r
128  * Used by both reg test tasks to send messages to the check task.  The message\r
129  * just lets the check task know that the sending is still functioning correctly.\r
130  * If a reg test task detects an error it will delete itself, and in so doing\r
131  * prevent itself from sending any more 'I'm Alive' messages to the check task.\r
132  */\r
133 static void prvSendImAlive( xQueueHandle xHandle, unsigned long ulTaskNumber );\r
134 \r
135 /*\r
136  * The check task is created with access to three memory regions (plus its\r
137  * stack).  Each memory region is configured with different parameters and\r
138  * prvTestMemoryRegions() demonstrates what can and cannot be accessed for each\r
139  * region.  prvTestMemoryRegions() also demonstrates a task that was created\r
140  * as a privileged task settings its own privilege level down to that of a user\r
141  * task.\r
142  */\r
143 static void prvTestMemoryRegions( void );\r
144 \r
145 /*-----------------------------------------------------------*/\r
146 \r
147 /* The handle of the queue used to communicate between tasks and between tasks\r
148 and interrupts.  Note that this is a file scope variable that falls outside of\r
149 any MPU region.  As such other techniques have to be used to allow the tasks\r
150 to gain access to the queue.  See the comments in the tasks themselves for \r
151 further information. */\r
152 static xQueueHandle xFileScopeCheckQueue = NULL;\r
153 \r
154 \r
155 \r
156 /*-----------------------------------------------------------*/\r
157 /* Data used by the 'check' task. ---------------------------*/\r
158 /*-----------------------------------------------------------*/\r
159 \r
160 /* Define the constants used to allocate the check task stack.  Note that the\r
161 stack size is defined in words, not bytes. */\r
162 #define mainCHECK_TASK_STACK_SIZE_WORDS 128\r
163 #define mainCHECK_TASK_STACK_ALIGNMENT ( mainCHECK_TASK_STACK_SIZE_WORDS * sizeof( portSTACK_TYPE ) )\r
164 \r
165 /* Declare the stack that will be used by the check task.  The kernel will\r
166  automatically create an MPU region for the stack.  The stack alignment must \r
167  match its size, so if 128 words are reserved for the stack then it must be \r
168  aligned to ( 128 * 4 ) bytes. */\r
169 static portSTACK_TYPE xCheckTaskStack[ mainCHECK_TASK_STACK_SIZE_WORDS ] mainALIGN_TO( mainCHECK_TASK_STACK_ALIGNMENT );\r
170 \r
171 /* Declare three arrays - an MPU region will be created for each array\r
172  using the xTaskParameters structure below.  Note that the arrays allocate \r
173 slightly more RAM than is actually assigned to the MPU region.  This is to \r
174 permit writes off the end of the array to be detected even when the arrays are \r
175 placed in adjacent memory locations (with no gaps between them).  The align \r
176 size must be a power of two. */\r
177 #define mainREAD_WRITE_ARRAY_SIZE 130\r
178 #define mainREAD_WRITE_ALIGN_SIZE 128\r
179 char cReadWriteArray[ mainREAD_WRITE_ARRAY_SIZE ] mainALIGN_TO( mainREAD_WRITE_ALIGN_SIZE );\r
180 \r
181 #define mainREAD_ONLY_ARRAY_SIZE 260\r
182 #define mainREAD_ONLY_ALIGN_SIZE 256\r
183 char cReadOnlyArray[ mainREAD_ONLY_ARRAY_SIZE ] mainALIGN_TO( mainREAD_ONLY_ALIGN_SIZE );\r
184 \r
185 #define mainPRIVILEGED_ONLY_ACCESS_ARRAY_SIZE 130\r
186 #define mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE 128\r
187 char cPrivilegedOnlyAccessArray[ mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE ] mainALIGN_TO( mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE );\r
188 \r
189 /* Fill in a xTaskParameters structure to define the check task. */\r
190 static const xTaskParameters xCheckTaskParameters =\r
191 {\r
192         prvCheckTask,                                                           /* pvTaskCode - the function that implements the task. */\r
193         ( signed char * ) "Check",                                      /* pcName                       */\r
194         mainCHECK_TASK_STACK_SIZE_WORDS,                        /* usStackDepth - defined in words, not bytes. */\r
195         ( void * ) 0x12121212,                                          /* pvParameters - this value is just to test that the parameter is being passed into the task correctly. */\r
196         ( 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
197         xCheckTaskStack,                                                        /* puxStackBuffer - the array to use as the task stack, as declared above. */\r
198 \r
199         /* xRegions - In this case the xRegions array is used to create MPU regions\r
200         for all three of the arrays declared directly above.  Each MPU region is\r
201         created with different parameters. */\r
202         {                                                                                       \r
203                 /* Base address                                 Length                                                                  Parameters */\r
204         { cReadWriteArray,                              mainREAD_WRITE_ALIGN_SIZE,                              portMPU_REGION_READ_WRITE },\r
205         { cReadOnlyArray,                               mainREAD_ONLY_ALIGN_SIZE,                               portMPU_REGION_READ_ONLY },\r
206         { cPrivilegedOnlyAccessArray,   mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE,  portMPU_REGION_PRIVILEGED_READ_WRITE }\r
207         }\r
208 };\r
209 \r
210 /* Three MPU regions are defined for use by the 'check' task when the task is \r
211 created.  These are only used to demonstrate the MPU features and are not\r
212 actually necessary for the check task to fulfill its primary purpose.  Instead\r
213 the MPU regions are replaced with those defined by xAltRegions prior to the \r
214 check task receiving any data on the queue or printing any messages to the\r
215 debug console.  The region configured by xAltRegions just gives the check task\r
216 access to the debug variables that form part of the Rowley library, and are\r
217 accessed within the debug_printf() function. */\r
218 extern unsigned long dbgCntrlWord_mempoll;\r
219 static const xMemoryRegion xAltRegions[ portNUM_CONFIGURABLE_REGIONS ] =\r
220         {                                                                                       \r
221                 /* Base address                                         Length          Parameters */\r
222         { ( void * ) &dbgCntrlWord_mempoll,     32,                     portMPU_REGION_READ_WRITE },\r
223         { 0,                                                            0,                      0 },\r
224         { 0,                                                            0,                      0 }\r
225         };\r
226 \r
227 \r
228 \r
229 /*-----------------------------------------------------------*/\r
230 /* Data used by the 'reg test' tasks. -----------------------*/\r
231 /*-----------------------------------------------------------*/\r
232 \r
233 /* Define the constants used to allocate the reg test task stacks.  Note that\r
234 that stack size is defined in words, not bytes. */\r
235 #define mainREG_TEST_STACK_SIZE_WORDS   128\r
236 #define mainREG_TEST_STACK_ALIGNMENT    ( mainREG_TEST_STACK_SIZE_WORDS * sizeof( portSTACK_TYPE ) )\r
237 \r
238 /* Declare the stacks that will be used by the reg test tasks.  The kernel will\r
239 automatically create an MPU region for the stack.  The stack alignment must \r
240 match its size, so if 128 words are reserved for the stack then it must be \r
241 aligned to ( 128 * 4 ) bytes. */\r
242 static portSTACK_TYPE xRegTest1Stack[ mainREG_TEST_STACK_SIZE_WORDS ] mainALIGN_TO( mainREG_TEST_STACK_ALIGNMENT );\r
243 static portSTACK_TYPE xRegTest2Stack[ mainREG_TEST_STACK_SIZE_WORDS ] mainALIGN_TO( mainREG_TEST_STACK_ALIGNMENT );\r
244 \r
245 /* Fill in a xTaskParameters structure per reg test task to define the tasks. */\r
246 static const xTaskParameters xRegTest1Parameters =\r
247 {\r
248         prvRegTest1Task,                                                /* pvTaskCode - the function that implements the task. */\r
249         ( signed char * ) "RegTest1",                   /* pcName                       */\r
250         mainREG_TEST_STACK_SIZE_WORDS,                  /* usStackDepth         */\r
251         ( void * ) 0x12345678,                                  /* pvParameters - this value is just to test that the parameter is being passed into the task correctly. */\r
252         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
253         xRegTest1Stack,                                                 /* puxStackBuffer - the array to use as the task stack, as declared above. */\r
254         {                                                                               /* xRegions - this task does not use any non-stack data. */\r
255                 /* Base address         Length          Parameters */\r
256         { 0x00,                         0x00,                   0x00 },\r
257         { 0x00,                         0x00,                   0x00 },\r
258         { 0x00,                         0x00,                   0x00 }\r
259         }\r
260 };\r
261 /*-----------------------------------------------------------*/\r
262 \r
263 static xTaskParameters xRegTest2Parameters =\r
264 {\r
265         prvRegTest2Task,                                /* pvTaskCode - the function that implements the task. */\r
266         ( signed char * ) "RegTest2",   /* pcName                       */\r
267         mainREG_TEST_STACK_SIZE_WORDS,  /* usStackDepth         */\r
268         ( void * ) NULL,                                /* pvParameters - this task uses the parameter to pass in a queue handle, but the queue is not created yet. */\r
269         tskIDLE_PRIORITY,                               /* uxPriority           */\r
270         xRegTest2Stack,                                 /* puxStackBuffer - the array to use as the task stack, as declared above. */\r
271         {                                                               /* xRegions - this task does not use any non-stack data. */\r
272                 /* Base address         Length          Parameters */\r
273         { 0x00,                         0x00,                   0x00 },\r
274         { 0x00,                         0x00,                   0x00 },\r
275         { 0x00,                         0x00,                   0x00 }\r
276         }\r
277 };\r
278 \r
279 /*-----------------------------------------------------------*/\r
280 \r
281 int main( void )\r
282 {\r
283         prvSetupHardware();\r
284 \r
285         /* Create the queue used to pass "I'm alive" messages to the check task. */\r
286         xFileScopeCheckQueue = xQueueCreate( 1, sizeof( unsigned long ) );\r
287 \r
288         /* One check task uses the task parameter to receive the queue handle.\r
289         This allows the file scope variable to be accessed from within the task.\r
290         The pvParameters member of xRegTest2Parameters can only be set after the\r
291         queue has been created. */\r
292         xRegTest2Parameters.pvParameters = xFileScopeCheckQueue;\r
293 \r
294         /* Create the three test tasks.  Handles to the created tasks are not\r
295         required, hence the second parameter is NULL. */\r
296         xTaskCreateRestricted( &xRegTest1Parameters, NULL );\r
297     xTaskCreateRestricted( &xRegTest2Parameters, NULL );\r
298         xTaskCreateRestricted( &xCheckTaskParameters, NULL );\r
299 \r
300         /* Start the scheduler. */\r
301         vTaskStartScheduler();\r
302 \r
303         /* Will only get here if there was insufficient memory to create the idle\r
304         task. */\r
305         for( ;; );\r
306         return 0;\r
307 }\r
308 /*-----------------------------------------------------------*/\r
309 \r
310 static void prvCheckTask( void *pvParameters )\r
311 {\r
312 /* This task is created in privileged mode so can access the file scope\r
313 queue variable.  Take a stack copy of this before the task is set into user\r
314 mode.  Once that task is in user mode the file scope queue variable will no\r
315 longer be accessible but the stack copy will. */\r
316 xQueueHandle xQueue = xFileScopeCheckQueue;\r
317 long lMessage;\r
318 unsigned long ulStillAliveCounts[ 2 ] = { 0 };\r
319 const char *pcStatusMessage = "PASS\r\n";\r
320 \r
321         /* Just to remove compiler warning. */\r
322         ( void ) pvParameters;\r
323 \r
324         /* Demonstrate how the various memory regions can and can't be accessed. \r
325         The task privilege is set down to user mode within this function. */\r
326         prvTestMemoryRegions();\r
327 \r
328         /* Change the memory regions allocated to this task to those initially\r
329         set up for demonstration purposes to those actually required by the task. */\r
330         vTaskAllocateMPURegions( NULL, xAltRegions );\r
331 \r
332         /* This loop performs the main function of the task, which is blocking\r
333         on a message queue then processing each message as it arrives. */\r
334         for( ;; )\r
335         {\r
336                 /* Wait for the next message to arrive. */\r
337                 xQueueReceive( xQueue, &lMessage, portMAX_DELAY );\r
338                 \r
339                 switch( lMessage )\r
340                 {\r
341                         case mainREG_TEST_1_STILL_EXECUTING     :       \r
342                                         /* Message from task 1, so task 1 must still be executing. */\r
343                                         ( ulStillAliveCounts[ 0 ] )++;\r
344                                         break;\r
345 \r
346                         case mainREG_TEST_2_STILL_EXECUTING     :                                               \r
347                                         /* Message from task 2, so task 2 must still be executing. */\r
348                                         ( ulStillAliveCounts[ 1 ] )++;\r
349                                         break;\r
350 \r
351                         case mainPRINT_SYSTEM_STATUS            :       \r
352                                         /* Message from tick hook, time to print out the system\r
353                                         status.  If messages has stopped arriving from either reg\r
354                                         test task then the status must be set to fail. */\r
355                                         if( ( ulStillAliveCounts[ 0 ] == 0 ) || ( ulStillAliveCounts[ 1 ] == 0 )  )\r
356                                         {\r
357                                                 /* One or both of the test tasks are no longer sending \r
358                                                 'still alive' messages. */\r
359                                                 pcStatusMessage = "FAIL\r\n";\r
360                                         }\r
361 \r
362                                         /* Print a pass/fail message to the terminal.  This will be\r
363                                         visible in the CrossWorks IDE. */\r
364                                         debug_printf( pcStatusMessage );\r
365 \r
366                                         /* Reset the count of 'still alive' messages. */\r
367                                         memset( ulStillAliveCounts, 0x00, sizeof( ulStillAliveCounts ) );\r
368                                         break;\r
369 \r
370                 default :\r
371                                         /* Something unexpected happened.  Delete this task so the \r
372                                         error is apparent (no output will be displayed). */\r
373                                         prvDeleteMe();\r
374                                         break;\r
375                 }\r
376         }\r
377 }\r
378 /*-----------------------------------------------------------*/\r
379 \r
380 static void prvTestMemoryRegions( void )\r
381 {\r
382 long l;\r
383 char cTemp;\r
384 \r
385         /* The check task is created in the privileged mode.  The privileged array \r
386         can be both read from and written to while this task is privileged. */\r
387         cPrivilegedOnlyAccessArray[ 0 ] = 'a';\r
388         if( cPrivilegedOnlyAccessArray[ 0 ] != 'a' )\r
389         {\r
390                 /* Something unexpected happened.  Delete this task so the error is\r
391                 apparent (no output will be displayed). */\r
392                 prvDeleteMe();\r
393         }\r
394 \r
395         /* Writing off the end of the RAM allocated to this task will *NOT* cause a\r
396         protection fault because the task is still executing in a privileged mode.  \r
397         Uncomment the following to test. */\r
398         /*cPrivilegedOnlyAccessArray[ mainPRIVILEGED_ONLY_ACCESS_ALIGN_SIZE ] = 'a';*/\r
399 \r
400         /* Now set the task into user mode. */\r
401         portSWITCH_TO_USER_MODE();\r
402          \r
403         /* Accessing the privileged only array will now cause a fault.  Uncomment \r
404         the following line to test. */    \r
405         /*cPrivilegedOnlyAccessArray[ 0 ] = 'a';*/\r
406 \r
407         /* The read/write array can still be successfully read and written. */\r
408         for( l = 0; l < mainREAD_WRITE_ALIGN_SIZE; l++ )\r
409         {\r
410                 cReadWriteArray[ l ] = 'a';\r
411                 if( cReadWriteArray[ l ] != 'a' )\r
412                 {\r
413                         /* Something unexpected happened.  Delete this task so the error is\r
414                         apparent (no output will be displayed). */\r
415                         prvDeleteMe();\r
416                 }\r
417         }\r
418 \r
419         /* But attempting to read or write off the end of the RAM allocated to this\r
420         task will cause a fault.  Uncomment either of the following two lines to \r
421         test. */\r
422         /* cReadWriteArray[ 0 ] = cReadWriteArray[ -1 ]; */\r
423         /* cReadWriteArray[ mainREAD_WRITE_ALIGN_SIZE ] = 0x00; */\r
424 \r
425         /* The read only array can be successfully read... */\r
426         for( l = 0; l < mainREAD_ONLY_ALIGN_SIZE; l++ )\r
427         {\r
428                 cTemp = cReadOnlyArray[ l ];\r
429         }\r
430 \r
431         /* ...but cannot be written.  Uncomment the following line to test. */\r
432         /* cReadOnlyArray[ 0 ] = 'a'; */\r
433 \r
434         /* Writing to the first and last locations in the stack array should not \r
435         cause a protection fault.  Note that doing this will cause the kernel to\r
436         detect a stack overflow if configCHECK_FOR_STACK_OVERFLOW is greater than \r
437         1. */\r
438     xCheckTaskStack[ 0 ] = 0;\r
439     xCheckTaskStack[ mainCHECK_TASK_STACK_SIZE_WORDS - 1 ] = 0;\r
440 \r
441         /* Writing off either end of the stack array should cause a protection \r
442         fault, uncomment either of the following two lines to test. */\r
443         /* xCheckTaskStack[ -1 ] = 0; */\r
444     /* xCheckTaskStack[ mainCHECK_TASK_STACK_SIZE_WORDS ] = 0; */\r
445 }\r
446 /*-----------------------------------------------------------*/\r
447 \r
448 static void prvRegTest1Task( void *pvParameters )\r
449 {\r
450 /* This task is created in privileged mode so can access the file scope\r
451 queue variable.  Take a stack copy of this before the task is set into user\r
452 mode.  Once that task is in user mode the file scope queue variable will no\r
453 longer be accessible but the stack copy will. */\r
454 xQueueHandle xQueue = xFileScopeCheckQueue;\r
455 \r
456         /* Now the queue handle has been obtained the task can switch to user \r
457         mode.  This is just one method of passing a handle into a protected\r
458         task, the other reg test task uses the task parameter instead. */\r
459     portSWITCH_TO_USER_MODE();\r
460 \r
461         /* First check that the parameter value is as expected. */\r
462         if( pvParameters != ( void * ) 0x12345678 )\r
463         {\r
464                 /* Error detected.  Delete the task so it stops communicating with\r
465                 the check task. */\r
466                 prvDeleteMe();\r
467         }\r
468 \r
469 \r
470         for( ;; )\r
471         {               \r
472                 /* This task tests the kernel context switch mechanism by reading and\r
473                 writing directly to registers - which requires the test to be written\r
474                 in assembly code. */\r
475                 __asm volatile \r
476                 (       \r
477                         "               MOV     R4, #104                        \n" /* Set registers to a known value.  R0 to R1 are done in the loop below. */\r
478                         "               MOV     R5, #105                        \n"\r
479                         "               MOV     R6, #106                        \n"\r
480                         "               MOV     R8, #108                        \n"\r
481                         "               MOV     R9, #109                        \n"\r
482                         "               MOV     R10, #110                       \n"\r
483                         "               MOV     R11, #111                       \n"\r
484                         "reg1loop:                                              \n"\r
485                         "               MOV     R0, #100                        \n" /* Set the scratch registers to known values - done inside the loop as they get clobbered. */\r
486                         "               MOV     R1, #101                        \n"\r
487                         "               MOV     R2, #102                        \n"\r
488                         "               MOV R3, #103                    \n"\r
489                         "               MOV     R12, #112                       \n"\r
490                         "               SVC #1                                  \n" /* Yield just to increase test coverage. */\r
491                         "               CMP     R0, #100                        \n" /* Check all the registers still contain their expected values. */\r
492                         "               BNE     prvDeleteMe                     \n" /* Value was not as expected, delete the task so it stops communicating with the check task. */\r
493                         "               CMP     R1, #101                        \n"\r
494                         "               BNE     prvDeleteMe                     \n"\r
495                         "               CMP     R2, #102                        \n"\r
496                         "               BNE     prvDeleteMe                     \n"\r
497                         "               CMP R3, #103                    \n"\r
498                         "               BNE     prvDeleteMe                     \n"\r
499                         "               CMP     R4, #104                        \n" \r
500                         "               BNE     prvDeleteMe                     \n" \r
501                         "               CMP     R5, #105                        \n"\r
502                         "               BNE     prvDeleteMe                     \n"\r
503                         "               CMP     R6, #106                        \n"\r
504                         "               BNE     prvDeleteMe                     \n"\r
505                         "               CMP     R8, #108                        \n"\r
506                         "               BNE     prvDeleteMe                     \n"\r
507                         "               CMP     R9, #109                        \n"\r
508                         "               BNE     prvDeleteMe                     \n"\r
509                         "               CMP     R10, #110                       \n"\r
510                         "               BNE     prvDeleteMe                     \n"\r
511                         "               CMP     R11, #111                       \n"\r
512                         "               BNE     prvDeleteMe                     \n"\r
513                         "               CMP     R12, #112                       \n"\r
514                         "               BNE     prvDeleteMe                     \n"\r
515                 );\r
516 \r
517                 /* Send mainREG_TEST_1_STILL_EXECUTING to the check task to indicate that this \r
518                 task is still functioning. */\r
519                 prvSendImAlive( xQueue, mainREG_TEST_1_STILL_EXECUTING );\r
520 \r
521                 /* Go back to check all the register values again. */\r
522                 __asm volatile( "               B reg1loop      " );\r
523         }\r
524 }\r
525 /*-----------------------------------------------------------*/\r
526 \r
527 static void prvRegTest2Task( void *pvParameters )\r
528 {\r
529 /* The queue handle is passed in as the task parameter.  This is one method of\r
530 passing data into a protected task, the other check task uses a different \r
531 method. */\r
532 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;\r
533 \r
534         for( ;; )\r
535         {\r
536                 /* This task tests the kernel context switch mechanism by reading and\r
537                 writing directly to registers - which requires the test to be written\r
538                 in assembly code. */\r
539                 __asm volatile \r
540                 (       \r
541                         "               MOV     R4, #4                          \n" /* Set registers to a known value.  R0 to R1 are done in the loop below. */\r
542                         "               MOV     R5, #5                          \n"\r
543                         "               MOV     R6, #6                          \n"\r
544                         "               MOV     R8, #8                          \n" /* Frame pointer is omitted as it must not be changed. */\r
545                         "               MOV     R9, #9                          \n"\r
546                         "               MOV     R10, 10                         \n"\r
547                         "               MOV     R11, #11                        \n"                                             \r
548                         "reg2loop:                                              \n"\r
549                         "               MOV     R0, #13                         \n" /* Set the scratch registers to known values - done inside the loop as they get clobbered. */\r
550                         "               MOV     R1, #1                          \n"\r
551                         "               MOV     R2, #2                          \n"\r
552                         "               MOV R3, #3                              \n"\r
553                         "               MOV     R12, #12                        \n"\r
554                         "               CMP     R0, #13                         \n" /* Check all the registers still contain their expected values. */\r
555                         "               BNE     prvDeleteMe                     \n" /* Value was not as expected, delete the task so it stops communicating with the check task */\r
556                         "               CMP     R1, #1                          \n"\r
557                         "               BNE     prvDeleteMe                     \n"\r
558                         "               CMP     R2, #2                          \n"\r
559                         "               BNE     prvDeleteMe                     \n"\r
560                         "               CMP R3, #3                              \n"\r
561                         "               BNE     prvDeleteMe                     \n"\r
562                         "               CMP     R4, #4                          \n"\r
563                         "               BNE     prvDeleteMe                     \n"\r
564                         "               CMP     R5, #5                          \n"\r
565                         "               BNE     prvDeleteMe                     \n"\r
566                         "               CMP     R6, #6                          \n"\r
567                         "               BNE     prvDeleteMe                     \n"\r
568                         "               CMP     R8, #8                          \n"\r
569                         "               BNE     prvDeleteMe                     \n"\r
570                         "               CMP     R9, #9                          \n"\r
571                         "               BNE     prvDeleteMe                     \n"\r
572                         "               CMP     R10, #10                        \n"\r
573                         "               BNE     prvDeleteMe                     \n"\r
574                         "               CMP     R11, #11                        \n"\r
575                         "               BNE     prvDeleteMe                     \n"\r
576                         "               CMP     R12, #12                        \n"\r
577                         "               BNE     prvDeleteMe                     \n"\r
578                 );\r
579 \r
580                 /* Send mainREG_TEST_2_STILL_EXECUTING to the check task to indicate that this \r
581                 task is still functioning. */\r
582                 prvSendImAlive( xQueue, mainREG_TEST_2_STILL_EXECUTING );\r
583 \r
584                 /* Go back to check all the register values again. */\r
585                 __asm volatile( "               B reg2loop      " );\r
586         }\r
587 }\r
588 /*-----------------------------------------------------------*/\r
589 \r
590 static void prvDeleteMe( void )\r
591 {\r
592         vTaskDelete( NULL );\r
593 }\r
594 /*-----------------------------------------------------------*/\r
595 \r
596 static void prvSendImAlive( xQueueHandle xHandle, unsigned long ulTaskNumber )\r
597 {\r
598         if( xHandle != NULL )\r
599         {\r
600                 xQueueSend( xHandle, &ulTaskNumber, mainDONT_BLOCK );\r
601         }\r
602 }\r
603 /*-----------------------------------------------------------*/\r
604 \r
605 static void prvSetupHardware( void )\r
606 {\r
607         /* If running on Rev A2 silicon, turn the LDO voltage up to 2.75V.  This is\r
608         a workaround to allow the PLL to operate reliably. */\r
609         if( DEVICE_IS_REVA2 )\r
610         {\r
611                 SysCtlLDOSet( SYSCTL_LDO_2_75V );\r
612         }\r
613 \r
614         /* Set the clocking to run from the PLL at 50 MHz */\r
615         SysCtlClockSet( SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_8MHZ );\r
616 }\r
617 /*-----------------------------------------------------------*/\r
618 \r
619 void vApplicationTickHook( void )\r
620 {\r
621 static unsigned long ulCallCount;\r
622 const unsigned long ulCallsBetweenSends = 5000 / portTICK_RATE_MS;\r
623 const unsigned long ulMessage = mainPRINT_SYSTEM_STATUS;\r
624 portBASE_TYPE xDummy;\r
625 \r
626         /* If configUSE_TICK_HOOK is set to 1 then this function will get called\r
627         from each RTOS tick.  It is called from the tick interrupt and therefore\r
628         will be executing in the privileged state. */\r
629 \r
630         ulCallCount++;\r
631 \r
632         /* Is it time to print out the pass/fail message again? */\r
633         if( ulCallCount >= ulCallsBetweenSends )\r
634         {\r
635                 ulCallCount = 0;\r
636 \r
637                 /* Send a message to the check task to command it to check that all\r
638                 the tasks are still running then print out the status. \r
639                 \r
640                 This is running in an ISR so has to use the "FromISR" version of\r
641                 xQueueSend().  Because it is in an ISR it is running with privileges\r
642                 so can access xFileScopeCheckQueue directly. */\r
643                 xQueueSendFromISR( xFileScopeCheckQueue, &ulMessage, &xDummy );\r
644         }\r
645 }\r
646 /*-----------------------------------------------------------*/\r
647 \r
648 void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed portCHAR *pcTaskName )\r
649 {\r
650         /* If configCHECK_FOR_STACK_OVERFLOW is set to either 1 or 2 then this \r
651         function will automatically get called if a task overflows its stack. */\r
652         ( void ) pxTask;\r
653         ( void ) pcTaskName;\r
654         for( ;; );\r
655 }\r
656 /*-----------------------------------------------------------*/\r
657 \r
658 void vApplicationMallocFailedHook( void )\r
659 {\r
660         /* If configUSE_MALLOC_FAILED_HOOK is set to 1 then this function will\r
661         be called automatically if a call to pvPortMalloc() fails.  pvPortMalloc()\r
662         is called automatically when a task, queue or semaphore is created. */\r
663         for( ;; );\r
664 }\r
665 /*-----------------------------------------------------------*/\r
666 \r
667 /* Just to keep the linker happy. */\r
668 void __error__( char *pcFilename, unsigned long ulLine )\r
669 {\r
670         ( void ) pcFilename;\r
671         ( void ) ulLine;\r
672         for( ;; );\r
673 }\r
674 /*-----------------------------------------------------------*/\r
675 \r
676 /* Just to keep the linker happy. */\r
677 int uipprintf( const char *fmt, ... )\r
678 {\r
679         ( void ) fmt;\r
680         return( 0 );\r
681 }\r
682 /*-----------------------------------------------------------*/\r
683 \r
684 void hard_fault_handler(unsigned int * hardfault_args)\r
685 {\r
686 unsigned int stacked_r0;\r
687 unsigned int stacked_r1;\r
688 unsigned int stacked_r2;\r
689 unsigned int stacked_r3;\r
690 unsigned int stacked_r12;\r
691 unsigned int stacked_lr;\r
692 unsigned int stacked_pc;\r
693 unsigned int stacked_psr;\r
694 \r
695         stacked_r0 = ((unsigned long) hardfault_args[0]);\r
696         stacked_r1 = ((unsigned long) hardfault_args[1]);\r
697         stacked_r2 = ((unsigned long) hardfault_args[2]);\r
698         stacked_r3 = ((unsigned long) hardfault_args[3]);\r
699 \r
700         stacked_r12 = ((unsigned long) hardfault_args[4]);\r
701         stacked_lr = ((unsigned long) hardfault_args[5]);\r
702         stacked_pc = ((unsigned long) hardfault_args[6]);\r
703         stacked_psr = ((unsigned long) hardfault_args[7]);\r
704 \r
705         /* Inspect stacked_pc to locate the offending instruction. */\r
706         for( ;; );\r
707 }\r
708 /*-----------------------------------------------------------*/\r
709 \r
710 void Fault_ISR( void ) __attribute__((naked));\r
711 void Fault_ISR( void )\r
712 {\r
713         __asm volatile\r
714         (\r
715                 " tst lr, #4                                                                            \n"\r
716                 " ite eq                                                                                        \n"\r
717                 " mrseq r0, msp                                                                         \n"\r
718                 " mrsne r0, psp                                                                         \n"\r
719                 " ldr r1, [r0, #24]                                                                     \n"\r
720                 " ldr r2, handler_address_const                                         \n"\r
721                 " bx r2                                                                                         \n"\r
722                 " handler_address_const: .word hard_fault_handler       \n"\r
723         );\r
724 }\r
725 /*-----------------------------------------------------------*/\r
726 \r
727 void MPU_Fault_ISR( void ) __attribute__((naked));\r
728 void MPU_Fault_ISR( void )\r
729 {\r
730         __asm volatile\r
731         (\r
732                 " tst lr, #4                                                                            \n"\r
733                 " ite eq                                                                                        \n"\r
734                 " mrseq r0, msp                                                                         \n"\r
735                 " mrsne r0, psp                                                                         \n"\r
736                 " ldr r1, [r0, #24]                                                                     \n"\r
737                 " ldr r2, handler_address_const                                         \n"\r
738                 " bx r2                                                                                         \n"\r
739                 " handler2_address_const: .word hard_fault_handler      \n"\r
740         );\r
741 }\r
742 /*-----------------------------------------------------------*/