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..78421e6346e62 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,41 @@ 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 */ + + ret = comp_enable(priv, true); + if (ret == OK && priv->lock) + { + comp_lock_set(priv, true); + } + + break; + } + + case ANIOC_COMP_DISABLE: + { + /* Disable comparator */ + + ret = comp_enable(priv, false); + break; + } + + default: + { + aerr("ERROR: Unknown cmd: %d\n", cmd); + ret = -ENOTTY; + break; + } + } + + return ret; } #endif @@ -954,57 +989,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..b93181d5c22f4 100644 --- a/boards/arm/stm32g4/nucleo-g431kb/configs/comp/defconfig +++ b/boards/arm/stm32g4/nucleo-g431kb/configs/comp/defconfig @@ -19,6 +19,7 @@ CONFIG_BUILTIN=y CONFIG_COMP=y CONFIG_DAC=y CONFIG_DEFAULT_SMALL=y +CONFIG_EXAMPLES_COMP=y CONFIG_EXAMPLES_DAC=y CONFIG_EXAMPLES_DAC_DEVPATH="/dev/dac5" CONFIG_FILE_STREAM=y @@ -37,6 +38,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 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()