From 8566b00df3e1125e16ae3b55d93670e75efec064 Mon Sep 17 00:00:00 2001 From: Mahesh Mahadevan Date: Thu, 28 Apr 2022 10:45:41 -0500 Subject: [PATCH] drivers: counter: Add error checking to MCUX CTImer Improve the error checking for the set_top_value function. Signed-off-by: Mahesh Mahadevan --- drivers/counter/counter_mcux_ctimer.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/drivers/counter/counter_mcux_ctimer.c b/drivers/counter/counter_mcux_ctimer.c index 3fbc5a6bb21..3e263c7de0b 100644 --- a/drivers/counter/counter_mcux_ctimer.c +++ b/drivers/counter/counter_mcux_ctimer.c @@ -135,27 +135,25 @@ static int mcux_lpc_ctimer_set_top_value(const struct device *dev, { const struct mcux_lpc_ctimer_config *config = dev->config; struct mcux_lpc_ctimer_data *data = dev->data; - bool counter_reset = true; - bool counter_interrupt = true; data->top_callback = cfg->callback; data->top_user_data = cfg->user_data; - if (cfg->flags & COUNTER_TOP_CFG_DONT_RESET) { - counter_reset = false; - } - - /* If top value specified is 0, then turn off the interrupt */ - if (cfg->ticks == 0) { - counter_interrupt = false; + if (!(cfg->flags & COUNTER_TOP_CFG_DONT_RESET)) { + CTIMER_Reset(config->base); + } else if (mcux_lpc_ctimer_read(config->base) >= cfg->ticks) { + if (cfg->flags & COUNTER_TOP_CFG_RESET_WHEN_LATE) { + CTIMER_Reset(config->base); + } + return -ETIME; } ctimer_match_config_t match_config = { .matchValue = cfg->ticks, - .enableCounterReset = counter_reset, + .enableCounterReset = true, .enableCounterStop = false, .outControl = kCTIMER_Output_NoAction, .outPinInitState = false, - .enableInterrupt = counter_interrupt }; + .enableInterrupt = true }; CTIMER_SetupMatch(config->base, NUM_CHANNELS, &match_config);