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

Flash Memory Issue

Hi,
I have written these 3 functions to erase/read and write.

/////////////////////////////////////////////////////////////////////////////// SNV

const uint32_t f_addr = 0x000FF000; // Last page start address 255

// Create a pointer to that address
uint32_t * p_addr = (uint32_t *)f_addr; //cast f_addr to a pointer

uint32_t erase_SNV()
{
    NRF_LOG_INFO("Memory Erased");
    return sd_flash_page_erase(255);
}


uint32_t write_SNV(int address,uint8_t value)
{
    uint32_t val;
    val=value;
    NRF_LOG_INFO("Data Write in Flash");
    ret_code_t err_code;
    err_code = sd_flash_write(p_addr+address, &val, sizeof(val));
    APP_ERROR_CHECK(err_code);
    return err_code;
}

uint32_t read_SNV(int address)
{
     NRF_LOG_INFO("The Data read from flash is: %d", *(p_addr)+address);
     return *(p_addr)+address;
}


When I write any value from address 1 it gives following wrong output(0 1 2 3).
    write_SNV(1,11);
    read_SNV(1);
    write_SNV(2,12);
    read_SNV(2);
    write_SNV(3,13);
    read_SNV(3);

  //  read_SNV(0);
    read_SNV(1);
    read_SNV(2);
    read_SNV(3);
 



But When I start address from 0 it gives the right values.

    write_SNV(0,10);
    read_SNV(0);
    write_SNV(1,11);
    read_SNV(1);
    write_SNV(2,12);
    read_SNV(2);
    write_SNV(3,13);
    read_SNV(3);

    read_SNV(0);
    read_SNV(1);
    read_SNV(2);
    read_SNV(3);




How can I save values in rondom addresses?
Thanks!

  • You need to erase your chip before programming it. I guess the content of that area is from another project that you tested once.

     

    Edvin said:
    Try using "nrfjprog --eraseall"
  • You need to erase your chip before programming it. I guess the content of that area is from another project that you tested once.

  • I am trying to save different data in the same location but getting different data. Confused

        char  m_hello_world[]   ="hello world";
    
        NRF_LOG_INFO("Writing \"%s\" to flash.", m_hello_world);
        rc = nrf_fstorage_write(&fstorage, 0x3f000, m_hello_world, sizeof(m_hello_world), NULL);
        APP_ERROR_CHECK(rc);
    
        wait_for_flash_ready(&fstorage);
        NRF_LOG_INFO("Done.");
    
     
        rc = nrf_fstorage_read(&fstorage, 0x3f000, m_hello_world_f, sizeof(m_hello_world_f));
        APP_ERROR_CHECK(rc);
    
    
        NRF_LOG_INFO("Reading from flash hex:");
        for(uint8_t i=0; i<sizeof(m_hello_world_f); i++)
        {
            NRF_LOG_RAW_INFO("%02x:", m_hello_world_f[i]);
        }
        NRF_LOG_RAW_INFO("\r\n");
        NRF_LOG_INFO("Reading from flash char:");
        for(uint8_t i=0; i<sizeof(m_hello_world_f); i++)
        {
            NRF_LOG_RAW_INFO("%c", m_hello_world_f[i]);
        }
        NRF_LOG_RAW_INFO("\r\n");
        NRF_LOG_INFO("done");
    
    
        char m_hello_world_1[]="AllWell";
    
    
        NRF_LOG_INFO("Writing \"%s\" to flash.", m_hello_world_1);
        rc = nrf_fstorage_write(&fstorage, 0x3f000, m_hello_world_1, sizeof(m_hello_world_1), NULL);
        APP_ERROR_CHECK(rc);
    
        wait_for_flash_ready(&fstorage);
        NRF_LOG_INFO("Done.");
    
     
        rc = nrf_fstorage_read(&fstorage, 0x3f000, m_hello_world_f, sizeof(m_hello_world_1));
        APP_ERROR_CHECK(rc);
    
    
        NRF_LOG_INFO("Reading from flash hex:");
        for(uint8_t i=0; i<sizeof(m_hello_world_f); i++)
        {
            NRF_LOG_RAW_INFO("%02x:", m_hello_world_f[i]);
        }
        NRF_LOG_RAW_INFO("\r\n");
        NRF_LOG_INFO("Reading from flash char:");
        for(uint8_t i=0; i<sizeof(m_hello_world_f); i++)
        {
            NRF_LOG_RAW_INFO("%c", m_hello_world_f[i]);
        }
        NRF_LOG_RAW_INFO("\r\n");
        NRF_LOG_INFO("done");
    
    

  • You don't erase the page in between.

    I have tried to say several times that when you write to flash, you can only change bits from 1 to 0. That means if you write a byte 0x0F to a byte that was already 0xFF, then it will have the value 0x0F after you write. If you write the byte 0xDD to a byte that already have 0xFF 0x0F,  then it will end up with the value 0x0D. That is how flash works. you need to erase the page using sd_flash_page_erase(), which will clear the entire flash page, and set it to all 0xFF.

    Edit: one of the 0xFF above should have been 0x0F. fixed now.

Related