From 53bc2d5fa055d1c2b5e4be838e5eef49f4a3ac68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikael=20=C3=85gren?= Date: Thu, 15 May 2025 15:51:27 +0200 Subject: [PATCH] fw: Update flash_write_data() to handle sizes larger than 4096 bytes --- hw/application_fpga/fw/tk1/flash.c | 12 +++++++++++- hw/application_fpga/fw/tk1/preload_app.c | 12 ++++++------ hw/application_fpga/fw/tk1/storage.c | 7 +++---- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/hw/application_fpga/fw/tk1/flash.c b/hw/application_fpga/fw/tk1/flash.c index 68b344c..5c0d374 100644 --- a/hw/application_fpga/fw/tk1/flash.c +++ b/hw/application_fpga/fw/tk1/flash.c @@ -176,7 +176,11 @@ int flash_write_data(uint32_t address, uint8_t *data, size_t size) return -1; } - if (size <= 0 || size > 4096) { + if (size <= 0) { + return -1; + } + + if (address % 256 != 0) { return -1; } @@ -184,6 +188,12 @@ int flash_write_data(uint32_t address, uint8_t *data, size_t size) uint8_t *p_data = data; size_t n_bytes = 0; + // Page Program allows 1-256 bytes of a page to be written. A page is + // 256 bytes. Behavior when writing past the end of a page is device + // specific. + // + // We set the address LSByte to 0 and only write 256 bytes or less in + // each transfer. uint8_t tx_buf[4] = { PAGE_PROGRAM, /* tx_buf[0] */ (address >> ADDR_BYTE_3_BIT) & 0xFF, /* tx_buf[1] */ diff --git a/hw/application_fpga/fw/tk1/preload_app.c b/hw/application_fpga/fw/tk1/preload_app.c index 80fa3b5..971077d 100644 --- a/hw/application_fpga/fw/tk1/preload_app.c +++ b/hw/application_fpga/fw/tk1/preload_app.c @@ -43,12 +43,12 @@ int preload_load(struct partition_table *part_table, uint8_t from_slot) return ret; } -// preload_store stores chunks of an app in app slot to_slot. data is -// a buffer of size size (max 4096 bytes) to be written at byte offset -// in the slot. offset needs to be kept and updated between each call. +// preload_store stores chunks of an app in app slot to_slot. data is a buffer +// of size size to be written at byte offset in the slot. offset needs to be +// kept and updated between each call. offset must be a multiple of 256. // -// When all data has been written call preload_store_finalize() with -// the last parameters. +// When all data has been written call preload_store_finalize() with the last +// parameters. // // Returns 0 on success. int preload_store(struct partition_table *part_table, uint32_t offset, @@ -77,7 +77,7 @@ int preload_store(struct partition_table *part_table, uint32_t offset, return -1; } - if (size > 4096) { + if (size > SIZE_PRE_LOADED_APP) { return -1; } diff --git a/hw/application_fpga/fw/tk1/storage.c b/hw/application_fpga/fw/tk1/storage.c index e2cc67a..1ba1f28 100644 --- a/hw/application_fpga/fw/tk1/storage.c +++ b/hw/application_fpga/fw/tk1/storage.c @@ -213,9 +213,8 @@ int storage_erase_sector(struct partition_table *part_table, uint32_t offset, return 0; } -// Writes the specified data to the offset inside of the allocated -// area. Assumes area has been erased before hand. Currently only -// handles writes to one sector, hence max size of 4096 bytes. +// Writes the specified data to the offset inside of the allocated area. +// Assumes area has been erased before hand. Offset must be a multiple of 256. // // Returns zero on success. int storage_write_data(struct partition_table *part_table, uint32_t offset, @@ -246,7 +245,7 @@ int storage_write_data(struct partition_table *part_table, uint32_t offset, return -1; } - if (size > 4096) { + if (size > SIZE_STORAGE_AREA) { return -1; }