]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_MPU_STM32L4_Discovery_Keil_STM32Cube/Demo/app_main.c
Add MPU projects for STM32L475 Discovery Kit IoT Node
[freertos] / FreeRTOS / Demo / CORTEX_MPU_STM32L4_Discovery_Keil_STM32Cube / Demo / app_main.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 /* Scheduler includes. */\r
28 #include "FreeRTOS.h"\r
29 #include "task.h"\r
30 \r
31 /* App includes. */\r
32 #include "app_main.h"\r
33 \r
34 /* Demo includes. */\r
35 #include "mpu_demo.h"\r
36 \r
37 #if defined( __ARMCC_VERSION )\r
38         extern uint32_t Image$$ER_IROM_FREERTOS_SYSTEM_CALLS$$Base;\r
39         extern uint32_t Image$$ER_IROM_FREERTOS_SYSTEM_CALLS$$Limit;\r
40 \r
41         /* Memory map needed for MPU setup. Must must match the one defined in\r
42          * the scatter-loading file (MPUDemo.sct). */\r
43         const uint32_t * __FLASH_segment_start__ = ( uint32_t * ) 0x08000000;\r
44         const uint32_t * __FLASH_segment_end__ = ( uint32_t * ) 0x08100000;\r
45         const uint32_t * __SRAM_segment_start__ = ( uint32_t * ) 0x20000000;\r
46         const uint32_t * __SRAM_segment_end__ = ( uint32_t * ) 0x20018000;\r
47 \r
48         const uint32_t * __privileged_functions_start__ = ( uint32_t * ) 0x08000000;\r
49         const uint32_t * __privileged_functions_end__ = ( uint32_t * ) 0x08008000;\r
50         const uint32_t * __privileged_data_start__ = ( uint32_t * ) 0x20000000;\r
51         const uint32_t * __privileged_data_end__ = ( uint32_t * ) 0x20000400;\r
52 \r
53         const uint32_t * __syscalls_flash_start__ = ( uint32_t * ) &( Image$$ER_IROM_FREERTOS_SYSTEM_CALLS$$Base );\r
54         const uint32_t * __syscalls_flash_end__ = ( uint32_t * ) &( Image$$ER_IROM_FREERTOS_SYSTEM_CALLS$$Limit );\r
55 #endif /* #if defined( __ARMCC_VERSION ) */\r
56 /*-----------------------------------------------------------*/\r
57 \r
58 /**\r
59  * @brief Mem fault handler.\r
60  */\r
61 void MemManage_Handler( void ) __attribute__ (( naked ));\r
62 /*-----------------------------------------------------------*/\r
63 \r
64 void app_main( void )\r
65 {\r
66         /* Start the MPU demo. */\r
67         vStartMPUDemo();\r
68 \r
69         /* Start the scheduler. */\r
70         vTaskStartScheduler();\r
71 \r
72         /* Should not get here. */\r
73         for( ;; );\r
74 }\r
75 /*-----------------------------------------------------------*/\r
76 \r
77 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
78 {\r
79         /* If configCHECK_FOR_STACK_OVERFLOW is set to either 1 or 2 then this\r
80         function will automatically get called if a task overflows its stack. */\r
81         ( void ) pxTask;\r
82         ( void ) pcTaskName;\r
83         for( ;; );\r
84 }\r
85 /*-----------------------------------------------------------*/\r
86 \r
87 void vApplicationMallocFailedHook( void )\r
88 {\r
89         /* If configUSE_MALLOC_FAILED_HOOK is set to 1 then this function will\r
90         be called automatically if a call to pvPortMalloc() fails.  pvPortMalloc()\r
91         is called automatically when a task, queue or semaphore is created. */\r
92         for( ;; );\r
93 }\r
94 /*-----------------------------------------------------------*/\r
95 \r
96 /* configUSE_STATIC_ALLOCATION is set to 1, so the application must provide an\r
97 implementation of vApplicationGetIdleTaskMemory() to provide the memory that is\r
98 used by the Idle task. */\r
99 void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )\r
100 {\r
101 /* If the buffers to be provided to the Idle task are declared inside this\r
102 function then they must be declared static - otherwise they will be allocated on\r
103 the stack and so not exists after this function exits. */\r
104 static StaticTask_t xIdleTaskTCB;\r
105 static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];\r
106 \r
107         /* Pass out a pointer to the StaticTask_t structure in which the Idle task's\r
108         state will be stored. */\r
109         *ppxIdleTaskTCBBuffer = &xIdleTaskTCB;\r
110 \r
111         /* Pass out the array that will be used as the Idle task's stack. */\r
112         *ppxIdleTaskStackBuffer = uxIdleTaskStack;\r
113 \r
114         /* Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer.\r
115         Note that, as the array is necessarily of type StackType_t,\r
116         configMINIMAL_STACK_SIZE is specified in words, not bytes. */\r
117         *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;\r
118 }\r
119 /*-----------------------------------------------------------*/\r
120 \r
121 /* configUSE_STATIC_ALLOCATION and configUSE_TIMERS are both set to 1, so the\r
122 application must provide an implementation of vApplicationGetTimerTaskMemory()\r
123 to provide the memory that is used by the Timer service task. */\r
124 void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize )\r
125 {\r
126 /* If the buffers to be provided to the Timer task are declared inside this\r
127 function then they must be declared static - otherwise they will be allocated on\r
128 the stack and so not exists after this function exits. */\r
129 static StaticTask_t xTimerTaskTCB;\r
130 static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];\r
131 \r
132         /* Pass out a pointer to the StaticTask_t structure in which the Timer\r
133         task's state will be stored. */\r
134         *ppxTimerTaskTCBBuffer = &xTimerTaskTCB;\r
135 \r
136         /* Pass out the array that will be used as the Timer task's stack. */\r
137         *ppxTimerTaskStackBuffer = uxTimerTaskStack;\r
138 \r
139         /* Pass out the size of the array pointed to by *ppxTimerTaskStackBuffer.\r
140         Note that, as the array is necessarily of type StackType_t,\r
141         configMINIMAL_STACK_SIZE is specified in words, not bytes. */\r
142         *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;\r
143 }\r
144 /*-----------------------------------------------------------*/\r
145 \r
146 void MemManage_Handler( void )\r
147 {\r
148         __asm volatile\r
149         (\r
150                 " tst lr, #4                                                                            \n"\r
151                 " ite eq                                                                                        \n"\r
152                 " mrseq r0, msp                                                                         \n"\r
153                 " mrsne r0, psp                                                                         \n"\r
154                 " ldr r1, handler_address_const                                         \n"\r
155                 " bx r1                                                                                         \n"\r
156                 "                                                                                                       \n"\r
157                 " handler_address_const: .word vHandleMemoryFault       \n"\r
158         );\r
159 }\r
160 /*-----------------------------------------------------------*/\r