From d178d7f47eddb07b1cbb687b3631f5a05ba775af Mon Sep 17 00:00:00 2001 From: hathach Date: Sat, 4 Mar 2017 02:33:03 +0700 Subject: [PATCH] fix Nffs port with nrf52 flash write --- cores/nRF5/nrf52_flash.c | 73 ++++++++++++++++++++------------- libraries/nffs/src/NewtNffs.cpp | 5 +-- 2 files changed, 45 insertions(+), 33 deletions(-) diff --git a/cores/nRF5/nrf52_flash.c b/cores/nRF5/nrf52_flash.c index 76fa55e5..e695d189 100644 --- a/cores/nRF5/nrf52_flash.c +++ b/cores/nRF5/nrf52_flash.c @@ -37,6 +37,10 @@ #include "Arduino.h" #include "nrf52_flash.h" +#if CFG_DEBUG +#define CFG_DEBUG_NFFS 0 +#endif + static SemaphoreHandle_t _evt_sem = NULL; static volatile uint32_t _op_result; @@ -56,7 +60,9 @@ int nrf52_flash_erase_sector(uint32_t sector_address) { uint32_t err; -// cprintf("Flash Erase at 0x08%lX \n", sector_address); +#if CFG_DEBUG_NFFS + cprintf("Flash Erase at 0x%08lX \n", sector_address); +#endif // delay and try again if busy while ( NRF_ERROR_BUSY == (err = sd_flash_page_erase(sector_address/NRF52K_FLASH_SECTOR_SZ)) ) @@ -73,7 +79,9 @@ static int write_and_wait(uint32_t addr, uint32_t const * const src, uint32_t si { uint32_t err; -// cprintf("Flash Write at 0x08%lX with %d bytes\n", addr, size); +#if CFG_DEBUG_NFFS + cprintf(" - 0x%08lX with %d bytes\n", addr, 4*size); +#endif // delay and try again if busy while ( NRF_ERROR_BUSY == (err = sd_flash_write( (uint32_t*) addr, src, size)) ) @@ -89,6 +97,10 @@ static int write_and_wait(uint32_t addr, uint32_t const * const src, uint32_t si int nrf52_flash_write(uint32_t address, const void *src, uint32_t num_bytes) { +#if CFG_DEBUG_NFFS + cprintf("Flash Write at 0x%08lX with %d bytes from 0x%08lX\n", address, num_bytes, (uint32_t) src); +#endif + /* SD Flash requires * 1. src data must be word aligned * 2. address must be word aligned @@ -124,43 +136,46 @@ int nrf52_flash_write(uint32_t address, const void *src, uint32_t num_bytes) } /*------------- Main write -------------*/ - uint32_t count = align4(num_bytes); - uint32_t* tempbuf; + uint32_t const n4 = align4(num_bytes); + uint32_t const leftover = (num_bytes & 0x03UL); - // src is not word aligned - miss = ((uint32_t)src) & 0x03; - if (miss) + if ( ((uint32_t)src) & 0x03 ) { + // src is not word aligned // Malloc and copy multiple of 4 bytes from src to write to flash - tempbuf = (uint32_t*) rtos_malloc( count ); + uint8_t* tempbuf = (uint8_t*) rtos_malloc( n4 + 4 ); VERIFY( tempbuf, NRF_ERROR_NO_MEM); - memcpy(tempbuf, src, count); + memcpy(tempbuf, src, num_bytes); + + // There is trailing bytes, read overflow from address + if ( leftover ) + { + memcpy(tempbuf+num_bytes, (uint8_t*) (address+num_bytes), 4-leftover); + } + + int err = write_and_wait(address, tempbuf, (n4 / 4) + (leftover ? 1 : 0) ); + + rtos_free(tempbuf); + + VERIFY_STATUS(err); }else { - tempbuf = (uint32_t*) src; - } + if ( n4 ) + { + VERIFY_STATUS( write_and_wait(address, src, n4 / 4) ); + } - VERIFY_STATUS( write_and_wait(address, tempbuf, count / 4) ); + /*------------- Trailing Bytes -------------*/ + if ( leftover ) + { + uint32_t val; - if (miss) - { - free(tempbuf); - } + memcpy(&val, (uint8_t*) (address+n4), 4); + memcpy(&val, src+n4, leftover); - address += count; - src += count; - num_bytes -= count; - - /*------------- Trailing Bytes -------------*/ - if ( num_bytes ) - { - uint32_t val; - - memcpy( &val, (uint8_t*) address, 4); - memcpy(&val, src, num_bytes); - - VERIFY_STATUS( write_and_wait(address, &val, 1) ); + VERIFY_STATUS( write_and_wait(address, &val, 1) ); + } } return 0; diff --git a/libraries/nffs/src/NewtNffs.cpp b/libraries/nffs/src/NewtNffs.cpp index 851be5e4..14d6d966 100644 --- a/libraries/nffs/src/NewtNffs.cpp +++ b/libraries/nffs/src/NewtNffs.cpp @@ -53,11 +53,10 @@ bool NewtNffs::begin(void) { // Follow code in nffs_pkg_init() if (_initialized) return true; - _initialized = true; // Init flash - hal_flash_init(); + nrf52_flash_init(); /* Initialize nffs's internal state. */ errnum = nffs_init(); @@ -78,8 +77,6 @@ bool NewtNffs::begin(void) // If not formatted, format it if (FS_ECORRUPT == errnum) { - PRINT_MESS("formatting nffs"); - errnum = nffs_format(descs); VERIFY_STATUS( errnum, false ); }