Skip to content

Commit

Permalink
drivers: comparator: stm32: Add support for STM32(G4) comparator
Browse files Browse the repository at this point in the history
Add the Devicetree binding

Move the data struct to source file, remove extraneous comments

Add all config options into DT binding

Make comment more generic

drivers: comparator: stm32: Add support for STM32(G4) comparator

Add support for built-in comparator on the STM32G4x series

Signed-off-by: Adam Mitchell <[email protected]>

Addressing linter comments

Address more lint issues

Update license to Apache-2.0

Blank lines after declarations
  • Loading branch information
bp-amitchone committed Oct 27, 2023
1 parent f7f800c commit b855f6c
Show file tree
Hide file tree
Showing 8 changed files with 405 additions and 0 deletions.
1 change: 1 addition & 0 deletions drivers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ add_subdirectory_ifdef(CONFIG_BT_DRIVERS bluetooth)
add_subdirectory_ifdef(CONFIG_CACHE_MANAGEMENT cache)
add_subdirectory_ifdef(CONFIG_CAN can)
add_subdirectory_ifdef(CONFIG_CLOCK_CONTROL clock_control)
add_subdirectory_ifdef(CONFIG_COMPARATOR comparator)
add_subdirectory_ifdef(CONFIG_CHARGER charger)
add_subdirectory_ifdef(CONFIG_CONSOLE console)
add_subdirectory_ifdef(CONFIG_COREDUMP_DEVICE coredump)
Expand Down
1 change: 1 addition & 0 deletions drivers/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ source "drivers/bluetooth/Kconfig"
source "drivers/cache/Kconfig"
source "drivers/can/Kconfig"
source "drivers/charger/Kconfig"
source "drivers/comparator/Kconfig"
source "drivers/clock_control/Kconfig"
source "drivers/console/Kconfig"
source "drivers/coredump/Kconfig"
Expand Down
7 changes: 7 additions & 0 deletions drivers/comparator/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# SPDX-License-Identifier: Apache-2.0

zephyr_syscall_header(${ZEPHYR_BASE}/include/zephyr/drivers/comparator.h)

zephyr_library()

zephyr_library_sources_ifdef(CONFIG_COMP_STM32 comp_stm32.c)
44 changes: 44 additions & 0 deletions drivers/comparator/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Comparator configuration options

# Copyright (c) 2023 Adam Mitchell, Brill Power Ltd
# SPDX-License-Identifier: Apache-2.0

#
# Comparator options
#
menuconfig COMPARATOR
bool "Comparator drivers"
help
Enable comparator driver configuration

if COMPARATOR

module = COMPARATOR
module-str = comparator
source "subsys/logging/Kconfig.template.log_config"

config COMP_INIT_PRIORITY
int "Comparator init priority"
default KERNEL_INIT_PRIORITY_DEVICE
depends on COMPARATOR
help
Comparator driver device initialization priority.

config COMP_ENABLE_AT_INIT
bool "Automatically enable comparator during initialisation function"
default y
depends on COMPARATOR
help
Enable comparators at the end of the initialisation
function rather than via explicit call in application code

config COMP_LOCK
bool "Lock comparator during initialisation function"
default n
depends on COMPARATOR
help
Prevent modification of comparator configuration after initialisation

source "drivers/dac/Kconfig.stm32"

endif # COMPARATOR
12 changes: 12 additions & 0 deletions drivers/comparator/Kconfig.stm32
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Comparator configuration options

# Copyright (c) 2023 Adam Mitchell, Brill Power Ltd
# SPDX-License-Identifier: Apache-2.0

config COMP_STM32
bool "STM32 Comparator driver"
default n
depends on DT_HAS_ST_STM32_COMP_ENABLED
select USE_STM32_LL_COMP
help
Enable the driver implementation for STM32 microcontrollers
144 changes: 144 additions & 0 deletions drivers/comparator/comp_stm32.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* Copyright (c) 2023 Adam Mitchell, Brill Power Ltd
*
* SPDX-License-Identifier: Apache-2.0
*/

#define DT_DRV_COMPAT st_stm32_comp

#include <errno.h>
#include <soc.h>

#include <zephyr/device.h>
#include <zephyr/drivers/clock_control/stm32_clock_control.h>
#include <zephyr/drivers/comparator.h>
#include <zephyr/drivers/pinctrl.h>
#include <zephyr/init.h>

#include <stm32_ll_bus.h>
#include <stm32_ll_comp.h>

#define LOG_LEVEL CONFIG_COMPARATOR_LOG_LEVEL
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(comp_stm32);

struct comp_cfg {
COMP_TypeDef *base;
struct stm32_pclken pclken;
const struct pinctrl_dev_config *pcfg;
};

struct comp_data
uint32_t input_plus;
uint32_t input_minus;
uint32_t input_hysteresis;
uint32_t output_polarity;
uint32_t output_blanking_source;
};

static void comp_stm32_enable(const struct device *dev)
{
const struct comp_cfg *cfg = dev->config;

LL_COMP_Enable(cfg->base);
}

