Skip to content

Commit bbad477

Browse files
committed
fix: startup
1 parent 056e231 commit bbad477

4 files changed

Lines changed: 100 additions & 1 deletion

File tree

src/fw/board/boards/board_em_lb525.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ static UART_HandleTypeDef s_dbg_uart_handle = {.Instance = USART3,
2828
}};
2929
static DMA_HandleTypeDef s_dbg_uart_rx_dma_handle = {
3030
.Instance = DMA1_Channel1,
31+
.Init.Request = DMA_REQUEST_7,
3132
};
3233
static UARTDevice DBG_UART_DEVICE = {.state = &s_dbg_uart_state,
3334
.tx_gpio = PAD_PA20,

src/fw/drivers/sf32lb/uart.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ static UARTPinFunction_t get_uart_pin_fun(UART_HandleTypeDef *uart) {
138138
}
139139

140140
static void prv_init(UARTDevice *dev, UARTInitMode_t mode) {
141+
if (mode == UART_FullDuplex) {
142+
dev->periph->Init.Mode = UART_MODE_TX_RX;
143+
} else {
144+
dev->periph->Init.Mode = (mode == UART_TxOnly) ? UART_MODE_TX : UART_MODE_RX;
145+
}
141146
if (HAL_UART_Init(dev->periph) != HAL_OK) {
142147
// Initialization Error
143148
WTF;

src/fw/startup/startup_sf32lb.c

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
}

src/fw/startup/wscript_build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ elif bld.is_asterix():
1414
elif bld.is_sifli():
1515
# sources.append(bld.path.make_node("../../../third_party/hal_sifli/SiFliSDK/drivers/cmsis/sf32lb52x/Templates/gcc/startup_bf0_hcpu.s"))
1616
sources.append(bld.path.make_node("../../../third_party/hal_sifli/SiFliSDK/drivers/cmsis/sf32lb52x/Templates/system_bf0_ap.c"))
17-
sources.append(bld.path.make_node("startup_stm32.c")) # good enough
17+
sources.append(bld.path.make_node("startup_sf32lb.c")) # good enough
1818
else:
1919
bld.fatal("No clock configuration file specified for this platform")
2020

0 commit comments

Comments
 (0)