adc: Add TI ADS7052 SPI driver

Add driver for TI ADS7052.

Signed-off-by: Al Semjonovs <asemjonovs@google.com>
This commit is contained in:
Al Semjonovs 2023-02-13 11:09:48 -07:00 committed by Maureen Helm
parent 336803e96f
commit 9fa35bc9a0
9 changed files with 411 additions and 3 deletions

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/dt-bindings/adc/adc.h>
#include "tdk_robokit1-pinctrl.dtsi"
/ {
@ -90,7 +91,8 @@
pinctrl-names = "default";
dmas = <&xdmac 0 DMA_PERID_SPI0_TX>, <&xdmac 1 DMA_PERID_SPI0_RX>;
dma-names = "tx", "rx";
cs-gpios =<&pioa 31 GPIO_ACTIVE_LOW>;
cs-gpios =<&pioa 31 GPIO_ACTIVE_LOW>,
<&pioc 31 GPIO_ACTIVE_LOW>;
status = "okay";
icm42688: icm42688p@0 {
@ -103,6 +105,22 @@
gyro-hz = <32000>;
gyro-fs = <2000>;
};
spi_adc: adc@1 {
compatible = "ti,ads7052";
reg = <1>;
#io-channel-cells = <1>;
#address-cells = <1>;
#size-cells = <0>;
spi-max-frequency = <12000000>;
channel@0 {
reg = <0>;
zephyr,gain = "ADC_GAIN_1";
zephyr,reference = "ADC_REF_VDD_1";
zephyr,vref-mv = <3300>;
zephyr,acquisition-time = <ADC_ACQ_TIME_DEFAULT>;
zephyr,resolution = <14>;
};
};
};
&spi1 {

View file

@ -28,6 +28,7 @@ zephyr_library_sources_ifdef(CONFIG_ADC_TEST adc_test.c)
zephyr_library_sources_ifdef(CONFIG_ADC_ADS1X1X adc_ads1x1x.c)
zephyr_library_sources_ifdef(CONFIG_ADC_GD32 adc_gd32.c)
zephyr_library_sources_ifdef(CONFIG_ADC_ADS1119 adc_ads1119.c)
zephyr_library_sources_ifdef(CONFIG_ADC_ADS7052 adc_ads7052.c)
zephyr_library_sources_ifdef(CONFIG_ADC_RPI_PICO adc_rpi_pico.c)
zephyr_library_sources_ifdef(CONFIG_ADC_XMC4XXX adc_xmc4xxx.c)
zephyr_library_sources_ifdef(CONFIG_ADC_ESP32 adc_esp32.c)

View file

@ -84,6 +84,8 @@ source "drivers/adc/Kconfig.gd32"
source "drivers/adc/Kconfig.ads1119"
source "drivers/adc/Kconfig.ads7052"
source "drivers/adc/Kconfig.rpi_pico"
source "drivers/adc/Kconfig.xmc4xxx"

View file

@ -0,0 +1,36 @@
# Copyright (c) 2023 Google LLC
#
# SPDX-License-Identifier: Apache-2.0
config ADC_ADS7052
bool "Texas instruments ADS7052 SPI"
default y
depends on DT_HAS_TI_ADS7052_ENABLED
select SPI
select ADC_CONFIGURABLE_INPUTS
help
Enable the driver implementation for the ADS7052
if ADC_ADS7052
config ADC_ADS7052_INIT_PRIORITY
int "ADS7052 init priority"
default 80
help
ADS7052 device initialization priority must come
after SPI initialization
config ADC_ADS7052_ACQUISITION_THREAD_STACK_SIZE
int "Stack size for the ADC data acquisition thread"
default 512
help
Size of the stack used for the internal data acquisition
thread.
config ADC_ADS7052_ACQUISITION_THREAD_PRIO
int "Priority for the ADC data acquisition thread"
default 0
help
Priority level for the internal ADC data acquisition thread.
endif # ADC_ADS7052

309
drivers/adc/adc_ads7052.c Normal file
View file

@ -0,0 +1,309 @@
/* TI ADS7052 ADC
*
* Copyright (c) 2023 Google, LLC
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT ti_ads7052
#include <zephyr/device.h>
#include <zephyr/drivers/adc.h>
#include <zephyr/drivers/spi.h>
#include <zephyr/logging/log.h>
#include <zephyr/kernel.h>
LOG_MODULE_REGISTER(adc_ads7052);
#define ADC_CONTEXT_USES_KERNEL_TIMER
#include "adc_context.h"
#define ADS7052_RESOLUTION 14U
struct ads7052_config {
struct spi_dt_spec bus;
uint8_t channels;
};
struct ads7052_data {
struct adc_context ctx;
const struct device *dev;
uint16_t *buffer;
uint16_t *repeat_buffer;
uint8_t channels;
struct k_thread thread;
struct k_sem sem;
K_KERNEL_STACK_MEMBER(stack, CONFIG_ADC_ADS7052_ACQUISITION_THREAD_STACK_SIZE);
};
static int adc_ads7052_channel_setup(const struct device *dev,
const struct adc_channel_cfg *channel_cfg)
{
const struct ads7052_config *config = dev->config;
if (channel_cfg->gain != ADC_GAIN_1) {
LOG_ERR("unsupported channel gain '%d'", channel_cfg->gain);
return -ENOTSUP;
}
if (channel_cfg->reference != ADC_REF_VDD_1) {
LOG_ERR("unsupported channel reference '%d'", channel_cfg->reference);
return -ENOTSUP;
}
if (channel_cfg->acquisition_time != ADC_ACQ_TIME_DEFAULT) {
LOG_ERR("unsupported acquisition_time '%d'", channel_cfg->acquisition_time);
return -ENOTSUP;
}
if (channel_cfg->channel_id >= config->channels) {
LOG_ERR("unsupported channel id '%d'", channel_cfg->channel_id);
return -ENOTSUP;
}
return 0;
}
static int ads7052_validate_buffer_size(const struct device *dev,
const struct adc_sequence *sequence)
{
uint8_t channels = 0;
size_t needed;
channels = POPCOUNT(sequence->channels);
needed = channels * sizeof(uint16_t);
if (sequence->options) {
needed *= (1 + sequence->options->extra_samplings);
}
if (sequence->buffer_size < needed) {
return -ENOMEM;
}
return 0;
}
/**
* @brief Send ADS7052 Offset calibration request
*
* On power-up, the host must provide 24 SCLKs in the first serial transfer to enter the OFFCAL
* state. During normal operation, the host must provide 64 SCLKs in the serial transfer frame to
* enter the OFFCAL state.
*/
static int ads7052_send_calibration(const struct device *dev, bool power_up)
{
const struct ads7052_config *config = dev->config;
int err;
uint8_t sclks_needed = power_up ? 24 : 64;
uint8_t num_bytes = sclks_needed / 8;
uint8_t tx_bytes[8] = {0};
const struct spi_buf tx_buf = {.buf = tx_bytes, .len = num_bytes};
const struct spi_buf_set tx = {.buffers = &tx_buf, .count = 1};
err = spi_write_dt(&config->bus, &tx);
return err;
}
static int ads7052_start_read(const struct device *dev, const struct adc_sequence *sequence)
{
const struct ads7052_config *config = dev->config;
struct ads7052_data *data = dev->data;
int err;
if (sequence->resolution != ADS7052_RESOLUTION) {
LOG_ERR("unsupported resolution %d", sequence->resolution);
return -ENOTSUP;
}
if (find_msb_set(sequence->channels) > config->channels) {
LOG_ERR("unsupported channels in mask: 0x%08x", sequence->channels);
return -ENOTSUP;
}
if (sequence->calibrate) {
ads7052_send_calibration(dev, false);
}
err = ads7052_validate_buffer_size(dev, sequence);
if (err) {
LOG_ERR("buffer size too small");
return err;
}
data->buffer = sequence->buffer;
adc_context_start_read(&data->ctx, sequence);
return adc_context_wait_for_completion(&data->ctx);
}
static int adc_ads7052_read_async(const struct device *dev, const struct adc_sequence *sequence,
struct k_poll_signal *async)
{
struct ads7052_data *data = dev->data;
int error;
adc_context_lock(&data->ctx, async ? true : false, async);
error = ads7052_start_read(dev, sequence);
adc_context_release(&data->ctx, error);
return error;
}
static int adc_ads7052_read(const struct device *dev, const struct adc_sequence *sequence)
{
return adc_ads7052_read_async(dev, sequence, NULL);
}
static void adc_context_start_sampling(struct adc_context *ctx)
{
struct ads7052_data *data = CONTAINER_OF(ctx, struct ads7052_data, ctx);
data->channels = ctx->sequence.channels;
data->repeat_buffer = data->buffer;
k_sem_give(&data->sem);
}
static void adc_context_update_buffer_pointer(struct adc_context *ctx, bool repeat_sampling)
{
struct ads7052_data *data = CONTAINER_OF(ctx, struct ads7052_data, ctx);
if (repeat_sampling) {
data->buffer = data->repeat_buffer;
}
}
/**
* @brief Get a 14-bit integer from raw ADC data.
*
* @param src Location of the big-endian 14-bit integer to get.
*
* @return 14-bit integer in host endianness.
*/
static inline int ads7052_get_be14(const uint8_t src[2])
{
return ((src[0] & 0x7F) << 7) | (src[1] >> 1);
}
/**
* @brief Read ADS7052 over SPI interface
*
* A leading 0 is output on the SDO pin on the CS falling edge.
* The most significant bit (MSB) of the output data is launched on the SDO pin on the rising edge
* after the first SCLK falling edge. Subsequent output bits are launched on the subsequent rising
* edges provided on SCLK. When all 14 output bits are shifted out, the device outputs 0's on the
* subsequent SCLK rising edges. The device enters the ACQ state after 18 clocks and a minimum time
* of tACQ must be provided for acquiring the next sample. If the device is provided with less than
* 18 SCLK falling edges in the present serial transfer frame, the device provides an invalid
* conversion result in the next serial transfer frame
*/
static int ads7052_read_channel(const struct device *dev, uint8_t channel, uint16_t *result)
{
const struct ads7052_config *config = dev->config;
int err;
uint8_t rx_bytes[3];
const struct spi_buf rx_buf[1] = {{.buf = rx_bytes, .len = sizeof(rx_bytes)}};
const struct spi_buf_set rx = {.buffers = rx_buf, .count = ARRAY_SIZE(rx_buf)};
err = spi_read_dt(&config->bus, &rx);
if (err) {
return err;
}
*result = ads7052_get_be14(rx_bytes);
*result &= BIT_MASK(ADS7052_RESOLUTION);
return 0;
}
static void ads7052_acquisition_thread(struct ads7052_data *data)
{
uint16_t result = 0;
uint8_t channel;
int err = 0;
err = ads7052_send_calibration(data->dev, true);
if (err) {
LOG_ERR("failed to send powerup sequence (err %d)", err);
}
while (true) {
k_sem_take(&data->sem, K_FOREVER);
while (data->channels != 0) {
channel = find_lsb_set(data->channels) - 1;
LOG_DBG("reading channel %d", channel);
err = ads7052_read_channel(data->dev, channel, &result);
if (err) {
LOG_ERR("failed to read channel %d (err %d)", channel, err);
adc_context_complete(&data->ctx, err);
break;
}
LOG_DBG("read channel %d, result = %d", channel, result);
*data->buffer++ = result;
WRITE_BIT(data->channels, channel, 0);
}
adc_context_on_sampling_done(&data->ctx, data->dev);
}
}
static int adc_ads7052_init(const struct device *dev)
{
const struct ads7052_config *config = dev->config;
struct ads7052_data *data = dev->data;
data->dev = dev;
adc_context_init(&data->ctx);
k_sem_init(&data->sem, 0, 1);
if (!spi_is_ready_dt(&config->bus)) {
LOG_ERR("SPI bus %s not ready", config->bus.bus->name);
return -ENODEV;
}
k_thread_create(&data->thread, data->stack,
CONFIG_ADC_ADS7052_ACQUISITION_THREAD_STACK_SIZE,
(k_thread_entry_t)ads7052_acquisition_thread, data, NULL, NULL,
CONFIG_ADC_ADS7052_ACQUISITION_THREAD_PRIO, 0, K_NO_WAIT);
adc_context_unlock_unconditionally(&data->ctx);
return 0;
}
static const struct adc_driver_api ads7052_api = {
.channel_setup = adc_ads7052_channel_setup,
.read = adc_ads7052_read,
#ifdef CONFIG_ADC_ASYNC
.read_async = adc_ads7052_read_async,
#endif
};
#define ADC_ADS7052_SPI_CFG SPI_OP_MODE_MASTER | SPI_MODE_CPHA | SPI_WORD_SET(8)
#define ADC_ADS7052_INIT(n) \
\
static const struct ads7052_config ads7052_cfg_##n = { \
.bus = SPI_DT_SPEC_INST_GET(n, ADC_ADS7052_SPI_CFG, 1U), \
.channels = 1, \
}; \
\
static struct ads7052_data ads7052_data_##n = { \
ADC_CONTEXT_INIT_TIMER(ads7052_data_##n, ctx), \
ADC_CONTEXT_INIT_LOCK(ads7052_data_##n, ctx), \
ADC_CONTEXT_INIT_SYNC(ads7052_data_##n, ctx), \
}; \
\
DEVICE_DT_INST_DEFINE(n, adc_ads7052_init, NULL, &ads7052_data_##n, &ads7052_cfg_##n, \
POST_KERNEL, CONFIG_ADC_ADS7052_INIT_PRIORITY, &ads7052_api);
DT_INST_FOREACH_STATUS_OKAY(ADC_ADS7052_INIT)

View file

@ -0,0 +1,17 @@
# Copyright (c) 2023, Google LLC
# SPDX-License-Identifier: Apache-2.0
description: Texas Instrument Single Channel SPI ADC
compatible: "ti,ads7052"
include: [adc-controller.yaml, spi-device.yaml]
on-bus: spi
properties:
"#io-channel-cells":
const: 1
io-channel-cells:
- input

View file

@ -0,0 +1,15 @@
/*
* Copyright (c) 2023, Google LLC
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/dt-bindings/adc/adc.h>
#include <zephyr/dt-bindings/spi/spi.h>
/ {
zephyr,user {
/* adjust channel number according to pinmux in board.dts */
io-channels = <&spi_adc 0>;
};
};

