drivers: dac: added driver for Microchip MCP4725

The MCP4725 is a single channel 12 bit DAC. It is controlled via I2C.

Signed-off-by: Kieran Mackey <kieran.mackey@lairdconnect.com>
This commit is contained in:
Kieran Mackey 2021-03-18 17:08:54 +00:00 committed by Kumar Gala
commit 38ed9c885e
29 changed files with 465 additions and 2 deletions

View file

@ -9,4 +9,5 @@ zephyr_library_sources_ifdef(CONFIG_DAC_SAM0 dac_sam0.c)
zephyr_library_sources_ifdef(CONFIG_DAC_DACX0508 dac_dacx0508.c)
zephyr_library_sources_ifdef(CONFIG_DAC_DACX3608 dac_dacx3608.c)
zephyr_library_sources_ifdef(CONFIG_DAC_SHELL dac_shell.c)
zephyr_library_sources_ifdef(CONFIG_DAC_MCP4725 dac_mcp4725.c)
zephyr_library_sources_ifdef(CONFIG_USERSPACE dac_handlers.c)

View file

@ -34,4 +34,6 @@ source "drivers/dac/Kconfig.dacx0508"
source "drivers/dac/Kconfig.dacx3608"
source "drivers/dac/Kconfig.mcp4725"
endif # DAC

View file

@ -0,0 +1,21 @@
# DAC configuration options
# Copyright (c) 2021 Laird Connectivity
#
# SPDX-License-Identifier: Apache-2.0
config DAC_MCP4725
bool "Microchip MCP4725 DAC driver"
depends on I2C
help
Enable the driver for the Microchip MCP4725.
if DAC_MCP4725
config DAC_MCP4725_INIT_PRIORITY
int "Init priority"
default 80
help
MCP4725 DAC device driver initialization priority.
endif # DAC_MCP4725

148
drivers/dac/dac_mcp4725.c Normal file
View file

@ -0,0 +1,148 @@
/*
* Copyright (c) 2021 Laird Connectivity
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT microchip_mcp4725
#include <zephyr.h>
#include <drivers/i2c.h>
#include <drivers/dac.h>
#include <logging/log.h>
LOG_MODULE_REGISTER(dac_mcp4725, CONFIG_DAC_LOG_LEVEL);
/* Information in this file comes from MCP4725 datasheet revision D
* found at https://ww1.microchip.com/downloads/en/DeviceDoc/22039d.pdf
*/
/* Defines for field values in MCP4725 DAC register */
#define MCP4725_DAC_MAX_VAL 0xFFF
#define MCP4725_FAST_MODE_POWER_DOWN_POS 4U
#define MCP4725_FAST_MODE_DAC_UPPER_VAL_POS 8U
#define MCP4725_FAST_MODE_DAC_UPPER_VAL_MASK 0xF
#define MCP4725_FAST_MODE_DAC_LOWER_VAL_MASK 0xFF
#define MCP4725_READ_RDY_POS 7U
#define MCP4725_READ_RDY_MASK (0x1 << MCP4725_READ_RDY_POS)
/* After writing eeprom, the MCP4725 can be in a busy state for 25 - 50ms
* See section 1.0 of MCP4725 datasheet, 'Electrical Characteristics'
*/
#define MCP4725_BUSY_TIMEOUT_MS 60U
struct mcp4725_config {
const struct device *i2c_dev;
uint16_t i2c_addr;
};
/* Read mcp4725 and check RDY status bit */
static int mcp4725_wait_until_ready(const struct device *dev, uint16_t i2c_addr)
{
uint8_t rx_data[5];
bool mcp4725_ready = false;
int ret;
int32_t timeout = k_uptime_get_32() + MCP4725_BUSY_TIMEOUT_MS;
/* Wait until RDY bit is set or return error if timer exceeds MCP4725_BUSY_TIMEOUT_MS */
while (!mcp4725_ready) {
ret = i2c_read(dev, rx_data, sizeof(rx_data), i2c_addr);
if (ret == 0) {
mcp4725_ready = rx_data[0] & MCP4725_READ_RDY_MASK;
} else {
/* I2C error */
return ret;
}
if (k_uptime_get_32() > timeout) {
return -ETIMEDOUT;
}
}
return 0;
}
/* MCP4725 is a single channel 12 bit DAC */
static int mcp4725_channel_setup(const struct device *dev,
const struct dac_channel_cfg *channel_cfg)
{
if (channel_cfg->channel_id != 0) {
return -EINVAL;
}
if (channel_cfg->resolution != 12) {
return -ENOTSUP;
}
return 0;
}
static int mcp4725_write_value(const struct device *dev, uint8_t channel,
uint32_t value)
{
const struct mcp4725_config *config = (struct mcp4725_config *)dev->config;
uint8_t tx_data[2];
int ret;
if (channel != 0) {
return -EINVAL;
}
/* Check value isn't over 12 bits */
if (value > MCP4725_DAC_MAX_VAL) {
return -ENOTSUP;
}
/* WRITE_MODE_FAST message format (2 bytes):
*
* || 15 14 | 13 12 | 11 10 9 8 || 7 6 5 4 3 2 1 0 ||
* || Fast mode (0) | Power-down bits (0) | DAC value[11:8] || DAC value[7:0] ||
*/
tx_data[0] = ((value >> MCP4725_FAST_MODE_DAC_UPPER_VAL_POS) &
MCP4725_FAST_MODE_DAC_UPPER_VAL_MASK);
tx_data[1] = (value & MCP4725_FAST_MODE_DAC_LOWER_VAL_MASK);
ret = i2c_write(config->i2c_dev, tx_data, sizeof(tx_data),
config->i2c_addr);
return ret;
}
static int dac_mcp4725_init(const struct device *dev)
{
const struct mcp4725_config *config = dev->config;
if (!device_is_ready(config->i2c_dev)) {
LOG_ERR("I2C device not found");
return -EINVAL;
}
/* Check we can read a 'RDY' bit from this device */
if (mcp4725_wait_until_ready(config->i2c_dev, config->i2c_addr)) {
return -EBUSY;
}
return 0;
}
static const struct dac_driver_api mcp4725_driver_api = {
.channel_setup = mcp4725_channel_setup,
.write_value = mcp4725_write_value,
};
#define INST_DT_MCP4725(index) \
static const struct mcp4725_config mcp4725_config_##index = { \
.i2c_dev = DEVICE_DT_GET(DT_INST_BUS(index)), \
.i2c_addr = DT_INST_REG_ADDR(index) \
}; \
\
DEVICE_DT_INST_DEFINE(index, dac_mcp4725_init, \
device_pm_control_nop, \
NULL, \
&mcp4725_config_##index, POST_KERNEL, \
CONFIG_DAC_MCP4725_INIT_PRIORITY, \
&mcp4725_driver_api);
DT_INST_FOREACH_STATUS_OKAY(INST_DT_MCP4725);

