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

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.

  • I am receiving data from the sensor with TWI communication and writing it to the sensor (sic?) by SPI communication.

    You mean, "writing it to the SD Card by SPI communication" ?

    Is your flag defined as volatile ?

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

    So what frequency is it?

    Is is the sampling that appears not to work,or the writing?

    Have you looked at  the hardware activity using a scope or analyser?

    Please fix the indentation on your posted code.

  • 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

  • Thank you for reply!

    How many bytes are you reading/writing each time you read the sensor?

    Reading of data from the sensor is carried out by reading 48 bits in total for each 16 bits of X, Y and Z axes.

    Writing to the SD card is 32 bytes in total.

    I also do not understand why you initialize and configure it every iteration of the while loop.

    Is it the initialization part about TWI_init ()?
    This function has been rewritten outside the while loop.

    scope/logic analyzer how long the operations will take in the current state.

    It was only about 10 Hz or so.


    I have something to ask.

    ・Is my usage of apptimer correct?

    Should I separate the timer for read_sensor and the timer for SD card?

    Is there a good way to shorten within a while loop?

    ・I heard that "sprintf" might be the cause, I tried commenting out, but I will not improve it.Can you think of any other causes?

    Thank you for much cooperation!!
  • Thank you for reply!I am sorry that I have lacked much explanation

    ・You mean, "writing it to the SD Card by SPI communication" ?

    Yes.sensor and TWI communication.I will communicate with the SD card via SPI.

    Is your flag defined as volatile ?

    How do I set it? I would appreciate it if you could tell me.

    So what frequency is it?

    This is the sampling frequency at which data read from the sensor is written to the SD card.

    Please fix the indentation on your posted code.

    fixed! Verification please.


    Is my usage of apptimer correct?

    If you have anything to fix such as the contents of a while loop, please point out.

    Thank you for your many cooperation!!

  • You should move everything except TWI read and SPI write outside the loop, there is no need to initialize the TWI/SPI interface more than once. Ideally you should also perform the SPI write while reading the next TWI value. The peripherals can operate concurrently in non-blocking mode. I have attached a code snippet that show how you can implement this in your application:

    bool spi_write_done = true;
    bool twi_read_done = false;
    
    twi_handler()
    {
    	twi_read_done = true;
    }
    
    spi_handler()
    {
    	spi_write_done = true;
    }
    
    main()
    {
    	twi_init(twi_handler);
    	spi_init(spi_handler);
    	
    	while(1)
    	{
    		twi_read_done = false;
    		twi_read(twi_buffer);										// Read sensor data into twi_buffer
    		
    		while((twi_read_done == false) || (spi_write_done == false));	// Wait for TWI read and previous SPI transfer to finish before starting SPI write
    		spi_buffer = twi_buffer;									// Copy sensor data from twi_buffer to spi_buffer. This will allow you to start a new twi_read operation immediately after starting SPI write operation.
    		
    		spi_write_done = false;
    		spi_write(spi_buffer);
    	}
    }

    (Note that this is not a complete, working code. It is only meant for illustrating a possible implementation.) 

Related