Device driver for Microchip MCP47xx DAC with I2C interface.
More...
Device driver for Microchip MCP47xx DAC with I2C interface.
Overview
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 |
- Note
- The following features of MCP47xx DAC devices are not supported at the moment:
- configuring and reading address bits to/from EEPROM
- writing DAC channel output values to the EEPROM
- setting the UDAC bit and using the LDAC pin for MCP4728
Usage
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
- functions have the prefix
mcp47xx_
and
- functions require an additional parameter, the pointer to the MCP47xx device of type mcp47xx_t.
Please refer the test application in tests/drivers/mcp47xx
for an example on how to use the driver.
SAUL Capabilities
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.
Using Multiple Devices
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,
},
};
- Author
- Gunar Schorcht gunar.nosp@m.@sch.nosp@m.orcht.nosp@m..net
|
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...
|
|
|
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.
|
|
◆ MCP4706_CHN_NUM
#define MCP4706_CHN_NUM (1) |
MCP4706 has 1 channel.
Definition at line 140 of file mcp47xx.h.
◆ MCP4716_CHN_NUM
#define MCP4716_CHN_NUM (1) |
MCP4716 has 1 channel.
Definition at line 141 of file mcp47xx.h.
◆ MCP4725_CHN_NUM
#define MCP4725_CHN_NUM (1) |
MCP4725 has 1 channel.
Definition at line 142 of file mcp47xx.h.
◆ MCP4726_CHN_NUM
#define MCP4726_CHN_NUM (1) |
MCP4726 has 1 channel.
Definition at line 143 of file mcp47xx.h.
◆ MCP4728_CHN_NUM
#define MCP4728_CHN_NUM (4) |
MCP4728 has 4 channels.
Definition at line 144 of file mcp47xx.h.
◆ MCP47XX_BASE_ADDR
#define MCP47XX_BASE_ADDR (0x60) |
MCP47xx I2C slave base address.
Addresses are then in the range from 0x60 to 0x67
Definition at line 133 of file mcp47xx.h.
◆ MCP47XX_CHN_NUM_MAX
#define MCP47XX_CHN_NUM_MAX (4) |
maximum number of channels
Definition at line 145 of file mcp47xx.h.
◆ mcp47xx_error_codes_t
MCP47xx driver error codes.
Enumerator |
---|
MCP47XX_OK | success
|
MCP47XX_ERROR_I2C | I2C communication error.
|
MCP47XX_ERROR_NOT_AVAIL | device not available
|
Definition at line 150 of file mcp47xx.h.
◆ mcp47xx_gain_t
MCP47xx gain configuration type.
- Note
- Gains are not supported by MCP4725.
Enumerator |
---|
MCP47XX_GAIN_1X | Gain is 1.0, supported by all MCP47xx variants.
|
MCP47XX_GAIN_2X | Gain is 2.0, not supported by MCP4725.
|
Definition at line 175 of file mcp47xx.h.
◆ 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.
- Note
- MCP47XX_NORMAL cannot be configured as power-down mode.
Enumerator |
---|
MCP47XX_NORMAL | Normal mode.
|
MCP47XX_PD_SMALL | Power down, small resistor 1 kOhm.
|
MCP47XX_PD_MEDIUM | Power down, medium resistor, 125 kOhm for MCP47x6, 100 kOhm otherwise.
|
MCP47XX_PD_LARGE | Power down, large resistor, 640 kOhm for MCP47x6, 125 kOhm otherwise.
|
Definition at line 201 of file mcp47xx.h.
◆ mcp47xx_variant_t
Supported MCP47xx variants.
It is used in configuration parameters to specify the MCP47xx expander used by device.
Enumerator |
---|
MCP4706 | 1 channel 8-bit DAC
|
MCP4716 | 1 channel 10-bit DAC
|
MCP4725 | 1 channel 12-bit DAC
|
MCP4726 | 1 channel 12-bit DAC
|
MCP4728 | 4 channel 12-bit DAC
|
Definition at line 162 of file mcp47xx.h.
◆ mcp47xx_vref_t
MCP47xx V_REF configuration type.
- Note
- Different MCP47xx variants allow different V_REF configurations
Enumerator |
---|
MCP47XX_VREF_VDD | V_REF = V_DD, supported by all MCP47xx.
|
MCP47XX_VREF_INT | V_REF = internal (2.048 V), MCP4728 only
|
MCP47XX_VREF_PIN | V_REF = VREF pin not buffered, MCP47x6 only.
|
MCP47XX_VREF_BUF | V_REF = VREF pin buffered, MCP47x6 only.
|
Definition at line 185 of file mcp47xx.h.
◆ mcp47xx_dac_channels()
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.
- Parameters
-
[in] | dev | descriptor of the MCP47xx DAC device |
- Return values
-
number | of channels on success or 0 on error |
◆ mcp47xx_dac_get()
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.
- Parameters
-
[in] | dev | descriptor of the MCP47xx DAC device |
[in] | chn | channel to set |
[out] | value | value to set line to |
- Return values
-
◆ mcp47xx_dac_init()
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.
- Parameters
-
[in] | dev | descriptor of the MCP47xx DAC device |
[in] | chn | channel to initialize |
- Return values
-
◆ mcp47xx_dac_poweroff()
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.
- Note
- If MCP47XX_NORMAL is used as power-down mode, the DAC can't be powerd off.
- Parameters
-
[in] | dev | descriptor of the MCP47xx DAC device |
[in] | chn | channel to power on |
- Return values
-
◆ mcp47xx_dac_poweron()
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.
- Parameters
-
[in] | dev | descriptor of the MCP47xx DAC device |
[in] | chn | channel to power on |
- Return values
-
◆ mcp47xx_dac_set()
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).
- Parameters
-
[in] | dev | descriptor of the MCP47xx DAC device |
[in] | chn | channel to set |
[in] | value | value to set line to |
- Return values
-
◆ mcp47xx_init()
Initialize the MCP47xx DAC.
All expander pins are set to be input and are pulled up.
- Parameters
-
[in] | dev | descriptor of the MCP47xx DAC device |
[in] | params | configuration parameters, see mcp47xx_params_t |
- Return values
-