2016-11-19 16:25:21 +00:00
|
|
|
/*
|
|
|
|
* Driver for 12-bit DAC MCP4725
|
|
|
|
*
|
|
|
|
* Part of esp-open-rtos
|
|
|
|
* Copyright (C) 2016 Ruslan V. Uss <unclerus@gmail.com>
|
|
|
|
* BSD Licensed as described in the file LICENSE
|
|
|
|
*/
|
|
|
|
#include "mcp4725.h"
|
|
|
|
|
|
|
|
#define CMD_DAC 0x40
|
|
|
|
#define CMD_EEPROM 0x60
|
|
|
|
#define BIT_READY 0x80
|
|
|
|
|
2017-09-01 09:29:32 +00:00
|
|
|
static void read_data(i2c_dev_t* dev, uint8_t *buf, uint8_t size)
|
2016-11-19 16:25:21 +00:00
|
|
|
{
|
2017-09-01 09:29:32 +00:00
|
|
|
i2c_slave_read(dev->bus, dev->addr , NULL, buf, size);
|
2016-11-19 16:25:21 +00:00
|
|
|
}
|
|
|
|
|
2017-09-01 09:29:32 +00:00
|
|
|
bool mcp4725_eeprom_busy(i2c_dev_t* dev)
|
2016-11-19 16:25:21 +00:00
|
|
|
{
|
|
|
|
uint8_t res;
|
2017-09-01 09:29:32 +00:00
|
|
|
read_data(dev, &res, 1);
|
2016-11-19 16:25:21 +00:00
|
|
|
|
|
|
|
return !(res & BIT_READY);
|
|
|
|
}
|
|
|
|
|
2017-09-01 09:29:32 +00:00
|
|
|
mcp4725_power_mode_t mcp4725_get_power_mode(i2c_dev_t* dev, bool eeprom)
|
2016-11-19 16:25:21 +00:00
|
|
|
{
|
|
|
|
uint8_t buf[4];
|
2017-09-01 09:29:32 +00:00
|
|
|
read_data(dev, buf, eeprom ? 4 : 1);
|
2016-11-19 16:25:21 +00:00
|
|
|
|
|
|
|
return (eeprom ? buf[3] >> 5 : buf[0] >> 1) & 0x03;
|
|
|
|
}
|
|
|
|
|
2017-09-01 09:29:32 +00:00
|
|
|
void mcp4725_set_power_mode(i2c_dev_t* dev, mcp4725_power_mode_t mode, bool eeprom)
|
2016-11-19 16:25:21 +00:00
|
|
|
{
|
2017-09-01 09:29:32 +00:00
|
|
|
uint16_t value = mcp4725_get_raw_output(dev, eeprom);
|
2016-11-19 16:25:21 +00:00
|
|
|
uint8_t data[] = {
|
|
|
|
(eeprom ? CMD_EEPROM : CMD_DAC) | ((uint8_t)mode << 1),
|
|
|
|
value >> 4,
|
|
|
|
value << 4
|
|
|
|
};
|
2017-09-01 09:29:32 +00:00
|
|
|
i2c_slave_write(dev->bus, dev->addr, &data[0], &data[1], 2);
|
2016-11-19 16:25:21 +00:00
|
|
|
}
|
|
|
|
|
2017-09-01 09:29:32 +00:00
|
|
|
uint16_t mcp4725_get_raw_output(i2c_dev_t* dev, bool eeprom)
|
2016-11-19 16:25:21 +00:00
|
|
|
{
|
|
|
|
uint8_t buf[5];
|
2017-09-01 09:29:32 +00:00
|
|
|
read_data(dev, buf, eeprom ? 5 : 3);
|
2016-11-19 16:25:21 +00:00
|
|
|
|
|
|
|
return eeprom
|
|
|
|
? ((uint16_t)(buf[3] & 0x0f) << 8) | buf[4]
|
|
|
|
: ((uint16_t)buf[0] << 4) | (buf[1] >> 4);
|
|
|
|
}
|
|
|
|
|
2017-09-01 09:29:32 +00:00
|
|
|
void mcp4725_set_raw_output(i2c_dev_t* dev, uint16_t value, bool eeprom)
|
2016-11-19 16:25:21 +00:00
|
|
|
{
|
|
|
|
uint8_t data[] = {
|
|
|
|
(eeprom ? CMD_EEPROM : CMD_DAC),
|
|
|
|
value >> 4,
|
|
|
|
value << 4
|
|
|
|
};
|
2017-09-01 09:29:32 +00:00
|
|
|
i2c_slave_write(dev->bus, dev->addr, &data[0], &data[1], 2);
|
2016-11-19 16:25:21 +00:00
|
|
|
}
|