channel gain switching in aefc by ioctl

Update arch/arm/include/samv7/sam_afec.h

remove "offset may be uninitialized" warning

Update arch/arm/include/samv7/sam_afec.h

Co-authored-by: Petro Karashchenko <petro.karashchenko@gmail.com>

Update arch/arm/include/samv7/sam_afec.h

Co-authored-by: Petro Karashchenko <petro.karashchenko@gmail.com>

Update arch/arm/src/samv7/sam_afec.c

Co-authored-by: Petro Karashchenko <petro.karashchenko@gmail.com>

Update arch/arm/src/samv7/sam_afec.c

Co-authored-by: Petro Karashchenko <petro.karashchenko@gmail.com>

Update arch/arm/include/samv7/sam_afec.h

Co-authored-by: Petro Karashchenko <petro.karashchenko@gmail.com>

Update arch/arm/include/samv7/sam_afec.h

Co-authored-by: Petro Karashchenko <petro.karashchenko@gmail.com>

remove blank line
This commit is contained in:
Simon Filgis 2022-11-14 09:59:56 +01:00 committed by Xiang Xiao
parent 3f878d8100
commit 882afc885e
3 changed files with 125 additions and 12 deletions

View file

@ -0,0 +1,69 @@
/****************************************************************************
* arch/arm/include/samv7/sam_afec.h
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
#ifndef __ARCH_ARM_INCLUDE_SAMV7_SAM_AFEC_H
#define __ARCH_ARM_INCLUDE_SAMV7_SAM_AFEC_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/analog/adc.h>
/* IOCTL Commands ***********************************************************
*
* Cmd: SAMV7_AFEC_IOCTRL_GAIN Arg: Channel and Gain
*
*/
#define ANIOC_SAMV7_AFEC_IOCTRL_GAIN _ANIOC(AN_SAMV7_AFEC_FIRST + 0)
/****************************************************************************
* Public Types
****************************************************************************/
typedef struct sam_afec_gain_param_s
{
uint8_t channel; /* channel number of aefc instance */
uint8_t gain; /* gain to be set for the channel 0:1, 1:2, 3:4 */
} sam_afec_gain_param_tds;
#ifndef __ASSEMBLY__
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
#undef EXTERN
#if defined(__cplusplus)
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
#undef EXTERN
#if defined(__cplusplus)
}
#endif
#endif /* __ASSEMBLY__ */
#endif /* __ARCH_ARM_INCLUDE_SAMV7_SAM_AFEC_H */

View file

@ -34,6 +34,7 @@
#include <debug.h>
#include <arch/board/board.h>
#include <arch/chip/sam_afec.h>
#include <nuttx/irq.h>
#include <nuttx/arch.h>
#include <nuttx/wqueue.h>
@ -826,6 +827,7 @@ static void afec_reset(struct adc_dev_s *dev)
gpio_pinset_t pinset = 0;
uint32_t afec_cher = 0;
uint32_t afec_cgr = 0;
for (int i = 0; i < priv->nchannels; i++)
{
DEBUGASSERT(priv->chanlist[i] < ADC_MAX_CHANNELS);
@ -837,11 +839,13 @@ static void afec_reset(struct adc_dev_s *dev)
afec_putreg(priv, SAM_AFEC_COCR_OFFSET, 0x200);
afec_cher |= AFEC_CH(priv->chanlist[i]);
afec_cgr |= AFEC_CGR_GAIN(priv->chanlist[i], 0);
}
/* Enable channels */
afec_putreg(priv, SAM_AFEC_CHER_OFFSET, afec_cher);
afec_putreg(priv, SAM_AFEC_CGR_OFFSET, afec_cgr);
return;
@ -1021,20 +1025,55 @@ static int afec_ioctl(struct adc_dev_s *dev, int cmd, unsigned long arg)
}
break;
#endif
case ANIOC_GET_NCHANNELS:
{
/* Return the number of configured channels */
case ANIOC_GET_NCHANNELS:
{
/* Return the number of configured channels */
ret = priv->nchannels;
}
break;
ret = priv->nchannels;
}
break;
case ANIOC_SAMV7_AFEC_IOCTRL_GAIN:
{
/* Set the requested gain of the associated channel */
default:
{
aerr("ERROR: Unknown cmd: %d\n", cmd);
ret = -ENOTTY;
}
break;
sam_afec_gain_param_tds *chgain = (sam_afec_gain_param_tds *) arg;
uint32_t afec_cgr = afec_getreg(priv, SAM_AFEC_CGR_OFFSET);
afec_cgr &= ~AFEC_CGR_GAIN_MASK(chgain->channel);
afec_cgr |= AFEC_CGR_GAIN(chgain->channel, chgain->gain);
afec_putreg(priv, SAM_AFEC_CGR_OFFSET, afec_cgr);
/* new gain is set, now adjust the offset register, use gain
* 1 as default and fallback
*/
uint16_t offset;
if (chgain->gain == 1)
{
offset = 0x100;
}
else if (chgain->gain == 2)
{
offset = 0x40;
}
else
{
offset = 0x200;
}
afec_putreg(priv, SAM_AFEC_CSELR_OFFSET,
AFEC_CSELR_CSEL(chgain->channel));
afec_putreg(priv, SAM_AFEC_COCR_OFFSET, offset);
}
break;
default:
{
aerr("ERROR: Unknown cmd: %d\n", cmd);
ret = -ENOTTY;
}
break;
}
return ret;

View file

@ -104,6 +104,11 @@
#define AN_MCP48XX_FIRST (AN_MAX1161X_FIRST + AN_MAX1161X_NCMDS)
#define AN_MCP48XX_NCMDS 3
/* See arch/arm/src/samv7/sam_afec.h */
#define AN_SAMV7_AFEC_FIRST (AN_MCP48XX_FIRST + AN_MCP48XX_NCMDS)
#define AN_SAMV7_AFEC_NCMDS 1
/****************************************************************************
* Public Function Prototypes
****************************************************************************/