Hi,
I am saving data in flash memory but when I read after restarting the device it shows FF.
How can I read from flash after restarting the device? and how can I check that there is already data?
Thanks
Hi,
I am saving data in flash memory but when I read after restarting the device it shows FF.
How can I read from flash after restarting the device? and how can I check that there is already data?
Thanks
My initial guess is that you are not waiting for the read finished callback before you print what you have read. Can you share some code showing how you read the data? What are you using to read? Fstorage? FDS? I suggest you check out the examples flash_fstorage or flash_fds, depending on which one you use.
Best regards,
Edvin
Hi Edvin
I have written these 2 functions to read and write data in flash memory.
void write_in_flash(uint8_t place)
{
ret_code_t rc;
place=place*16;
sd_flash_page_erase(0x47);
rc = nrf_fstorage_write(&fstorage, flash_start_address+place, save_data, sizeof(save_data), NULL);
APP_ERROR_CHECK(rc);
wait_for_flash_ready(&fstorage);
NRF_LOG_INFO("Done.");
}
void read_in_flash(uint8_t place)
{
ret_code_t rc;
place=place*16;
rc = nrf_fstorage_read(&fstorage, flash_start_address+place, read_data, sizeof(save_data));
APP_ERROR_CHECK(rc);
NRF_LOG_INFO("Reading from flash hex:");
for(uint8_t i=0; i<sizeof(read_data); i++)
{
NRF_LOG_RAW_INFO("%02x:", read_data[i]);
}
NRF_LOG_RAW_INFO("\r\n");
NRF_LOG_INFO("done");
}void Charcteristics_10(uint8_t test)
{
NRF_LOG_INFO("Function Called");
save_data[0]=0x55;
save_data[1]=0x55;
write_in_flash(2);
read_in_flash(2);
if(test==10)
{
save_data[0]=0x55;
save_data[1]=0x55;
write_in_flash(2);
read_in_flash(2);
}
if(test==20)
{
read_in_flash(2);
}
}
Does any suggestion please?
How many bytes are you writing? And what values do you write? Remember that I don't know anything apart from what you have written here. How am I supposed to know what your log is supposed to write?
According to your snippets so far, the save_data[] array is two bytes long (but it doesn't say). And the values are {0x55, 0x55}.
This is what you read, but the save_data[] array looks like it is 8 bytes long.
Muqarrab said:BUT when I call the same function after connecting the device it not work.
So what do you read then? It seems you forgot to mention that in the log? Or do you not call the function after you have connected?
Hi Edvin,
1-These are two functions to save/read data from flash. These are okay?
void write_in_flash(uint8_t place)
{
ret_code_t rc;
place=place*16;
sd_flash_page_erase(0x47);
rc = nrf_fstorage_write(&fstorage, flash_start_address+place, save_data, sizeof(save_data), NULL);
APP_ERROR_CHECK(rc);
wait_for_flash_ready(&fstorage);
NRF_LOG_INFO("Done.");
}
void read_in_flash(uint8_t place)
{
ret_code_t rc;
place=place*16;
rc = nrf_fstorage_read(&fstorage, flash_start_address+place, read_data, sizeof(save_data));
APP_ERROR_CHECK(rc);
wait_for_flash_ready(&fstorage);
NRF_LOG_INFO("Reading from flash hex:");
for(uint8_t i=0; i<sizeof(read_data); i++)
{
NRF_LOG_RAW_INFO("%02x:", read_data[i]);
}
NRF_LOG_RAW_INFO("\r\n");
NRF_LOG_INFO("done");
}
2-When I called these functions without connection it works.
void Charcteristics_10(uint8_t test)
{
NRF_LOG_INFO("Function Called");
save_data[0]=0x55;
save_data[1]=0x55;
write_in_flash(2);
read_in_flash(2);
}

3-When I called the same function after connecting the device I get no response also device does not connect. (Stuck in connecting)
case BLE_GAP_EVT_CONNECTED:
NRF_LOG_INFO("Connected.");
err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
APP_ERROR_CHECK(err_code);
m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
err_code = nrf_ble_qwr_conn_handle_assign(&m_qwr, m_conn_handle);
APP_ERROR_CHECK(err_code);
connection_state=1;
NRF_LOG_INFO("Connected LED Set");
nrf_gpio_pin_set(State_LED);
Charcteristics_10(5);
break;
Muqarrab said:3-When I called the same function after connecting the device I get no response also device does not connect. (Stuck in connecting)
From where are you calling the flash read and write functions? I don't think you included that in your snippets.
What is your connection interval? What are you connected to? A phone or another nRF? What is your role? Central or Peripheral?
Perhaps you can upload the entire project, so that I don't have to ask for all these things, in case there is some information missing? At least all the relevant functions.
Hi Edvin
A phone or another nRF?
With a mobile phone.
What is your role?
Peripheral.
I have attached the project kindly check. These are the functions I have written for read/write data in flash memory.
void write_in_flash(uint8_t place)
void read_in_flash(uint8_t place)
void Charcteristics_10(uint8_t test) // In this function I called above functions.
Edvin said:From where are you calling the flash read and write functions? I don't think you included that in your snippets.
Ok. So I asked where you called this from. I meant when it is not working, and the answer is: "From the BLE_GAP_EVT_CONNECTED" event.
You can't do that. That is, you can, but you can't use the wait_for_flash_ready() from this interrupt. The reason for this is that this will wait for an event in the fstorage module, but this interrupt priority is the same as the interrupt you are currently in. Therefore the interrupt it is waiting for will not be able to be processed before you release the ble_evt_handler() interrupt, and you are in what we call a deadlock. (both threads are waiting for the other to finish).
For easier debugging using the log, you can set #define NRF_LOG_DEFERRED 0 in sdk_config.h.
So you need to move the flash write function (or at least the wait_for_flash_ready()) to outside the interrupt.
Best regards,
Edvin
Edvin said:From where are you calling the flash read and write functions? I don't think you included that in your snippets.
Ok. So I asked where you called this from. I meant when it is not working, and the answer is: "From the BLE_GAP_EVT_CONNECTED" event.
You can't do that. That is, you can, but you can't use the wait_for_flash_ready() from this interrupt. The reason for this is that this will wait for an event in the fstorage module, but this interrupt priority is the same as the interrupt you are currently in. Therefore the interrupt it is waiting for will not be able to be processed before you release the ble_evt_handler() interrupt, and you are in what we call a deadlock. (both threads are waiting for the other to finish).
For easier debugging using the log, you can set #define NRF_LOG_DEFERRED 0 in sdk_config.h.
So you need to move the flash write function (or at least the wait_for_flash_ready()) to outside the interrupt.
Best regards,
Edvin