From b4d9f78b69fb8b15099f8e974a42a31e81f68e58 Mon Sep 17 00:00:00 2001 From: bkleiner Date: Thu, 7 Nov 2024 17:49:24 +0100 Subject: [PATCH] blackbox: automatically erase flash --- src/driver/blackbox/m25p16.c | 22 ++++++++++++++++++++++ src/driver/blackbox/m25p16.h | 1 + src/io/blackbox.h | 3 +++ src/io/blackbox_device.h | 2 +- src/io/blackbox_device_flash.c | 17 ++++++++++++----- 5 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/driver/blackbox/m25p16.c b/src/driver/blackbox/m25p16.c index 8ce31afcf..0c8ddad11 100644 --- a/src/driver/blackbox/m25p16.c +++ b/src/driver/blackbox/m25p16.c @@ -154,6 +154,28 @@ bool m25p16_write_addr(const uint8_t cmd, const uint32_t addr, uint8_t *data, co return true; } +bool m25p16_chip_erase() { + if (!m25p16_is_ready()) { + return false; + } + + { + const spi_txn_segment_t segs[] = { + spi_make_seg_const(M25P16_WRITE_ENABLE), + }; + spi_seg_submit(&bus, segs); + } + { + const spi_txn_segment_t segs[] = { + spi_make_seg_const(M25P16_BULK_ERASE), + }; + spi_seg_submit(&bus, segs); + } + + spi_txn_continue(&bus); + return true; +} + void m25p16_get_bounds(blackbox_device_bounds_t *blackbox_bounds) { uint8_t raw_id[3]; m25p16_read_command(M25P16_READ_IDENTIFICATION, raw_id, 3); diff --git a/src/driver/blackbox/m25p16.h b/src/driver/blackbox/m25p16.h index 6ef6b8968..614ab47d3 100644 --- a/src/driver/blackbox/m25p16.h +++ b/src/driver/blackbox/m25p16.h @@ -35,3 +35,4 @@ uint8_t m25p16_read_command(const uint8_t cmd, uint8_t *data, const uint32_t len uint8_t m25p16_read_addr(const uint8_t cmd, const uint32_t addr, uint8_t *data, const uint32_t len); bool m25p16_write_addr(const uint8_t cmd, const uint32_t addr, uint8_t *data, const uint32_t len); bool m25p16_page_program(const uint32_t addr, const uint8_t *buf, const uint32_t size); +bool m25p16_chip_erase(); diff --git a/src/io/blackbox.h b/src/io/blackbox.h index 376d9066f..e440ccea5 100644 --- a/src/io/blackbox.h +++ b/src/io/blackbox.h @@ -1,6 +1,9 @@ #pragma once #include "core/profile.h" +#include "util/util.h" + +#define BLACKBOX_VERSION MAKE_SEMVER(0, 1, 0) #define BLACKBOX_SCALE 1000 #define BLACKBOX_DEBUG_SIZE 10 diff --git a/src/io/blackbox_device.h b/src/io/blackbox_device.h index bee9c9a85..032a2754f 100644 --- a/src/io/blackbox_device.h +++ b/src/io/blackbox_device.h @@ -5,7 +5,7 @@ #include "io/blackbox.h" #include "util/ring_buffer.h" -#define BLACKBOX_HEADER_MAGIC 0xdeadbeef +#define BLACKBOX_HEADER_MAGIC (0xdeadbeef ^ (sizeof(blackbox_t) << 24) ^ BLACKBOX_VERSION) // max size for a given entry #define BLACKBOX_MAX_SIZE 255 diff --git a/src/io/blackbox_device_flash.c b/src/io/blackbox_device_flash.c index 7c4aa69fe..b485da628 100644 --- a/src/io/blackbox_device_flash.c +++ b/src/io/blackbox_device_flash.c @@ -18,6 +18,8 @@ typedef enum { STATE_READ_HEADER, + STATE_ERASE_CHIP, + STATE_ERASE_HEADER, STATE_WRITE_HEADER, } blackbox_device_state_t; @@ -95,12 +97,18 @@ bool blackbox_device_flash_update() { blackbox_device_header.magic = BLACKBOX_HEADER_MAGIC; blackbox_device_header.file_num = 0; - state = STATE_ERASE_HEADER; + state = STATE_ERASE_CHIP; } else { state = STATE_IDLE; } return false; + case STATE_ERASE_CHIP: + if (m25p16_chip_erase()) { + state = STATE_WRITE_HEADER; + } + return false; + case STATE_ERASE_HEADER: { if (m25p16_write_addr(M25P16_SECTOR_ERASE, 0x0, NULL, 0)) { state = STATE_WRITE_HEADER; @@ -120,12 +128,11 @@ bool blackbox_device_flash_update() { } void blackbox_device_flash_reset() { - m25p16_command(M25P16_WRITE_ENABLE); - m25p16_command(M25P16_BULK_ERASE); - + while (!m25p16_chip_erase()) + ; m25p16_wait_for_ready(); - state = STATE_ERASE_HEADER; + state = STATE_WRITE_HEADER; } uint32_t blackbox_device_flash_usage() {