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

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

commit e433409fcc276e6c753109d95c2335f73bc76f47
Author: Daniel P. Carvalho <[email protected]>
AuthorDate: Mon Sep 14 21:11:52 2026 -0300

    stm32: implement comp ioctl and update nucleo-g431kb defconfig
    
    Implement ao_ioctl in stm32_comp_m3m4_v2.c to handle ANIOC_COMP_ENABLE and
    ANIOC_COMP_DISABLE commands. Also add CONFIG_STM32_COMP_INIT_DISABLED to
    allow keeping the comparator disabled after driver initialization until
    explicitly enabled.
    
    Update nucleo-g431kb:comp defconfig to enable CONFIG_EXAMPLES_COMP and
    set default DAC path for comparator ramp verification.
    
    Assisted-by: Gemini:gemini-2.5-pro
    Signed-off-by: Daniel P. Carvalho <[email protected]>
---
 arch/arm/src/common/stm32/Kconfig.comp             |  6 ++
 arch/arm/src/common/stm32/stm32_comp_m3m4_v2.c     | 96 ++++++++++++++--------
 .../stm32g4/nucleo-g431kb/configs/comp/defconfig   |  3 +
 3 files changed, 72 insertions(+), 33 deletions(-)

diff --git a/arch/arm/src/common/stm32/Kconfig.comp 
b/arch/arm/src/common/stm32/Kconfig.comp
index 57c4c8a5d1a..9bf0506f98b 100644
--- a/arch/arm/src/common/stm32/Kconfig.comp
+++ b/arch/arm/src/common/stm32/Kconfig.comp
@@ -4,6 +4,12 @@
 
 # COMP supported only for M3M4 for now
 
+config STM32_COMP_INIT_DISABLED
+       bool "Do not enable the comparator at initialization"
+       default n
+       ---help---
+               The comparator is kept disabled until ioctl() enable command.
+
 if STM32_HAVE_IP_COMP_M3M4_V2
 
 if STM32_COMP1
diff --git a/arch/arm/src/common/stm32/stm32_comp_m3m4_v2.c 
b/arch/arm/src/common/stm32/stm32_comp_m3m4_v2.c
index 8939e4a6ce9..53ebc1b1567 100644
--- a/arch/arm/src/common/stm32/stm32_comp_m3m4_v2.c
+++ b/arch/arm/src/common/stm32/stm32_comp_m3m4_v2.c
@@ -764,6 +764,7 @@ static int comp_config(struct stm32_comp_s *priv)
 
   comp_putreg_csr(priv, regval);
 
+#ifndef CONFIG_STM32_COMP_INIT_DISABLED
   /* Enable Comparator */
 
   comp_enable(priv, true);
@@ -774,6 +775,7 @@ static int comp_config(struct stm32_comp_s *priv)
     {
       comp_lock_set(priv, true);
     }
+#endif
 
   return OK;
 }
@@ -918,8 +920,36 @@ static int comp_read(struct comp_dev_s *dev)
 #ifdef CONFIG_COMP
 static int comp_ioctl(struct comp_dev_s *dev, int cmd, unsigned long arg)
 {
-#warning "Missing logic"
-  return -ENOTTY;
+  FAR struct stm32_comp_s *priv = (FAR struct stm32_comp_s *)dev->ad_priv;
+  int ret = OK;
+
+  switch (cmd)
+    {
+      case ANIOC_COMP_ENABLE:
+        {
+          /* Enable comparator */
+
+          comp_enable(priv, true);
+          break;
+        }
+
+      case ANIOC_COMP_DISABLE:
+        {
+          /* Disable comparator */
+
+          comp_enable(priv, false);
+          break;
+        }
+
+      default:
+        {
+          aerr("ERROR: Unknown cmd: %d\n", cmd);
+          ret = -ENOTTY;
+          break;
+        }
+    }
+
+  return ret;
 }
 #endif
 
