]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_MPU_M33F_Simulator_Keil_GCC/NonSecure/main_ns.c
Use the linker script variables for MPU setup for Keil Simulator Demo
[freertos] / FreeRTOS / Demo / CORTEX_MPU_M33F_Simulator_Keil_GCC / NonSecure / main_ns.c
1 /*\r
2  * FreeRTOS Kernel V10.2.1\r
3  * Copyright (C) 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /* FreeRTOS includes. */\r
29 #include "FreeRTOS.h"\r
30 #include "task.h"\r
31 \r
32 /* Demo includes. */\r
33 #include "tz_demo.h"\r
34 #include "mpu_demo.h"\r
35 \r
36 /* Externs needed by the MPU setup code. These are defined in Scatter-Loading\r
37  * description file (FreeRTOSDemo_ns.sct). */\r
38 extern uint32_t Image$$ER_IROM_NS_PRIVILEGED$$Base;\r
39 extern uint32_t Image$$ER_IROM_NS_PRIVILEGED_ALIGN$$Limit;\r
40 extern uint32_t Image$$ER_IROM_NS_FREERTOS_SYSTEM_CALLS$$Base;\r
41 extern uint32_t Image$$ER_IROM_NS_FREERTOS_SYSTEM_CALLS_ALIGN$$Limit;\r
42 extern uint32_t Image$$ER_IROM_NS_UNPRIVILEGED$$Base;\r
43 extern uint32_t Image$$ER_IROM_NS_UNPRIVILEGED_ALIGN$$Limit;\r
44 \r
45 extern uint32_t Image$$ER_IRAM_NS_PRIVILEGED$$Base;\r
46 extern uint32_t Image$$ER_IRAM_NS_PRIVILEGED_ALIGN$$Limit;\r
47 extern uint32_t Image$$ER_IRAM_NS_UNPRIVILEGED$$Base;\r
48 extern uint32_t Image$$ER_IRAM_NS_UNPRIVILEGED_ALIGN$$Limit;\r
49 \r
50 /* Privileged flash. */\r
51 const uint32_t * __privileged_functions_start__         = ( uint32_t * ) &( Image$$ER_IROM_NS_PRIVILEGED$$Base );\r
52 const uint32_t * __privileged_functions_end__           = ( uint32_t * ) ( ( uint32_t ) &( Image$$ER_IROM_NS_PRIVILEGED_ALIGN$$Limit ) - 0x1 ); /* Last address in privileged Flash region. */\r
53 \r
54 /* Flash containing system calls. */\r
55 const uint32_t * __syscalls_flash_start__                       = ( uint32_t * ) &( Image$$ER_IROM_NS_FREERTOS_SYSTEM_CALLS$$Base );\r
56 const uint32_t * __syscalls_flash_end__                         = ( uint32_t * ) ( ( uint32_t ) &( Image$$ER_IROM_NS_FREERTOS_SYSTEM_CALLS_ALIGN$$Limit ) - 0x1 ); /* Last address in Flash region containing system calls. */\r
57 \r
58 /* Unprivileged flash. Note that the section containing system calls is\r
59  * unprivileged so that unprivileged tasks can make system calls. */\r
60 const uint32_t * __unprivileged_flash_start__           = ( uint32_t * ) &( Image$$ER_IROM_NS_UNPRIVILEGED$$Base );\r
61 const uint32_t * __unprivileged_flash_end__                     = ( uint32_t * ) ( ( uint32_t ) &( Image$$ER_IROM_NS_UNPRIVILEGED_ALIGN$$Limit ) - 0x1 ); /* Last address in un-privileged Flash region. */\r
62 \r
63 /* RAM with priviledged access only. This contains kernel data. */\r
64 const uint32_t * __privileged_sram_start__                      = ( uint32_t * ) &( Image$$ER_IRAM_NS_PRIVILEGED$$Base );\r
65 const uint32_t * __privileged_sram_end__                        = ( uint32_t * ) ( ( uint32_t ) &( Image$$ER_IRAM_NS_PRIVILEGED_ALIGN$$Limit ) - 0x1 ); /* Last address in privileged RAM. */\r
66 ;\r
67 /* Unprivileged RAM. */\r
68 const uint32_t * __unprivileged_sram_start__            = ( uint32_t * ) &( Image$$ER_IRAM_NS_UNPRIVILEGED$$Base );\r
69 const uint32_t * __unprivileged_sram_end__                      = ( uint32_t * ) ( ( uint32_t ) &( Image$$ER_IRAM_NS_UNPRIVILEGED_ALIGN$$Limit ) - 0x1 ); /* Last address in un-privileged RAM. */\r
70 /*-----------------------------------------------------------*/\r
71 \r
72 /**\r
73  * @brief Create all demo tasks.\r
74  */\r
75 static void prvCreateTasks( void );\r
76 \r
77 /**\r
78  * @brief The mem fault handler.\r
79  *\r
80  * It calls a function called vHandleMemoryFault.\r
81  */\r
82 void MemManage_Handler( void ) __attribute__ ( ( naked ) );\r
83 /*-----------------------------------------------------------*/\r
84 \r
85 /*\r
86  *      Instructions to Build and Run:\r
87  *       - The Keil multi-project workspace FreeRTOSDemo.uvmpw contains projects for\r
88  *         both the secure project, and non secure project.\r
89  *       - Set the FreeRTOSDemo_s project as Active - Right click on\r
90  *         "Project: FreeRTOSDemo_s" and select "Set as Active Project".\r
91  *       - Build the FreeRTOSDemo_s project using "Project --> Build" or by pressing\r
92  *         "F7".\r
93  *       - Set the FreeRTOSDemo_ns project as Active - Right click on\r
94  *         "Project: FreeRTOSDemo_ns" and select "Set as Active Project".\r
95  *       - Build the FreeRTOSDemo_ns project using "Project --> Build" or by\r
96  *         pressing "F7".\r
97  *       - Start Debug Session using "Debug -> Start/Stop Debug Session" or by\r
98  *         pressing "Ctrl+F5".\r
99  */\r
100 \r
101 /* Non-Secure main. */\r
102 int main( void )\r
103 {\r
104         /* Create tasks. */\r
105         prvCreateTasks();\r
106 \r
107         /* Start scheduler. */\r
108         vTaskStartScheduler();\r
109 \r
110         /* Should not reach here as the scheduler is already started. */\r
111         for( ; ; )\r
112         {\r
113         }\r
114 }\r
115 /*-----------------------------------------------------------*/\r
116 \r
117 static void prvCreateTasks( void )\r
118 {\r
119         /* Create tasks for the MPU Demo. */\r
120         vStartMPUDemo();\r
121 \r
122         /* Create tasks for the TZ Demo. */\r
123         vStartTZDemo();\r
124 \r
125 }\r
126 /*-----------------------------------------------------------*/\r
127 \r
128 /* Stack overflow hook. */\r
129 void vApplicationStackOverflowHook( TaskHandle_t xTask, signed char *pcTaskName )\r
130 {\r
131         /* Force an assert. */\r
132         configASSERT( pcTaskName == 0 );\r
133 }\r
134 /*-----------------------------------------------------------*/\r
135 \r
136 /* configUSE_STATIC_ALLOCATION is set to 1, so the application must provide an\r
137  * implementation of vApplicationGetIdleTaskMemory() to provide the memory that\r
138  * is used by the Idle task. */\r
139 void vApplicationGetIdleTaskMemory(     StaticTask_t ** ppxIdleTaskTCBBuffer,\r
140                                                                         StackType_t ** ppxIdleTaskStackBuffer,\r
141                                                                         uint32_t * pulIdleTaskStackSize )\r
142 {\r
143         /* If the buffers to be provided to the Idle task are declared inside this\r
144          * function then they must be declared static - otherwise they will be\r
145          * allocated on the stack and so not exists after this function exits. */\r
146         static StaticTask_t xIdleTaskTCB;\r
147         static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ] __attribute__((aligned(32)));\r
148 \r
149         /* Pass out a pointer to the StaticTask_t structure in which the Idle\r
150          * task's state will be stored. */\r
151         *ppxIdleTaskTCBBuffer = &xIdleTaskTCB;\r
152 \r
153         /* Pass out the array that will be used as the Idle task's stack. */\r
154         *ppxIdleTaskStackBuffer = uxIdleTaskStack;\r
155 \r
156         /* Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer.\r
157          * Note that, as the array is necessarily of type StackType_t,\r
158          * configMINIMAL_STACK_SIZE is specified in words, not bytes. */\r
159         *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;\r
160 }\r
161 /*-----------------------------------------------------------*/\r
162 \r
163 /* configUSE_STATIC_ALLOCATION and configUSE_TIMERS are both set to 1, so the\r
164  * application must provide an implementation of vApplicationGetTimerTaskMemory()\r
165  * to provide the memory that is used by the Timer service task. */\r
166 void vApplicationGetTimerTaskMemory( StaticTask_t ** ppxTimerTaskTCBBuffer,\r
167                                                                          StackType_t ** ppxTimerTaskStackBuffer,\r
168                                                                          uint32_t * pulTimerTaskStackSize )\r
169 {\r
170         /* If the buffers to be provided to the Timer task are declared inside this\r
171          * function then they must be declared static - otherwise they will be\r
172          * allocated on the stack and so not exists after this function exits. */\r
173         static StaticTask_t xTimerTaskTCB;\r
174         static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ] __attribute__((aligned(32)));\r
175 \r
176         /* Pass out a pointer to the StaticTask_t structure in which the Timer\r
177          * task's state will be stored. */\r
178         *ppxTimerTaskTCBBuffer = &xTimerTaskTCB;\r
179 \r
180         /* Pass out the array that will be used as the Timer task's stack. */\r
181         *ppxTimerTaskStackBuffer = uxTimerTaskStack;\r
182 \r
183         /* Pass out the size of the array pointed to by *ppxTimerTaskStackBuffer.\r
184          * Note that, as the array is necessarily of type StackType_t,\r
185          * configTIMER_TASK_STACK_DEPTH is specified in words, not bytes. */\r
186         *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;\r
187 }\r
188 /*-----------------------------------------------------------*/\r
189 \r
190 void MemManage_Handler( void )\r
191 {\r
192         __asm volatile\r
193         (\r
194                 " tst lr, #4                                                                            \n"\r
195                 " ite eq                                                                                        \n"\r
196                 " mrseq r0, msp                                                                         \n"\r
197                 " mrsne r0, psp                                                                         \n"\r
198                 " ldr r1, handler_address_const                                         \n"\r
199                 " bx r1                                                                                         \n"\r
200                 "                                                                                                       \n"\r
201                 " handler_address_const: .word vHandleMemoryFault       \n"\r
202         );\r
203 }\r
204 /*-----------------------------------------------------------*/\r