Added old multiplexer driver

This commit is contained in:
Your Name
2026-09-24 17:00:45 +03:00
parent b8b346140e
commit abc042bf56
15 changed files with 377 additions and 12 deletions
@@ -0,0 +1,4 @@
target_sources(app PRIVATE ${CMAKE_CURRENT_LIST_DIR}/cd74hc4067.c)
target_include_directories(app PRIVATE ${CMAKE_CURRENT_LIST_DIR})
@@ -0,0 +1,23 @@
menuconfig CD74HC4067
bool "TI CD74HC4067 analog multiplexer"
depends on GPIO
help
Enable support for the TI CD74HC4067 16-channel analog
multiplexer controlled through GPIO selection pins.
if CD74HC4067
config CD74HC4067_INIT_PRIORITY
int "Initialization priority"
default 80
help
Device initialization priority for the CD74HC4067 driver.
# Creates "CONFIG_CD74HC4067_LOG_LEVEL"
module = CD74HC4067
module-str = cd74hc4067
source "subsys/logging/Kconfig.template.log_config"
endif # CD74HC4067
@@ -0,0 +1,191 @@
#define DT_DRV_COMPAT cd74hc4067
#include <errno.h>
#include <zephyr/logging/log.h>
#include "cd74hc4067.h"
LOG_MODULE_REGISTER(cd74hc4067);
//-------------------------------------------------------------------------
// Declarations
static int cd74hc4067_init(const struct device *dev);
static int cd74hc4067_enable_pin(const struct device *dev, bool state);
//-------------------------------------------------------------------------
// Private
static int cd74hc4067_init(const struct device *dev) {
int ret;
const struct cd74hc4067_cfg *cfg = dev->config;
struct cd74hc4067_data *data = dev->data;
k_mutex_init(&data->lock);
data->current_channel = 0U;
data->enabled = !cfg->has_enable;
// Init the select pins
for (int i = 0; i < CD74HC4067_SELECT_PIN_COUNT; i++) {
const struct gpio_dt_spec *pin = &(cfg->select[i]);
if (!gpio_is_ready_dt(pin)) {
LOG_ERR("Select pin %d not ready.\r\n", i);
return -ENODEV;
}
ret = gpio_pin_configure_dt(pin, GPIO_OUTPUT_INACTIVE);
if (ret < 0) {
LOG_ERR("Could not configure select pin %d as output\r\n", i);
return -ENODEV;
}
}
if (cfg->has_enable) {
// Init the enable pin
const struct gpio_dt_spec *enable = &cfg->enable;
LOG_DBG("Initializing enable pin.\r\n");
if (!gpio_is_ready_dt(enable)) {
LOG_ERR("Enable pin not ready.\r\n");
return -ENODEV;
}
ret = gpio_pin_configure_dt(enable, GPIO_OUTPUT_INACTIVE);
if (ret < 0) {
LOG_ERR("Could not configure ENABLE pin as output\r\n");
return -ENODEV;
}
}
return 0;
}
static int cd74hc4067_enable_pin(const struct device *dev, bool state) {
const struct cd74hc4067_cfg *cfg = dev->config;
struct cd74hc4067_data *data = dev->data;
int ret = 0;
if (!cfg->has_enable) {
if (!state) {
return -ENOTSUP;
}
data->enabled = true;
return 0;
}
if (!device_is_ready(cfg->enable.port)) {
LOG_ERR("%s enable GPIO not ready", dev->name);
return -ENODEV;
}
ret = gpio_pin_set_dt(&cfg->enable, state ? 0 : 1);
if (ret == 0) {
data->enabled = state;
}
return ret;
}
//-------------------------------------------------------------------------
// Public
int cd74hc4067_enable(const struct device *dev) {
struct cd74hc4067_data *data = dev->data;
int ret;
ret = k_mutex_lock(&data->lock, K_FOREVER);
if (ret != 0) {
return ret;
}
ret = cd74hc4067_enable_pin(dev, true);
k_mutex_unlock(&data->lock);
return ret;
}
int cd74hc4067_disable(const struct device *dev) {
struct cd74hc4067_data *data = dev->data;
int ret;
ret = k_mutex_lock(&data->lock, K_FOREVER);
if (ret != 0) {
return ret;
}
ret = cd74hc4067_enable_pin(dev, false);
k_mutex_unlock(&data->lock);
return ret;
}
int cd74hc4067_select_channel(const struct device *dev, uint8_t channel) {
if (channel >= CD74HC4067_SELECT_CHANNEL_COUNT) {
LOG_ERR("Channel not found.\r\n");
return -ENODEV;
}
int ret;
const struct cd74hc4067_cfg *cfg = dev->config;
struct cd74hc4067_data *data = dev->data;
ret = k_mutex_lock(&data->lock, K_FOREVER);
if (ret < 0) {
LOG_ERR("Mutex error");
return ret;
}
for (int i = 0; i < CD74HC4067_SELECT_PIN_COUNT; i++) {
bool state = (channel >> i) & 0x1;
ret = gpio_pin_set_dt(&cfg->select[i], state);
if (ret < 0) {
LOG_ERR("Unable to set channel pin: %d", ret);
goto out;
}
}
data->current_channel = channel;
out:
k_mutex_unlock(&data->lock);
return ret;
}
//-------------------------------------------------------------------------
// Devicetree
// static const struct cd74hc4067_api cd74hc4067_api_funcs = {
// .enable = cd74hc4067_enable,
// .disable = cd74hc4067_disable,
// .select_channel = cd74hc4067_select_channel,
// };
#define CD74HC4067_DEFINE(inst) \
\
static const struct cd74hc4067_cfg cd74hc4067_cfg_##inst = { \
.select = { \
GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst), select_gpios, 0), \
GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst), select_gpios, 1), \
GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst), select_gpios, 2), \
GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst), select_gpios, 3), \
}, \
.enable = COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, enable_gpio), \
(GPIO_DT_SPEC_GET(DT_DRV_INST(inst), enable_gpio)), \
((struct gpio_dt_spec){0})), \
.has_enable = DT_INST_NODE_HAS_PROP(inst, enable_gpio), \
}; \
static struct cd74hc4067_data cd74hc4067_data_##inst; \
DEVICE_DT_INST_DEFINE( inst, \
cd74hc4067_init, \
NULL, \
&cd74hc4067_data_##inst, \
&cd74hc4067_cfg_##inst, \
POST_KERNEL, \
CONFIG_CD74HC4067_INIT_PRIORITY, \
NULL);
DT_INST_FOREACH_STATUS_OKAY(CD74HC4067_DEFINE)
@@ -0,0 +1,45 @@
#ifndef SERVO2350_DRIVERS_MULTIPLEXER_CD74HC4067_H_
#define SERVO2350_DRIVERS_MULTIPLEXER_CD74HC4067_H_
#include <stdint.h>
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/drivers/gpio.h>
#ifdef __cplusplus
extern "C" {
#endif
// struct cd74hc4067_api {
// int (*enable)(const struct device *dev);
// int (*disable)(const struct device *dev);
// int (*select_channel)(const struct device *dev, uint8_t channel);
// };
struct cd74hc4067_data {
struct k_mutex lock;
uint8_t current_channel;
bool enabled;
};
#define CD74HC4067_SELECT_CHANNEL_COUNT 16U
#define CD74HC4067_SELECT_PIN_COUNT 4U
struct cd74hc4067_cfg {
struct gpio_dt_spec select[CD74HC4067_SELECT_PIN_COUNT];
struct gpio_dt_spec enable;
bool has_enable;
};
int cd74hc4067_enable(const struct device *dev);
int cd74hc4067_disable(const struct device *dev);
int cd74hc4067_select_channel(const struct device *dev, uint8_t channel);
#ifdef __cplusplus
}
#endif
#endif /* SERVO2350_DRIVERS_MULTIPLEXER_CD74HC4067_H_ */