Skip to content

Commit

Permalink
blackbox: automatically erase flash
Browse files Browse the repository at this point in the history
  • Loading branch information
bkleiner committed Nov 7, 2024
1 parent c3606a9 commit b4d9f78
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 6 deletions.
22 changes: 22 additions & 0 deletions src/driver/blackbox/m25p16.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/driver/blackbox/m25p16.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
3 changes: 3 additions & 0 deletions src/io/blackbox.h
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/io/blackbox_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 12 additions & 5 deletions src/io/blackbox_device_flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ typedef enum {

STATE_READ_HEADER,

STATE_ERASE_CHIP,

STATE_ERASE_HEADER,
STATE_WRITE_HEADER,
} blackbox_device_state_t;
Expand Down Expand Up @@ -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;
Expand All @@ -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() {
Expand Down

0 comments on commit b4d9f78

Please sign in to comment.