The problem is a little weird and takes me a few hours to address it. But I still don't know how this happens.
Here is the problem, in my application I use saadc to measure voltage. Then I use fds to store some sensor data. I notice that even I set rec_id (e.g. 0x1111) to write sensor data to flash. The rec_id is always changed to saadc value. I suspect the buffer used for saadc is shared with rec_id. But I don't understand why. Is there any way I can change the address for storing rec_id or saadc_buffer?
here is my saadc_callback
/**
* @brief SAADC (battery) sensor event callback.
*/
void saadc_callback(nrf_drv_saadc_evt_t const * p_saadc_event)
{
if (p_saadc_event->type == NRF_DRV_SAADC_EVT_DONE)
{
ret_code_t err_code;
char data_arr_volt[20];
err_code = nrf_drv_saadc_buffer_convert(p_saadc_event->data.done.p_buffer, SAMPLES_IN_BUFFER);
APP_ERROR_CHECK(err_code);
#ifdef UART_PRINTING_ENABLED
NRF_LOG_INFO("saadc value= %d\r\n", p_saadc_event->data.done.p_buffer[0]);
printf("voltage value %d on pin AIN0\r\n",p_saadc_event->data.done.p_buffer[0]);
#endif
sprintf(data_arr_volt, "%d volt on AIN0_V\r\n", p_saadc_event->data.done.p_buffer[0]); //get two decimal
// ble_nus_data_send(&m_nus, data_array, &length, conn_handle);
uint16_t length = strlen(data_arr_volt);
if (m_conn_handle == 0)
{
m_conn_handle = m_conn_handle+1;
err_code = ble_nus_data_send(&m_nus, (uint8_t *)data_arr_volt, &length, m_conn_handle);
}
else if(m_conn_handle == 1)
{
m_conn_handle = m_conn_handle-1;
err_code = ble_nus_data_send(&m_nus, (uint8_t *)data_arr_volt, &length, m_conn_handle);
}
}
}
}
Here is a regular fds_write()
uint32_t file_id = 0x02000;
uint32_t rec_id_write = 0x1111;
uint32_t rec_id_read = 0x1111;
uint32_t rec_id_delete = 0x1111;
/* Data to write in flash. */
struct
{
char string[50] ; //Change this size as required.
}fds_data;
fds_record_t record =
{
.file_id = 0x02000,
.key = 0x1111,
.data.p_data = &fds_data,
/* The length of a record is always expressed in 4-byte units (words). */
.data.length_words = 50,//(sizeof(fds_data_str) + 3) / 4, //Also change this if array size is changed.
};
void fds_write()
{
ret_code_t rc;
strcpy( fds_data.string, "\n test_fds.\n" );
#ifdef UART_PRINTING_ENABLED
NRF_LOG_INFO("Writing start");
printf("Writing start");
#endif
fds_record_desc_t desc = {0};
record.file_id = file_id;
// fds_data.string = "test"; //{'a', 'b', 'c','d'};
for(int i=0;i<1;i++)
{
memset(&desc, 0x00, sizeof(fds_record_desc_t));
record.key = rec_id_write;
rc = fds_record_write(&desc, &record);
wait_for_write();
if(rc == FDS_SUCCESS) {
#ifdef UART_PRINTING_ENABLED
NRF_LOG_INFO("\nData written with id %d \n",rec_id_write);//
printf("\nData written with id %d\n",rec_id_write);//
#endif
}
else {
#ifdef UART_PRINTING_ENABLED
NRF_LOG_INFO("\nWrite Failed, id %d\n",rec_id_write);
printf("\nWrite Failed, id %d\n",rec_id_write);
#endif
}
rec_id_write++;
}
}
Thanks,
-Lei