Skip to content

Commit

Permalink
add blackbox to simulator
Browse files Browse the repository at this point in the history
  • Loading branch information
bkleiner committed Aug 10, 2023
1 parent e55d9b5 commit 64e21e5
Show file tree
Hide file tree
Showing 9 changed files with 223 additions and 31 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,5 @@ go.sum

/.pio
__pycache__
flash.bin
flash.bin
blackbox.bin
5 changes: 3 additions & 2 deletions src/config/feature.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#define USE_SOFT_SERIAL
#define USE_SDCARD
#define USE_DATA_FLASH
#define USE_BLACKBOX

#define USE_VTX
#define USE_DIGITAL_VTX
Expand All @@ -24,4 +23,6 @@
#define USE_RX_SPI_FLYSKY
#define USE_RX_SPI_EXPRESS_LRS
#endif
#endif
#endif

#define USE_BLACKBOX
8 changes: 8 additions & 0 deletions src/io/blackbox_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "core/project.h"
#include "io/blackbox_device_flash.h"
#include "io/blackbox_device_sdcard.h"
#include "io/blackbox_device_simulator.h"
#include "util/cbor_helper.h"
#include "util/util.h"

Expand Down Expand Up @@ -78,9 +79,16 @@ void blackbox_device_init() {
target_set_feature(FEATURE_BLACKBOX);
} else
#endif
#ifdef SIMULATOR
{
dev = &blackbox_device_simulator;
target_set_feature(FEATURE_BLACKBOX);
}
#else
{
target_reset_feature(FEATURE_BLACKBOX);
}
#endif

blackbox_device_header.magic = BLACKBOX_HEADER_MAGIC;
blackbox_device_header.file_num = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/io/blackbox_device_flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "driver/spi_m25p16.h"
#include "util/util.h"

#ifdef USE_DATA_FLASH

#define FILES_SECTOR_OFFSET blackbox_bounds.sector_size
#define PAGE_SIZE M25P16_PAGE_SIZE

Expand All @@ -22,8 +24,6 @@ typedef enum {
STATE_WRITE_HEADER,
} blackbox_device_state_t;

#ifdef USE_DATA_FLASH

static blackbox_device_state_t state = STATE_DETECT;
static uint8_t should_flush = 0;

Expand Down
12 changes: 0 additions & 12 deletions src/io/blackbox_device_flash.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,4 @@

#include "blackbox_device.h"

void blackbox_device_flash_init();
blackbox_device_result_t blackbox_device_flash_update();
void blackbox_device_flash_reset();
void blackbox_device_flash_write_header();
void blackbox_device_flash_flush();

uint32_t blackbox_device_flash_usage();
bool blackbox_device_flash_ready();

void blackbox_device_flash_read(const uint32_t file_index, const uint32_t offset, uint8_t *buffer, const uint32_t size);
void blackbox_device_flash_write(const uint8_t *buffer, const uint8_t size);

extern blackbox_device_vtable_t blackbox_device_flash;
4 changes: 2 additions & 2 deletions src/io/blackbox_device_sdcard.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "blackbox_device_sdcard.h"

#ifdef USE_SDCARD

#include <string.h>

#include "core/project.h"
Expand All @@ -9,8 +11,6 @@
#define FILES_SECTOR_OFFSET 1
#define PAGE_SIZE SDCARD_PAGE_SIZE

#ifdef USE_SDCARD

