This commit is contained in:
hathach committed 2019-10-10 18:51:39 +07:00
1 parent b9d586b56c
commit d8099f484d
5 files changed
+43 -31

No files matched your search

@@ -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 <LFS_BLOCK_SIZE; i++)
{
flash_nrf5x_write8(addr + i, 0xFF);
}
// flash_nrf5x_flush();
// flash_nrf5x_flush();
return 0;
return 0;
}
// Sync the state of the underlying block device. Negative error codes
@@ -41,7 +41,7 @@ static inline uint32_t page_offset_of (uint32_t addr)
return addr & (FLASH_CACHE_SIZE - 1);
}
uint32_t flash_cache_write (flash_cache_t* fc, uint32_t dst, void const * src, uint32_t len)
int flash_cache_write (flash_cache_t* fc, uint32_t dst, void const * src, uint32_t len)
{
uint8_t const * src8 = (uint8_t const *) src;
uint32_t remain = len;
@@ -95,7 +95,7 @@ void flash_cache_flush (flash_cache_t* fc)
fc->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;
}
@@ -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
}
@@ -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)
@@ -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));
}
//--------------------------------------------------------------------+