From 2678ec31c745397d829ce94885fb6be07e1073a1 Mon Sep 17 00:00:00 2001 From: "Daniel P. Carvalho" Date: Mon, 14 Sep 2026 21:11:44 -0300 Subject: [PATCH 1/4] analog: add ANIOC_COMP_ENABLE and ANIOC_COMP_DISABLE commands Define standard IOCTL commands to enable and disable analog comparator devices from user-space applications. Assisted-by: Gemini:gemini-2.5-pro Signed-off-by: Daniel P. Carvalho --- include/nuttx/analog/ioctl.h | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/include/nuttx/analog/ioctl.h b/include/nuttx/analog/ioctl.h index 9a027a3f2d290..83bcadc2328e5 100644 --- a/include/nuttx/analog/ioctl.h +++ b/include/nuttx/analog/ioctl.h @@ -86,8 +86,17 @@ * IN: None * OUT: struct dac_info_s * */ +/* COMP */ + +#define ANIOC_COMP_ENABLE _ANIOC(0x000d) /* Enable comparator + * IN: None + * OUT: None */ +#define ANIOC_COMP_DISABLE _ANIOC(0x000e) /* Disable comparator + * IN: None + * OUT: None */ + #define AN_FIRST 0x0001 /* First common command */ -#define AN_NCMDS 12 /* Number of common commands */ +#define AN_NCMDS 14 /* Number of common commands */ /* User defined ioctl commands are also supported. These will be forwarded * by the upper-half driver to the lower-half driver via the ioctl() From bac17ff2546f5c48c0305892f3fe63d28800023d Mon Sep 17 00:00:00 2001 From: "Daniel P. Carvalho" Date: Mon, 14 Sep 2026 21:11:52 -0300 Subject: [PATCH 2/4] 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 --- arch/arm/src/common/stm32/Kconfig.comp | 6 ++ .../arm/src/common/stm32/stm32_comp_m3m4_v2.c | 96 ++++++++++++------- .../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 57c4c8a5d1a2b..9bf0506f98b6a 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 8939e4a6ce93c..53ebc1b156739 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 d47e5c3e774e0..4f33ff372e6a5 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 From 40379a6bb73da808af9405fcfc415caaaedc6a73 Mon Sep 17 00:00:00 2001 From: "Daniel P. Carvalho" Date: Tue, 15 Sep 2026 05:59:57 -0300 Subject: [PATCH 3/4] boards: stm32g4: regenerate nucleo-g431kb comp defconfig CONFIG_EXAMPLES_COMP_DACPATH matched its Kconfig default value, which make savedefconfig drops as redundant. The stale explicit line made the committed defconfig differ from what a clean savedefconfig produces, failing CI's defconfig-completeness check even though the board builds fine either way. Assisted-by: Claude:claude-sonnet-5 Signed-off-by: Daniel P. Carvalho --- boards/arm/stm32g4/nucleo-g431kb/configs/comp/defconfig | 1 - 1 file changed, 1 deletion(-) diff --git a/boards/arm/stm32g4/nucleo-g431kb/configs/comp/defconfig b/boards/arm/stm32g4/nucleo-g431kb/configs/comp/defconfig index 4f33ff372e6a5..b93181d5c22f4 100644 --- a/boards/arm/stm32g4/nucleo-g431kb/configs/comp/defconfig +++ b/boards/arm/stm32g4/nucleo-g431kb/configs/comp/defconfig @@ -20,7 +20,6 @@ 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 From 3119ecf8b63ddbf8aa952886520252ae747753a8 Mon Sep 17 00:00:00 2001 From: "Daniel P. Carvalho" Date: Fri, 18 Sep 2026 11:27:04 -0300 Subject: [PATCH 4/4] stm32/comp: propagate enable error and handle lock in ioctl In comp_ioctl(), propagate the return code of comp_enable() to caller so failures (such as when the comparator CSR register is locked) return -EPERM. Also call comp_lock_set() if the comparator was configured with locking, handling cases where initialization was delayed. Assisted-by: Gemini:gemini-3.8-pro Signed-off-by: Daniel P. Carvalho --- arch/arm/src/common/stm32/stm32_comp_m3m4_v2.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) 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 53ebc1b156739..78421e6346e62 100644 --- a/arch/arm/src/common/stm32/stm32_comp_m3m4_v2.c +++ b/arch/arm/src/common/stm32/stm32_comp_m3m4_v2.c @@ -929,7 +929,12 @@ static int comp_ioctl(struct comp_dev_s *dev, int cmd, unsigned long arg) { /* Enable comparator */ - comp_enable(priv, true); + ret = comp_enable(priv, true); + if (ret == OK && priv->lock) + { + comp_lock_set(priv, true); + } + break; } @@ -937,7 +942,7 @@ static int comp_ioctl(struct comp_dev_s *dev, int cmd, unsigned long arg) { /* Disable comparator */ - comp_enable(priv, false); + ret = comp_enable(priv, false); break; }