This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 882afc885e channel gain switching in aefc by ioctl
882afc885e is described below

commit 882afc885e4cc7e436f8a2b4ccd577fdf11a4e25
Author: Simon Filgis <si...@ingenieurbuero-filgis.de>
AuthorDate: Mon Nov 14 09:59:56 2022 +0100

    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.karashche...@gmail.com>
    
    Update arch/arm/include/samv7/sam_afec.h
    
    Co-authored-by: Petro Karashchenko <petro.karashche...@gmail.com>
    
    Update arch/arm/src/samv7/sam_afec.c
    
    Co-authored-by: Petro Karashchenko <petro.karashche...@gmail.com>
    
    Update arch/arm/src/samv7/sam_afec.c
    
    Co-authored-by: Petro Karashchenko <petro.karashche...@gmail.com>
    
    Update arch/arm/include/samv7/sam_afec.h
    
    Co-authored-by: Petro Karashchenko <petro.karashche...@gmail.com>
    
    Update arch/arm/include/samv7/sam_afec.h
    
    Co-authored-by: Petro Karashchenko <petro.karashche...@gmail.com>
    
    remove blank line
---
 arch/arm/include/samv7/sam_afec.h | 69 +++++++++++++++++++++++++++++++++++++++
 arch/arm/src/samv7/sam_afec.c     | 65 ++++++++++++++++++++++++++++--------
 include/nuttx/analog/ioctl.h      |  5 +++
 3 files changed, 126 insertions(+), 13 deletions(-)

diff --git a/arch/arm/include/samv7/sam_afec.h 
b/arch/arm/include/samv7/sam_afec.h
new file mode 100644
index 0000000000..76f6a790c7
--- /dev/null
+++ b/arch/arm/include/samv7/sam_afec.h
@@ -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 */
diff --git a/arch/arm/src/samv7/sam_afec.c b/arch/arm/src/samv7/sam_afec.c
index f169d45be7..84eb9ad567 100644
--- a/arch/arm/src/samv7/sam_afec.c
+++ b/arch/arm/src/samv7/sam_afec.c
@@ -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 */
-
-          ret = priv->nchannels;
-        }
-        break;
+    case ANIOC_GET_NCHANNELS:
+      {
+        /* Return the number of configured channels */
+
+        ret = priv->nchannels;
+      }
+      break;
+    case ANIOC_SAMV7_AFEC_IOCTRL_GAIN:
+      {
+        /* Set the requested gain of the associated channel */
+
+        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
+         */
 
-      default:
-        {
-          aerr("ERROR: Unknown cmd: %d\n", cmd);
-          ret = -ENOTTY;
-        }
-        break;
+        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;
diff --git a/include/nuttx/analog/ioctl.h b/include/nuttx/analog/ioctl.h
index 5656f1c986..eedc4a9bc2 100644
--- a/include/nuttx/analog/ioctl.h
+++ b/include/nuttx/analog/ioctl.h
@@ -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
  ****************************************************************************/

Reply via email to