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

Stuck Timer Value always reading Zero!

Hi,

I am using Timer0 to measure external pulse width. Using GPIOTE, PPI task and events. The problem here is, I am reading Timer0 Capture register as Zero always. Here I am giving PWM externally (10% duty cycle). Am I missing something here, does it need a read time interval? Kindly do the needful.

Code Snippets:

void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{

if(pin == PIN_IN_2)
{
printf("Timer value: %d\r\n",(nrf_drv_timer_capture_get(&m_timer, NRF_TIMER_CC_CHANNEL0)));
nrf_drv_timer_clear(&m_timer);
}
}

static void gpiote_init(void)
{
ret_code_t err_code;

err_code = nrf_drv_gpiote_init();
APP_ERROR_CHECK(err_code);
nrf_drv_gpiote_in_config_t in_config = GPIOTE_CONFIG_IN_SENSE_LOTOHI(true);
in_config.pull = NRF_GPIO_PIN_PULLDOWN;
err_code = nrf_drv_gpiote_in_init(PIN_IN_2, &in_config, in_pin_handler);
APP_ERROR_CHECK(err_code);

}

void timer_init()
{
ret_code_t err_code;

nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
timer_cfg.bit_width = NRF_TIMER_BIT_WIDTH_32;
err_code = nrf_drv_timer_init(&m_timer, &timer_cfg, timer_handler);
APP_ERROR_CHECK(err_code);
}

void ppi_init()
{
ret_code_t err_code;

err_code = nrf_drv_ppi_init();
APP_ERROR_CHECK(err_code);

err_code = nrf_drv_ppi_channel_alloc(&ppi_channel_1);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_ppi_channel_alloc(&ppi_channel_2);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_ppi_channel_alloc(&ppi_channel_3);
APP_ERROR_CHECK(err_code);

uint32_t gpiote_evt_addr_2 = nrf_drv_gpiote_in_event_addr_get(PIN_IN_2);

uint32_t timer_start_task_addr = nrf_drv_timer_task_address_get(&m_timer, NRF_TIMER_TASK_START);
uint32_t timer_stop_task_addr = nrf_drv_timer_task_address_get(&m_timer, NRF_TIMER_TASK_STOP);
uint32_t timer_compare_task_addr = nrf_drv_timer_task_address_get(&m_timer, NRF_TIMER_TASK_CAPTURE2);

if(LATCH_GPIOTE==0){
err_code = nrf_drv_ppi_channel_assign(ppi_channel_1, gpiote_evt_addr_2, timer_start_task_addr);
APP_ERROR_CHECK(err_code);
LATCH_GPIOTE++;
}
else if (LATCH_GPIOTE==1){
err_code = nrf_drv_ppi_channel_assign(ppi_channel_2, gpiote_evt_addr_2, timer_compare_task_addr);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_ppi_channel_assign(ppi_channel_3, gpiote_evt_addr_2, timer_stop_task_addr);
APP_ERROR_CHECK(err_code);
LATCH_GPIOTE-=1;
}

err_code = nrf_drv_ppi_channel_enable(ppi_channel_1);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_ppi_channel_enable(ppi_channel_2);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_ppi_channel_enable(ppi_channel_3);
APP_ERROR_CHECK(err_code);
}

int main(void)
{
APP_ERROR_CHECK(NRF_LOG_INIT(NULL));

gpiote_init();
timer_init();
ppi_init();
printf("Example started\r\n");
NRF_LOG_INFO("Example start\r\n");


while(true){
__WFE();
}
}

Parents Reply Children
Related