|
| 1 | +/* |
| 2 | + This program is free software: you can redistribute it and/or modify |
| 3 | + it under the terms of the GNU General Public License as published by |
| 4 | + the Free Software Foundation, either version 3 of the License, or |
| 5 | + (at your option) any later version. |
| 6 | +
|
| 7 | + This program is distributed in the hope that it will be useful, |
| 8 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | + GNU General Public License for more details. |
| 11 | +
|
| 12 | + You should have received a copy of the GNU General Public License |
| 13 | + along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 14 | + */ |
| 15 | +/* |
| 16 | + ArduPilot bootloader. This implements the same protocol originally |
| 17 | + developed for PX4, but builds on top of the ChibiOS HAL |
| 18 | +
|
| 19 | + It does not use the full AP_HAL API in order to keep the firmware |
| 20 | + size below the maximum of 16kByte required for F4 based |
| 21 | + boards. Instead it uses the ChibiOS APIs directly |
| 22 | + */ |
| 23 | + |
| 24 | +#include <AP_HAL/AP_HAL.h> |
| 25 | +#include "ch.h" |
| 26 | +#include "hal.h" |
| 27 | +#include "hwdef.h" |
| 28 | +#include <AP_HAL_ChibiOS/hwdef/common/usbcfg.h> |
| 29 | +#include <AP_HAL_ChibiOS/hwdef/common/stm32_util.h> |
| 30 | +#include <AP_HAL_ChibiOS/hwdef/common/watchdog.h> |
| 31 | +#include "support.h" |
| 32 | +#include "bl_protocol.h" |
| 33 | +#include "can.h" |
| 34 | +#include <stdio.h> |
| 35 | +#if EXTERNAL_PROG_FLASH_MB |
| 36 | +#include <AP_FlashIface/AP_FlashIface_JEDEC.h> |
| 37 | +#endif |
| 38 | + |
| 39 | +extern "C" { |
| 40 | + int main(void); |
| 41 | +} |
| 42 | + |
| 43 | +struct boardinfo board_info = { |
| 44 | + .board_type = APJ_BOARD_ID, |
| 45 | + .board_rev = 0, |
| 46 | + .fw_size = (BOARD_FLASH_SIZE - (FLASH_BOOTLOADER_LOAD_KB + FLASH_RESERVE_END_KB + APP_START_OFFSET_KB))*1024, |
| 47 | + .extf_size = (EXTERNAL_PROG_FLASH_MB * 1024 * 1024) |
| 48 | +}; |
| 49 | + |
| 50 | +#ifndef HAL_BOOTLOADER_TIMEOUT |
| 51 | +#define HAL_BOOTLOADER_TIMEOUT 5000 |
| 52 | +#endif |
| 53 | + |
| 54 | +#ifndef HAL_STAY_IN_BOOTLOADER_VALUE |
| 55 | +#define HAL_STAY_IN_BOOTLOADER_VALUE 0 |
| 56 | +#endif |
| 57 | + |
| 58 | +#if EXTERNAL_PROG_FLASH_MB |
| 59 | +AP_FlashIface_JEDEC ext_flash; |
| 60 | +#endif |
| 61 | + |
| 62 | +int main(void) |
| 63 | +{ |
| 64 | + if (BOARD_FLASH_SIZE > 1024 && check_limit_flash_1M()) { |
| 65 | + board_info.fw_size = (1024 - (FLASH_BOOTLOADER_LOAD_KB + APP_START_OFFSET_KB))*1024; |
| 66 | + } |
| 67 | + |
| 68 | + bool try_boot = false; |
| 69 | + uint32_t timeout = HAL_BOOTLOADER_TIMEOUT; |
| 70 | + |
| 71 | +#ifdef HAL_BOARD_AP_PERIPH_ZUBAXGNSS |
| 72 | + // setup remapping register for ZubaxGNSS |
| 73 | + uint32_t mapr = AFIO->MAPR; |
| 74 | + mapr &= ~AFIO_MAPR_SWJ_CFG; |
| 75 | + mapr |= AFIO_MAPR_SWJ_CFG_JTAGDISABLE; |
| 76 | + AFIO->MAPR = mapr | AFIO_MAPR_CAN_REMAP_REMAP2 | AFIO_MAPR_SPI3_REMAP; |
| 77 | +#endif |
| 78 | + |
| 79 | +#ifndef NO_FASTBOOT |
| 80 | + enum rtc_boot_magic m = check_fast_reboot(); |
| 81 | + bool was_watchdog = stm32_was_watchdog_reset(); |
| 82 | + if (was_watchdog) { |
| 83 | + try_boot = true; |
| 84 | + timeout = 0; |
| 85 | + } else if (m == RTC_BOOT_HOLD) { |
| 86 | + timeout = 0; |
| 87 | + } else if (m == RTC_BOOT_FAST) { |
| 88 | + try_boot = true; |
| 89 | + timeout = 0; |
| 90 | + } |
| 91 | +#if HAL_USE_CAN == TRUE || HAL_NUM_CAN_IFACES |
| 92 | + else if ((m & 0xFFFFFF00) == RTC_BOOT_CANBL) { |
| 93 | + try_boot = false; |
| 94 | + timeout = 10000; |
| 95 | + can_set_node_id(m & 0xFF); |
| 96 | + } |
| 97 | + can_check_update(); |
| 98 | + if (!can_check_firmware()) { |
| 99 | + // bad firmware CRC, don't try and boot |
| 100 | + timeout = 0; |
| 101 | + try_boot = false; |
| 102 | + } |
| 103 | +#ifndef BOOTLOADER_DEV_LIST |
| 104 | + else if (timeout != 0) { |
| 105 | + // fast boot for good firmware |
| 106 | + try_boot = true; |
| 107 | + timeout = 1000; |
| 108 | + } |
| 109 | +#endif |
| 110 | + if (was_watchdog && m != RTC_BOOT_FWOK) { |
| 111 | + // we've had a watchdog within 30s of booting main CAN |
| 112 | + // firmware. We will stay in bootloader to allow the user to |
| 113 | + // load a fixed firmware |
| 114 | + stm32_watchdog_clear_reason(); |
| 115 | + try_boot = false; |
| 116 | + timeout = 0; |
| 117 | + } |
| 118 | +#endif |
| 119 | +#if defined(HAL_GPIO_PIN_VBUS) && defined(HAL_ENABLE_VBUS_CHECK) |
| 120 | +#if HAL_USE_SERIAL_USB == TRUE |
| 121 | + else if (palReadLine(HAL_GPIO_PIN_VBUS) == 0) { |
| 122 | + try_boot = true; |
| 123 | + timeout = 0; |
| 124 | + } |
| 125 | +#endif |
| 126 | +#endif |
| 127 | + |
| 128 | + // if we fail to boot properly we want to pause in bootloader to give |
| 129 | + // a chance to load new app code |
| 130 | + set_fast_reboot(RTC_BOOT_OFF); |
| 131 | +#endif |
| 132 | + |
| 133 | +#ifdef HAL_GPIO_PIN_STAY_IN_BOOTLOADER |
| 134 | + // optional "stay in bootloader" pin |
| 135 | + if (palReadLine(HAL_GPIO_PIN_STAY_IN_BOOTLOADER) == HAL_STAY_IN_BOOTLOADER_VALUE) { |
| 136 | + try_boot = false; |
| 137 | + timeout = 0; |
| 138 | + } |
| 139 | +#endif |
| 140 | + |
| 141 | + if (try_boot) { |
| 142 | + jump_to_app(); |
| 143 | + } |
| 144 | + |
| 145 | +#if defined(BOOTLOADER_DEV_LIST) |
| 146 | + init_uarts(); |
| 147 | +#endif |
| 148 | +#if HAL_USE_CAN == TRUE || HAL_NUM_CAN_IFACES |
| 149 | + can_start(); |
| 150 | +#endif |
| 151 | + flash_init(); |
| 152 | + |
| 153 | + |
| 154 | +#if EXTERNAL_PROG_FLASH_MB |
| 155 | + while (!ext_flash.init()) { |
| 156 | + // keep trying until we get it working |
| 157 | + // there's no future without it |
| 158 | + chThdSleep(1000); |
| 159 | + } |
| 160 | +#endif |
| 161 | + |
| 162 | +#if defined(BOOTLOADER_DEV_LIST) |
| 163 | + while (true) { |
| 164 | + bootloader(timeout); |
| 165 | + jump_to_app(); |
| 166 | + } |
| 167 | +#else |
| 168 | + // CAN only |
| 169 | + while (true) { |
| 170 | + uint32_t t0 = AP_HAL::millis(); |
| 171 | + while (timeout == 0 || AP_HAL::millis() - t0 <= timeout) { |
| 172 | + can_update(); |
| 173 | + chThdSleep(chTimeMS2I(1)); |
| 174 | + } |
| 175 | + jump_to_app(); |
| 176 | + } |
| 177 | +#endif |
| 178 | +} |
| 179 | + |
| 180 | + |
0 commit comments