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

address conflict between saadc buffer and fds rec_id

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

  • Hi,

    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.

    It is not supposed to be shared. It sounds more like the SAADC DMA buffer is inadvertently overwriting your record id variable. Is it possible that you may have a buffer overrun? Can you show how you are defining the buffer?

    Best regards,

    Vidar

     

  • Hi Vidar,

    Happy holidays!

    I didn't do any specific definition for buffer. The saadc is mostly from saadc example. Here are buffer related settings

    #define SAMPLES_IN_BUFFER                             1

    static nrf_saadc_value_t m_saadc_buffer[SAMPLES_IN_BUFFER];

    Here is the saadc_init

    /**
     * @brief SAADC (battery) sensor initiate.
     */
    void saadc_init(void)
    {
        ret_code_t err_code;
    
    		nrf_drv_saadc_config_t saadc_config = NRF_DRV_SAADC_DEFAULT_CONFIG;
    		saadc_config.low_power_mode = true;   
        saadc_config.resolution = NRF_SAADC_RESOLUTION_12BIT;
        nrf_saadc_channel_config_t channel_config 
            = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN1);
    		channel_config.gain = NRF_SAADC_GAIN1_6;// gain is 1/6 with 0.6V reference. the full range will be 3.6V for 12bit ADC 4096; NRF_SAADC_GAIN1_4;
    		channel_config.reference = NRF_SAADC_REFERENCE_INTERNAL;// 0.6V internal is more stable than VDD/4; NRF_SAADC_REFERENCE_VDD4;
        err_code = nrf_drv_saadc_init(&saadc_config, saadc_callback);
        APP_ERROR_CHECK(err_code);
    //	NRF_LOG_INFO("error code drv_saadc_init = %d. \r\n", err_code);
    
        err_code = nrf_drv_saadc_channel_init(0, &channel_config);
        APP_ERROR_CHECK(err_code);
    
        err_code = nrf_drv_saadc_buffer_convert(m_saadc_buffer, SAMPLES_IN_BUFFER);
        APP_ERROR_CHECK(err_code);
    }

    If it's due to buffer overrun, how can I avoid it by changing some parameters?

    Thanks a lot!

    -Lei

  • Hi Vidar,

    I compared my code with saadc example and noticed the difference. In saadc example, in "saadc_init", it used    

    static nrf_saadc_value_t m_saadc_buffer[1][SAMPLES_IN_BUFFER];

    err_code = nrf_drv_saadc_buffer_convert(m_saadc_buffer[0], SAMPLES_IN_BUFFER);
        APP_ERROR_CHECK(err_code);.

    In my case, I just used

    static nrf_saadc_value_t m_saadc_buffer[SAMPLES_IN_BUFFER];

    err_code = nrf_drv_saadc_buffer_convert(m_saadc_buffer, SAMPLES_IN_BUFFER);
        APP_ERROR_CHECK(err_code);

    It seems solving the problem if I change my code the same as saadc example. But I don't understand why. This only happened recently when I added fds into my application. Before that, everything was fine.

    Thanks,

    -Lei

  • Hi Lei,

    Sorry for the delayed response. The variable declaration looks to be correct, and I can't really think of any other obvious reasons for the issue you describe. Would you be able to share a minimal version of your project here, or in a private ticekt, so I can try to debug it?

    Thanks,

    Vidar

  • Happy New Year! Vidar.

    For some reason, I cannot repeat the problem right now. I will create a private ticket if the problem comes back again. But I think as you said it might be due to buffer overrun.

    Thanks,

    -Lei

Related