Data from sensor can not be sampled at 1000 Hz.

Hi,

I am using nRF_SDK_15.0.0_a53641 and writing a program with keil.

I am receiving data from the sensor with TWI communication and writing it to the sensor by SPI communication.

So I sampled the data with reference to the apptimer example,

timer_ms = 1;
time_ticks = nrf_drv_timer_ms_to_ticks(&TIMER_TWI, time_ms);

As I tried to write data every 1 ms (1000 Hz), it runs at sampling frequency much lower than 1000 Hz.

Is it due to some other configuration?

The frequency setting for TWI communication with the sensor and SPI communication with the SD card is the default.

I will attach a part of the program below.

/**
 * @brief Handler for timer events.
 */
void timer_twi_event_handler(nrf_timer_event_t event_type, void* p_context)
{

    switch (event_type)
    {
        case NRF_TIMER_EVENT_COMPARE1:
					
        
			flag = 1;
					
            break;

            default:
            //Do nothing.
            break;
    }
}

int main(void)
{
	float time = 0.000;
	float g = 9.81;
	float t = 0.001;
	uint32_t time_ms = 1; //Time(in miliseconds) between consecutive compare events.
	uint32_t time_ticks;
    uint32_t err_code = NRF_SUCCESS;

	//SPI start condition
	SPI_start();
	
		
    bsp_board_init(BSP_INIT_LEDS);
	
	
    APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    NRF_LOG_DEFAULT_BACKENDS_INIT();

    NRF_LOG_INFO("FATFS example started.");
	
	//Configure TIMER_LED for generating simple light effect - leds on board will invert his state one after the other.
    nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
    err_code = nrf_drv_timer_init(&TIMER_TWI, &timer_cfg, timer_twi_event_handler);
    APP_ERROR_CHECK(err_code);

    time_ticks = nrf_drv_timer_ms_to_ticks(&TIMER_TWI, time_ms);

    nrf_drv_timer_extended_compare(
         &TIMER_TWI, NRF_TIMER_CC_CHANNEL1, time_ticks, NRF_TIMER_SHORT_COMPARE1_CLEAR_MASK, true);

    nrf_drv_timer_enable(&TIMER_TWI);

	TWI_start();//ST
	nrf_delay_us(5);
    twi_init();
	nrf_delay_us(5);
    LM75B_set_mode();//SAD+W,SUB(SUB[7] set to 1)
   	nrf_delay_us(100);
   	
    while (true)
    {

		if(flag){
			time += t;
   	        TWI_start();//SR
		    nrf_delay_us(600);//600-500
		    read_sensor_data();//SAD+R
		    nrf_delay_us(4);
	        TWI_stop();//SP
		    nrf_delay_us(5);
	
	        //uint8 to short(16bit)
	        x = (short)(m_sample[1] << 8 | m_sample[0]);
	        y = (short)(m_sample[3] << 8 | m_sample[2]);
		    z = (short)(m_sample[5] << 8 | m_sample[4]);

    
            x_g = (((float)x*2*2)/(float)65536);
		    y_g = (((float)y*2*2)/(float)65536);
		    z_g = (((float)z*2*2)/(float)65536);
		
		    x_g = x_g * g;
		    y_g = y_g * g;
		    z_g = z_g * g;

		    sprintf(time_b,"%lf",time);
	        sprintf(buf0,"%lf",x_g);
	        sprintf(buf1,"%lf",y_g);
	        sprintf(buf2,"%lf",z_g);
		

            fatfs_example(time_b,buf0,buf1,buf2);
		
		    flag = 0;
			
			
		}
		__WFI();
    }
}

Thank you.

Parents
  • Hi,

    I'm pretty sure you are trying to do too much in that while loop to complete it every 1 ms. The maximum frequency of TWI is 400 kHz. How many bytes are you reading/writing each time you read the sensor? I also do not understand why you initialize and configure it every iteration of the while loop. I also suspect that the multiple sprintf calls you are making can take some time. I would recommend that you toggle GPIOs in start and end of while loop to check with a scope/logic analyzer how long the operations will take in the current state. Then try to reduce this time.

    Best regards,
    Jørgen

Reply Children
No Data