View file

@ -0,0 +1,15 @@
# Copyright (c) 2021 Laird Connectivity
# SPDX-License-Identifier: Apache-2.0
description: Microchip MCP4725 12-bit DAC
compatible: "microchip,mcp4725"
include: [dac-controller.yaml, i2c-device.yaml]
properties:
"#io-channel-cells":
const: 1
io-channel-cells:
- output

View file

@ -88,6 +88,48 @@ follows:
DAC output is available on connector J4 pin 11.
Building and Running for BL652
======================================
The BL652 DVK PCB contains a footprint for a MCP4725 to use as an external
DAC. Note this is not populated by default. The sample can be built and
executed for the :ref:`bl652_dvk` as follows:
.. zephyr-app-commands::
:zephyr-app: samples/drivers/dac
:board: bl652_dvk
:goals: build flash
:compact:
DAC output is available on pin 1 of the MCP4725.
Building and Running for BL653
======================================
The BL653 DVK PCB contains a footprint for a MCP4725 to use as an external
DAC. Note this is not populated by default. The sample can be built and
executed for the :ref:`bl653_dvk` as follows:
.. zephyr-app-commands::
:zephyr-app: samples/drivers/dac
:board: bl653_dvk
:goals: build flash
:compact:
DAC output is available on pin 1 of the MCP4725.
Building and Running for BL654
======================================
The BL654 DVK PCB contains a footprint for a MCP4725 to use as an external
DAC. Note this is not populated by default. The sample can be built and
executed for the :ref:`bl654_dvk` as follows:
.. zephyr-app-commands::
:zephyr-app: samples/drivers/dac
:board: bl654_dvk
:goals: build flash
:compact:
DAC output is available on pin 1 of the MCP4725.
Sample output
=============

View file

@ -0,0 +1,6 @@
#
# Copyright (c) 2021 Laird Connectivity
#
# SPDX-License-Identifier: Apache-2.0
#
CONFIG_DAC_MCP4725=y

View file