static void comp_stm32_disable(const struct device *dev)
{
const struct comp_cfg *cfg = dev->config;

LL_COMP_Disable(cfg->base);
}

#ifdef CONFIG_COMP_LOCK
static void comp_stm32_lock(const struct device *dev)
{
const struct comp_cfg *cfg = dev->config;

LL_COMP_Lock(cfg->base);
}
#endif /* CONFIG_COMP_LOCK */

static int comp_stm32_init(const struct device *dev)
{
const struct comp_cfg *cfg = dev->config;
const struct comp_data *data = dev->data;
const struct device *const clk = DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE);

if (!device_is_ready(clk)) {
LOG_ERR("clock control device not ready");
return -ENODEV;
}

if (clock_control_on(clk, (clock_control_subsys_t *)&cfg->pclken) != 0) {
return -EIO;
}

int err = pinctrl_apply_state(cfg->pcfg, PINCTRL_STATE_DEFAULT);

if (err < 0) {
LOG_ERR("%s pinctrl setup failed (%d)", cfg->base, err);
return err;
}

LL_COMP_InitTypeDef Comp_InitStruct = { 0 };

Comp_InitStruct.InputPlus = data->input_plus;
Comp_InitStruct.InputMinus = data->input_minus;
Comp_InitStruct.InputHysteresis = data->input_hysteresis;
Comp_InitStruct.OutputPolarity = data->output_polarity;
Comp_InitStruct.OutputBlankingSource = data->output_blanking_source;

err = LL_COMP_Init(cfg->base, &Comp_InitStruct);
if (err != SUCCESS) {
LOG_ERR("%s initialisation failed!", cfg->base);
return -ENODEV;
}

#ifdef CONFIG_COMP_ENABLE_AT_INIT
LL_COMP_Enable(cfg->base);
#endif /* CONFIG_COMP_ENABLE_AT_INIT */

#ifdef CONFIG_COMP_LOCK
LL_COMP_Lock(cfg->base);
#endif /* CONFIG_COMP_LOCK */

return 0;
}

static const struct comp_driver_api comp_stm32_driver_api = {
.enable = comp_stm32_enable,
.disable = comp_stm32_disable,
#ifdef CONFIG_COMP_LOCK
.lock = comp_stm32_lock,
#endif /* CONFIG_COMP_LOCK */
};


#define STM32_COMP_INIT(index) \
\
PINCTRL_DT_INST_DEFINE(index); \
\
static const struct comp_cfg comp_stm32_cfg_##index = { \
.base = (COMP_TypeDef *)DT_INST_REG_ADDR(index), \
.pclken = { \
.enr = DT_INST_CLOCKS_CELL(index, bits), \
.bus = DT_INST_CLOCKS_CELL(index, bus), \
}, \
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(index), \
}; \
\
static struct comp_data comp_stm32_data_##index = { \
.input_plus = DT_INST_PROP_OR(index, input_plus, LL_COMP_INPUT_PLUS_IO1), \
.input_minus = DT_INST_PROP_OR(index, input_minus, LL_COMP_INPUT_MINUS_IO1), \
.input_hysteresis = DT_INST_PROP_OR(index, input_hysteresis, LL_COMP_HYSTERESIS_NONE), \

Check warning on line 134 in drivers/comparator/comp_stm32.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

LONG_LINE

drivers/comparator/comp_stm32.c:134 line length of 104 exceeds 100 columns
.output_polarity = DT_INST_PROP_OR(index, output_polarity, LL_COMP_OUTPUTPOL_NONINVERTED), \

Check warning on line 135 in drivers/comparator/comp_stm32.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

LONG_LINE

drivers/comparator/comp_stm32.c:135 line length of 108 exceeds 100 columns
.output_blanking_source = \
DT_INST_PROP_OR(index, output_blanking_source, LL_COMP_BLANKINGSRC_NONE) \
}; \
\
DEVICE_DT_INST_DEFINE(index, &comp_stm32_init, NULL, &comp_stm32_data_##index, \
&comp_stm32_cfg_##index, POST_KERNEL, CONFIG_COMP_INIT_PRIORITY, \

Check warning on line 141 in drivers/comparator/comp_stm32.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

LONG_LINE

drivers/comparator/comp_stm32.c:141 line length of 116 exceeds 100 columns
&comp_stm32_driver_api);

DT_INST_FOREACH_STATUS_OKAY(STM32_COMP_INIT)
107 changes: 107 additions & 0 deletions dts/bindings/comparator/st,stm32-comp.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Copyright (c) 2023 Adam Mitchell, Brill Power Ltd
# SPDX-License-Identifier: Apache-2.0

description: ST STM32 family comparator

compatible: "st,stm32-comp"

include: [base.yaml, pinctrl-device.yaml]

properties:
reg:
required: true

pinctrl-0:
required: true

pinctrl-names:
required: true

