diff --git a/CMakeLists.txt b/CMakeLists.txt index d6b365c..0ab661f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,3 +2,8 @@ # SPDX-License-Identifier: Apache-2.0 zephyr_include_directories(include) + +# scan headers for __subsystem driver API structs (required for DEVICE_API) +zephyr_syscall_include_directories(include) + +add_subdirectory(drivers) diff --git a/Kconfig b/Kconfig index e69de29..1ff0c88 100644 --- a/Kconfig +++ b/Kconfig @@ -0,0 +1,4 @@ +# Copyright (c) The Libre Solar Project Contributors +# SPDX-License-Identifier: Apache-2.0 + +rsource "drivers/Kconfig" diff --git a/drivers/CMakeLists.txt b/drivers/CMakeLists.txt new file mode 100644 index 0000000..c7644ee --- /dev/null +++ b/drivers/CMakeLists.txt @@ -0,0 +1,4 @@ +# Copyright (c) The Libre Solar Project Contributors +# SPDX-License-Identifier: Apache-2.0 + +add_subdirectory_ifdef(CONFIG_HALF_BRIDGE half_bridge) diff --git a/drivers/Kconfig b/drivers/Kconfig new file mode 100644 index 0000000..cc3d638 --- /dev/null +++ b/drivers/Kconfig @@ -0,0 +1,8 @@ +# Copyright (c) The Libre Solar Project Contributors +# SPDX-License-Identifier: Apache-2.0 + +menu "Libre Solar drivers" + +rsource "half_bridge/Kconfig" + +endmenu diff --git a/drivers/half_bridge/CMakeLists.txt b/drivers/half_bridge/CMakeLists.txt new file mode 100644 index 0000000..91065c4 --- /dev/null +++ b/drivers/half_bridge/CMakeLists.txt @@ -0,0 +1,4 @@ +# Copyright (c) The Libre Solar Project Contributors +# SPDX-License-Identifier: Apache-2.0 + +zephyr_library() diff --git a/drivers/half_bridge/Kconfig b/drivers/half_bridge/Kconfig new file mode 100644 index 0000000..37095d6 --- /dev/null +++ b/drivers/half_bridge/Kconfig @@ -0,0 +1,22 @@ +# Copyright (c) The Libre Solar Project Contributors +# SPDX-License-Identifier: Apache-2.0 + +menuconfig HALF_BRIDGE + bool "Half-bridge drivers" + help + Enable drivers for half bridges consisting of two complementary PWM outputs with dead time, + e.g. used in DC/DC converters. + +if HALF_BRIDGE + +config HALF_BRIDGE_INIT_PRIORITY + int "Half-bridge init priority" + default KERNEL_INIT_PRIORITY_DEVICE + help + Half-bridge driver device initialization priority. + +module = HALF_BRIDGE +module-str = half_bridge +source "subsys/logging/Kconfig.template.log_config" + +endif # HALF_BRIDGE diff --git a/dts/bindings/half-bridge/half-bridge-common.yaml b/dts/bindings/half-bridge/half-bridge-common.yaml new file mode 100644 index 0000000..feb5aff --- /dev/null +++ b/dts/bindings/half-bridge/half-bridge-common.yaml @@ -0,0 +1,16 @@ +# Copyright (c) The Libre Solar Project Contributors +# SPDX-License-Identifier: Apache-2.0 + +description: Common properties for half bridges consisting of two complementary PWM outputs + +properties: + frequency: + type: int + required: true + description: Switching frequency in Hz + + deadtime-ns: + type: int + required: true + description: | + Dead time in nanoseconds between switching off one MOSFET and switching on the other one diff --git a/dts/bindings/vendor-prefixes.txt b/dts/bindings/vendor-prefixes.txt new file mode 100644 index 0000000..227dccc --- /dev/null +++ b/dts/bindings/vendor-prefixes.txt @@ -0,0 +1,6 @@ +# Copyright (c) The Libre Solar Project Contributors +# SPDX-License-Identifier: Apache-2.0 +# +# Additional devicetree vendor prefixes used in this module + +libresolar Libre Solar Project diff --git a/include/libresolar/drivers/half_bridge.h b/include/libresolar/drivers/half_bridge.h new file mode 100644 index 0000000..d313324 --- /dev/null +++ b/include/libresolar/drivers/half_bridge.h @@ -0,0 +1,213 @@ +/* + * Copyright (c) The Libre Solar Project Contributors + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef LIBRESOLAR_DRIVERS_HALF_BRIDGE_H_ +#define LIBRESOLAR_DRIVERS_HALF_BRIDGE_H_ + +/** + * @file + * @brief Half-bridge driver API + * + * A half bridge consists of a high-side and a low-side switch driven by two complementary PWM + * signals with a dead time in between, e.g. as used in synchronous buck or boost converters. + * + * The PWM duty cycle refers to the high-side switch. It is set via a compare value between 0 and + * the period value of the underlying timer, so that the duty cycle can be changed with the + * minimum possible step size. Drivers clamp the compare value to configurable limits to protect + * the hardware from invalid settings. + */ + +#include + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @defgroup half_bridge_interface Half-bridge + * @{ + */ + +/** @cond INTERNAL_HIDDEN */ + +__subsystem struct half_bridge_driver_api +{ + int (*start)(const struct device *dev); + int (*stop)(const struct device *dev); + bool (*is_enabled)(const struct device *dev); + int (*set_compare)(const struct device *dev, uint32_t compare); + uint32_t (*get_compare)(const struct device *dev); + uint32_t (*get_period)(const struct device *dev); + int (*set_compare_limits)(const struct device *dev, uint32_t min, uint32_t max); +}; + +/** @endcond */ + +/** + * Start the PWM generation + * + * A valid compare value has to be set before starting. Drivers may refuse to start if the + * compare value is zero. + * + * @param dev Half-bridge device + * + * @retval 0 on success + * @retval -EINVAL if the output could not be started with the current compare value + */ +static inline int half_bridge_start(const struct device *dev) +{ + return DEVICE_API_GET(half_bridge, dev)->start(dev); +} + +/** + * Stop the PWM generation, i.e. switch off both high-side and low-side switches + * + * @param dev Half-bridge device + * + * @retval 0 on success + */ +static inline int half_bridge_stop(const struct device *dev) +{ + return DEVICE_API_GET(half_bridge, dev)->stop(dev); +} + +/** + * Get status of the PWM output + * + * @param dev Half-bridge device + * + * @returns true if PWM output is enabled + */ +static inline bool half_bridge_is_enabled(const struct device *dev) +{ + return DEVICE_API_GET(half_bridge, dev)->is_enabled(dev); +} + +/** + * Set raw timer compare value + * + * This function allows to change the PWM with minimum step size. The value is clamped to the + * limits set with half_bridge_set_compare_limits(). + * + * @param dev Half-bridge device + * @param compare Timer compare value (between 0 and period) + * + * @retval 0 on success + */ +static inline int half_bridge_set_compare(const struct device *dev, uint32_t compare) +{ + return DEVICE_API_GET(half_bridge, dev)->set_compare(dev, compare); +} + +/** + * Get raw timer compare value + * + * @param dev Half-bridge device + * + * @returns Timer compare value (between 0 and period) + */ +static inline uint32_t half_bridge_get_compare(const struct device *dev) +{ + return DEVICE_API_GET(half_bridge, dev)->get_compare(dev); +} + +/** + * Get timer period value + * + * The duty cycle is calculated as compare / period. + * + * @param dev Half-bridge device + * + * @returns Timer period value + */ +static inline uint32_t half_bridge_get_period(const struct device *dev) +{ + return DEVICE_API_GET(half_bridge, dev)->get_period(dev); +} + +/** + * Set limits for the raw timer compare value + * + * The current compare value is clamped to the new limits. + * + * @param dev Half-bridge device + * @param min Minimum compare value (e.g. for limiting the input voltage) + * @param max Maximum compare value (e.g. for charge pump of high-side gate driver) + * + * @retval 0 on success + * @retval -EINVAL if min > max or max > period + */ +static inline int half_bridge_set_compare_limits(const struct device *dev, uint32_t min, + uint32_t max) +{ + return DEVICE_API_GET(half_bridge, dev)->set_compare_limits(dev, min, max); +} + +/** + * Set the duty cycle of the PWM signal + * + * The resulting compare value is clamped to the configured limits. + * + * @param dev Half-bridge device + * @param duty Duty cycle between 0.0 and 1.0 + * + * @retval 0 on success + * @retval -EINVAL if duty cycle is out of range (setting is ignored) + */ +static inline int half_bridge_set_duty_cycle(const struct device *dev, float duty) +{ + if (duty < 0.0F || duty > 1.0F) { + return -EINVAL; + } + + return half_bridge_set_compare(dev, half_bridge_get_period(dev) * duty); +} + +/** + * Read the currently set duty cycle + * + * @param dev Half-bridge device + * + * @returns Duty cycle between 0.0 and 1.0 + */ +static inline float half_bridge_get_duty_cycle(const struct device *dev) +{ + return (float)half_bridge_get_compare(dev) / half_bridge_get_period(dev); +} + +/** + * Set duty cycle limits + * + * @param dev Half-bridge device + * @param min_duty Minimum duty cycle (e.g. 0.5 for limiting input voltage) + * @param max_duty Maximum duty cycle (e.g. 0.97 for charge pump) + * + * @retval 0 on success + * @retval -EINVAL if limits are out of range + */ +static inline int half_bridge_set_duty_limits(const struct device *dev, float min_duty, + float max_duty) +{ + if (min_duty < 0.0F || max_duty > 1.0F || min_duty > max_duty) { + return -EINVAL; + } + + uint32_t period = half_bridge_get_period(dev); + + return half_bridge_set_compare_limits(dev, period * min_duty, period * max_duty); +} + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* LIBRESOLAR_DRIVERS_HALF_BRIDGE_H_ */