Merge pull request #465 from UncleRus/pcf8591_update

PCF8591 driver update
This commit is contained in:
Ruslan V. Uss 2017-10-25 13:01:40 +05:00 committed by GitHub
commit 7cd1afc52d
3 changed files with 54 additions and 23 deletions

View file

@ -23,7 +23,7 @@ static void measure(void *pvParameters)
while (1) while (1)
{ {
vTaskDelay(1000 / portTICK_PERIOD_MS); 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));
} }
} }

View file

@ -1,20 +1,41 @@
#include <stddef.h> /**
#include <stdint.h> * 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 <pnt239@gmail.com>
* 2017 Ruslan V. Uss <unclerus@gmail.com>
* BSD Licensed as described in the file LICENSE
*/
#include "pcf8591.h" #include "pcf8591.h"
/** #include <stddef.h>
* CAUTION: PLEASE SET LOW FREQUENCY
*/
#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 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); i2c_slave_read(dev->bus, dev->addr, &control_reg, &res, 1);
return res; 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);
}

View file

@ -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 * an 8-bit digital-to-analog conversion PCF8591
* *
* Part of esp-open-rtos * Part of esp-open-rtos
* Copyright (C) 2017 Pham Ngoc Thanh <pnt239@gmail.com> * Copyright (C) 2017 Pham Ngoc Thanh <pnt239@gmail.com>
* 2017 Ruslan V. Uss <unclerus@gmail.com>
* BSD Licensed as described in the file LICENSE * BSD Licensed as described in the file LICENSE
*/ */
#ifndef _EXTRAS_PCF8591_H_ #ifndef _EXTRAS_PCF8591_H_
#define _EXTRAS_PCF8591_H_ #define _EXTRAS_PCF8591_H_
#include <stdint.h>
#include <i2c/i2c.h> #include <i2c/i2c.h>
#ifdef __cplusplus #ifdef __cplusplus
@ -16,25 +18,33 @@ extern "C"
{ {
#endif #endif
/**
* CAUTION: PLEASE SET I2C_FREQUENCY_400K IS 'false' IN 'i2c.h' FILE
*/
#define PCF8591_DEFAULT_ADDRESS 0x48 #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. * Read input value of an analog pin.
* @param[in] addr Pointer to device * @param[in] dev Pointer to device
* @param[in] analog_pin pin number: * @param[in] conf Analog inputs configuration
* 0 - AIN0 * @param[in] channel Analog channel
* 1 - AIN1
* 2 - AIN2
* 3 - AIN3
* @return analog value * @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 #ifdef __cplusplus