1. 简介
阅读PX4的代码不能用裸机的思维去读,不能一开始就去找main函数,PX4的代码运用了自己的OS:Nuttx。
本文的环境:
- 硬件: Holybro Pixhawk 4 (FMUv5)
- 固件: px4_fmu-v5_default
2. fmu(主处理器STM32F765)的bootloader
fmu的bootloader放在下面路径中的main.c
中
./PX4-Autopilot/platforms/nuttx/src/bootloader/stm/stm32_common
开头注释说明了这是f7的bootloader,最下面有一个bootloader_main
函数
首先进行一些初始化、检查bootloader寄存器标志位、检查force-bootloader pins等,最主要的是下面两段代码:
- 尝试进入 app
/* Try to boot the app if we think we should just go straight there */ /* 尝试进入 app ,如果我们认为不需要进入 bootloader */ if (try_boot) { /* set the boot-to-bootloader flag so that if boot fails on reset we will stop here */ #ifdef BOARD_BOOT_FAIL_DETECT board_set_rtc_signature(BOOT_RTC_SIGNATURE); #endif /* try to boot immediately */ /* 进入 app */ jump_to_app(); // If it failed to boot, reset the boot signature and stay in bootloader board_set_rtc_signature(BOOT_RTC_SIGNATURE); /* booting failed, stay in the bootloader forever */ timeout = 0; }
- 死循环运行 bootloader
while (1) { /* run the bootloader, come back after an app is uploaded or we time out */ /* 运行 bootloader */ bootloader(timeout); /* if the force-bootloader pins are strapped, just loop back */ if (board_test_force_pin()) { continue; } #if INTERFACE_USART /* if the USART port RX line is still receiving a break, just loop back */ if (board_test_usart_receiving_break()) { continue; } #endif /* set the boot-to-bootloader flag so that if boot fails on reset we will stop here */ #ifdef BOARD_BOOT_FAIL_DETECT board_set_rtc_signature(BOOT_RTC_SIGNATURE); #endif /* look to see if we can boot the app */ jump_to_app(); /* launching the app failed - stay in the bootloader forever */ timeout = 0; }