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

How to correct the sampling time?

Dear everyone,

My platform is SDK16 and IDE is KEIL V5.25.

I set timer 2 to 104uS to sample a serial data. It isn't UART data, no start bit and stop bit.The start bytes are 0x55,0x55,0x01,0x01. But The sample time and data rate aren't very match. It cannot sample data correctly as attach picture. The function of tda5235_sample_handler is for correcting the sample time at falling edge of RX data. But it seems ineffective. I use a gpio of TDA5235B_SS_PIN (toggle it)  to debug the sample time. Could anyone guide me how to correct the sample time?

void m_timer2_event_handler(nrf_timer_event_t event_type, void *p_context)
{
    if (lf_rf_task.items.rssi_valid)
    {
        if (lf_rf_task.items.sync_rx == 1)
        {
            lf_rf_task.sample_rx_buf[lf_rf_task.sample_rx_len] &= ~1;
            nrf_gpio_pin_toggle(TDA5235B_SS_PIN);
            if (nrf_gpio_pin_read(TDA5235A_PP1_PIN))
                lf_rf_task.sample_rx_buf[lf_rf_task.sample_rx_len] |= 1;
            if (++lf_rf_task.sample_rx_bit >= 8)
            {
                lf_rf_task.sample_rx_bit = 0;
                if (++lf_rf_task.sample_rx_len >= 9)
                {
                    lf_rf_task.items.sync_rx = 0;
                    lf_rf_task.items.sample_rx_valid = 1;
                }
            }
            else
            {
                lf_rf_task.sample_rx_buf[lf_rf_task.sample_rx_len] <<= 1;
                lf_rf_task.items.sync_rx = 1;
            }
        }
        else
        {
            rrrr &= ~1;
            nrf_gpio_pin_toggle(TDA5235B_SS_PIN);
            if (nrf_gpio_pin_read(TDA5235A_PP1_PIN))
                rrrr |= 1;
            if (++lf_rf_task.sample_rx_bit >= 24)
            {
                if ((rrrr & 0xffffff) == 0x550101)
                {
                    // sync received
                    lf_rf_task.sample_rx_len = 0;
                    lf_rf_task.sample_rx_bit = 0;
                    lf_rf_task.items.sync_rx = 1;
                }
                else
                {
                    rrrr <<= 1;
                    lf_rf_task.sample_rx_bit--;
                }
            }
        }
    }
    else
    {
        // rssi just under threshole, clear counter
        lf_rf_task.items.sync_rx = 0;
    }
    nrf_drv_saadc_sample();
}

void tda5235_sample_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{
    NRF_TIMER2->TASKS_COUNT = 100; //  it is ineffective.
}

static void tda5235_gpio_init(void)
{
    ret_code_t err_code;

    nrf_gpio_cfg_input(TDA5235A_PP1_PIN, NRF_GPIO_PIN_PULLUP);
    nrf_gpio_cfg_input(TDA5235A_PP2_PIN, NRF_GPIO_PIN_PULLUP);

    nrf_drv_gpiote_in_config_t in_config = GPIOTE_CONFIG_IN_SENSE_HITOLO(true);
    in_config.pull = NRF_GPIO_PIN_PULLUP;
    err_code = nrf_drv_gpiote_in_init(TDA5235A_PP2_PIN, &in_config, tda5235_int_handler);
    APP_ERROR_CHECK(err_code);
    nrf_drv_gpiote_in_event_enable(TDA5235A_PP2_PIN, true);

    // for data sample correction
    err_code = nrf_drv_gpiote_in_init(TDA5235A_PP1_PIN, &in_config, tda5235_sample_handler);
    APP_ERROR_CHECK(err_code);
    nrf_drv_gpiote_in_event_enable(TDA5235A_PP1_PIN, true);
}

Parents
  • Ted,

    I think you misunderstood how the Timer works.

    void tda5235_sample_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
    {
        NRF_TIMER2->TASKS_COUNT = 100; //  it is ineffective.
    }

    TASKS_COUNT does not take the count of the counter to a number after which you expect a timeout. All TASKS_XXX in the nRF device only take a value of 1. It means that for 1, trigger that particular task. When you are writing 100 to TASKS_COUNT you are basically trying to write binary 1100100 into it. Since it is only 1 bit register, you are effectively writing 0 into it. And this TASKS only work in COUNTER mode.

    I guess what you are trying to do is to get a timer callback after 100 ticks. If that is what you are trying to do then probably you need to set this in TIMER2->CC register and restart the timer.

    I strongly suggest you not to access the registers of the timers directly and use the timer library instead.

Reply
  • Ted,

    I think you misunderstood how the Timer works.

    void tda5235_sample_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
    {
        NRF_TIMER2->TASKS_COUNT = 100; //  it is ineffective.
    }

    TASKS_COUNT does not take the count of the counter to a number after which you expect a timeout. All TASKS_XXX in the nRF device only take a value of 1. It means that for 1, trigger that particular task. When you are writing 100 to TASKS_COUNT you are basically trying to write binary 1100100 into it. Since it is only 1 bit register, you are effectively writing 0 into it. And this TASKS only work in COUNTER mode.

    I guess what you are trying to do is to get a timer callback after 100 ticks. If that is what you are trying to do then probably you need to set this in TIMER2->CC register and restart the timer.

    I strongly suggest you not to access the registers of the timers directly and use the timer library instead.

Children
No Data
Related