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

Flash Write/Read Basic Way

Im using sdk 14.0 and s132 When i add flashwrite codes to my project which has bluetooth communication ,it does not work I wanna send data to flash and i wanna read/write it What is the simplest way to do this

Im using NRF52832.

Thank you

  • I would use fds.c (short for Flash Data Storage). You can a relative simple use in components/libraries/eddystone/es_flash.c Other examples also use FDS.

    Using flash is not trivial, and fds.c (in cooperation with fstorage.c) will hide away all the nasty stuff required to do it correctly.

  • hi,thanks your answer,but I cant find fds example in examples/peripheral/flash_fds.@@"

  • Updated my answer to include something which actually exists :)

  • Hi, you can use Fstorage with the SoftDevice backend for simple read, write and erase operations. Here is a simple example that I wrote. Make sure you have the correct include files. You can change the start_addr and end_addr to any free space you have on your chip. I believe this code should work. This just erases a page and then writes some data in that page. You can read the data using nrf_fstorage_read(....). You can also verify that the data is written by using nrfjprog.

    static void fs_event_handler(nrf_fstorage_evt_t * evt){
        if (evt->result != NRF_SUCCESS){
            NRF_LOG_ERROR("Flash error");
    
        } else {
            NRF_LOG_INFO("Flash event success");
        }
    }
    
    NRF_FSTORAGE_DEF(nrf_fstorage_t m_fs) =
    {
        .evt_handler = fs_event_handler,
        .start_addr = 0x0007F000,
        .end_addr = 0x000FF000,
    };
    
    int main(void){
    	uint32_t err_code;
    	static uint32_t number = 123;
    	static uint32_t pages_to_erase = 1;
    	
    	err_code = nrf_fstorage_init(&m_fs, &nrf_fstorage_sd, NULL);
        APP_ERROR_CHECK(err_code);
    
    			
    
    				
    	ret_code_t rc = nrf_fstorage_erase(
                        &m_fs,   				/* The instance to use. */
                        m_fs.start_addr,     		/* The address of the flash pages to erase. */
                        pages_to_erase, 		/* The number of pages to erase. */
                        NULL);       			/* Optional parameter, backend-dependent. */
    
    	if (rc == NRF_SUCCESS)
    	{
    			/* The operation was accepted.
    				 Upon completion, the NRF_FSTORAGE_ERASE_RESULT event
    				is sent to the callback function registered by the instance. */
    			
    			NRF_LOG_INFO("ERASE SUCCESSFUL");
    	}
    	else
    	{
    			NRF_LOG_INFO("ERASE UNSUCCESFUL ");
    				/* Handle error.*/
    	}
    
    
    
        rc = nrf_fstorage_write(&m_fs, m_fs.start_addr, &number, 4, NULL);      //Write data to memory address. Check it with command: nrfjprog --memrd addr --n 32
    	if (rc == NRF_SUCCESS)
    	{
    		
    				NRF_LOG_INFO("WRITE SUCCESSFUL");
    	}
    	else
    	{
    			NRF_LOG_INFO("WRITE UNSUCCESFUL ");
    				/* Handle error.*/
    	}
    
    
    }
    
  • Note that garbage collection and other mundane tasks will have to be handled in a live system. FDS makes this a lot simpler for the developer compared to using fstorage directly :)

Related