在 MicroPython 中有一个 pyb.bootloader() 函数用于进入 DFU 模式。
可以在in their source repository找到实现的C代码。
我已经广泛使用了 STM32F4 版本 (the #else block) 和 F7 版本几次(尽管已经有一段时间了)。
我将把函数的主体放在这里,因为如果该文件发生更改,上述链接可能会变得陈旧:
// Activate the bootloader without BOOT* pins.
STATIC NORETURN mp_obj_t machine_bootloader(void) {
pyb_usb_dev_deinit();
storage_flush();
HAL_RCC_DeInit();
HAL_DeInit();
#if defined(MCU_SERIES_F7)
// arm-none-eabi-gcc 4.9.0 does not correctly inline this
// MSP function, so we write it out explicitly here.
//__set_MSP(*((uint32_t*) 0x1FF00000));
__ASM volatile ("movw r3, #0x0000\nmovt r3, #0x1FF0\nldr r3, [r3, #0]\nMSR msp, r3\n" : : : "r3", "sp");
((void (*)(void)) *((uint32_t*) 0x1FF00004))();
#else
__HAL_REMAPMEMORY_SYSTEMFLASH();
// arm-none-eabi-gcc 4.9.0 does not correctly inline this
// MSP function, so we write it out explicitly here.
//__set_MSP(*((uint32_t*) 0x00000000));
__ASM volatile ("movs r3, #0\nldr r3, [r3, #0]\nMSR msp, r3\n" : : : "r3", "sp");
((void (*)(void)) *((uint32_t*) 0x00000004))();
#endif
while (1);
}
pyb_usb_dev_deinit() 函数关闭 USB,storage_flush 写出所有缓存的文件系统数据。 HAL 函数来自 STM32Cube HAL 文件。
如果您使用较新版本的 dfu-util(IIRC 0.8 或更新版本),那么您可以指定 -s :leave 命令行选项以在刷新结束时执行新刷新的程序。结合以上内容,我无需接触电路板即可完成闪存/测试周期,并且仅在固件硬崩溃时使用 BOOT0/RESET。
还有一个名为 pydfu.py:https://github.com/micropython/micropython/blob/master/tools/pydfu.py 的 python DFU flasher,它比 dfu-util 快一点。