fix Nffs port with nrf52 flash write

This commit is contained in:
hathach committed 2017-03-04 02:33:03 +07:00
1 parent fd85800d88
commit d178d7f47e
2 files changed
+45 -33

No files matched your search

+44 -29
View File
@@ -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;
+1 -4
View File
@@ -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 );
}