Motion_EC_Stm32_archived/Middlewares/Coulomb/coulomb.h

136 lines
4.2 KiB
C

/*
* coulomb.h
*
* Created on: Apr 27, 2020
* Author: oarap
*/
#ifndef _COULOMB_H_
#define _COULOMB_H_
#include "stdbool.h"
#include "i2c.h"
#include "stm32f1xx_hal.h"
#define COULOMB_REG_NUM 0x18
#define COULOMB_ADDR (0x64 << 1) //device address
//#define COULOMB_WRITE_CMD 0x00 //coulomb write command
//#define COULOMB_READ_CMD 0x01 //coulomb read command
#define LTC2943_STATUS_REG 0x00
#define LTC2943_CONTROL_REG 0x01
#define LTC2943_ACCUM_CHARGE_MSB_REG 0x02
#define LTC2943_ACCUM_CHARGE_LSB_REG 0x03
#define LTC2943_CHARGE_THRESH_HIGH_MSB_REG 0x04
#define LTC2943_CHARGE_THRESH_HIGH_LSB_REG 0x05
#define LTC2943_CHARGE_THRESH_LOW_MSB_REG 0x06
#define LTC2943_CHARGE_THRESH_LOW_LSB_REG 0x07
#define LTC2943_VOLTAGE_MSB_REG 0x08
#define LTC2943_VOLTAGE_LSB_REG 0x09
#define LTC2943_VOLTAGE_THRESH_HIGH_MSB_REG 0x0A
#define LTC2943_VOLTAGE_THRESH_HIGH_LSB_REG 0x0B
#define LTC2943_VOLTAGE_THRESH_LOW_MSB_REG 0x0C
#define LTC2943_VOLTAGE_THRESH_LOW_LSB_REG 0x0D
#define LTC2943_CURRENT_MSB_REG 0x0E
#define LTC2943_CURRENT_LSB_REG 0x0F
#define LTC2943_CURRENT_THRESH_HIGH_MSB_REG 0x10
#define LTC2943_CURRENT_THRESH_HIGH_LSB_REG 0x11
#define LTC2943_CURRENT_THRESH_LOW_MSB_REG 0x12
#define LTC2943_CURRENT_THRESH_LOW_LSB_REG 0x13
#define LTC2943_TEMPERATURE_MSB_REG 0x14
#define LTC2943_TEMPERATURE_LSB_REG 0x15
#define LTC2943_TEMPERATURE_THRESH_HIGH_REG 0x16
#define LTC2943_TEMPERATURE_THRESH_LOW_REG 0x17
typedef struct
{//原始数据(寄存器数据)
uint8_t status;
uint8_t control;
uint16_t accumulated_charge; //剩余电量
uint16_t charge_threshold_H; //充电阈值上限
uint16_t charge_threshold_L; //充电阈值下限
uint16_t voltage; //电压
uint16_t voltage_threshold_H; //电压阈值上限
uint16_t voltage_threshold_L; //电压阈值下限
uint16_t current; //电流
uint16_t current_threshold_H; //电流阈值上限
uint16_t current_threshold_L; //电流阈值下限
uint16_t temperature; //温度
uint16_t temperature_threshold_H; //温度阈值上限(只有高字节有效)
uint16_t temperature_threshold_L; //温度阈值下限(只有高字节有效)
}ltc2943_raw_t;
typedef struct
{//实际数据(转换后,物理量纲)
struct
{
bool current_alret;
bool accumulated_charge_alert;
bool temperature_alret;
bool charge_alert_H;
bool charge_alert_L;
bool voltage_alert;
bool uvlo_alert;
}status;
struct
{
uint8_t adc_mode;
uint8_t prescaler;
uint8_t alcc_configure;
bool shutdown;
}control;
float accumulated_charge; //Ah 剩余容量
float charge_threshold_H; //Ah 充电阈值上限
float charge_threshold_L; //Ah 充电阈值下限
float voltage; //V 电压
float voltage_threshold_H; //V 电压阈值上限
float voltage_threshold_L; //V 电压阈值下限
float current; //A 电流
float current_threshold_H; //A 电流阈值上限
float current_threshold_L; //A 电流阈值下限
float temperature; //℃ 温度
float temperature_threshold_H; //℃ 温度阈值上限
float temperature_threshold_L; //℃ 温度阈值下限
}ltc2943_actual_t;
typedef struct
{
ltc2943_raw_t raw;
ltc2943_actual_t actual;
}ltc2943_t;
typedef struct
{
bool ready; //设备是否就绪
ltc2943_t write;
ltc2943_t read;
}coulomb_t;
extern coulomb_t coulomb;
//静态方法
static HAL_StatusTypeDef is_coulomb_ready();
static HAL_StatusTypeDef coulomb_write_reg(uint8_t addr,uint8_t* pData);
static HAL_StatusTypeDef coulomb_read_reg(uint8_t addr,uint8_t* pData);
static uint16_t uint8_to_uint16(uint8_t *pData,uint8_t msbIndex,uint8_t lsbIndex);
static uint8_t uint16_to_uint8_msb(uint16_t data);
//写配置方法
void coulomb_write_config_init();
void coulomb_write_config_actual_to_raw();
bool coulomb_write_config();
//读寄存器方法
bool coulomb_read_status_and_config();
void coulomb_read_status_raw_to_actual();
void coulomb_read_config_raw_to_actual();
bool coulomb_read_alcc();
#endif /* _COULOMB_H_ */