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

SOFTDEVICE: INVALID MEMORY ACCESS

Hi, 

I am working on ble_app_template.
I am trying to save a value in the flash memory of nRF52840.
I am using the following 2 functions to write/read in flash memory but getting this error.

SOFTDEVICE: INVALID MEMORY ACCESS

I am using this memory as a starting memory to save values.(As mentioned in this link:  Flash - Non-volatile memory)

uint32_t f_addr = 0x4001E001;

Any solution for this error? 
Thanks

uint32_t f_addr = 0x4001E001; 


// Create a pointer to that address
uint32_t * p_addr = &f_addr; 

uint32_t val = 123456; 

void write_T()
{
    nrf_nvmc_write_word(f_addr, val);
}

void read_T()
{
     NRF_LOG_INFO("The Data read from flash is: %d", *(p_addr));
}

Parents Reply
  • In hex that's 0xFF000, the value of p_addr. 

    When you dereference p_addr it will contain the value of f_addr, and not the data in flash at address 0x000FF000. You need treat p_addr as a double, not single pointer.

    const uint32_t f_addr = 0x000FF000; 
    
    
    // Create a pointer to that address
    //initialization value must be const
    uint32_t * p_addr = (uint32_t *)f_addr; //cast f_addr to a pointer
    
    uint32_t val = 123456; 
    
    uint32_t write_T()
    {
        return sd_flash_write(p_addr, &val, sizeof(val));
    }
    
    uint32_t erase_T()
    {
        return sd_flash_page_erase(255);
    }
    
    void read_T()
    {
        // dereference the pointer that p_addr points to
        NRF_LOG_INFO("The Data read from flash is: %u", *(uint32_t *)*(p_addr));
    }
    

Children
Related