Add bootloader

This commit is contained in:
Valentin Dabstep 2025-05-22 18:03:43 +03:00
parent 4f42094b0e
commit 6844ca9a8d
35 changed files with 27266 additions and 13 deletions

View file

@ -5,4 +5,4 @@ Import("env")
env.Replace(COMPILATIONDB_INCLUDE_TOOLCHAIN=True)
# override compilation DB path
env.Replace(COMPILATIONDB_PATH="compile_commands.json")
env.Replace(COMPILATIONDB_PATH="compile_commands.json")

View file

@ -0,0 +1,10 @@
Import("env")
# Custom HEX from ELF
env.AddPostAction(
"$BUILD_DIR/${PROGNAME}.elf",
env.VerboseAction(" ".join([
"$OBJCOPY", "-O", "ihex", "-R", ".eeprom",
"$BUILD_DIR/${PROGNAME}.elf", "$BUILD_DIR/${PROGNAME}.hex"
]), "Building $BUILD_DIR/${PROGNAME}.hex")
)

View file

@ -18,11 +18,17 @@ upload_protocol = stlink
debug_tool = stlink
monitor_speed = 19200
monitor_parity = N
build_flags =
-DSTM32F446xx
-D HAL_CAN_MODULE_ENABLED
-D SIMPLEFOC_PWM_LOWSIDE_ACTIVE_HIGH
-Wl,-T"${platformio.packages_dir}/framework-arduinoststm32/variants/STM32F4xx/F446R(C-E)T/ldscript.ld"
-Wl,--defsym=LD_FLASH_OFFSET=0x8000
-DAPP_OFFSET=0x8000
lib_deps =
askuric/Simple FOC@^2.3.4
pazi88/STM32_CAN@^1.1.2
extra_scripts = pre:gen_compile_commands.py
extra_scripts =
post:hex_compile.py
pre:gen_compile_commands.py

View file

@ -34,11 +34,11 @@ void setup_foc(MagneticSensorAS5045 *encoder, BLDCMotor *motor,
motor->foc_modulation = FOCModulationType::SpaceVectorPWM;
// PID Configuration
motor->PID_velocity.P = 0.75;
motor->PID_velocity.I = 20;
motor->LPF_velocity.Tf = 0.005;
motor->P_angle.P = 0.5;
motor->LPF_angle.Tf = 0.001;
motor->PID_velocity.P = 0.75f;
motor->PID_velocity.I = 20.0f;
motor->LPF_velocity.Tf = 0.005f;
motor->P_angle.P = 0.5f;
motor->LPF_angle.Tf = 0.001f;
// Motor limits
motor->velocity_limit = 40; // Speed limit in rad/s (382 rpm)

View file

@ -145,16 +145,22 @@ uint16_t calc_crc_struct(FLASH_RECORD* res){
/* from 32 to 8 bit */
for(int i = 0;i < 4;i++)
arr_res[i + 2] = (uint8_t)(res->value >> i * 4);
arr_res[i + 2] = (uint8_t)(res->value >> i * 8);
crc_res = validate_crc16(arr_res,FLASH_RECORD_SIZE - 2);
return crc_res;
}
void disable_flash_protection() {
HAL_FLASH_Unlock();
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR);
HAL_FLASH_Lock();
}
/* read struct from FLASH */
void flash_read(uint32_t addr,FLASH_RECORD* ptr){
disable_flash_protection();
uint8_t* flash_ptr = (uint8_t*)addr;
uint8_t* dest = (uint8_t*)ptr;
for(int i = 0;i < FLASH_RECORD_SIZE;i++)

View file

@ -63,8 +63,7 @@ volatile uint32_t ipsr_value = 0;
void setup(){
// Vector table initialization (commented out)
// __set_MSP(*(volatile uintф32_t*)0x08008000);
// SCB->VTOR = (volatile uint32_t)0x08008000;
SCB->VTOR = (uint32_t)0x08008004;
Serial.setRx(HARDWARE_SERIAL_RX_PIN);
Serial.setTx(HARDWARE_SERIAL_TX_PIN);
@ -78,8 +77,9 @@ volatile uint32_t ipsr_value = 0;
Can.setBaudRate(1000000);
// Настройка прерываний CAN
CAN2->IER |= CAN_IER_FMPIE0;
flash_rec = load_params();
flash_rec = load_params(); //for update write_ptr
// write_param(addr_id,39);
// flash_rec = load_params();
// Initialize FOC system
setup_foc(&encoder, &motor, &driver, &current_sense, &command, doMotor);
@ -87,7 +87,7 @@ volatile uint32_t ipsr_value = 0;
CAN2->IER |= CAN_IER_FMPIE0 | // Сообщение в FIFO0
CAN_IER_FFIE0 | // FIFO0 full
CAN_IER_FOVIE0; // FIFO0 overflow
HAL_NVIC_SetPriority(CAN2_RX0_IRQn, 1, 0);
// HAL_NVIC_SetPriority(CAN2_RX0_IRQn, 1, 0);
// HAL_NVIC_EnableIRQ(CAN2_RX0_IRQn);
// Default motor configuration
@ -101,6 +101,7 @@ volatile uint32_t ipsr_value = 0;
void loop() {
__enable_irq();
foc_step(&motor, &command);
CAN_message_t msg;
@ -114,6 +115,7 @@ void loop() {
/* If receive data from CAN */
if(CAN_GET) {
listen_can(msg);
CAN_GET = false;
}
}