k_timer_sync is not accurate

Dear,

- nRF connect SDK 1.9.1

- nRF5340 DK

We use 5340 spi to control an adc chip. The sample rate we need is 1kHz, so we use k_timer to produce a 1ms timer.

However, we find that the actual interval is not accurate 1ms but over 1ms. Is there a problem with the way we use functions?Or are there other timer functions that are more accurate?

				k_timer_status_sync(&my_sync_timer);
				k_timer_start(&my_sync_timer, K_USEC(1000), K_NO_WAIT);
				tx_buffer[0] = tx_adc_command[2*i];
				tx_buffer[1] = tx_adc_command[(2*i+1)];
				err = nrfx_spim_xfer(&spim, &xfer_desc, 0);

Parents
  • Hello

    If you start your timer like this, it will expire each millisecond, and you should not have to start it again, which could be the reason your interval is over 1ms:

    k_timer_start(&my_sync_timer, K_USEC(1000), K_USEC(1000));

    Have you tried this?
    Best regards,
    Einar

  • Dear Einar,

    Thank you for your immediate reply. I have tried your advice, but the interval is still over 1ms. The whole sample code is posted below. To achieve 1ms cycle, each loop will not start until 1ms timer over, so I think it is necessary to start timer for each loop. Is the delay caused by k_timer_start?

    Best regards,

    Zhaoyuan

    for(uint16_t i = 0; i<times*16+2; i++)
    		{
    			k_timer_status_sync(&my_sync_timer);
    			k_timer_start(&my_sync_timer, K_USEC(1000), K_USEC(1000));
    
    			tx_buffer[0] = channel;
    			tx_buffer[1] = 0;
    			err = nrfx_spim_xfer(&spim, &xfer_desc, 0);
    
    			if (err != NRFX_SUCCESS) {	
    				printk("nrfx_spim_xfer() failed: 0x%08x\n", err);
    				return false;
    			}
    			if(i>=2)
    			{
    				spim_buffer[2*(i-2)] = rx_buffer[0];
    				spim_buffer[2*(i-2)+1] = rx_buffer[1];
    			}	
    				// printk("%ld\n%ld\n", t0, t1);
    			
    		}

  • Yes, the delay is probably caused by k_timer_start. When you call this function, the timer is reset and counts from 0.

    I would suggest something like this instead:

    k_timer_start(&my_timer, K_USEC(1000), K_USEC(1000));
    for(uint16_t i = 0; i<16; i++)
    	{
    		k_timer_status_sync(&my_timer);
    		printk("Timer sync: %i\n", i);
    	}

    -Einar

Reply Children
No Data
Related