I have enabled Timer 2 with 31250Hz frequency. I wanted to call an interrupt after 10ms, ie 312 ticks for the timer. But the interrupt is not getting called, I have spent some time finding the problem, but couldn't find.
Please check the following code, and help me to fix it.
#define UART_TX_BUF_SIZE 256 /**< UART TX buffer size. */
#define UART_RX_BUF_SIZE 1 /**< UART RX buffer size. */
const nrf_drv_timer_t decode_timeout_timer = NRF_DRV_TIMER_INSTANCE(2); //Time out timer, used to stop decoding
static bool m_timer_on;
static uint32_t m_tsop_pin;
static uint32_t err_code = NRF_SUCCESS;
static uint16_t m_array_index;
static void decode_timeout_timer_event_handler(nrf_timer_event_t event_type, void * p_context)
{
printf("decode timeout event handler got called\n");
if(event_type == NRF_TIMER_EVENT_COMPARE0)
{
printf("TIMEOUT\n");
nrf_drv_timer_pause(&decode_timeout_timer);
}
}
void tsop_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{
printf("\nTimer value is: timer2 %u\n", nrf_drv_timer_capture(&decode_timeout_timer, NRF_TIMER_CC_CHANNEL0));
if(pin == m_tsop_pin && action == NRF_GPIOTE_POLARITY_TOGGLE )
{
if(!m_timer_on)
{
printf("Starting decoding...\n");
m_timer_on = true;
nrf_drv_timer_enable(&decode_timeout_timer);
}
}
}
static void gpiote_init()
{
if(!nrf_drv_gpiote_is_init())
{
err_code = nrf_drv_gpiote_init();
}
nrf_drv_gpiote_in_config_t config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(true);
config.pull = NRF_GPIO_PIN_PULLUP;
err_code = nrf_drv_gpiote_in_init(m_tsop_pin, &config, tsop_pin_handler);
nrf_drv_gpiote_in_event_enable(m_tsop_pin, true);
}
static void decode_timeout_timer_init()
{
nrf_drv_timer_config_t timer_config = {
.frequency = NRF_TIMER_FREQ_31250Hz,
.mode = NRF_TIMER_MODE_TIMER,
.bit_width = NRF_TIMER_BIT_WIDTH_16,
.interrupt_priority = APP_IRQ_PRIORITY_HIGH,
.p_context = (void *)(uint32_t) decode_timeout_timer.instance_id
};
err_code |= nrf_drv_timer_init(&decode_timeout_timer, &timer_config, decode_timeout_timer_event_handler);
uint32_t time_out_ms = 10;
uint32_t time_ticks;
time_ticks = nrf_drv_timer_ms_to_ticks(&decode_timeout_timer, time_out_ms);
printf("time_ticks: %u", time_ticks);
//nrf_drv_timer_compare_int_enable(&decode_timeout_timer, NRF_TIMER_CC_CHANNEL0);
nrf_drv_timer_compare(&decode_timeout_timer, NRF_TIMER_CC_CHANNEL0, time_ticks, true);
}
void uart_error_handle(app_uart_evt_t * p_event)
{
if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR)
{
APP_ERROR_HANDLER(p_event->data.error_communication);
}
else if (p_event->evt_type == APP_UART_FIFO_ERROR)
{
APP_ERROR_HANDLER(p_event->data.error_code);
}
}
/**
* @brief Function for application main entry.
*/
int main(void)
{
uint32_t err_code;
const app_uart_comm_params_t comm_params =
{
RX_PIN_NUMBER,
TX_PIN_NUMBER,
RTS_PIN_NUMBER,
CTS_PIN_NUMBER,
APP_UART_FLOW_CONTROL_ENABLED,
false,
UART_BAUDRATE_BAUDRATE_Baud38400
};
APP_UART_FIFO_INIT(&comm_params,
UART_RX_BUF_SIZE,
UART_TX_BUF_SIZE,
uart_error_handle,
APP_IRQ_PRIORITY_LOW,
err_code);
APP_ERROR_CHECK(err_code);
m_tsop_pin = 4;
decode_timeout_timer_init();
gpiote_init();
while (true)
{
// Do Nothing - GPIO can be toggled without software intervention.
}
}
/** @} */