This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

fstorage write problems, reading back incorrect data

Using Softdevice s140 v6.1.0
nRF5 SDK v15.2.0

I am trying to write to the flash, here are my read and write functions:

//writes to the flash
uint32_t flash_write(uint32_t m_addr, uint32_t m_data)
{ 
    ret_code_t err_code;
    NRF_LOG_INFO("Writing \"%x\" to flash address \"%x\".", m_data, m_addr);
    err_code = nrf_fstorage_write(&fstorage, (FLASH_START_ADDR + (4*m_addr)), &m_data, sizeof(m_data), NULL);
    APP_ERROR_CHECK(err_code);
    // wait_for_flash_ready(&fstorage);

    return err_code;
}

//reads from the flash
uint32_t flash_read(uint32_t m_addr)
{ 
    ret_code_t err_code;
    uint32_t m_data;
    err_code = nrf_fstorage_read(&fstorage, (FLASH_START_ADDR + (4*m_addr)), &m_data, 4);
    APP_ERROR_CHECK(err_code);
    NRF_LOG_INFO("Read \"%x\" from flash address \"%x\".", m_data, m_addr);
    return m_data;
}

Two problems:

  1. The write function does not work unless i comment out the //wait_for_flash_ready(&fstorage);
  2. When writing 0x00000000 to an address, and then reading the same address back, I get a strange value: 0x2003FE50
    Yes, I am aware that you have to erase the page before writing, but I have checked that the data at that address was already 0xFFFFFFFF

Here's the rest of my flash related code:

#define FLASH_START_ADDR    0x70000
#define FLASH_LENGTH        0x10000
#define FLASH_LENGTH_32BIT  (FLASH_LENGTH/4)

static void fstorage_evt_handler(nrf_fstorage_evt_t * p_evt);
bool flashBusy = false;

NRF_FSTORAGE_DEF(nrf_fstorage_t fstorage) =
{
    /* Set a handler for fstorage events. */
    .evt_handler = fstorage_evt_handler,
    //start and end addresses
    .start_addr = FLASH_START_ADDR,
    .end_addr   = (FLASH_START_ADDR + FLASH_LENGTH - 1),
};

//Error Handler for fstorage events
static void fstorage_evt_handler(nrf_fstorage_evt_t * p_evt)
{
    if (p_evt->result != NRF_SUCCESS)
    {
        NRF_LOG_INFO("--> Event received: ERROR while executing an fstorage operation.");
        return;
    }

    switch (p_evt->id)
    {
        case NRF_FSTORAGE_EVT_WRITE_RESULT:
        {
            NRF_LOG_INFO("--> Event received: wrote %d bytes at address 0x%x.",
                         p_evt->len, p_evt->addr);
        } break;

        case NRF_FSTORAGE_EVT_ERASE_RESULT:
        {
            NRF_LOG_INFO("--> Event received: erased %d page from address 0x%x.",
                         p_evt->len, p_evt->addr);
        } break;

        default:
            break;
    }
}

//blocking function to wait for flash to be ready
void wait_for_flash_ready(nrf_fstorage_t const * p_fstorage)
{
    /* While fstorage is busy, sleep and wait for an event. */
    while (nrf_fstorage_is_busy(p_fstorage))
    {
        (void)sd_app_evt_wait();
    }
}

//function to set up the flash memory
void flash_init()
{   
    ret_code_t err_code;
    nrf_fstorage_api_t * p_fs_api;
    p_fs_api = &nrf_fstorage_sd;
    err_code = nrf_fstorage_init(&fstorage, p_fs_api, NULL);
    APP_ERROR_CHECK(err_code);
}

Parents Reply Children
Related