Device driver for Microchip MCP47xx DAC with I2C interface More...
Device driver for Microchip MCP47xx DAC with I2C interface
The driver supports the different Microchip MCP47xx DAC variants.
Expander | Channels | Resolution |
---|---|---|
MCP4706 | 1 | 8-bit |
MCP4716 | 1 | 10-bit |
MCP4725 | 1 | 12-bit |
MCP4726 | 1 | 12-bit |
MCP4728 | 4 | 12-bit |
Multiple MCP47xx DAC devices and different variants can be used at the same time.
The driver interface is kept as compatible as possible with the peripheral DAC interface. The only differences are that
mcp47xx_
andPlease refer the test application in tests/drivers/mcp47xx
for an example on how to use the driver.
The driver provides SAUL capabilities that are compatible with SAUL actuators of type SAUL_ACT_DIMMER. Each MCP47xx channel can be mapped directly to SAUL by defining an according entry in MCP47XX_SAUL_DAC_PARAMS
. Please refer file $RIOTBASE/drivers/mcp47xx/include/mcp47xx_params.h
for an example.
mcp47xx_saul_dac_params_t mcp47xx_saul_dac_params[] = { { .name = "DAC00", .dev = 0, .channel = 0, .initial = 32768, }, };
For each DAC channel that should be used with SAUL, an entry with a name, the device, the channel, and the initial value has to be defined as shown above.
It is possible to used multiple devices and different variants of MCP47xx DAC devices at the same time. The application has to configure all devices by defining the configuration parameter set mcp47xx_params
of type mcp47xx_params_t. As an example, the default configuration for one MCP4725 device is defined in drivers/mcp47xx/mcp47xx_params.h
.
The application can override it by placing a file mcp47xx_params.h
in the application directory $(APPDIR)
. For example, the definition of the configuration parameter set for the two devices (one MCP4725 and one MCP4728) could looks like:
static const mcp47xx_params_t mcp47xx_params[] = { { .variant = MCP4725, .dev = I2C_DEV(0), .addr = MCP47XX_BASE_ADDR + 2, .gain = MCP47XX_GAIN_1X, .vref = MCP47XX_VDD, .pd_mode = MCP47XX_PD_LARGE, }, { .variant = MCP4728, .dev = I2C_DEV(0), .addr = MCP47XX_BASE_ADDR + 3, .gain = MCP47XX_GAIN_2X, .vref = MCP47XX_VREF_INT, .pd_mode = MCP47XX_PD_LARGE, }, };
Files | |
file | mcp47xx.h |
file | mcp47xx_params.h |
Default configuration for Microchip MCP47xx DAC with I2C interface. | |
Data Structures | |
struct | mcp47xx_params_t |
MCP47xx device configuration parameters. More... | |
struct | mcp47xx_t |
MCP47xx device data structure type. More... | |
struct | mcp47xx_saul_dac_params_t |
MCP47xx configuration structure for mapping DAC channels to SAUL. More... | |
Enumerations | |
enum | mcp47xx_error_codes_t { MCP47XX_OK , MCP47XX_ERROR_I2C , MCP47XX_ERROR_NOT_AVAIL } |
MCP47xx driver error codes. More... | |
enum | mcp47xx_variant_t { MCP4706 , MCP4716 , MCP4725 , MCP4726 , MCP4728 } |
Supported MCP47xx variants. More... | |
enum | mcp47xx_gain_t { MCP47XX_GAIN_1X = 0 , MCP47XX_GAIN_2X = 1 } |
MCP47xx gain configuration type. More... | |
enum | mcp47xx_vref_t { MCP47XX_VREF_VDD = 0 , MCP47XX_VREF_INT = 1 , MCP47XX_VREF_PIN = 2 , MCP47XX_VREF_BUF = 3 } |
MCP47xx V_REF configuration type. More... | |
enum | mcp47xx_pd_mode_t { MCP47XX_NORMAL = 0 , MCP47XX_PD_SMALL = 1 , MCP47XX_PD_MEDIUM = 2 , MCP47XX_PD_LARGE = 3 } |
MCP47xx Power-down mode selection type. More... | |
Functions | |
int | mcp47xx_init (mcp47xx_t *dev, const mcp47xx_params_t *params) |
Initialize the MCP47xx DAC. | |
int | mcp47xx_dac_init (mcp47xx_t *dev, uint8_t chn) |
Initialize a MCP47xx DAC channel. | |
void | mcp47xx_dac_set (mcp47xx_t *dev, uint8_t chn, uint16_t value) |
Write a value to a MCP47xx DAC channel. | |
void | mcp47xx_dac_get (mcp47xx_t *dev, uint8_t chn, uint16_t *value) |
Get the current value of a MCP47xx DAC channel. | |
void | mcp47xx_dac_poweron (mcp47xx_t *dev, uint8_t chn) |
Enables the MCP47xx DAC device. | |
void | mcp47xx_dac_poweroff (mcp47xx_t *dev, uint8_t chn) |
Disables the MCP47xx DAC device. | |
uint8_t | mcp47xx_dac_channels (mcp47xx_t *dev) |
Returns the number of channels of MCP47xx DAC device. | |
MCP47xx I2C slave addresses | |
MCP47xx I2C slave addresses are defined as an offset to a base address, which depends on the expander used. The address offset is in the range of 0 to 7. | |
#define | MCP47XX_BASE_ADDR (0x60) |
MCP47xx I2C slave base address. | |
MCP47xx Channel number | |
#define | MCP4706_CHN_NUM (1) |
MCP4706 has 1 channel. | |
#define | MCP4716_CHN_NUM (1) |
MCP4716 has 1 channel. | |
#define | MCP4725_CHN_NUM (1) |
MCP4725 has 1 channel. | |
#define | MCP4726_CHN_NUM (1) |
MCP4726 has 1 channel. | |
#define | MCP4728_CHN_NUM (4) |
MCP4728 has 4 channels. | |
#define | MCP47XX_CHN_NUM_MAX (4) |
maximum number of channels | |
#define MCP47XX_BASE_ADDR (0x60) |
#define MCP47XX_CHN_NUM_MAX (4) |
enum mcp47xx_gain_t |
enum mcp47xx_pd_mode_t |
MCP47xx Power-down mode selection type.
Defines the possible power-down modes used for MCP47xx device configuration. The mode is used by function mcp47xx_dac_poweroff to set the DAC into the configured power-down mode.
enum mcp47xx_variant_t |
enum mcp47xx_vref_t |
MCP47xx V_REF configuration type.
uint8_t mcp47xx_dac_channels | ( | mcp47xx_t * | dev | ) |
Returns the number of channels of MCP47xx DAC device.
This function returns the number of channels of the device MCP47xx DAC device.
[in] | dev | descriptor of the MCP47xx DAC device |
number | of channels on success or 0 on error |
void mcp47xx_dac_get | ( | mcp47xx_t * | dev, |
uint8_t | chn, | ||
uint16_t * | value ) |
Get the current value of a MCP47xx DAC channel.
The value is always given as 16-bit value and is internally scaled to the actual resolution that the DAC unit provides, e.g., 12-bit.
[in] | dev | descriptor of the MCP47xx DAC device |
[in] | chn | channel to set |
[out] | value | value to set line to |
int mcp47xx_dac_init | ( | mcp47xx_t * | dev, |
uint8_t | chn ) |
Initialize a MCP47xx DAC channel.
After the initialization, the DAC channel is active and its output is set to 0.
[in] | dev | descriptor of the MCP47xx DAC device |
[in] | chn | channel to initialize |
MCP47XX_OK | on success |
MCP47XX_ERROR_* | a negative error code on error, see mcp47xx_error_codes_t |
void mcp47xx_dac_poweroff | ( | mcp47xx_t * | dev, |
uint8_t | chn ) |
Disables the MCP47xx DAC device.
MCP47xx is switched to the power-down mode configured by the configuration parameter mcp47xx_params_t::pd_mode. V_OUT is loaded with the configured resistor to ground. Most of the channel circuits are powered off.
[in] | dev | descriptor of the MCP47xx DAC device |
[in] | chn | channel to power on |
void mcp47xx_dac_poweron | ( | mcp47xx_t * | dev, |
uint8_t | chn ) |
Enables the MCP47xx DAC device.
MCP47xx is enabled and the output is set to the last set value.
[in] | dev | descriptor of the MCP47xx DAC device |
[in] | chn | channel to power on |
void mcp47xx_dac_set | ( | mcp47xx_t * | dev, |
uint8_t | chn, | ||
uint16_t | value ) |
Write a value to a MCP47xx DAC channel.
The value is always given as 16-bit value and is internally scaled to the actual resolution that the DAC unit provides, e.g., 12-bit. So to get the maximum output voltage, this function has to be called with value set to 65535 (UINT16_MAX).
[in] | dev | descriptor of the MCP47xx DAC device |
[in] | chn | channel to set |
[in] | value | value to set line to |
int mcp47xx_init | ( | mcp47xx_t * | dev, |
const mcp47xx_params_t * | params ) |
Initialize the MCP47xx DAC.
All expander pins are set to be input and are pulled up.
[in] | dev | descriptor of the MCP47xx DAC device |
[in] | params | configuration parameters, see mcp47xx_params_t |
MCP47XX_OK | on success |
MCP47XX_ERROR_* | a negative error code on error, see mcp47xx_error_codes_t |