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

Save data on flash using Soft Device

Hi!

First of all, I'm using the nrf52 PCA 10040 DK, with SDK11.0.0, and my Soft Device is 132 2.0.2.

I'm developing an app for comunicating with a sensor via SPI (send commands and data, and recieve data), and also, share information via Bluetooth with other boards.

The first step I made was create a library for encrypting data and orders, send it via SPI, and decrypt responses. In my aplication, I need to store some data permanently in the flash, so I used the example for writing and read data in the flash provided in the SDK, in particular, I use these functions:

static void flash_page_erase(uint32_t * page_address){
// Turn on flash erase enable and wait until the NVMC is ready:
NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Een << NVMC_CONFIG_WEN_Pos);
while (NRF_NVMC->READY == NVMC_READY_READY_Busy){
    // Do nothing.
}
// Erase page:
NRF_NVMC->ERASEPAGE = (uint32_t)page_address;
while (NRF_NVMC->READY == NVMC_READY_READY_Busy){
    // Do nothing.
}
// Turn off flash erase enable and wait until the NVMC is ready:
NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos);
while (NRF_NVMC->READY == NVMC_READY_READY_Busy){
    // Do nothing.
}}

and:

static void flash_word_write(uint32_t * address, uint32_t value){
// Turn on flash write enable and wait until the NVMC is ready:
NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos);
while (NRF_NVMC->READY == NVMC_READY_READY_Busy){
    // Do nothing.
}
*address = value;
	nrf_delay_ms(1); //en el peor caso esta operación tarda 338us
while (NRF_NVMC->READY == NVMC_READY_READY_Busy){
    // Do nothing.
}
// Turn off flash write enable and wait until the NVMC is ready:
NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos);
while (NRF_NVMC->READY == NVMC_READY_READY_Busy){
    // Do nothing.
}}

With this, my application works perfectly.

I include SD, it's activated in the main, the next step is the reset of the sensor module, and erase a page of the flash. The application makes the reset on the module, but after that, doesn't erase the page in the flash, and jump back tho the line where the reset of the module is performed, I mean, it seems like the application enters a loop, I've noticed that the program counter decreases when arrives to the flash page erase line.

I think I don't overlap memory with the Soft Device, application memory and data memory

this is a sample of the code:

configura_gpios_placa();

ble_stack_init();

//initialize the SPI
nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG(SPI_INSTANCE);
spi_config.ss_pin = SPI_CS_PIN;
mensaje = nrf_drv_spi_init(&spi, &spi_config, spi_event_handler);

//reset of the m_rx_buf and the module
memset(m_rx_buf, 0, m_length);
nrf_delay_ms(500);
nrf_gpio_cfg_output(31);
nrf_gpio_pin_write(31,0);
nrf_delay_ms(250);
nrf_gpio_pin_write(31,1);
nrf_delay_ms(500);
spi_xfer_done = false;

//initialize the RNG
inicializar_rng(); 

empieza_reset(); //functions for turn on the 4 LEDs to indicate the reset process
factory_reset(); //reset of the sensor 
recibir_respuesta_datos_auth(data, verify_auth); //recieve if the reset was succesful
direccion = (uint32_t *) 0x0007f000; //adress of the las page in flash
flash_page_erase(direccion);
nrf_delay_ms(90);

So, if I want to save and read data on the flash memory, or erase data, when I use the Soft Device, what library should I use? There is some example?

Thanks guys!

  • Maybe you can use pstorage? An example can be found here: devzone.nordicsemi.com/.../

    However, it is very old so you might want to work on it a bit.

  • Thank you!

    In the page where the pstorage is explained, there is an advertisement that says it's very old, I have read something about the pstorage in the forum, but i thinked that I couldn't use it because it's to old, I'll try to use it anyways, as I'll try it, I`ll tell you something

  • I am currently using pstorage in my code (SDK 11, S130, nrf51822) and I think it is working as expected. I cannot say if it is harming the hardware but it is doing what I want it to do. If you want I can add it here.

  • If you're using SDK 11 I would recommend using fstorage and fds instead of pstorage. They are newer, they are much more flexible and I don't think that pstorage is going to be developed much more, but they will be.

  • Thank you, it seems to be easier to use, I've been reading the documentation of the pstorage and I think it's more complex. As I saw in the pstorage documentation that Nordic recommends use fstorage, but is experimental, I thought that wasn't a good idea use it in my code. Thank you again!

1 2