OLD | NEW |
| (Empty) |
1 // Copyright (c) 2016, the Dartino project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE.md file. | |
4 | |
5 #include <stm32f7xx_hal.h> | |
6 #include <stm32746g_discovery_sdram.h> | |
7 #include <cmsis_os.h> | |
8 | |
9 #include "src/shared/assert.h" | |
10 | |
11 #include "platforms/stm/disco_fletch/src/page_allocator.h" | |
12 #include "platforms/stm/disco_fletch/src/cmpctmalloc.h" | |
13 #include "platforms/stm/disco_fletch/src/fletch_entry.h" | |
14 | |
15 // Definition of functions in generated/Src/mx_main.c. | |
16 extern "C" { | |
17 void SystemClock_Config(void); | |
18 void MX_GPIO_Init(void); | |
19 void MX_DCMI_Init(void); | |
20 void MX_DMA2D_Init(void); | |
21 void MX_FMC_Init(void); | |
22 void MX_ETH_Init(void); | |
23 void MX_I2C1_Init(void); | |
24 void MX_LTDC_Init(void); | |
25 void MX_QUADSPI_Init(void); | |
26 void MX_SDMMC1_SD_Init(void); | |
27 void MX_SPDIFRX_Init(void); | |
28 void MX_USART1_UART_Init(void); | |
29 } // extern "C" | |
30 | |
31 // This object is initialized during EarlyInit. | |
32 PageAllocator* page_allocator; | |
33 | |
34 #define MAX_STACK_SIZE 0x2000 | |
35 | |
36 // Wrapping of the static initialization to configure the C/C++ heap | |
37 // first. | |
38 void EarlyInit(); | |
39 extern "C" void __real___libc_init_array(); | |
40 extern "C" void __wrap___libc_init_array() { | |
41 EarlyInit(); | |
42 __real___libc_init_array(); | |
43 } | |
44 | |
45 // Wrapping of all malloc/free calls in newlib. This should cause the | |
46 // newlib malloc to never be used, and sbrk should never be called for | |
47 // memory. | |
48 extern "C" void *__wrap__malloc_r(struct _reent *reent, size_t size) { | |
49 return cmpct_alloc(size); | |
50 } | |
51 | |
52 extern "C" void *__wrap__realloc_r( | |
53 struct _reent *reent, void *ptr, size_t size) { | |
54 return cmpct_realloc(ptr, size); | |
55 } | |
56 | |
57 extern "C" void *__wrap__calloc_r( | |
58 struct _reent *reent, size_t nmemb, size_t size) { | |
59 if (nmemb == 0 || size == 0) return NULL; | |
60 size = nmemb * size; | |
61 void *ptr = cmpct_alloc(size); | |
62 memset(ptr, 0, size); | |
63 return ptr; | |
64 } | |
65 | |
66 extern "C" void __wrap__free_r(struct _reent *reent, void *ptr) { | |
67 cmpct_free(ptr); | |
68 } | |
69 | |
70 // Early initialization before static initialization. This will | |
71 // configure the C/C++ heap. | |
72 void EarlyInit() { | |
73 // Get the free location in system RAM just after the bss segment. | |
74 extern char end asm("end"); | |
75 uintptr_t heap_start = reinterpret_cast<uintptr_t>(&end); | |
76 | |
77 // Reserve a map for 72 pages (256k + 16k + 16k) in .bss. | |
78 const size_t kDefaultPageMapSize = 72; | |
79 static uint8_t default_page_map[kDefaultPageMapSize]; | |
80 // Allocate a PageAllocator in system RAM. | |
81 page_allocator = reinterpret_cast<PageAllocator*>(heap_start); | |
82 page_allocator->Initialize(); | |
83 heap_start += sizeof(PageAllocator); | |
84 // Use the NVIC offset register to locate the main stack pointer. | |
85 uintptr_t min_stack_ptr = | |
86 (uintptr_t) (*(unsigned int*) *(unsigned int*) 0xE000ED08); | |
87 // Locate the stack bottom address. | |
88 min_stack_ptr -= MAX_STACK_SIZE; | |
89 // Add the system RAM as the initial arena. | |
90 uint32_t arena_id = page_allocator->AddArena( | |
91 "System RAM", heap_start, min_stack_ptr - heap_start, | |
92 default_page_map, kDefaultPageMapSize); | |
93 ASSERT(arena_id == 1); | |
94 | |
95 // Initialize the compact C/C++ heap implementation. | |
96 cmpct_init(); | |
97 } | |
98 | |
99 extern "C" void* page_alloc(size_t pages) { | |
100 return page_allocator->AllocatePages(pages); | |
101 } | |
102 | |
103 extern "C" void page_free(void* start, size_t pages) { | |
104 return page_allocator->FreePages(start, pages); | |
105 } | |
106 | |
107 int main() { | |
108 // Reset of all peripherals, and initialize the Flash interface and | |
109 // the Systick. | |
110 HAL_Init(); | |
111 | |
112 // Configure the system clock. Thie functions is defined in | |
113 // generated/Src/main.c. | |
114 SystemClock_Config(); | |
115 | |
116 // Initialize all configured peripherals. These functions are | |
117 // defined in generated/Src/mx_main.c. We are not calling | |
118 // MX_FMC_Init, as BSP_SDRAM_Init will do all initialization of the | |
119 // FMC. | |
120 MX_GPIO_Init(); | |
121 MX_DCMI_Init(); | |
122 MX_DMA2D_Init(); | |
123 MX_ETH_Init(); | |
124 MX_I2C1_Init(); | |
125 MX_LTDC_Init(); | |
126 MX_QUADSPI_Init(); | |
127 MX_SDMMC1_SD_Init(); | |
128 MX_SPDIFRX_Init(); | |
129 MX_USART1_UART_Init(); | |
130 | |
131 // Initialize the SDRAM (including FMC). | |
132 BSP_SDRAM_Init(); | |
133 | |
134 osThreadDef(mainTask, FletchEntry, osPriorityNormal, 0, 4 * 1024); | |
135 osThreadId mainTaskHandle = osThreadCreate(osThread(mainTask), NULL); | |
136 USE(mainTaskHandle); | |
137 | |
138 // Start the scheduler. | |
139 osKernelStart(); | |
140 | |
141 // We should never get as the scheduler should never terminate. | |
142 FATAL("Returned from scheduler"); | |
143 } | |
OLD | NEW |