Skip to content

Commit

Permalink
samv7/adc: fix handling of ANIOC_TRIGGER ioctl
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
michallenc authored and xiaoxiang781216 committed Jan 23, 2025
1 parent 2c4fe28 commit 758b3ba
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions arch/arm/src/samv7/sam_afec.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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,
};
Expand Down Expand Up @@ -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,
};
Expand Down Expand Up @@ -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");

Expand All @@ -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");

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 758b3ba

Please sign in to comment.