input-plus:
required: true
type: int
enum:
- 0 # LL_COMP_INPUT_PLUS_IO1
- 256 # LL_COMP_INPUT_PLUS_IO2
description: |
Specify non-inverting input source
input-minus:
required: true
type: int
enum:
# LL_COMP_INPUT_MINUS_DAC3_CH1 / LL_COMP_INPUT_MINUS_DAC3_CH2
# LL_COMP_INPUT_MINUS_DAC4_CH1 / LL_COMP_INPUT_MINUS_DAC4_CH2
- 64

# LL_COMP_INPUT_MINUS_DAC1_CH1 / LL_COMP_INPUT_MINUS_DAC1_CH2
# LL_COMP_INPUT_MINUS_DAC2_CH1
- 80

- 96 # LL_COMP_INPUT_MINUS_IO1
- 112 # LL_COMP_INPUT_MINUS_IO2
- 8388656 # LL_COMP_INPUT_MINUS_VREFINT
- 12582912 # LL_COMP_INPUT_MINUS_1_4VREFINT
- 12582928 # LL_COMP_INPUT_MINUS_1_2VREFINT
- 12582944 # LL_COMP_INPUT_MINUS_3_4VREFINT
description: |
Specify inverting input source
input-hysteresis:
required: true
type: int
enum:
- 0 # LL_COMP_HYSTERESIS_NONE
- 65536 # LL_COMP_HYSTERESIS_10MV / LL_COMP_HYSTERESIS_LOW
- 131072 # LL_COMP_HYSTERESIS_20MV
- 196608 # LL_COMP_HYSTERESIS_30MV
- 262144 # LL_COMP_HYSTERESIS_40MV / LL_COMP_HYSTERESIS_MEDIUM
- 327680 # LL_COMP_HYSTERESIS_50MV
- 393216 # LL_COMP_HYSTERESIS_60MV
- 458752 # LL_COMP_HYSTERESIS_70MV / LL_COMP_HYSTERESIS_HIGH
description: |
Specify hysteresis mode for inverting input
output-polarity:
required: true
type: int
enum:
- 0 # LL_COMP_OUTPUTPOL_NONINVERTED
- 32768 # LL_COMP_OUTPUTPOL_INVERTED
description: |
Specify output polarity
output-blanking-source:
required: true
type: int
enum:
- 0 # LL_COMP_BLANKINGSRC_NONE

# LL_COMP_BLANKINGSRC_TIM1_OC5_COMP1 / LL_COMP_BLANKINGSRC_TIM1_OC5_COMP2
# LL_COMP_BLANKINGSRC_TIM1_OC5_COMP3 / LL_COMP_BLANKINGSRC_TIM1_OC5_COMP7
# LL_COMP_BLANKINGSRC_TIM2_OC3_COMP5 / LL_COMP_BLANKINGSRC_TIM3_OC4_COMP4
# LL_COMP_BLANKINGSRC_TIM8_OC5_COMP6
- 524288

# LL_COMP_BLANKINGSRC_TIM2_OC3_COMP1 / LL_COMP_BLANKINGSRC_TIM2_OC3_COMP2
# LL_COMP_BLANKINGSRC_TIM2_OC4_COMP6 / LL_COMP_BLANKINGSRC_TIM3_OC3_COMP3
# LL_COMP_BLANKINGSRC_TIM8_OC5_COMP4 / LL_COMP_BLANKINGSRC_TIM8_OC5_COMP5
# LL_COMP_BLANKINGSRC_TIM8_OC5_COMP7
- 1048576
# LL_COMP_BLANKINGSRC_TIM2_OC4_COMP3 / LL_COMP_BLANKINGSRC_TIM3_OC3_COMP1
# LL_COMP_BLANKINGSRC_TIM3_OC3_COMP2 / LL_COMP_BLANKINGSRC_TIM3_OC3_COMP5
# LL_COMP_BLANKINGSRC_TIM3_OC3_COMP7 / LL_COMP_BLANKINGSRC_TIM15_OC1_COMP4
# LL_COMP_BLANKINGSRC_TIM15_OC2_COMP6
- 1572864

# LL_COMP_BLANKINGSRC_TIM1_OC5_COMP4 / LL_COMP_BLANKINGSRC_TIM1_OC5_COMP5
# LL_COMP_BLANKINGSRC_TIM1_OC5_COMP6 / LL_COMP_BLANKINGSRC_TIM8_OC5_COMP1
# LL_COMP_BLANKINGSRC_TIM8_OC5_COMP2 / LL_COMP_BLANKINGSRC_TIM8_OC5_COMP3
# LL_COMP_BLANKINGSRC_TIM15_OC2_COMP7
- 2097152

- 2621440 # LL_COMP_BLANKINGSRC_TIM20_OC5
- 3145728 # LL_COMP_BLANKINGSRC_TIM15_OC1
- 3670016 # LL_COMP_BLANKINGSRC_TIM4_OC3
description: |
Specify output blanking source
Loading

0 comments on commit b855f6c

Please sign in to comment.