@@ -954,57 +984,57 @@ struct comp_dev_s *stm32_compinitialize(int intf)
   switch (intf)
     {
 #ifdef CONFIG_STM32_COMP1
-    case 1:
-      ainfo("COMP1 selected\n");
-      dev = &g_comp1dev;
-      break;
+      case 1:
+        ainfo("COMP1 selected\n");
+        dev = &g_comp1dev;
+        break;
 #endif
 
 #ifdef CONFIG_STM32_COMP2
-    case 2:
-      ainfo("COMP2 selected\n");
-      dev = &g_comp2dev;
-      break;
+      case 2:
+        ainfo("COMP2 selected\n");
+        dev = &g_comp2dev;
+        break;
 #endif
 
 #ifdef CONFIG_STM32_COMP3
-    case 3:
-      ainfo("COMP3 selected\n");
-      dev = &g_comp3dev;
-      break;
+      case 3:
+        ainfo("COMP3 selected\n");
+        dev = &g_comp3dev;
+        break;
 #endif
 
 #ifdef CONFIG_STM32_COMP4
-    case 4:
-      ainfo("COMP4 selected\n");
-      dev = &g_comp4dev;
-      break;
+      case 4:
+        ainfo("COMP4 selected\n");
+        dev = &g_comp4dev;
+        break;
 #endif
 
 #ifdef CONFIG_STM32_COMP5
-    case 5:
-      ainfo("COMP5 selected\n");
-      dev = &g_comp5dev;
-      break;
+      case 5:
+        ainfo("COMP5 selected\n");
+        dev = &g_comp5dev;
+        break;
 #endif
 
 #ifdef CONFIG_STM32_COMP6
-    case 6:
-      ainfo("COMP6 selected\n");
-      dev = &g_comp6dev;
-      break;
+      case 6:
+        ainfo("COMP6 selected\n");
+        dev = &g_comp6dev;
+        break;
 #endif
 
 #ifdef CONFIG_STM32_COMP7
-    case 7:
-      ainfo("COMP7 selected\n");
-      dev = &g_comp7dev;
-      break;
+      case 7:
+        ainfo("COMP7 selected\n");
+        dev = &g_comp7dev;
+        break;
 #endif
 
-    default:
-      aerr("ERROR: No COMP interface defined\n");
-      return NULL;
+      default:
+        aerr("ERROR: No COMP interface defined\n");
+        return NULL;
     }
 
   /* Configure selected comparator */
diff --git a/boards/arm/stm32g4/nucleo-g431kb/configs/comp/defconfig 
b/boards/arm/stm32g4/nucleo-g431kb/configs/comp/defconfig
index d47e5c3e774..4f33ff372e6 100644
--- a/boards/arm/stm32g4/nucleo-g431kb/configs/comp/defconfig
+++ b/boards/arm/stm32g4/nucleo-g431kb/configs/comp/defconfig
@@ -19,6 +19,8 @@ CONFIG_BUILTIN=y
 CONFIG_COMP=y
 CONFIG_DAC=y
 CONFIG_DEFAULT_SMALL=y
+CONFIG_EXAMPLES_COMP=y
+CONFIG_EXAMPLES_COMP_DACPATH="/dev/dac5"
 CONFIG_EXAMPLES_DAC=y
 CONFIG_EXAMPLES_DAC_DEVPATH="/dev/dac5"
 CONFIG_FILE_STREAM=y
@@ -37,6 +39,7 @@ CONFIG_STM32_COMP2=y
 CONFIG_STM32_COMP2_HYST=3
 CONFIG_STM32_COMP2_INM=4
 CONFIG_STM32_COMP2_OUT=y
+CONFIG_STM32_COMP_INIT_DISABLED=y
 CONFIG_STM32_DAC3=y
 CONFIG_STM32_DAC3CH2=y
 CONFIG_STM32_DAC3CH2_MODE=3

Reply via email to