From d8099f484d07184b5b5c09231e105020f7aa232f Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 10 Oct 2019 18:51:39 +0700 Subject: [PATCH] clean up --- .../src/InternalFileSystem.cpp | 19 +++++++------- .../InternalFileSytem/src/flash/flash_cache.c | 6 +++-- .../InternalFileSytem/src/flash/flash_cache.h | 4 +-- .../InternalFileSytem/src/flash/flash_nrf5x.c | 26 ++++++++++++++----- .../InternalFileSytem/src/flash/flash_nrf5x.h | 19 ++++++-------- 5 files changed, 43 insertions(+), 31 deletions(-) diff --git a/libraries/InternalFileSytem/src/InternalFileSystem.cpp b/libraries/InternalFileSytem/src/InternalFileSystem.cpp index 123a0580..9b35802f 100644 --- a/libraries/InternalFileSytem/src/InternalFileSystem.cpp +++ b/libraries/InternalFileSytem/src/InternalFileSystem.cpp @@ -47,24 +47,23 @@ static int _internal_flash_read (const struct lfs_config *c, lfs_block_t block, { (void) c; - uint32_t addr = lba2addr(block) + off; + uint32_t addr = lba2addr(block) + off; flash_nrf5x_read(buffer, addr, size); - return 0; + return 0; } // Program a region in a block. The block must have previously // been erased. Negative error codes are propogated to the user. // May return LFS_ERR_CORRUPT if the block should be considered bad. -static int _internal_flash_prog (const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, - lfs_size_t size) +static int _internal_flash_prog (const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size) { (void) c; - uint32_t addr = lba2addr(block) + off; + uint32_t addr = lba2addr(block) + off; flash_nrf5x_write(addr, buffer, size); - return 0; + return 0; } // Erase a block. A block must be erased before being programmed. @@ -75,17 +74,17 @@ static int _internal_flash_erase (const struct lfs_config *c, lfs_block_t block) { (void) c; - uint32_t addr = lba2addr(block); + uint32_t addr = lba2addr(block); - // implement as write 0xff to whole block address + // implement as write 0xff to whole block address for(int i=0; i cache_addr = FLASH_CACHE_INVALID_ADDR; } -void flash_cache_read (flash_cache_t* fc, void* dst, uint32_t addr, uint32_t count) +int flash_cache_read (flash_cache_t* fc, void* dst, uint32_t addr, uint32_t count) { // there is no check for overflow / wraparound for dst + count, addr + count. // this might be a useful thing to add for at least debug builds. @@ -198,4 +198,6 @@ void flash_cache_read (flash_cache_t* fc, void* dst, uint32_t addr, uint32_t cou // not using the cache, so just forward to read from flash fc->read(dst, addr, count); } + + return (int) count; } diff --git a/libraries/InternalFileSytem/src/flash/flash_cache.h b/libraries/InternalFileSytem/src/flash/flash_cache.h index 517eb2dc..bb42ba66 100644 --- a/libraries/InternalFileSytem/src/flash/flash_cache.h +++ b/libraries/InternalFileSytem/src/flash/flash_cache.h @@ -46,9 +46,9 @@ typedef struct extern "C" { #endif -uint32_t flash_cache_write (flash_cache_t* fc, uint32_t dst, void const *src, uint32_t count); +int flash_cache_write (flash_cache_t* fc, uint32_t dst, void const *src, uint32_t count); void flash_cache_flush (flash_cache_t* fc); -void flash_cache_read (flash_cache_t* fc, void* dst, uint32_t addr, uint32_t count); +int flash_cache_read (flash_cache_t* fc, void* dst, uint32_t addr, uint32_t count); #ifdef __cplusplus } diff --git a/libraries/InternalFileSytem/src/flash/flash_nrf5x.c b/libraries/InternalFileSytem/src/flash/flash_nrf5x.c index 01b92d69..c489e85f 100644 --- a/libraries/InternalFileSytem/src/flash/flash_nrf5x.c +++ b/libraries/InternalFileSytem/src/flash/flash_nrf5x.c @@ -29,6 +29,17 @@ #include "delay.h" #include "rtos.h" + +#ifdef NRF52840_XXAA + #define BOOTLOADER_ADDR 0xF4000 +#else + #define BOOTLOADER_ADDR 0x74000 +#endif + +// defined in linker script +extern uint32_t __flash_arduino_start[]; +//extern uint32_t __flash_arduino_end[]; + //--------------------------------------------------------------------+ // MACRO TYPEDEF CONSTANT ENUM DECLARATION //--------------------------------------------------------------------+ @@ -65,17 +76,20 @@ void flash_nrf5x_flush (void) flash_cache_flush(&_cache); } -uint32_t flash_nrf5x_write (uint32_t dst, void const * src, uint32_t len) +int flash_nrf5x_write (uint32_t dst, void const * src, uint32_t len) { - // TODO prevent write SD + bootloader region + // Softdevice region + VERIFY(dst >= ((uint32_t) __flash_arduino_start), -1); + + // Bootloader region + VERIFY(dst < BOOTLOADER_ADDR, -1); + return flash_cache_write(&_cache, dst, src, len); } -uint32_t flash_nrf5x_read (void* dst, uint32_t src, uint32_t len) +int flash_nrf5x_read (void* dst, uint32_t src, uint32_t len) { - // return cache value if available - flash_cache_read(&_cache, dst, src, len); - return len; + return flash_cache_read(&_cache, dst, src, len); } bool flash_nrf5x_erase(uint32_t addr) diff --git a/libraries/InternalFileSytem/src/flash/flash_nrf5x.h b/libraries/InternalFileSytem/src/flash/flash_nrf5x.h index b6cb4656..9fc25a4a 100644 --- a/libraries/InternalFileSytem/src/flash/flash_nrf5x.h +++ b/libraries/InternalFileSytem/src/flash/flash_nrf5x.h @@ -36,27 +36,24 @@ void flash_nrf5x_flush (void); bool flash_nrf5x_erase(uint32_t addr); -uint32_t flash_nrf5x_write (uint32_t dst, void const * src, uint32_t len); -uint32_t flash_nrf5x_read (void* dst, uint32_t src, uint32_t len); +int flash_nrf5x_write (uint32_t dst, void const * src, uint32_t len); +int flash_nrf5x_read (void* dst, uint32_t src, uint32_t len); //--------------------------------------------------------------------+ // Write helper //--------------------------------------------------------------------+ -static inline uint32_t flash_nrf5x_write8 (uint32_t dst, uint8_t num) +static inline int flash_nrf5x_write8 (uint32_t dst, uint8_t num) { - flash_nrf5x_write(dst, &num, sizeof(num)); - return sizeof(num); + return flash_nrf5x_write(dst, &num, sizeof(num)); } -static inline uint32_t flash_nrf5x_write16 (uint32_t dst, uint8_t num) +static inline int flash_nrf5x_write16 (uint32_t dst, uint8_t num) { - flash_nrf5x_write(dst, &num, sizeof(num)); - return sizeof(num); + return flash_nrf5x_write(dst, &num, sizeof(num)); } -static inline uint32_t flash_nrf5x_write32 (uint32_t dst, uint8_t num) +static inline int flash_nrf5x_write32 (uint32_t dst, uint8_t num) { - flash_nrf5x_write(dst, &num, sizeof(num)); - return sizeof(num); + return flash_nrf5x_write(dst, &num, sizeof(num)); } //--------------------------------------------------------------------+