drivers: sensor: current_amp: add driver
Add current sense amplifier driver. Implements https://github.com/zephyrproject-rtos/zephyr/issues/60415 Co-authored-by: Marco Argiolas <marco.argiolas@ftpsolutions.com.au> Signed-off-by: Nick Ward <nix.ward@gmail.com>
This commit is contained in:
parent
1e06ba2328
commit
17f2046821
5 changed files with 135 additions and 0 deletions
|
@ -27,6 +27,7 @@ add_subdirectory_ifdef(CONFIG_BMM150 bmm150)
|
|||
add_subdirectory_ifdef(CONFIG_BMP388 bmp388)
|
||||
add_subdirectory_ifdef(CONFIG_BQ274XX bq274xx)
|
||||
add_subdirectory_ifdef(CONFIG_CCS811 ccs811)
|
||||
add_subdirectory_ifdef(CONFIG_CURRENT_AMP current_amp)
|
||||
add_subdirectory_ifdef(CONFIG_DHT dht)
|
||||
add_subdirectory_ifdef(CONFIG_DPS310 dps310)
|
||||
add_subdirectory_ifdef(CONFIG_DS18B20 ds18b20)
|
||||
|
|
|
@ -83,6 +83,7 @@ source "drivers/sensor/bmm150/Kconfig"
|
|||
source "drivers/sensor/bmp388/Kconfig"
|
||||
source "drivers/sensor/bq274xx/Kconfig"
|
||||
source "drivers/sensor/ccs811/Kconfig"
|
||||
source "drivers/sensor/current_amp/Kconfig"
|
||||
source "drivers/sensor/dht/Kconfig"
|
||||
source "drivers/sensor/dps310/Kconfig"
|
||||
source "drivers/sensor/ds18b20/Kconfig"
|
||||
|
|
5
drivers/sensor/current_amp/CMakeLists.txt
Normal file
5
drivers/sensor/current_amp/CMakeLists.txt
Normal file
|
@ -0,0 +1,5 @@
|
|||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
zephyr_library()
|
||||
|
||||
zephyr_library_sources(current_amp.c)
|
13
drivers/sensor/current_amp/Kconfig
Normal file
13
drivers/sensor/current_amp/Kconfig
Normal file
|
@ -0,0 +1,13 @@
|
|||
# Current sense amplifier driver
|
||||
#
|
||||
# Copyright (c) 2023 FTP Technologies
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
config CURRENT_AMP
|
||||
bool "Current sense amplifier driver"
|
||||
default y
|
||||
depends on DT_HAS_CURRENT_SENSE_AMPLIFIER_ENABLED
|
||||
depends on ADC
|
||||
help
|
||||
Enable current sense amplifier driver.
|
115
drivers/sensor/current_amp/current_amp.c
Normal file
115
drivers/sensor/current_amp/current_amp.c
Normal file
|
@ -0,0 +1,115 @@
|
|||
/*
|
||||
* Copyright (c) 2023 FTP Technologies
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#define DT_DRV_COMPAT current_sense_amplifier
|
||||
|
||||
#include <zephyr/drivers/adc.h>
|
||||
#include <zephyr/drivers/adc/current_sense_amplifier.h>
|
||||
#include <zephyr/drivers/sensor.h>
|
||||
|
||||
#include <zephyr/logging/log.h>
|
||||
LOG_MODULE_REGISTER(current_amp, CONFIG_SENSOR_LOG_LEVEL);
|
||||
|
||||
struct current_sense_amplifier_data {
|
||||
struct adc_sequence sequence;
|
||||
int16_t raw;
|
||||
};
|
||||
|
||||
static int fetch(const struct device *dev, enum sensor_channel chan)
|
||||
{
|
||||
const struct current_sense_amplifier_dt_spec *config = dev->config;
|
||||
struct current_sense_amplifier_data *data = dev->data;
|
||||
int ret;
|
||||
|
||||
if ((chan != SENSOR_CHAN_CURRENT) && (chan != SENSOR_CHAN_ALL)) {
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
ret = adc_read(config->port.dev, &data->sequence);
|
||||
if (ret != 0) {
|
||||
LOG_ERR("adc_read: %d", ret);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int get(const struct device *dev, enum sensor_channel chan, struct sensor_value *val)
|
||||
{
|
||||
const struct current_sense_amplifier_dt_spec *config = dev->config;
|
||||
struct current_sense_amplifier_data *data = dev->data;
|
||||
int32_t raw_val = data->raw;
|
||||
int32_t i_ma;
|
||||
int ret;
|
||||
|
||||
__ASSERT_NO_MSG(val != NULL);
|
||||
|
||||
if (chan != SENSOR_CHAN_CURRENT) {
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
ret = adc_raw_to_millivolts_dt(&config->port, &raw_val);
|
||||
if (ret != 0) {
|
||||
LOG_ERR("raw_to_mv: %d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
i_ma = raw_val;
|
||||
current_sense_amplifier_scale_dt(config, &i_ma);
|
||||
|
||||
LOG_DBG("%d/%d, %dmV, current:%duA", data->raw,
|
||||
(1 << data->sequence.resolution) - 1, raw_val, i_ma);
|
||||
|
||||
val->val1 = i_ma / 1000;
|
||||
val->val2 = i_ma % 1000;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct sensor_driver_api current_api = {
|
||||
.sample_fetch = fetch,
|
||||
.channel_get = get,
|
||||
};
|
||||
|
||||
static int current_init(const struct device *dev)
|
||||
{
|
||||
const struct current_sense_amplifier_dt_spec *config = dev->config;
|
||||
struct current_sense_amplifier_data *data = dev->data;
|
||||
int ret;
|
||||
|
||||
if (!adc_is_ready_dt(&config->port)) {
|
||||
LOG_ERR("ADC is not ready");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
ret = adc_channel_setup_dt(&config->port);
|
||||
if (ret != 0) {
|
||||
LOG_ERR("setup: %d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = adc_sequence_init_dt(&config->port, &data->sequence);
|
||||
if (ret != 0) {
|
||||
LOG_ERR("sequence init: %d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
data->sequence.buffer = &data->raw;
|
||||
data->sequence.buffer_size = sizeof(data->raw);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define CURRENT_SENSE_AMPLIFIER_INIT(inst) \
|
||||
static struct current_sense_amplifier_data current_amp_##inst##_data; \
|
||||
\
|
||||
static const struct current_sense_amplifier_dt_spec current_amp_##inst##_config = \
|
||||
CURRENT_SENSE_AMPLIFIER_DT_SPEC_GET(DT_DRV_INST(inst)); \
|
||||
\
|
||||
SENSOR_DEVICE_DT_INST_DEFINE(inst, ¤t_init, NULL, ¤t_amp_##inst##_data, \
|
||||
¤t_amp_##inst##_config, POST_KERNEL, \
|
||||
CONFIG_SENSOR_INIT_PRIORITY, ¤t_api);
|
||||
|
||||
DT_INST_FOREACH_STATUS_OKAY(CURRENT_SENSE_AMPLIFIER_INIT)
|
Loading…
Add table
Add a link
Reference in a new issue