/** * @brief Timer0 interrupt handler. * * @param[in] event_type Timer event. * @param[in] p_context General purpose parameter set during initialization of * the timer. This parameter can be used to pass * additional information to the handler function, for * example, the timer ID. */ static void counter_compare_handler(nrf_timer_event_t event_type, void* p_context) { if(event_type == NRF_TIMER_EVENT_COMPARE0) { uint16_t val = nrf_drv_timer_capture_get(&m_timer1, NRF_TIMER_CC_CHANNEL1); nrf_drv_timer_pause(&m_timer1); nrf_drv_timer_clear(&m_timer1); /* Handle finished measurement. */ conversion_handler(val); } }
/** @brief Function for initializing PPI used in infrared signal decoding * The PPI is needed to convert the timer event into a task. */ uint32_t ir_ppi_init(void) { uint32_t gpiote_event_addr; uint32_t timer_task_addr; nrf_ppi_channel_t ppi_channel; ret_code_t err_code; nrf_drv_gpiote_in_config_t config; config.sense = NRF_GPIOTE_POLARITY_HITOLO; config.pull = NRF_GPIO_PIN_PULLUP; config.hi_accuracy = false; config.is_watcher = false; nrf_drv_timer_config_t timer_config; timer_config.frequency = NRF_TIMER_FREQ_1MHz; timer_config.mode = NRF_TIMER_MODE_TIMER; timer_config.bit_width = NRF_TIMER_BIT_WIDTH_32; timer_config.interrupt_priority = 3; err_code = nrf_drv_timer_init(&ir_timer, &timer_config, timer_dummy_handler); APP_ERROR_CHECK(err_code); // Set up GPIOTE err_code = nrf_drv_gpiote_in_init(IR_RECEIVER_PIN_1, &config, ir_in_pin_handler); APP_ERROR_CHECK(err_code); err_code = nrf_drv_gpiote_in_init(IR_RECEIVER_PIN_2, &config, ir_in_pin_handler); APP_ERROR_CHECK(err_code); err_code = nrf_drv_gpiote_in_init(IR_RECEIVER_PIN_3, &config, ir_in_pin_handler); APP_ERROR_CHECK(err_code); // Set up timer for capturing nrf_drv_timer_capture_get(&ir_timer, NRF_TIMER_CC_CHANNEL0); // Set up PPI channel err_code = nrf_drv_ppi_channel_alloc(&ppi_channel); APP_ERROR_CHECK(err_code); timer_task_addr = nrf_drv_timer_capture_task_address_get(&ir_timer, NRF_TIMER_CC_CHANNEL0); gpiote_event_addr = nrf_drv_gpiote_in_event_addr_get(IR_RECEIVER_PIN_1); //err_code = nrf_drv_ppi_channel_assign(ppi_channel, gpiote_event_addr, timer_task_addr); //APP_ERROR_CHECK(err_code); //err_code = nrf_drv_ppi_channel_enable(ppi_channel); //APP_ERROR_CHECK(err_code); nrf_drv_gpiote_in_event_enable(IR_RECEIVER_PIN_1, true); nrf_drv_gpiote_in_event_enable(IR_RECEIVER_PIN_2, true); nrf_drv_gpiote_in_event_enable(IR_RECEIVER_PIN_3, true); // Enable timer nrf_drv_timer_enable(&ir_timer); return 0; }