Rework struct, fix trigger system, complete sync, add licence, fixes...

This commit is contained in:
lilian 2016-12-29 09:00:33 +01:00
parent 4a696fa07e
commit 70e2158102
4 changed files with 81 additions and 35 deletions

View file

@ -1,8 +1,11 @@
/*
* ina3221.c
/**
* INA3221 driver for esp-open-rtos.
*
* Created on: 4 oct. 2016
* Author: lilian
* Copyright (c) 2016 Zaltora (https://github.com/Zaltora)
*
* MIT Licensed as described in the file LICENSE
*
* @todo Interupt system for critical and warning alert pin
*/
#include "ina3221.h"
@ -25,6 +28,7 @@ static int _wireWriteRegister (uint8_t addr, uint8_t reg, uint16_t value)
if (!i2c_write(value & 0xFF))
goto error;
i2c_stop();
debug("Data write to %02X : %02X+%04X\n",addr,reg,value);
return 0 ;
@ -36,7 +40,7 @@ static int _wireWriteRegister (uint8_t addr, uint8_t reg, uint16_t value)
static int _wireReadRegister(uint8_t addr, uint8_t reg, uint16_t *value)
{
uint8_t* tampon = (uint8_t*)&value;
uint8_t tampon[2] = { 0 } ;
i2c_start();
if (!i2c_write(addr<<1)) // adress + W
@ -50,6 +54,9 @@ static int _wireReadRegister(uint8_t addr, uint8_t reg, uint16_t *value)
tampon[1] = i2c_read(0);
tampon[0] = i2c_read(1);
i2c_stop();
*value = tampon[1]<<8 | tampon[0] ;
debug("Data read from %02X: %02X+%04X\n",addr,reg,*value);
return 0;
error:
@ -59,20 +66,33 @@ static int _wireReadRegister(uint8_t addr, uint8_t reg, uint16_t *value)
}
int ina3221_trigger(ina3221_t *dev)
{
return _wireWriteRegister(dev->addr, INA3221_REG_CONFIG, dev->config.config_register);
}
int ina3221_getStatus(ina3221_t *dev)
{
return _wireReadRegister(dev->addr, INA3221_REG_MASK, &dev->mask.mask_register);
}
int ina3221_sync(ina3221_t *dev)
{
uint16_t ptr_config ;
uint16_t ptr_data;
int err = 0;
if ((err = _wireReadRegister(dev->addr, INA3221_REG_CONFIG, &ptr_config))) // Read config
//////////////////////// Sync config register
if ((err = _wireReadRegister(dev->addr, INA3221_REG_CONFIG, &ptr_data))) // Read config
return err;
if( ptr_config != dev->config.config_register) {
if( ptr_data != dev->config.config_register) {
if ((err = _wireWriteRegister(dev->addr, INA3221_REG_CONFIG, dev->config.config_register))) // Update config
return err;
}
//////////////////////// Sync mask register config
if ((err = _wireReadRegister(dev->addr, INA3221_REG_MASK, &ptr_data))) // Read mask
return err;
if( (ptr_data & INA3221_MASK_CONFIG) != (dev->mask.mask_register & INA3221_MASK_CONFIG)) {
if ((err = _wireWriteRegister(dev->addr, INA3221_REG_MASK, dev->mask.mask_register & INA3221_MASK_CONFIG))) // Update config
return err;
}
return 0;
}
@ -97,14 +117,14 @@ int ina3221_enableChannelSum(ina3221_t *dev ,bool ch1, bool ch2, bool ch3)
dev->mask.scc1 = ch1;
dev->mask.scc2 = ch2;
dev->mask.scc3 = ch3;
return _wireWriteRegister(dev->addr, INA3221_REG_MASK, dev->mask.mask_register);
return _wireWriteRegister(dev->addr, INA3221_REG_MASK, dev->mask.mask_register & INA3221_MASK_CONFIG);
}
int ina3221_enableLatchPin(ina3221_t *dev ,bool warning, bool critical)
{
dev->mask.wen = warning;
dev->mask.cen = critical;
return _wireWriteRegister(dev->addr, INA3221_REG_MASK, dev->mask.mask_register);
return _wireWriteRegister(dev->addr, INA3221_REG_MASK, dev->mask.mask_register & INA3221_MASK_CONFIG);
}
int ina3221_setAverage(ina3221_t *dev, ina3221_avg_t avg)

View file

@ -1,14 +1,17 @@
/*
* ina3221.h
/**
* INA3221 driver for esp-open-rtos.
*
* Copyright (c) 2016 Zaltora (https://github.com/Zaltora)
*
* MIT Licensed as described in the file LICENSE
*
* Created on: 4 oct. 2016
* Author: lilian
*/
#ifndef INA3221_H_
#define INA3221_H_
#include <errno.h>
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
@ -42,6 +45,7 @@ extern "C" {
#define INA3221_DEFAULT_POWER_UPPER_LIMIT (0x2710) //10V
#define INA3221_DEFAULT_POWER_LOWER_LIMIT (0x2328) //9V
#define INA3221_MASK_CONFIG (0x7C00)
/*
* Numbrer of samples
*/
@ -109,16 +113,12 @@ typedef union
uint16_t cvrf : 1 ; // Conversion ready flag (1: ready) // LSB
uint16_t tcf : 1 ; // Timing control flag
uint16_t pvf : 1 ; // Power valid flag
uint16_t wf3 : 1 ; // Warning alert flag (Read mask to clear)
uint16_t wf2 : 1 ; // Warning alert flag (Read mask to clear)
uint16_t wf1 : 1 ; // Warning alert flag (Read mask to clear)
uint16_t wf : 3 ; // Warning alert flag (Read mask to clear) (order : Channel1:channel2:channel3)
uint16_t sf : 1 ; // Sum alert flag (Read mask to clear)
uint16_t cf3 : 1 ; // Critical alert flag (Read mask to clear)
uint16_t cf2 : 1 ; // Critical alert flag (Read mask to clear)
uint16_t cf1 : 1 ; // Critical alert flag (Read mask to clear)
uint16_t cf : 3 ; // Critical alert flag (Read mask to clear) (order : Channel1:channel2:channel3)
uint16_t cen : 1 ; // Critical alert latch (1:enable)
uint16_t wen : 1 ; // Warning alert latch (1:enable)
uint16_t scc3 : 1 ; // channel 2 sum (1:enable)
uint16_t scc3 : 1 ; // channel 3 sum (1:enable)
uint16_t scc2 : 1 ; // channel 2 sum (1:enable)
uint16_t scc1 : 1 ; // channel 1 sum (1:enable)
uint16_t : 1 ; //Reserved //MSB
@ -137,19 +137,26 @@ typedef struct {
} ina3221_t;
/**
* sync internal config buffer with external device register
* sync internal config buffer and mask with external device register ( When struct is manually set )
* @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)
* send current config register to trig a measurement in single-shot mode
* @param dev Pointer to device descriptor
* @return Non-zero if error occured
*/
int ina3221_trigger(ina3221_t *dev);
/**
* get mask register from the device ( Used to read flags )
* @param dev Pointer to device descriptor
* @return Non-zero if error occured
*/
int ina3221_getStatus(ina3221_t *dev);
/**
* Set options for bus and shunt
* @param dev Pointer to device descriptor