From 60b671ea44ae657aa141d201378a3eaac3e9f4fc Mon Sep 17 00:00:00 2001 From: UncleRus Date: Fri, 13 Oct 2017 00:49:15 +0500 Subject: [PATCH] Added DAC support to PCF8591 driver --- examples/pcf8591/main.c | 2 +- extras/pcf8591/pcf8591.c | 39 ++++++++++++++++++++++++++++++--------- extras/pcf8591/pcf8591.h | 36 +++++++++++++++++++++++------------- 3 files changed, 54 insertions(+), 23 deletions(-) diff --git a/examples/pcf8591/main.c b/examples/pcf8591/main.c index a4dc8a3..442d3c1 100644 --- a/examples/pcf8591/main.c +++ b/examples/pcf8591/main.c @@ -23,7 +23,7 @@ static void measure(void *pvParameters) while (1) { vTaskDelay(1000 / portTICK_PERIOD_MS); - printf("Value: %d\n", pcf8591_read(&dev, 0x03)); + printf("Value: %d\n", pcf8591_read(&dev, PCF8591_IC_4_SINGLES, 3)); } } diff --git a/extras/pcf8591/pcf8591.c b/extras/pcf8591/pcf8591.c index ba06a5e..f1cec30 100644 --- a/extras/pcf8591/pcf8591.c +++ b/extras/pcf8591/pcf8591.c @@ -1,20 +1,41 @@ -#include -#include - +/** + * Driver for 8-bit analog-to-digital conversion and + * an 8-bit digital-to-analog conversion PCF8591 + * + * Part of esp-open-rtos + * Copyright (C) 2017 Pham Ngoc Thanh + * 2017 Ruslan V. Uss + * BSD Licensed as described in the file LICENSE + */ #include "pcf8591.h" -/** - * CAUTION: PLEASE SET LOW FREQUENCY - */ +#include -#define PCF8591_CTRL_REG_READ 0x03 +#define BV(x) (1 << (x)) -uint8_t pcf8591_read(i2c_dev_t* dev, uint8_t analog_pin) +#define CTRL_AD_CH_MASK 0x03 + +#define CTRL_AD_IN_PRG 4 +#define CTRL_AD_IN_PRG_MASK (0x03 << CTRL_AD_IN_PRG) + +#define CTRL_DA_OUT_EN 6 + +uint8_t pcf8591_read(i2c_dev_t *dev, pcf8591_input_conf_t conf, uint8_t channel) { uint8_t res = 0; - uint8_t control_reg = PCF8591_CTRL_REG_READ & analog_pin; + uint8_t control_reg = + ((conf << CTRL_AD_IN_PRG) & CTRL_AD_IN_PRG_MASK) | + (channel & CTRL_AD_CH_MASK) | + BV(CTRL_DA_OUT_EN); i2c_slave_read(dev->bus, dev->addr, &control_reg, &res, 1); return res; } + +void pcf8591_write(i2c_dev_t *dev, uint8_t value) +{ + uint8_t buf[2] = { BV(CTRL_DA_OUT_EN), value }; + + i2c_slave_write(dev->bus, dev->addr, NULL, buf, 2); +} diff --git a/extras/pcf8591/pcf8591.h b/extras/pcf8591/pcf8591.h index 5327323..f136a46 100644 --- a/extras/pcf8591/pcf8591.h +++ b/extras/pcf8591/pcf8591.h @@ -1,14 +1,16 @@ /** - * Driver for 8-bit analog-to-digital conversion and + * Driver for 8-bit analog-to-digital conversion and * an 8-bit digital-to-analog conversion PCF8591 * * Part of esp-open-rtos * Copyright (C) 2017 Pham Ngoc Thanh + * 2017 Ruslan V. Uss * BSD Licensed as described in the file LICENSE */ #ifndef _EXTRAS_PCF8591_H_ #define _EXTRAS_PCF8591_H_ +#include #include #ifdef __cplusplus @@ -16,25 +18,33 @@ extern "C" { #endif -/** - * CAUTION: PLEASE SET I2C_FREQUENCY_400K IS 'false' IN 'i2c.h' FILE - */ - #define PCF8591_DEFAULT_ADDRESS 0x48 -void pcf8591_init(void); //FIXME : library incomplete ? +/** + * Analog inputs configuration, see datasheet + */ +typedef enum { + PCF8591_IC_4_SINGLES = 0, //!< Four single-ended inputs + PCF8591_IC_DIFF, //!< Three differential inputs + PCF8591_IC_2_SINGLES_DIFF, //!< Two single-ended and differnetial mixed + PCF8591_IC_2_DIFFS //!< Two differential inputs +} pcf8591_input_conf_t; /** * Read input value of an analog pin. - * @param[in] addr Pointer to device - * @param[in] analog_pin pin number: - * 0 - AIN0 - * 1 - AIN1 - * 2 - AIN2 - * 3 - AIN3 + * @param[in] dev Pointer to device + * @param[in] conf Analog inputs configuration + * @param[in] channel Analog channel * @return analog value */ -uint8_t pcf8591_read(i2c_dev_t* dev, uint8_t analog_pin); +uint8_t pcf8591_read(i2c_dev_t *dev, pcf8591_input_conf_t conf, uint8_t channel); + +/** + * Write value to analog output + * @param[in] dev Pointer to device + * @param[in] value DAC value + */ +void pcf8591_write(i2c_dev_t *dev, uint8_t value); #ifdef __cplusplus