|
| 1 | +/* |
| 2 | + * Copyright 2025 SiFli |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +//! Initial firmware startup, contains the vector table that the bootloader loads. |
| 18 | +//! Based on "https://github.com/pfalcon/cortex-uni-startup/blob/master/startup.c" |
| 19 | +//! by Paul Sokolovsky (public domain) |
| 20 | + |
| 21 | +#include <stdbool.h> |
| 22 | +#include <stdint.h> |
| 23 | +#include <string.h> |
| 24 | + |
| 25 | +#include "board/board.h" |
| 26 | +#include "mcu/cache.h" |
| 27 | +#include "util/attributes.h" |
| 28 | + |
| 29 | +//! These symbols are defined in the linker script for use in initializing |
| 30 | +//! the data sections. uint8_t since we do arithmetic with section lengths. |
| 31 | +//! These are arrays to avoid the need for an & when dealing with linker symbols. |
| 32 | +extern uint8_t __data_load_start[]; |
| 33 | +extern uint8_t __data_start[]; |
| 34 | +extern uint8_t __data_end[]; |
| 35 | +extern uint8_t __bss_start[]; |
| 36 | +extern uint8_t __bss_end[]; |
| 37 | +extern uint8_t _estack[]; |
| 38 | + |
| 39 | +extern uint8_t __retm_ro_load_start[]; |
| 40 | +extern uint8_t __retm_ro_start[]; |
| 41 | +extern uint8_t __retm_ro_end[]; |
| 42 | + |
| 43 | +//! Firmware main function, ResetHandler calls this |
| 44 | +extern int main(void); |
| 45 | + |
| 46 | +//! STM32 system initialization function, defined in the standard peripheral library |
| 47 | +extern void SystemInit(void); |
| 48 | + |
| 49 | +static void boot_uart_tx(char *data, int len) { |
| 50 | + int i; |
| 51 | + |
| 52 | + for (i = 0; i < len; i++) { |
| 53 | + while ((USART1->ISR & UART_FLAG_TXE) == 0); |
| 54 | + USART1->TDR = (uint32_t)data[i]; |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +//! This function is what gets called when the processor first |
| 59 | +//! starts execution following a reset event. The data and bss |
| 60 | +//! sections are initialized, then we call the firmware's main |
| 61 | +//! function |
| 62 | +__attribute__((naked)) NORETURN Reset_Handler(void) { |
| 63 | + // Set MSPLIM |
| 64 | + // __set_MSPLIM((uint32_t)(&__STACK_LIMIT)); |
| 65 | + // __set_PSPLIM((uint32_t)(&__STACK_LIMIT)); |
| 66 | + // TODO: |
| 67 | + __set_MSPLIM((uint32_t)(0)); |
| 68 | + __set_PSPLIM((uint32_t)(0)); |
| 69 | + boot_uart_tx("Reset_Handler\n", 14); |
| 70 | + |
| 71 | + // Copy data section from flash to RAM |
| 72 | + memcpy(__data_start, __data_load_start, __data_end - __data_start); |
| 73 | + boot_uart_tx("data copied\n", 12); |
| 74 | + |
| 75 | + memcpy(__retm_ro_start, __retm_ro_load_start, __retm_ro_end - __retm_ro_start); |
| 76 | + boot_uart_tx("retm copied\n", 12); |
| 77 | + |
| 78 | + // Clear the bss section, assumes .bss goes directly after .data |
| 79 | + |
| 80 | + memset(__bss_start, 0, __bss_end - __bss_start); |
| 81 | + boot_uart_tx("bss cleared\n", 12); |
| 82 | + |
| 83 | + SystemInit(); |
| 84 | + |
| 85 | + icache_enable(); |
| 86 | + dcache_enable(); |
| 87 | + |
| 88 | + main(); |
| 89 | + |
| 90 | + // Main shouldn't return |
| 91 | + while (true) { |
| 92 | + } |
| 93 | +} |
0 commit comments