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

how to write or read to flash?

nRF52832  how to write or read to internal flash? SDK IS 15:00

Parents Reply Children
  • NRF_LOG_INFO("Writing \"%x\" to flash.", m_data);
    rc = nrf_fstorage_write(&fstorage, 0x3e200, &m_data, sizeof(m_data), NULL);
    APP_ERROR_CHECK(rc);

    wait_for_flash_ready(&fstorage);
    NRF_LOG_INFO("Donelql.");


    rc = nrf_fstorage_read(&fstorage, 0x3e200, read_data, sizeof(read_data));
    APP_ERROR_CHECK(rc);
    wait_for_flash_ready(&fstorage);

    Above is my program. The first time I let m_data =0xDEADBEEF , reading data is 0xDEADBEEF, that is OK. But the second time I let m_data =0x5E8D7E4F;    reading data is 0x5E8D3E4F, that is FAULT . why?.

  • This driver allows writing to the non-volatile memory (NVM) regions of the nRF5. In order to write to NVM the controller must be powered on and the relevant page must be erased.

     ret_code_t rc = nrf_fstorage_erase(&fstorage, 0x3e200, 1, NULL);   

     nrf_fstorage_eras   doesnot work. why?  

  • The first time I let m_data =0xDEADBEEF , reading data is 0xDEADBEEF, that is OK. But the second time I let m_data =0x5E8D7E4F;    reading data is 0x5E8D3E4F, that is FAULT . why?.

    At the hardware level, an erased Flash bit is a 1.

    A write can only change a 1 to a 0 - to change a 0 to a 1, you need to do an erase.

    The library documentation,  and the example,  do not make it at all clear that the library does not do this for you.

    You must "manually" do an erase before a write.

    If you look at the bit patterns you are writing, you can see what is happening:

    |       D       |       E       |       A       |       D       |       B       |       E       |       E       |       F       |
    +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
    | 1 : 1 : 0 : 1 | 1 : 1 : 1 : 0 : 1 | 0 : 1 : 0 : 1 | 1 : 0 : 1 : 1 : 0 | 1 : 1 : 1 : 1 | 1 : 0 : 1 : 1 : 1 | 0 : 1 : 1 : 1 | 1 :
    +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
    
    |       5       |       E       |       8       |       D       |       7       |       E       |       4       |       F       |
    +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
    | 0 : 1 : 0 : 1 | 1 : 1 : 1 : 0 : 1 | 0 : 0 : 0 : 1 | 1 : 0 : 1 : 0 : 1 | 1 : 1 : 1 : 1 | 1 : 0 : 0 : 1 : 0 | 0 : 1 : 1 : 1 | 1 :
    +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
    
    +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
    | 0 : 1 : 0 : 1 | 1 : 1 : 1 : 0 : 1 | 0 : 0 : 0 : 1 | 1 : 0 : 1 : 0 : 0 | 1 : 1 : 1 : 1 | 1 : 0 : 0 : 1 : 0 | 0 : 1 : 1 : 1 | 1 :
    +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
    |               |               |               |               |    ***        |               |               |               |
    |       5       |       E       |       8       |       D       |       3       |       E       |       4       |       F       |
    

    It is only in the case of the 'B' nibble trying to change to '7' that you try to change a 0 to a 1 - every other bit is either unchanged, or a 1 being cleared to 0

Related