From 246e7904847b1d5b819cca8882feeb85d0b13ca3 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Wed, 15 Jul 2020 07:31:14 +1000 Subject: [PATCH] AP_Bootloader: use new hal CAN interface for CAN support --- Tools/AP_Bootloader/bl_protocol.cpp | 56 ------------------ Tools/AP_Bootloader/can.cpp | 14 +++-- Tools/AP_Bootloader/support.cpp | 57 +++++++++++++++++++ Tools/AP_Bootloader/support.h | 3 + .../hwdef/CubeOrange-periph/hwdef.dat | 2 + 5 files changed, 71 insertions(+), 61 deletions(-) diff --git a/Tools/AP_Bootloader/bl_protocol.cpp b/Tools/AP_Bootloader/bl_protocol.cpp index ff8851483d..d65aa22079 100644 --- a/Tools/AP_Bootloader/bl_protocol.cpp +++ b/Tools/AP_Bootloader/bl_protocol.cpp @@ -321,62 +321,6 @@ cout_word(uint32_t val) cout((uint8_t *)&val, 4); } -/* - we use a write buffer for flashing, both for efficiency and to - ensure that we only ever do 32 byte aligned writes on STM32H7. If - you attempt to do writes on a H7 of less than 32 bytes or not - aligned then the flash can end up in a CRC error state, which can - generate a hardware fault (a double ECC error) on flash read, even - after a power cycle - */ -static struct { - uint32_t buffer[8]; - uint32_t address; - uint8_t n; -} fbuf; - -/* - flush the write buffer - */ -static bool flash_write_flush(void) -{ - if (fbuf.n == 0) { - return true; - } - fbuf.n = 0; - return flash_func_write_words(fbuf.address, fbuf.buffer, ARRAY_SIZE(fbuf.buffer)); -} - -/* - write to flash with buffering to 32 bytes alignment - */ -static bool flash_write_buffer(uint32_t address, const uint32_t *v, uint8_t nwords) -{ - if (fbuf.n > 0 && address != fbuf.address + fbuf.n*4) { - if (!flash_write_flush()) { - return false; - } - } - while (nwords > 0) { - if (fbuf.n == 0) { - fbuf.address = address; - memset(fbuf.buffer, 0xff, sizeof(fbuf.buffer)); - } - uint8_t n = MIN(ARRAY_SIZE(fbuf.buffer)-fbuf.n, nwords); - memcpy(&fbuf.buffer[fbuf.n], v, n*4); - address += n*4; - v += n; - nwords -= n; - fbuf.n += n; - if (fbuf.n == ARRAY_SIZE(fbuf.buffer)) { - if (!flash_write_flush()) { - return false; - } - } - } - return true; -} - #define TEST_FLASH 0 #if TEST_FLASH diff --git a/Tools/AP_Bootloader/can.cpp b/Tools/AP_Bootloader/can.cpp index 991aca1674..1a8208be05 100644 --- a/Tools/AP_Bootloader/can.cpp +++ b/Tools/AP_Bootloader/can.cpp @@ -71,7 +71,7 @@ static uint8_t node_id_allocation_transfer_id; static uavcan_protocol_NodeStatus node_status; static uint32_t send_next_node_id_allocation_request_at_ms; static uint8_t node_id_allocation_unique_id_offset; -static uint32_t app_first_word = 0xFFFFFFFF; +static uint32_t app_first_words[8]; static struct { uint64_t ofs; @@ -225,11 +225,11 @@ static void handle_file_read_response(CanardInstance* ins, CanardRxTransfer* tra flash_func_erase_sector(fw_update.sector+1); } for (uint16_t i=0; ipayload_len < 1 || transfer->payload_len > sizeof(fw_update.path)+1) { return; } + + memset(app_first_words, 0xff, sizeof(app_first_words)); + if (fw_update.node_id == 0) { uint32_t offset = 0; canardDecodeScalar(transfer, 0, 8, false, (void*)&fw_update.node_id); diff --git a/Tools/AP_Bootloader/support.cpp b/Tools/AP_Bootloader/support.cpp index 12c661acf0..f4e53386ea 100644 --- a/Tools/AP_Bootloader/support.cpp +++ b/Tools/AP_Bootloader/support.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include "support.h" #include "mcu_f1.h" #include "mcu_f3.h" @@ -161,6 +162,62 @@ uint32_t flash_func_read_sn(uint32_t idx) return *(uint32_t *)(UDID_START + idx); } +/* + we use a write buffer for flashing, both for efficiency and to + ensure that we only ever do 32 byte aligned writes on STM32H7. If + you attempt to do writes on a H7 of less than 32 bytes or not + aligned then the flash can end up in a CRC error state, which can + generate a hardware fault (a double ECC error) on flash read, even + after a power cycle + */ +static struct { + uint32_t buffer[8]; + uint32_t address; + uint8_t n; +} fbuf; + +/* + flush the write buffer + */ +bool flash_write_flush(void) +{ + if (fbuf.n == 0) { + return true; + } + fbuf.n = 0; + return flash_func_write_words(fbuf.address, fbuf.buffer, ARRAY_SIZE(fbuf.buffer)); +} + +/* + write to flash with buffering to 32 bytes alignment + */ +bool flash_write_buffer(uint32_t address, const uint32_t *v, uint8_t nwords) +{ + if (fbuf.n > 0 && address != fbuf.address + fbuf.n*4) { + if (!flash_write_flush()) { + return false; + } + } + while (nwords > 0) { + if (fbuf.n == 0) { + fbuf.address = address; + memset(fbuf.buffer, 0xff, sizeof(fbuf.buffer)); + } + uint8_t n = MIN(ARRAY_SIZE(fbuf.buffer)-fbuf.n, nwords); + memcpy(&fbuf.buffer[fbuf.n], v, n*4); + address += n*4; + v += n; + nwords -= n; + fbuf.n += n; + if (fbuf.n == ARRAY_SIZE(fbuf.buffer)) { + if (!flash_write_flush()) { + return false; + } + } + } + return true; +} + uint32_t get_mcu_id(void) { return *(uint32_t *)DBGMCU_BASE; diff --git a/Tools/AP_Bootloader/support.h b/Tools/AP_Bootloader/support.h index 8096db348a..87450bef2a 100644 --- a/Tools/AP_Bootloader/support.h +++ b/Tools/AP_Bootloader/support.h @@ -30,6 +30,9 @@ uint32_t flash_func_read_sn(uint32_t idx); void flash_set_keep_unlocked(bool); void lock_bl_port(void); +bool flash_write_flush(void); +bool flash_write_buffer(uint32_t address, const uint32_t *v, uint8_t nwords); + uint32_t get_mcu_id(void); uint32_t get_mcu_desc(uint32_t len, uint8_t *buf); bool check_limit_flash_1M(void); diff --git a/libraries/AP_HAL_ChibiOS/hwdef/CubeOrange-periph/hwdef.dat b/libraries/AP_HAL_ChibiOS/hwdef/CubeOrange-periph/hwdef.dat index 30ee6df194..f262cd19e9 100644 --- a/libraries/AP_HAL_ChibiOS/hwdef/CubeOrange-periph/hwdef.dat +++ b/libraries/AP_HAL_ChibiOS/hwdef/CubeOrange-periph/hwdef.dat @@ -7,6 +7,8 @@ undef ROMFS undef HAL_HAVE_SAFETY_SWITCH undef IMU undef HAL_CHIBIOS_ARCH_FMUV3 +undef BOOTLOADER_DEV_LIST + # board ID for firmware load APJ_BOARD_ID 1400