diff --git a/.cproject b/.cproject index d158aa2..70cebba 100644 --- a/.cproject +++ b/.cproject @@ -53,6 +53,7 @@ + diff --git a/.settings/org.eclipse.ltk.core.refactoring.prefs b/.settings/org.eclipse.ltk.core.refactoring.prefs new file mode 100644 index 0000000..b196c64 --- /dev/null +++ b/.settings/org.eclipse.ltk.core.refactoring.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +org.eclipse.ltk.core.refactoring.enable.project.refactoring.history=false diff --git a/Core/Src/freertos.c b/Core/Src/freertos.c index 2a7df6c..9c9b0a0 100644 --- a/Core/Src/freertos.c +++ b/Core/Src/freertos.c @@ -320,8 +320,11 @@ void StartCoulombRead(void *argument) /* Infinite loop */ for(;;) { - IIC - osDelay(10); + coulomb_read_status(); + if(NaviKit.system_runing) + osDelay(500); + else + osDelay(5000); } /* USER CODE END StartCoulombRead */ } diff --git a/Middlewares/Coulomb/coulomb.c b/Middlewares/Coulomb/coulomb.c index a2870a6..250d2c1 100644 --- a/Middlewares/Coulomb/coulomb.c +++ b/Middlewares/Coulomb/coulomb.c @@ -1,21 +1,73 @@ +/* + * @Description: file content + * @Author: CK.Zh + * @Date: 2020-04-27 18:01:58 + * @LastEditors: CK.Zh + * @LastEditTime: 2020-04-28 16:42:03 + */ #include "coulomb.h" -#include "i2c.h" +coulomb_t coulomb; +//bool is_coulomb_ready() +//{ +//// HAL_I2C_IsDeviceReady(hi2c1,COULOMB_ADDR); +//} -bool is_coulomb_ready() +static HAL_StatusTypeDef coulomb_write_reg(uint8_t addr,uint8_t* pData) { -// HAL_I2C_IsDeviceReady(hi2c1,COULOMB_ADDR); + return HAL_I2C_Mem_Write(&hi2c1,COULOMB_ADDR ,addr,1,pData, 1, 10); } -void coulomb_write_reg(uint8_t reg_addr,uint8_t *data) +static uint16_t coulomb_uint8_to_uint16(uint8_t *pData,uint8_t msb,uint8_t lsb) { - HAL_I2C_Master_Transmit(&hi2c1,((COULOMB_ADDR<<1) | COULOMB_WRITE_CMD), data, 1, 100); + return ((pData[msb]<<8) | pData[lsb]); } -void coulomb_read_reg(uint8_t reg_addr,uint8_t *data) +HAL_StatusTypeDef coulomb_read_status() { - HAL_I2C_Master_Receive(&hi2c1,((COULOMB_ADDR<<1) | COULOMB_READ_CMD), data, 1, 100); -} + uint8_t ctl_reg_set = 0xEc; + coulomb_write_reg(LTC2943_CONTROL_REG,&ctl_reg_set); + uint8_t pData[0x18]; + HAL_StatusTypeDef ret; -bool coulomb_read_alcc() -{ - return HAL_GPIO_ReadPin(COULOMB_ALCC_GPIO_Port,COULOMB_ALCC_Pin); + HAL_I2C_Mem_Read(&hi2c1,COULOMB_ADDR ,0,1,&pData, 1, 10); + ret = HAL_I2C_Mem_Read(&hi2c1,COULOMB_ADDR ,0,0x18,&pData[1], 0x18, 100); + + if(ret == HAL_OK ) + { + coulomb.raw.status = pData[LTC2943_STATUS_REG]; + coulomb.raw.control = pData[LTC2943_CONTROL_REG]; + + coulomb.raw.accumulated_charge =coulomb_uint8_to_uint16(pData,LTC2943_ACCUM_CHARGE_MSB_REG,LTC2943_ACCUM_CHARGE_LSB_REG); + coulomb.raw.charge_threshold_H = coulomb_uint8_to_uint16(pData,LTC2943_CHARGE_THRESH_HIGH_MSB_REG,LTC2943_CHARGE_THRESH_HIGH_LSB_REG); + coulomb.raw.charge_threshold_L = coulomb_uint8_to_uint16(pData,LTC2943_CHARGE_THRESH_LOW_MSB_REG,LTC2943_CHARGE_THRESH_LOW_LSB_REG); + + coulomb.raw.voltage = coulomb_uint8_to_uint16(pData,LTC2943_VOLTAGE_MSB_REG,LTC2943_VOLTAGE_LSB_REG); + coulomb.raw.voltage_threshold_H = coulomb_uint8_to_uint16(pData,LTC2943_VOLTAGE_THRESH_HIGH_MSB_REG,LTC2943_VOLTAGE_THRESH_HIGH_LSB_REG); + coulomb.raw.voltage_threshold_L = coulomb_uint8_to_uint16(pData,LTC2943_VOLTAGE_THRESH_LOW_MSB_REG,LTC2943_VOLTAGE_THRESH_LOW_LSB_REG); + + coulomb.raw.current = coulomb_uint8_to_uint16(pData,LTC2943_CURRENT_MSB_REG,LTC2943_CURRENT_LSB_REG); + coulomb.raw.current_threshold_H = coulomb_uint8_to_uint16(pData,LTC2943_CURRENT_THRESH_HIGH_MSB_REG,LTC2943_CURRENT_THRESH_HIGH_LSB_REG); + coulomb.raw.current_threshold_L = coulomb_uint8_to_uint16(pData,LTC2943_CURRENT_THRESH_LOW_MSB_REG,LTC2943_CURRENT_THRESH_LOW_LSB_REG); + + coulomb.raw.temperature = coulomb_uint8_to_uint16(pData,LTC2943_TEMPERATURE_MSB_REG,LTC2943_TEMPERATURE_LSB_REG); + coulomb.raw.temperature_threshold_H = pData[LTC2943_TEMPERATURE_THRESH_HIGH_REG]; + coulomb.raw.temperature_threshold_L = pData[LTC2943_TEMPERATURE_THRESH_LOW_REG]; + + coulomb.actual.status.current_alret = ((coulomb.raw.status>>6) & 0x01) ? true : false; + coulomb.actual.status.accumulated_charge_alert = ((coulomb.raw.status>>5) & 0x01) ? true : false; + coulomb.actual.status.temperature_alret = ((coulomb.raw.status>>4) & 0x01) ? true : false; + coulomb.actual.status.charge_alert_H = ((coulomb.raw.status>>3) & 0x01) ? true : false; + coulomb.actual.status.charge_alert_L = ((coulomb.raw.status>>2) & 0x01) ? true : false; + coulomb.actual.status.voltage_alert = ((coulomb.raw.status>>1) & 0x01) ? true : false; + coulomb.actual.status.uvlo_alert = ( coulomb.raw.status & 0x01) ? true : false; + + coulomb.actual.voltage = coulomb.raw.voltage / 65535.0 *23.6; //电压转换 + coulomb.actual.current = (coulomb.raw.current - 32767) / 32767.0 * 30 ; //电流转换 +// coulomb.actual.temperature = coulomb.raw.temperature / 65535 *23.6; //温度转换 + } + return ret; } +// +//bool coulomb_read_alcc() +//{ +// return HAL_GPIO_ReadPin(COULOMB_ALCC_GPIO_Port,COULOMB_ALCC_Pin); +//} diff --git a/Middlewares/Coulomb/coulomb.h b/Middlewares/Coulomb/coulomb.h index 1ee16aa..a1cee81 100644 --- a/Middlewares/Coulomb/coulomb.h +++ b/Middlewares/Coulomb/coulomb.h @@ -9,16 +9,96 @@ #define _COULOMB_H_ #include "stdbool.h" +#include "i2c.h" +#include "stm32f1xx_hal.h" -#define COULOMB_ADDR 0x64 //device address -#define COULOMB_WRITE_CMD 0x00 //coulomb write command -#define COULOMB_READ_CMD 0x01 //coulomb read command +#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 -bool is_coulomb_ready(); -void coulomb_write_reg(uint8_t reg_addr,uint8_t *data); -void coulomb_read_reg(uint8_t reg_addr,uint8_t *data); +typedef struct +{ + struct + {//原始数据 + uint8_t status; + uint8_t control; -bool coulomb_read_alcc() + 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; //温度 + uint8_t temperature_threshold_H; //温度阈值上限 + uint8_t temperature_threshold_L; //温度阈值下限 + }raw; + 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; + float accumulated_charge; + float charge_threshold_H; //充电阈值上限 + float charge_threshold_L; //充电阈值下限 + + float voltage; //电压 + float voltage_threshold_H; //电压阈值上限 + float voltage_threshold_L; //电压阈值下限 + + float current; //电流 + float current_threshold_H; //电流阈值上限 + float current_threshold_L; //电流阈值下限 + + float temperature; //温度 + float temperature_threshold_H; //温度阈值上限 + float temperature_threshold_L; //温度阈值下限 + }actual; +}coulomb_t; +extern coulomb_t coulomb; + +//bool 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); +HAL_StatusTypeDef coulomb_read_status(); +//bool coulomb_read_alcc(); #endif /* _COULOMB_H_ */