@ -0,0 +1,14 @@
/*
* Copyright 2021 Laird Connectivity
*
* SPDX-License-Identifier: Apache-2.0
*/
&i2c0 {
dac0: mcp4725@60 {
compatible = "microchip,mcp4725";
reg = <0x60>;
label = "MCP4725";
#io-channel-cells = <1>;
};
};

View file

@ -0,0 +1,6 @@
#
# Copyright (c) 2021 Laird Connectivity
#
# SPDX-License-Identifier: Apache-2.0
#
CONFIG_DAC_MCP4725=y

View file

@ -0,0 +1,14 @@
/*
* Copyright 2021 Laird Connectivity
*
* SPDX-License-Identifier: Apache-2.0
*/
&i2c0 {
dac0: mcp4725@60 {
compatible = "microchip,mcp4725";
reg = <0x60>;
label = "MCP4725";
#io-channel-cells = <1>;
};
};

View file

@ -0,0 +1,6 @@
#
# Copyright (c) 2021 Laird Connectivity
#
# SPDX-License-Identifier: Apache-2.0
#
CONFIG_DAC_MCP4725=y

View file

@ -0,0 +1,14 @@
/*
* Copyright 2021 Laird Connectivity
*
* SPDX-License-Identifier: Apache-2.0
*/
&i2c0 {
dac0: mcp4725@60 {
compatible = "microchip,mcp4725";
reg = <0x60>;
label = "MCP4725";
#io-channel-cells = <1>;
};
};

View file

@ -5,7 +5,8 @@ tests:
tags: DAC
platform_allow: |
arduino_zero frdm_k22f frdm_k64f nucleo_f091rc nucleo_g071rb nucleo_g431rb
nucleo_l073rz nucleo_l152re twr_ke18f nucleo_f767zi nucleo_f429zi
nucleo_l073rz nucleo_l152re twr_ke18f nucleo_f767zi nucleo_f429zi bl652_dvk
bl653_dvk bl654_dvk
depends_on: dac
harness: console
harness_config:

View file

@ -0,0 +1,7 @@
#
# Copyright (c) 2021 Laird Connectivity
#
# SPDX-License-Identifier: Apache-2.0
#
CONFIG_I2C=y
CONFIG_DAC_MCP4725=y

View file

@ -0,0 +1,15 @@
/*
* Copyright 2021 Laird Connectivity
*
* SPDX-License-Identifier: Apache-2.0
*/
&i2c0 {
dac0: mcp4725@60 {
/* MCP4725 not populated at factory */
compatible = "microchip,mcp4725";
reg = <0x60>;
label = "MCP4725";
#io-channel-cells = <1>;
};
};

View file

@ -0,0 +1,7 @@
#
# Copyright (c) 2021 Laird Connectivity
#
# SPDX-License-Identifier: Apache-2.0
#
CONFIG_I2C=y
CONFIG_DAC_MCP4725=y

View file

@ -0,0 +1,15 @@
/*
* Copyright 2021 Laird Connectivity
*
* SPDX-License-Identifier: Apache-2.0
*/
&i2c0 {
dac0: mcp4725@60 {
/* MCP4725 not populated at factory */
compatible = "microchip,mcp4725";
reg = <0x60>;
label = "MCP4725";
#io-channel-cells = <1>;
};
};

View file

@ -0,0 +1,7 @@
#
# Copyright (c) 2021 Laird Connectivity
#
# SPDX-License-Identifier: Apache-2.0
#
CONFIG_I2C=y
CONFIG_DAC_MCP4725=y

View file

@ -0,0 +1,15 @@
/*
* Copyright 2021 Laird Connectivity
*
* SPDX-License-Identifier: Apache-2.0
*/
&i2c0 {
dac0: mcp4725@60 {
/* MCP4725 not populated at factory */
compatible = "microchip,mcp4725";
reg = <0x60>;
label = "MCP4725";
#io-channel-cells = <1>;
};
};

View file

@ -37,6 +37,16 @@
#define DAC_RESOLUTION 12
#define DAC_CHANNEL_ID 0
#elif defined(CONFIG_BOARD_BL652_DVK) || \
defined(CONFIG_BOARD_BL653_DVK) || \
defined(CONFIG_BOARD_BL654_DVK)
/* Note external DAC MCP4725 is not populated on BL652_DVK, BL653_DVK and
* BL654_DVK at factory
*/
#define DAC_DEVICE_NAME DT_LABEL(DT_NODELABEL(dac0))
#define DAC_RESOLUTION 12
#define DAC_CHANNEL_ID 0
#else
#error "Unsupported board."
#endif

View file

