From 758b3baef03325a71606e403f39187c5237b27d6 Mon Sep 17 00:00:00 2001 From: Michal Lenc Date: Thu, 23 Jan 2025 16:51:29 +0100 Subject: [PATCH] samv7/adc: fix handling of ANIOC_TRIGGER ioctl ADC peripheral can be configured also for PWM trigger, so the ifdef should depend only on CONFIG_SAMV7_AFEC_SWTRIG. Also handle the situation when ANIOC_TRIGGER is called and the peripheral is not configured for SW trigger (but the other one may be, so config option is set). Trigger values saved to the private structure are also changed to enums for better code clarity. Signed-off-by: Michal Lenc --- arch/arm/src/samv7/sam_afec.c | 38 ++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/arch/arm/src/samv7/sam_afec.c b/arch/arm/src/samv7/sam_afec.c index 8091affa90..8f8753b471 100644 --- a/arch/arm/src/samv7/sam_afec.c +++ b/arch/arm/src/samv7/sam_afec.c @@ -90,6 +90,13 @@ * Private Types ****************************************************************************/ +enum samv7_afec_triggers +{ + AFEC_TRIGGER_SW = 0, + AFEC_TRIGGER_TIMER, + AFEC_TRIGGER_PWM +}; + struct samv7_dev_s { const struct adc_callback_s *cb; /* Upper driver callback */ @@ -186,14 +193,14 @@ static struct samv7_dev_s g_adcpriv0 = .initialized = 0, .resolution = CONFIG_SAMV7_AFEC0_RES, #if defined (CONFIG_SAMV7_AFEC0_PWMTRIG) - .trigger = 2, + .trigger = AFEC_TRIGGER_PWM, .event_line = CONFIG_SAMV7_AFEC0_PWMEVENT, #elif defined (CONFIG_SAMV7_AFEC0_TIOATRIG) - .trigger = 1, + .trigger = AFEC_TRIGGER_TIMER, .timer_channel = CONFIG_SAMV7_AFEC0_TIOACHAN, .frequency = CONFIG_SAMV7_AFEC0_TIOAFREQ, #else - .trigger = 0, + .trigger = AFEC_TRIGGER_SW, #endif .base = SAM_AFEC0_BASE, }; @@ -230,14 +237,14 @@ static struct samv7_dev_s g_adcpriv1 = .initialized = 0, .resolution = CONFIG_SAMV7_AFEC1_RES, #if defined (CONFIG_SAMV7_AFEC1_PWMTRIG) - .trigger = 2, + .trigger = AFEC_TRIGGER_PWM, .event_line = CONFIG_SAMV7_AFEC0_PWMEVENT, #elif defined (CONFIG_SAMV7_AFEC1_TIOATRIG) - .trigger = 1, + .trigger = AFEC_TRIGGER_TIMER, .timer_channel = CONFIG_SAMV7_AFEC1_TIOACHAN, .frequency = CONFIG_SAMV7_AFEC1_TIOAFREQ, #else - .trigger = 0, + .trigger = AFEC_TRIGGER_SW, #endif .base = SAM_AFEC1_BASE, }; @@ -630,7 +637,7 @@ static int sam_afec_trigger(struct samv7_dev_s *priv) int ret = OK; #ifdef CONFIG_SAMV7_AFEC_SWTRIG - if (priv->trigger == 0) + if (priv->trigger == AFEC_TRIGGER_SW) { ainfo("Setup software trigger\n"); @@ -643,7 +650,7 @@ static int sam_afec_trigger(struct samv7_dev_s *priv) #endif #ifdef CONFIG_SAMV7_AFEC_TIOATRIG - if (priv->trigger == 1) + if (priv->trigger == AFEC_TRIGGER_TIMER) { ainfo("Setup timer/counter trigger\n"); @@ -676,7 +683,7 @@ static int sam_afec_trigger(struct samv7_dev_s *priv) #endif #ifdef CONFIG_SAMV7_AFEC_PWMTRIG - if (priv->trigger == 2) + if (priv->trigger == AFEC_TRIGGER_PWM) { regval = afec_getreg(priv, SAM_AFEC_MR_OFFSET); regval &= ~AFEC_MR_TRGSEL_MASK; @@ -755,7 +762,7 @@ static void afec_reset(struct adc_dev_s *dev) #endif #ifdef CONFIG_SAMV7_AFEC_TIOATRIG - if (priv->trigger == 1) + if (priv->trigger == AFEC_TRIGGER_TIMER) { sam_afec_freetimer(priv); } @@ -1026,10 +1033,17 @@ static int afec_ioctl(struct adc_dev_s *dev, int cmd, unsigned long arg) switch (cmd) { -#ifndef CONFIG_SAMV7_AFEC_TIOATRIG +#ifdef CONFIG_SAMV7_AFEC_SWTRIG case ANIOC_TRIGGER: { - afec_putreg(priv, SAM_AFEC_CR_OFFSET, AFEC_CR_START); + if (priv->trigger == AFEC_TRIGGER_SW) + { + afec_putreg(priv, SAM_AFEC_CR_OFFSET, AFEC_CR_START); + } + else + { + ret = -ENOTTY; + } } break; #endif