typedef enum {
STATE_DETECT,
STATE_IDLE,
Expand Down
12 changes: 0 additions & 12 deletions src/io/blackbox_device_sdcard.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,4 @@

#include "blackbox_device.h"

void blackbox_device_sdcard_init();
blackbox_device_result_t blackbox_device_sdcard_update();
void blackbox_device_sdcard_reset();
void blackbox_device_sdcard_write_header();
void blackbox_device_sdcard_flush();

uint32_t blackbox_device_sdcard_usage();
bool blackbox_device_sdcard_ready();

void blackbox_device_sdcard_read(const uint32_t file_index, const uint32_t offset, uint8_t *buffer, const uint32_t size);
void blackbox_device_sdcard_write(const uint8_t *buffer, const uint8_t size);

extern blackbox_device_vtable_t blackbox_device_sdcard;
199 changes: 199 additions & 0 deletions src/io/blackbox_device_simulator.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
#include "blackbox_device_simulator.h"

#include <stdio.h>

#include "core/project.h"
#include "util/util.h"

#ifdef SIMULATOR

#define FILENAME "blackbox.bin"
#define FILES_SECTOR_OFFSET 1024
#define PAGE_SIZE 256

typedef enum {
STATE_DETECT,
STATE_IDLE,

STATE_START_WRITE,
STATE_FILL_WRITE_BUFFER,
STATE_CONTINUE_WRITE,
STATE_FINISH_WRITE,

STATE_READ_HEADER,
STATE_WRITE_HEADER,
} blackbox_device_state_t;

static blackbox_device_state_t state = STATE_DETECT;
static uint8_t should_flush = 0;
static FILE *file = NULL;

void blackbox_device_simulator_init() {
file = fopen(FILENAME, "rb+");
if (file == NULL) {
file = fopen(FILENAME, "wb+");
}
state = STATE_DETECT;
}

static void simulator_read(uint32_t addr, uint8_t *data, uint32_t size) {
fseek(file, addr, SEEK_SET);
fread(data, size, 1, file);
}

static void simulator_write(uint32_t addr, uint8_t *data, uint32_t size) {
fseek(file, addr, SEEK_SET);
fwrite(data, size, 1, file);
fflush(file);
}

blackbox_device_result_t blackbox_device_simulator_update() {
static uint32_t offset = 0;
static uint32_t write_size = PAGE_SIZE;

const uint32_t to_write = ring_buffer_available(&blackbox_encode_buffer);

simulator_do_more:
switch (state) {
case STATE_DETECT: {
const uint32_t size = 104857600;
blackbox_bounds.page_size = PAGE_SIZE;
blackbox_bounds.pages_per_sector = 1;
blackbox_bounds.sectors = size / PAGE_SIZE;
blackbox_bounds.sector_size = PAGE_SIZE;
blackbox_bounds.total_size = size;
state = STATE_READ_HEADER;
return BLACKBOX_DEVICE_WAIT;
}

case STATE_READ_HEADER:
simulator_read(0x0, (uint8_t *)&blackbox_device_header, sizeof(blackbox_device_header_t));
if (blackbox_device_header.magic == BLACKBOX_HEADER_MAGIC) {
state = STATE_IDLE;
break;
}

blackbox_device_header.magic = BLACKBOX_HEADER_MAGIC;
blackbox_device_header.file_num = 0;
state = STATE_WRITE_HEADER;
break;

case STATE_IDLE:
if (should_flush == 1) {
if (to_write > 0 && offset < blackbox_bounds.total_size) {
state = STATE_START_WRITE;
} else {
state = STATE_WRITE_HEADER;
should_flush = 0;
}
goto simulator_do_more;
}
if (to_write >= PAGE_SIZE) {
state = STATE_START_WRITE;
goto simulator_do_more;
}
break;

case STATE_START_WRITE: {
offset = blackbox_current_file()->start + blackbox_current_file()->size;
if (offset >= blackbox_bounds.total_size) {
state = STATE_IDLE;
break;
}
state = STATE_FILL_WRITE_BUFFER;
goto simulator_do_more;
}

case STATE_FILL_WRITE_BUFFER: {
write_size = PAGE_SIZE;
if (to_write < PAGE_SIZE) {
if (should_flush == 0) {
break;
}
if (to_write == 0) {
state = STATE_FINISH_WRITE;
goto simulator_do_more;
}

write_size = to_write;
}

ring_buffer_read_multi(&blackbox_encode_buffer, blackbox_write_buffer, write_size);
state = STATE_CONTINUE_WRITE;
break;
}

case STATE_CONTINUE_WRITE: {
simulator_write(offset, blackbox_write_buffer, PAGE_SIZE);
blackbox_current_file()->size += write_size;
state = STATE_FINISH_WRITE;
return BLACKBOX_DEVICE_WRITE;
}

case STATE_FINISH_WRITE: {
state = STATE_IDLE;
goto simulator_do_more;
}

case STATE_WRITE_HEADER: {
simulator_write(0x0, (uint8_t *)&blackbox_device_header, sizeof(blackbox_device_header_t));
state = STATE_IDLE;
return BLACKBOX_DEVICE_WAIT;
}
}

return BLACKBOX_DEVICE_IDLE;
}

void blackbox_device_simulator_reset() {
file = freopen(FILENAME, "wb+", file);
state = STATE_WRITE_HEADER;
}

uint32_t blackbox_device_simulator_usage() {
if (blackbox_device_header.file_num == 0) {
return FILES_SECTOR_OFFSET;
}
return blackbox_current_file()->start + blackbox_current_file()->size;
}

void blackbox_device_simulator_flush() {
should_flush = 1;
}

void blackbox_device_simulator_write_header() {
state = STATE_WRITE_HEADER;
}

bool blackbox_device_simulator_ready() {
return state == STATE_IDLE;
}

void blackbox_device_simulator_write(const uint8_t *buffer, const uint8_t size) {
if (size >= ring_buffer_free(&blackbox_encode_buffer)) {
return;
}

ring_buffer_write_multi(&blackbox_encode_buffer, buffer, size);
}

void blackbox_device_simulator_read(const uint32_t file_index, const uint32_t offset, uint8_t *buffer, const uint32_t size) {
const blackbox_device_file_t *file = &blackbox_device_header.files[file_index];
simulator_read(file->start + offset, buffer, size);
}

blackbox_device_vtable_t blackbox_device_simulator = {
.init = blackbox_device_simulator_init,
.update = blackbox_device_simulator_update,
.reset = blackbox_device_simulator_reset,
.write_header = blackbox_device_simulator_write_header,
.flush = blackbox_device_simulator_flush,

.usage = blackbox_device_simulator_usage,
.ready = blackbox_device_simulator_ready,

.read = blackbox_device_simulator_read,
.write = blackbox_device_simulator_write,
};

#endif
7 changes: 7 additions & 0 deletions src/io/blackbox_device_simulator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include <stdbool.h>

#include "blackbox_device.h"

extern blackbox_device_vtable_t blackbox_device_simulator;

0 comments on commit 64e21e5

Please sign in to comment.