Hi! I'm trying to use a counter to count the pulse widths of an IR signal packet everytime it tirggers an event on a GPIO, however I've been going in loops without sucess.
Here is my current code:
void timer_handler(nrf_timer_event_t event_type, void * p_context)
{
switch (event_type)
{
case NRF_TIMER_EVENT_COMPARE0:
if (timer_started){
timer_started = false;
NRF_TIMER0->TASKS_CAPTURE[0] = 1;
pulse_duration = NRF_TIMER0->CC[0];
//pulse_duration = nrf_timer_cc_read(m_timer.p_reg, NRF_TIMER_CC_CHANNEL0);
NRF_LOG_INFO("PULSE DURATION: %u", pulse_duration);
}
break;
default:
//Do nothing.
break;
}
}
void gpiote_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{
nrf_drv_gpiote_out_toggle(LED_1);
//NRF_LOG_INFO("GPIOTE HANDLER: %d | %d", pin, action);
if(nrf_gpio_pin_read(pin) == 1) {
pulse_count++;
NRF_LOG_INFO("PULSE Count: %u", pulse_count);
nrf_drv_timer_enable(&m_timer);
nrf_drv_timer_clear(&m_timer);
}
else {
NRF_LOG_INFO("PIN IS 0");
nrf_drv_timer_disable(&m_timer);
}
}
/**
* @brief Function for application main entry.
*/
int main(void)
{
ret_code_t err_code;
// Initialize.
log_init();
timers_init();
err_code = nrf_drv_gpiote_init();
APP_ERROR_CHECK(err_code);
nrf_drv_gpiote_out_config_t out_config = GPIOTE_CONFIG_OUT_SIMPLE(false);
err_code = nrf_drv_gpiote_out_init(LED_1, &out_config);
APP_ERROR_CHECK(err_code);
nrf_drv_gpiote_in_config_t in_config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(true);
in_config.pull = NRF_GPIO_PIN_NOPULL;
err_code = nrf_drv_gpiote_in_init(IR_RECEIVER_PIN, &in_config, gpiote_handler);
APP_ERROR_CHECK(err_code);
nrf_drv_gpiote_in_event_enable(IR_RECEIVER_PIN, true);
nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
err_code = nrf_drv_timer_init(&m_timer, &timer_cfg, timer_handler);
APP_ERROR_CHECK(err_code);
nrf_drv_timer_extended_compare(&m_timer, NRF_TIMER_CC_CHANNEL0, 0xFFFFFF, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true);
nrf_drv_timer_enable(&m_timer);
timer_started = false;
// Enter main loop.
for (;;) {
idle_state_handle();
}
}
I've used both methods to count pulse_duration incluing the one currently commented, in the current case I always get 0, in the commented one I get a value "16777215" which seems to be an overflowing 24 bit timer, however mine is 16bit not 24. What am I doing wrong here, can someone help?
Thanks in advance.