@ -2,5 +2,7 @@ common:
tags: dac drivers userspace
tests:
drivers.dac:
platform_allow: frdm_k22f frdm_k64f nucleo_l073rz nucleo_l152re twr_ke18f nucleo_f767zi nucleo_f429zi
platform_allow:
frdm_k22f frdm_k64f nucleo_l073rz nucleo_l152re twr_ke18f nucleo_f767zi
nucleo_f429zi bl652_dvk bl653_dvk bl654_dvk
depends_on: dac

View file

@ -0,0 +1,7 @@
#
# Copyright (c) 2021 Laird Connectivity
#
# SPDX-License-Identifier: Apache-2.0
#
CONFIG_I2C=y
CONFIG_DAC_MCP4725=y

View file

@ -0,0 +1,15 @@
/*
* Copyright 2021 Laird Connectivity
*
* SPDX-License-Identifier: Apache-2.0
*/
&i2c0 {
dac0: mcp4725@60 {
/* MCP4725 not populated at factory */
compatible = "microchip,mcp4725";
reg = <0x60>;
label = "MCP4725";
#io-channel-cells = <1>;
};
};

View file

@ -0,0 +1,7 @@
#
# Copyright (c) 2021 Laird Connectivity
#
# SPDX-License-Identifier: Apache-2.0
#
CONFIG_I2C=y
CONFIG_DAC_MCP4725=y

View file

@ -0,0 +1,15 @@
/*
* Copyright 2021 Laird Connectivity
*
* SPDX-License-Identifier: Apache-2.0
*/
&i2c0 {
dac0: mcp4725@60 {
/* MCP4725 not populated at factory */
compatible = "microchip,mcp4725";
reg = <0x60>;
label = "MCP4725";
#io-channel-cells = <1>;
};
};

View file

@ -0,0 +1,7 @@
#
# Copyright (c) 2021 Laird Connectivity
#
# SPDX-License-Identifier: Apache-2.0
#
CONFIG_I2C=y
CONFIG_DAC_MCP4725=y

View file

@ -0,0 +1,15 @@
/*
* Copyright 2021 Laird Connectivity
*
* SPDX-License-Identifier: Apache-2.0
*/
&i2c0 {
mcp4725@60 {
/* MCP4725 not populated at factory */
compatible = "microchip,mcp4725";
reg = <0x60>;
label = "MCP4725";
#io-channel-cells = <1>;
};
};

View file

@ -111,6 +111,28 @@
#define ADC_ACQUISITION_TIME ADC_ACQ_TIME_DEFAULT
#define ADC_CHANNEL_ID 23
#elif defined(CONFIG_BOARD_BL652_DVK) || \
defined(CONFIG_BOARD_BL653_DVK) || \
defined(CONFIG_BOARD_BL654_DVK)
#include <hal/nrf_saadc.h>
/* DAC output from MCP4725 pin 1
* ADC_1 input read from pin SIO_3
* Note external DAC MCP4725 is not populated on BL652_DVK, BL653_DVK and
* BL654_DVK at factory
*/
#define DAC_DEVICE_NAME DT_LABEL(DT_NODELABEL(dac0))
#define DAC_RESOLUTION 12
#define DAC_CHANNEL_ID 0
#define ADC_DEVICE_NAME DT_LABEL(DT_NODELABEL(adc))
#define ADC_RESOLUTION 12
#define ADC_GAIN ADC_GAIN_1_4
#define ADC_REFERENCE ADC_REF_VDD_1_4
#define ADC_ACQUISITION_TIME ADC_ACQ_TIME_DEFAULT
#define ADC_CHANNEL_ID 1
#define ADC_1ST_CHANNEL_INPUT NRF_SAADC_INPUT_AIN1
#else
#error "Unsupported board."
#endif
@ -125,6 +147,12 @@ static const struct adc_channel_cfg adc_ch_cfg = {
.reference = ADC_REFERENCE,
.acquisition_time = ADC_ACQUISITION_TIME,
.channel_id = ADC_CHANNEL_ID,
#if defined(CONFIG_BOARD_BL652_DVK) || \
defined(CONFIG_BOARD_BL653_DVK) || \
defined(CONFIG_BOARD_BL654_DVK)
.input_positive = ADC_1ST_CHANNEL_INPUT,
#endif
};
static const struct device *init_dac(void)

View file

@ -8,3 +8,4 @@ tests:
fixture: dac_adc_loopback
platform_allow: |
frdm_k22f frdm_k64f nucleo_f207zg nucleo_l073rz nucleo_l152re twr_ke18f
bl652_dvk bl653_dvk bl654_dvk