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

NRF52 real sampling rate different

Hello, I was wondering why I am not getting a theoretical sampling rate as I set them to with a timer.

I am using PPI to trigger a SAADC sampling with the timer to set to 8ms. However, I am not getting a sampling rate with 125Hz. My Connection intervals for BT are Min = 20 and Max=30. Also, I am using 2 buffers to ensure that sampling happens even if other higher priority events than saadc_callback

   uint32_t ticks = nrf_drv_timer_ms_to_ticks(&m_timer, 8);
    nrf_drv_timer_extended_compare(&m_timer, NRF_TIMER_CC_CHANNEL0, ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, false);
    nrf_drv_timer_enable(&m_timer);

    uint32_t timer_compare_event_addr = nrf_drv_timer_compare_event_address_get(&m_timer, NRF_TIMER_CC_CHANNEL0);
    uint32_t saadc_sample_event_addr = nrf_drv_saadc_sample_task_get();

    /* setup ppi channel so that timer compare event is triggering sample task in SAADC */
    err_code = nrf_drv_ppi_channel_alloc(&m_ppi_channel);
    APP_ERROR_CHECK(err_code);
    
    err_code = nrf_drv_ppi_channel_assign(m_ppi_channel, timer_compare_event_addr, saadc_sample_event_addr);
    APP_ERROR_CHECK(err_code);

I am wondering if there are any solutions for the inconsistency of the sampling rate or a way I can calculate the inconsistency and optimize for it. My project highly depends on constant sampling rate and having no blind spots for sampling.

Thank you, John

  • Hi,

    Since you are using 8ms sample intervals the HFCLK based TIMER is not recommended as it adds a lot of current consumption. Instead you can use the LFCLK.

    The sample rate is relatively slow, so you should not need to be worried about being interrupted by SoftDevice activity. To ensure that you do not collide with the SoftDevice you can read the buffers from a lower priority interrupt when the application has time.

    Instead of using the SAADC directly have you tried to set a GPIO using the task and measuring the frequency, if so, what did you get?

    Best regards,

    Øyvind

  • Hello, Thank you for your thoughtful answer. I will go ahead and do the GPIO test today.

    In My question, I forgot to indicate that I was using timeslot for multiprotocol with nrf_esb. Can that be the reason it would not work?

    Also, I have one question regarding your answer. With soft device working, I thought that HFCLK will always run because I do not have code anywhere that turns the HFCLK on. Is this not true and will the HFCLK only be turned on when a radio event is happening?

    Thank you, John Lee

  • Well, running a lot of things at the same time means there are more things that can potentially go wrong. However I think this should be fairly simple to get running. By the way, it may be worth looking into the Application timer tutorial if you will use the LFCLK.

    The SoftDevice uses the HFCLK at two times, when the radio is running and when it has to calibrate the LFRC (Low frequency RC clock source, if you do not have a separate LF crystal). The HFCLK adds a lot of current consumption when running, and should therefore be left off when not in use.

  • Sorry for the late update! I did some testing and as you said the ppi and ssadc timings were very accurate. However, it turned out that I was losing data while they were being sent to the phone via BLE. With the connection interval at 10ms and sending 16 bytes per packet, I was able to only receive 80% of the total packet I send. This was regardless of the sampling rate(I tried 125Hz 500Hz 1000Hz). I did some research online and some say that BLE is not a reliable protocol and is expected to lose some packets on air. Is this true about BLE? I expect the packet loss rate to be much lower. If the data loss rate is true is there other ways I can reduce the packet loss?

  • Hi,

    Yes and no. You can expect that there will be some packet loss, but BLE is a reliable protocol. Every packet is acknowledged if it is received, so if it does not get received it will get retransmitted. This is handled automatically for you by the SoftDevice.

    What you are likely seeing is that the measurements that occur during BLE connections are failing. You can work around/with this, but I suggest doing some reading on time critical measurements and SoftDevice usage first and then creating a new question as we are moving beyond the scope of the initial topic.

Related