View file

@ -32,6 +32,7 @@ static const struct adc_dt_spec adc_channels[] = {
void main(void)
{
int err;
uint32_t count = 0;
uint16_t buf;
struct adc_sequence sequence = {
.buffer = &buf,
@ -42,7 +43,7 @@ void main(void)
/* Configure channels individually prior to sampling. */
for (size_t i = 0U; i < ARRAY_SIZE(adc_channels); i++) {
if (!device_is_ready(adc_channels[i].dev)) {
printk("ADC controller device not ready\n");
printk("ADC controller device %s not ready\n", adc_channels[i].dev->name);
return;
}
@ -54,7 +55,7 @@ void main(void)
}
while (1) {
printk("ADC reading:\n");
printk("ADC reading[%u]:\n", count++);
for (size_t i = 0U; i < ARRAY_SIZE(adc_channels); i++) {
int32_t val_mv;

View file

@ -100,6 +100,7 @@
<&test_gpio 0 0>,
<&test_gpio 0 0>,
<&test_gpio 0 0>,
<&test_gpio 0 0>,
<&test_gpio 0 0>;
test_spi_mcp3204: mcp3204@0 {
@ -172,6 +173,14 @@
drdyb-gpios = <&test_gpio 0 0>;
#io-channel-cells = <2>;
};
test_spi_ads7052: ads7052@9 {
compatible = "ti,ads7052";
reg = <0x9>;
spi-max-frequency = <12000000>;
#io-channel-cells = <1>;
};
};
};
};