enhance nffs hal flash operation

This commit is contained in:
hathach committed 2017-03-01 18:07:32 +07:00
1 parent c9ac58478c
commit 2efe5dcbc1
2 files changed
+51 -26

No files matched your search

@@ -24,7 +24,19 @@ NffsFile file;
void setup()
{
Serial.begin(115200);
Serial.println("Nffs Read Write File Example");
Serial.println();
// Wait for user input to run. Otherwise the code will
// always run immediately after flash and create the FILENAME in advance
Serial.print("Enter to any keys to continue:");
while ( !Serial.available() )
{
delay(1);
}
Serial.println();
Serial.println();
// Bluefruit module must be initialized for Nffs to work
// Since Bluefruit's SOC event handling task is required for flash operation
@@ -38,7 +50,7 @@ void setup()
// file existed
if ( file.exists() )
{
Serial.println(FILENAME " file existed");
Serial.println(FILENAME " file exists");
uint32_t readlen;
char buffer[64] = { 0 };
+38 -25
View File
@@ -54,7 +54,7 @@ const struct hal_flash nrf52k_flash_dev = {
#define NRF52K_FLASH_READY() (NRF_NVMC->READY == NVMC_READY_READY_Ready)
static SemaphoreHandle_t _evt_sem = NULL;
static uint32_t _op_result;
volatile static uint32_t _op_result;
void hal_flash_event_cb(uint32_t event)
{
@@ -62,8 +62,7 @@ void hal_flash_event_cb(uint32_t event)
xSemaphoreGive(_evt_sem);
}
static int
hal_flash_check_addr(const struct hal_flash *hf, uint32_t addr)
static int hal_flash_check_addr(const struct hal_flash *hf, uint32_t addr)
{
if (addr < hf->hf_base_addr || addr > hf->hf_base_addr + hf->hf_size) {
return -1;
@@ -76,8 +75,7 @@ const struct hal_flash *hal_bsp_flash_dev(uint8_t flash_id)
return &nrf52k_flash_dev;
}
int
hal_flash_read(uint8_t id, uint32_t address, void *dst, uint32_t num_bytes)
int hal_flash_read(uint8_t id, uint32_t address, void *dst, uint32_t num_bytes)
{
(void) id;
memcpy(dst, (void *)address, num_bytes);
@@ -86,13 +84,22 @@ hal_flash_read(uint8_t id, uint32_t address, void *dst, uint32_t num_bytes)
static int write_and_wait(uint32_t addr, uint32_t const * const src, uint32_t size)
{
VERIFY_STATUS( sd_flash_write( (uint32_t*) addr, src, size) );
uint32_t err;
// cprintf("Flash Write at 0x08%lX with %d bytes\n", addr, size);
// delay and try again if busy
while ( NRF_ERROR_BUSY == (err = sd_flash_write( (uint32_t*) addr, src, size)) )
{
delay(1);
}
VERIFY_STATUS(err);
xSemaphoreTake(_evt_sem, portMAX_DELAY);
return (_op_result == NRF_EVT_FLASH_OPERATION_SUCCESS ) ? 0 : 1;
return (_op_result == NRF_EVT_FLASH_OPERATION_SUCCESS ) ? 0 : (-1);
}
int
hal_flash_write(uint8_t id, uint32_t address, const void *src, uint32_t num_bytes)
int hal_flash_write(uint8_t id, uint32_t address, const void *src, uint32_t num_bytes)
{
(void) id;
@@ -107,13 +114,13 @@ hal_flash_write(uint8_t id, uint32_t address, const void *src, uint32_t num_byte
* 3. Trailing bytes
*/
/*------------- Fixing non-aligned Address -------------*/
/*------------- non-aligned Address -------------*/
// Address is not aligned
uint32_t miss = address & 0x03;
if ( miss )
{
/* Address is not aligned. Read 4 byte from aligned position
/* Address is not aligned. Combine odd bytes data + existed data
* then update and write back
*/
@@ -173,8 +180,7 @@ hal_flash_write(uint8_t id, uint32_t address, const void *src, uint32_t num_byte
return 0;
}
int
hal_flash_erase(uint8_t id, uint32_t address, uint32_t num_bytes)
int hal_flash_erase(uint8_t id, uint32_t address, uint32_t num_bytes)
{
const struct hal_flash *hf;
uint32_t start, size;
@@ -215,30 +221,37 @@ hal_flash_erase(uint8_t id, uint32_t address, uint32_t num_bytes)
return 0;
}
int
hal_flash_erase_sector(uint8_t id, uint32_t sector_address)
int hal_flash_erase_sector(uint8_t id, uint32_t sector_address)
{
(void) id;
if (NRF_SUCCESS != sd_flash_page_erase( sector_address / NRF52K_FLASH_SECTOR_SZ ) ) return -1;
uint32_t err;
// cprintf("Flash Erase at 0x08%lX \n", sector_address);
// delay and try again if busy
while ( NRF_ERROR_BUSY == (err = sd_flash_page_erase(sector_address/NRF52K_FLASH_SECTOR_SZ)) )
{
delay(1);
}
VERIFY_STATUS(err);
xSemaphoreTake(_evt_sem, portMAX_DELAY);
return (_op_result == NRF_EVT_FLASH_OPERATION_SUCCESS ) ? 0 : 1;
return (_op_result == NRF_EVT_FLASH_OPERATION_SUCCESS ) ? 0 : (-1);
}
int
hal_flash_sector_info(int idx, uint32_t *address, uint32_t *sz)
int hal_flash_sector_info(int idx, uint32_t *address, uint32_t *sz)
{
assert(idx < nrf52k_flash_dev.hf_sector_cnt);
*address = idx * NRF52K_FLASH_SECTOR_SZ;
*sz = NRF52K_FLASH_SECTOR_SZ;
return 0;
assert(idx < nrf52k_flash_dev.hf_sector_cnt);
*address = idx * NRF52K_FLASH_SECTOR_SZ;
*sz = NRF52K_FLASH_SECTOR_SZ;
return 0;
}
int hal_flash_init(void)
{
_evt_sem = xSemaphoreCreateCounting(10, 0);
return (_evt_sem != NULL) ? 0 : 1;
_evt_sem = xSemaphoreCreateCounting(10, 0);
return (_evt_sem != NULL) ? 0 : 1;
}
uint8_t hal_flash_align(uint8_t flash_id)