From 2ebb9c7df11694e50c4113821f53ce7880f832f8 Mon Sep 17 00:00:00 2001 From: lilian Date: Sun, 11 Dec 2016 20:42:38 +0100 Subject: [PATCH] Initial --- extras/ina3221/component.mk | 9 ++ extras/ina3221/ina3221.c | 156 ++++++++++++++++++++++++ extras/ina3221/ina3221.h | 233 ++++++++++++++++++++++++++++++++++++ 3 files changed, 398 insertions(+) create mode 100644 extras/ina3221/component.mk create mode 100644 extras/ina3221/ina3221.c create mode 100644 extras/ina3221/ina3221.h diff --git a/extras/ina3221/component.mk b/extras/ina3221/component.mk new file mode 100644 index 0000000..9f39aef --- /dev/null +++ b/extras/ina3221/component.mk @@ -0,0 +1,9 @@ +# Component makefile for extras/ina3221 + +# expected anyone using this driver includes it as 'ina3221/ina3221.h' +INC_DIRS += $(ina3221_ROOT).. + +# args for passing into compile rule generation +ina3221_SRC_DIR = $(ina3221_ROOT) + +$(eval $(call component_compile_rules,ina3221)) diff --git a/extras/ina3221/ina3221.c b/extras/ina3221/ina3221.c new file mode 100644 index 0000000..677ef39 --- /dev/null +++ b/extras/ina3221/ina3221.c @@ -0,0 +1,156 @@ +/* + * ina3221.c + * + * Created on: 4 oct. 2016 + * Author: lilian + */ + +#include "ina3221.h" + +#ifdef INA3221_DEBUG +#define debug(fmt, ...) printf("%s: " fmt "\n", "SSD1306", ## __VA_ARGS__) +#else +#define debug(fmt, ...) +#endif + +static int _wireWriteRegister (uint8_t addr, uint8_t reg, uint16_t value) +{ + i2c_start(); + if (!i2c_write(addr<<1)) // adress + W + goto error ; + if (!i2c_write(reg)) + goto error; + if (!i2c_write((value >> 8) & 0xFF)) + goto error; + if (!i2c_write(value & 0xFF)) + goto error ; + i2c_stop(); + + return 0 ; + + error: + debug("Error while xmitting I2C slave INA3221\n"); + i2c_stop(); + return -EIO ; +} + +static int _wireReadRegister(uint8_t addr, uint8_t reg, uint16_t *value) +{ + uint8_t* tampon = (uint8_t*)&value ; + + i2c_start(); + if (!i2c_write(addr<<1)) // adress + W + goto error ; + if (!i2c_write(reg)) + goto error; + i2c_stop(); + i2c_start(); // restart condition + if (!i2c_write((addr<<1) | 1)) // adress + R + goto error; + tampon[0] = i2c_read(0); + tampon[1] = i2c_read(1); + i2c_stop(); + + //*value = (tampon[1] << 8) | tampon[0] ; + + return 0 ; + + error: + debug("Error while xmitting I2C slave INA3221\n"); + i2c_stop(); + return -EIO ; +} + +int ina3221_trigger(ina3221_t *dev) +{ + return _wireReadRegister(dev->addr, INA3221_REG_MASK, dev->config->mask_register); +} + +int ina3221_sync(ina3221_t *dev) +{ + return _wireReadRegister(dev->addr, INA3221_REG_CONFIG, dev->config->config_register); +} + +int ina3221_setting(ina3221_t *dev ,bool mode, bool bus, bool shunt) +{ + dev->config.mode = mode ; + dev->config.ebus = bus ; + dev->config.esht = shunt ; + return _wireWriteRegister(dev->addr, INA3221_REG_CONFIG, dev->config.config_register); +} + +int ina3221_enableChannel(ina3221_t *dev ,bool ch1, bool ch2, bool ch3) +{ + dev->config.ch1 = ch1 ; + dev->config.ch2 = ch2 ; + dev->config.ch3 = ch3 ; + return _wireWriteRegister(dev->addr, INA3221_REG_CONFIG, dev->config.config_register); +} + +int ina3221_setAverage(ina3221_t *dev, ina3221_avg_t avg) +{ + dev->config.avg = avg ; + return _wireWriteRegister(dev->addr, INA3221_REG_CONFIG, dev->config.config_register); +} + +int ina3221_setBusConversionTime(ina3221_t *dev,ina3221_ct_t ct) +{ + dev->config.vbus = ct ; + return _wireWriteRegister(dev->addr, INA3221_REG_CONFIG, dev->config.config_register); +} + +int ina3221_setShuntConversionTime(ina3221_t *dev,ina3221_ct_t ct) +{ + dev->config.vsht = ct ; + return _wireWriteRegister(dev->addr, INA3221_REG_CONFIG, dev->config.config_register); +} + +int ina3221_reset(ina3221_t *dev) +{ + dev->config.config_register = INA3221_DEFAULT_CONFIG ; //internal lib reset + dev->config.reset = 1 ; + return _wireWriteRegister(dev->addr, INA3221_REG_CONFIG, dev->config.config_register); // send reset to device +} + +static int _get_raw(uint8_t addr, uint8_t reg, ina3221_channel_t channel, uint16_t *raw) +{ + return _wireReadRegister(addr,reg+channel*2, raw); +} + +int ina3221_getBusVoltage(ina3221_t *dev, ina3221_channel_t channel, uint32_t *voltage) +{ + uint16_t raw_value ; + int err = 0 ; + if ((err = _get_raw(dev->addr,INA3221_REG_BUSVOLTAGE_1, channel, &raw_value))) + return err; + *voltage = (raw_value>>3)*8 ; //mV + return 0; +} + +int ina3221_getShuntValue(ina3221_t *dev, ina3221_channel_t channel, uint32_t *voltage, uint32_t *current) +{ + uint16_t raw_value ; + int err = 0 ; + if ((err = _get_raw(dev->addr,INA3221_REG_SHUNTVOLTAGE_1, channel, &raw_value))) + return err; + *voltage = (raw_value>>3)*40 ; //uV + *current = *voltage/dev->shunt[channel] ; //mA = uV / mOhm + return 0; +} + +static int _set_raw(uint8_t addr, uint8_t reg, ina3221_channel_t channel, uint16_t raw) +{ + return _wireWriteRegister(addr,reg+channel*2, raw); +} + +int ina3221_setCriticalAlert(ina3221_t *dev, ina3221_channel_t channel, uint32_t current) +{ + uint16_t raw_value = (current*dev->shunt[channel]/40)<<3 ; + return _set_raw(dev->addr,INA3221_REG_CRITICAL_ALERT_1, channel, raw_value) ; +} + +int ina3221_setWarningAlert(ina3221_t *dev, ina3221_channel_t channel, uint32_t current) +{ + uint16_t raw_value = (current*dev->shunt[channel]/40)<<3 ; + return _set_raw(dev->addr,INA3221_REG_WARNING_ALERT_1, channel, raw_value) ; +} diff --git a/extras/ina3221/ina3221.h b/extras/ina3221/ina3221.h new file mode 100644 index 0000000..384c86c --- /dev/null +++ b/extras/ina3221/ina3221.h @@ -0,0 +1,233 @@ +/* + * ina3221.h + * + * Created on: 4 oct. 2016 + * Author: lilian + */ + +#ifndef INA3221_H_ +#define INA3221_H_ + +#include "espressif/esp_common.h" +#include "esp8266.h" +#include "i2c/i2c.h" + +#define INA3221_ADDR_0 (0x40) // A0 to GND +#define INA3221_ADDR_1 (0x41) // A0 to Vs+ +#define INA3221_ADDR_2 (0x42) // A0 to SDA +#define INA3221_ADDR_3 (0x43) // A0 to SCL + +#define BUS_NUMBER 3 //Number of shunt available + +#define INA3221_REG_CONFIG (0x00) +#define INA3221_REG_SHUNTVOLTAGE_1 (0x01) +#define INA3221_REG_BUSVOLTAGE_1 (0x02) +#define INA3221_REG_CRITICAL_ALERT_1 (0x07) +#define INA3221_REG_WARNING_ALERT_1 (0x08) +#define INA3221_REG_SHUNT_VOLTAGE_SUM (0x0D) +#define INA3221_REG_SHUNT_VOLTAGE_SUM_LIMIT (0x0E) +#define INA3221_REG_MASK (0x0F) +#define INA3221_REG_VALID_POWER_UPPER_LIMIT (0x10) +#define INA3221_REG_VALID_POWER_LOWER_LIMIT (0x11) + + +#define INA3221_DEFAULT_CONFIG (0x7127) //Default register after reset +#define INA3221_DEFAULT_POWER_UPPER_LIMIT (0x2710) //10V +#define INA3221_DEFAULT_POWER_LOWER_LIMIT (0x2328) //9V +/* + * Numbrer of samples + */ +typedef enum { + INA3221_AVG_1 = 0, //Default + INA3221_AVG_4, + INA3221_AVG_16, + INA3221_AVG_64, + INA3221_AVG_128, + INA3221_AVG_256, + INA3221_AVG_512, + INA3221_AVG_1024, +} ina3221_avg_t; + +/* + * Channel selection list + */ +typedef enum { + CHANNEL_1 = 0, + CHANNEL_2, + CHANNEL_3, +} ina3221_channel_t; + +/* + * Conversion time in us + */ +typedef enum { + INA3221_CT_140 = 0, + INA3221_CT_204, + INA3221_CT_332, + INA3221_CT_588, + INA3221_CT_1100, //Default + INA3221_CT_2116, + INA3221_CT_4156, + INA3221_CT_8244, +} ina3221_ct_t ; + +/* + * Config description register + */ +typedef union +{ + struct { + uint8_t reset : 1 ; //Set this bit to 1 to reset device + uint8_t ch1 : 1 ; // Enable/Disable channel 1 + uint8_t ch2 : 1 ; // Enable/Disable channel 2 + uint8_t ch3 : 1 ; // Enable/Disable channel 3 + uint8_t avg : 3 ; // number of sample collected and averaged together + uint8_t vbus : 3 ; // Bus voltage conversion time + uint8_t vsht : 3 ; // Shunt voltage conversion time + uint8_t mode : 1 ; // Single shot measure or continious mode + uint8_t ebus : 1 ; // Enable/Disable bus measure + uint8_t esht : 1 ; // Enable/Disable shunt measure + }; + uint16_t config_register ; +} ina3221_config_t ; + + +/* + * Mask/enable description register + */ +typedef union +{ + struct { + uint8_t : 1 ; //Reserved + uint8_t scc1 : 1 ; // channel 1 sum (1:enable) + uint8_t scc2 : 1 ; // channel 2 sum (1:enable) + uint8_t scc3 : 1 ; // channel 2 sum (1:enable) + uint8_t wen : 1 ; // Warning alert latch (1:enable) + uint8_t cen : 1 ; // Critical alert latch (1:enable) + uint8_t cf1 : 1 ; // Critical alert flag (Read mask to clear) + uint8_t cf2 : 1 ; // Critical alert flag (Read mask to clear) + uint8_t cf3 : 1 ; // Critical alert flag (Read mask to clear) + uint8_t sf : 1 ; // Sum alert flag (Read mask to clear) + uint8_t wf1 : 1 ; // Warning alert flag (Read mask to clear) + uint8_t wf2 : 1 ; // Warning alert flag (Read mask to clear) + uint8_t wf3 : 1 ; // Warning alert flag (Read mask to clear) + uint8_t pvf : 1 ; // Power valid flag + uint8_t tcf : 1 ; // Timing control flag + uint8_t cvrf : 1 ; // Conversion ready flag (1: ready) + }; + uint16_t mask_register ; +} ina3221_mask_t ; + +/* + * Device description + */ +typedef struct { + uint8_t addr ; // ina3221 I2C address + ina3221_config_t config ; //Memory of ina3221 config + ina3221_mask_t mask ; //Memory of mask_config + uint16_t shunt[BUS_NUMBER] ; //Memory of shunt value (mOhm) +} ina3221_t; + +/** + * get config register from the device + * @param dev Pointer to device descriptor + * @return Non-zero if error occured + */ +int ina3221_sync(ina3221_t *dev); + +/** + * get mask register from the device (trig measurement if single-shot mode) + * @param dev Pointer to device descriptor + * @return Non-zero if error occured + */ +int ina3221_trigger(ina3221_t *dev); + +/** + * Set options for bus and shunt + * @param dev Pointer to device descriptor + * @param mode Selection of measurement (true :continuous // false:single-shot) + * @param bus Enable/Disable bus measures + * @param shunt Enable/Disable shunt measures + * @return Non-zero if error occured + */ +int ina3221_setting(ina3221_t *dev ,bool mode, bool bus, bool shunt); + +/** + * Select channel + * @param dev Pointer to device descriptor + * @param ch1 Enable/Disable channel 1 ( true : enable // false : disable ) + * @param ch2 Enable/Disable channel 2 ( true : enable // false : disable ) + * @param ch3 Enable/Disable channel 3 ( true : enable // false : disable ) + * @return Non-zero if error occured + */ +int ina3221_enableChannel(ina3221_t *dev ,bool ch1, bool ch2, bool ch3); + +/** + * Set average ( number(s) of point measured ) + * @param dev Pointer to device descriptor + * @param avg Value of average selection + * @return Non-zero if error occured + */ +int ina3221_setAverage(ina3221_t *dev, ina3221_avg_t avg); + +/** + * Set conversion time ( time for a measurement ) for bus. + * @param dev Pointer to device descriptor + * @param ct Value of conversion time selection + * @return Non-zero if error occured + */ +int ina3221_setBusConversionTime(ina3221_t *dev,ina3221_ct_t ct); + +/** + * Set conversion time ( time for a measurement ) for shunt. + * @param dev Pointer to device descriptor + * @param ct Value of conversion time selection + * @return Non-zero if error occured + */ +int ina3221_setShuntConversionTime(ina3221_t *dev,ina3221_ct_t ct); + +/** + * Reset device and config like POR (Power-On-Reset) + * @param dev Pointer to device descriptor + * @return Non-zero if error occured + */ +int ina3221_reset(ina3221_t *dev); + +/** + * Get Bus voltage (mV) + * @param dev Pointer to device descriptor + * @param channel Select channel value to get + * @param voltage Data pointer to get bus voltage (mV) + * @return Non-zero if error occured + */ +int ina3221_getBusVoltage(ina3221_t *dev, ina3221_channel_t channel, uint32_t *voltage); + +/** + * Get Shunt voltage (uV) and current (mA) + * @param dev Pointer to device descriptor + * @param channel Select channel value to get + * @param voltage Data pointer to get shunt voltage (uV) + * @param current Data pointer to get shunt voltage (mA) + * @return Non-zero if error occured + */ +int ina3221_getShuntValue(ina3221_t *dev, ina3221_channel_t channel, uint32_t *voltage, uint32_t *current); + +/** + * Set Critical alert (when measurement(s) is greater that value set ) + * @param dev Pointer to device descriptor + * @param channel Select channel value to set + * @param current Value to set (mA) + * @return Non-zero if error occured + */ +int ina3221_setCriticalAlert(ina3221_t *dev, ina3221_channel_t channel, uint32_t current); + +/** + * Set Warning alert (when average measurement(s) is greater that value set ) + * @param dev Pointer to device descriptor + * @param channel Select channel value to set + * @param current Value to set (mA) + * @return Non-zero if error occured + */ +int ina3221_setWarningAlert(ina3221_t *dev, ina3221_channel_t channel, uint32_t current); + +#endif /* INA3221_H_ */