From 8dc3bbed50356a17494201c7f99ed193b29915d6 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 19 Oct 2018 15:04:33 +0700 Subject: [PATCH] clean up --- cores/nRF5/flash/flash_cache.c | 18 +++++++++--------- cores/nRF5/flash/flash_cache.h | 4 ++-- cores/nRF5/flash/flash_nrf52.c | 2 +- cores/nRF5/flash/flash_nrf52.h | 2 ++ 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/cores/nRF5/flash/flash_cache.c b/cores/nRF5/flash/flash_cache.c index 17fce728..42d2ea1a 100644 --- a/cores/nRF5/flash/flash_cache.c +++ b/cores/nRF5/flash/flash_cache.c @@ -43,12 +43,12 @@ //--------------------------------------------------------------------+ static inline uint32_t page_addr_of (uint32_t addr) { - return addr & ~(FLASH_CACHE_PAGE_SIZE - 1); + return addr & ~(FLASH_CACHE_SIZE - 1); } static inline uint32_t page_offset_of (uint32_t addr) { - return addr & (FLASH_CACHE_PAGE_SIZE - 1); + return addr & (FLASH_CACHE_SIZE - 1); } uint32_t flash_cache_write (flash_cache_t* fc, uint32_t dst, void const * src, uint32_t len) @@ -62,7 +62,7 @@ uint32_t flash_cache_write (flash_cache_t* fc, uint32_t dst, void const * src, u uint32_t const page_addr = page_addr_of(dst); uint32_t const offset = page_offset_of(dst); - uint32_t wr_bytes = FLASH_CACHE_PAGE_SIZE - offset; + uint32_t wr_bytes = FLASH_CACHE_SIZE - offset; wr_bytes = min32(remain, wr_bytes); // Page changes, flush old and update new cache @@ -78,9 +78,9 @@ uint32_t flash_cache_write (flash_cache_t* fc, uint32_t dst, void const * src, u } uint32_t const last_byte = offset + wr_bytes; - if ( last_byte < FLASH_CACHE_PAGE_SIZE ) + if ( last_byte < FLASH_CACHE_SIZE ) { - fc->read(fc->cache_buf + last_byte, page_addr + last_byte, FLASH_CACHE_PAGE_SIZE - last_byte); + fc->read(fc->cache_buf + last_byte, page_addr + last_byte, FLASH_CACHE_SIZE - last_byte); } } @@ -99,10 +99,10 @@ void flash_cache_flush (flash_cache_t* fc) if ( fc->cache_addr == FLASH_CACHE_INVALID_ADDR ) return; // skip erase & program if verify() exists, and memory matches - if ( !(fc->verify && fc->verify(fc->cache_addr, fc->cache_buf, FLASH_CACHE_PAGE_SIZE)) ) + if ( !(fc->verify && fc->verify(fc->cache_addr, fc->cache_buf, FLASH_CACHE_SIZE)) ) { fc->erase(fc->cache_addr); - fc->program(fc->cache_addr, fc->cache_buf, FLASH_CACHE_PAGE_SIZE); + fc->program(fc->cache_addr, fc->cache_buf, FLASH_CACHE_SIZE); } fc->cache_addr = FLASH_CACHE_INVALID_ADDR; @@ -113,7 +113,7 @@ void flash_cache_read (flash_cache_t* fc, void* dst, uint32_t addr, uint32_t cou // overwrite with cache value if available if ( (fc->cache_addr != FLASH_CACHE_INVALID_ADDR) && !(addr < fc->cache_addr && addr + count <= fc->cache_addr) && - !(addr >= fc->cache_addr + FLASH_CACHE_PAGE_SIZE) ) + !(addr >= fc->cache_addr + FLASH_CACHE_SIZE) ) { int dst_off = fc->cache_addr - addr; int src_off = 0; @@ -124,7 +124,7 @@ void flash_cache_read (flash_cache_t* fc, void* dst, uint32_t addr, uint32_t cou dst_off = 0; } - int cache_bytes = minof(FLASH_CACHE_PAGE_SIZE-src_off, count - dst_off); + int cache_bytes = minof(FLASH_CACHE_SIZE-src_off, count - dst_off); // start to cached if ( dst_off ) fc->read(dst, addr, dst_off); diff --git a/cores/nRF5/flash/flash_cache.h b/cores/nRF5/flash/flash_cache.h index 7eb6fa06..6e52b685 100644 --- a/cores/nRF5/flash/flash_cache.h +++ b/cores/nRF5/flash/flash_cache.h @@ -40,7 +40,7 @@ #include #include -#define FLASH_CACHE_PAGE_SIZE 4096 +#define FLASH_CACHE_SIZE 4096 // must be a erasable page size #define FLASH_CACHE_INVALID_ADDR 0xffffffff typedef struct @@ -51,7 +51,7 @@ typedef struct bool (*verify) (uint32_t addr, void const * buf, uint32_t len); uint32_t cache_addr; - uint8_t cache_buf[FLASH_CACHE_PAGE_SIZE] __attribute__((aligned(4))); + uint8_t cache_buf[FLASH_CACHE_SIZE] __attribute__((aligned(4))); } flash_cache_t; #ifdef __cplusplus diff --git a/cores/nRF5/flash/flash_nrf52.c b/cores/nRF5/flash/flash_nrf52.c index 2d3ed736..11563223 100644 --- a/cores/nRF5/flash/flash_nrf52.c +++ b/cores/nRF5/flash/flash_nrf52.c @@ -106,7 +106,7 @@ bool fal_erase (uint32_t addr) // delay and retry if busy uint32_t err; - while ( NRF_ERROR_BUSY == (err = sd_flash_page_erase(addr / FLASH_CACHE_PAGE_SIZE)) ) + while ( NRF_ERROR_BUSY == (err = sd_flash_page_erase(addr / FLASH_NRF52_PAGE_SIZE)) ) { delay(1); } diff --git a/cores/nRF5/flash/flash_nrf52.h b/cores/nRF5/flash/flash_nrf52.h index 5eb97136..24c0c948 100644 --- a/cores/nRF5/flash/flash_nrf52.h +++ b/cores/nRF5/flash/flash_nrf52.h @@ -39,6 +39,8 @@ #include "common_inc.h" +#define FLASH_NRF52_PAGE_SIZE 4096 + #ifdef __cplusplus extern "C" { #endif