|
@@ -0,0 +1,291 @@
|
|
|
+#include "sgm832.h"
|
|
|
+
|
|
|
+// 定义全局变量
|
|
|
+ u16 g_Power_bl = 0;
|
|
|
+ u16 g_Power_sys = 0;
|
|
|
+ u16 g_Bus_bl = 0;
|
|
|
+ u16 g_Bus_sys = 0;
|
|
|
+
|
|
|
+//software IIC3 to SGM832
|
|
|
+void SGM832_WriteCmd(u8 address, u16 cmd)
|
|
|
+{
|
|
|
+ //send cmd
|
|
|
+ IIC3_Send_8bitMultiBytes(SGM832_I2C_ADDR_BL, 0x00, (u8*)&cmd, sizeof(cmd));
|
|
|
+
|
|
|
+ //IIC3_Send_8bitMultiBytes(SGM832_I2C_ADDR_BL, 0x00, &cmd, 1);
|
|
|
+}
|
|
|
+
|
|
|
+u8 SGM832_WriteData(u8 address, u8 reg, u8 *data, u8 num)
|
|
|
+{
|
|
|
+ u8 ret=0;
|
|
|
+
|
|
|
+ ret = IIC3_Send_8bitMultiBytes(address, reg, data, num);
|
|
|
+
|
|
|
+ if(ret)
|
|
|
+ {
|
|
|
+ return (1);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ printf("Write SGM832 failed\n");
|
|
|
+ return (0);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+u8 SGM832_Read2Bytes(u8 address, u8 reg, u16 *data)
|
|
|
+{
|
|
|
+ //调用 IIC3_Read_8bitMultiBytes_sgm832 read 2 bytes data and print
|
|
|
+ //u16 data =0;
|
|
|
+ u8 ret=0;
|
|
|
+ ret = IIC3_Read_8bitMultiBytes_sgm832(address, reg, data, 1);
|
|
|
+
|
|
|
+ if(ret)
|
|
|
+ {
|
|
|
+ return (1);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ printf("Read SGM832 failed\n");
|
|
|
+ return (0);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/*===============================================================================================================================*
|
|
|
+* Sgm832_Calibration
|
|
|
+* @address: sgm832 i2c slaver address
|
|
|
+* @return: none
|
|
|
+* @description: Set sgm832 calibration register, CAL=0.00512/(Current_LSB*Rshunt), Current_LSB=Max_Expected_Current/(2^15),
|
|
|
+* Max_Expected_Current = 5A, Rshunt = 0.028Ω, Current_LSB=5A/(2^15) ≈ 0.00015A,
|
|
|
+* CAL=0.00512/(0.00015A*0.028Ω) ≈ 1198 = 0x04AE, so CAL = 0x04AE
|
|
|
+*=================================================================================================================================*/
|
|
|
+void Sgm832_Calibration(u8 address)
|
|
|
+{
|
|
|
+ u8 calibrate_write_data[2] = {0x04, 0xAE};
|
|
|
+ u8 ret = 0;
|
|
|
+
|
|
|
+ ret = SGM832_WriteData(address, CALIBRATION_REGISTER, calibrate_write_data, 2);
|
|
|
+
|
|
|
+ if(ret == 0)
|
|
|
+ {
|
|
|
+ printf("Calibrate SGM832 failed\n");
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void sgm832_Get_ConfigReg(u8 address)
|
|
|
+{
|
|
|
+ u16 u16_ConfigReg = 0;
|
|
|
+ u8 ret = SGM832_Read2Bytes(address, CONFIGURATION_REGISTER, &u16_ConfigReg);
|
|
|
+ if(ret)
|
|
|
+ {
|
|
|
+ printf( "ConfigReg: 0x%04x\n", u16_ConfigReg);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ printf("Read ConfigReg failed\n");
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+u16 sgm832_Get_ShuntVoltage(u8 address)
|
|
|
+{
|
|
|
+ float f_ShuntVoltage;
|
|
|
+ u16 u16_ShuntVoltage;
|
|
|
+ u8 ret = SGM832_Read2Bytes(address, SHUNT_VOLTAGE_REGISTER, &u16_ShuntVoltage);
|
|
|
+ //printf( "ShuntVoltage: 0x%04x\n", u16_ShuntVoltage);
|
|
|
+ //浮点输出 lsb: 2.5uV max:81.9175mV
|
|
|
+ if(ret)
|
|
|
+ {
|
|
|
+ if (u16_ShuntVoltage & 0x8000)
|
|
|
+ {
|
|
|
+ // Sign-extend for negative ShuntVoltage
|
|
|
+ u16_ShuntVoltage = ~u16_ShuntVoltage + 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ f_ShuntVoltage = u16_ShuntVoltage * 2.5 * 0.001;
|
|
|
+
|
|
|
+ printf( "ShuntVoltage: %.4f mV\n", f_ShuntVoltage);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ printf("Read ShuntVoltage failed\n");
|
|
|
+ }
|
|
|
+ return u16_ShuntVoltage;
|
|
|
+}
|
|
|
+
|
|
|
+u16 sgm832_Get_BusVoltage(u8 address)
|
|
|
+{
|
|
|
+ u16 u16_BusVoltage = 0;
|
|
|
+ u8 ret = SGM832_Read2Bytes(address, BUS_VOLTAGE_REGISTER, &u16_BusVoltage);
|
|
|
+
|
|
|
+ if(ret)
|
|
|
+ {
|
|
|
+ printf( "BUS_VOLTAGE_REGISTER: %d\n", u16_BusVoltage);
|
|
|
+ //浮点输出 lsb: 1.25mV max:40.96V
|
|
|
+ //u16_BusVoltage = (float)u16_BusVoltage * 1.25 * 0.001;
|
|
|
+ //printf( "BusVoltage: %d V\n", u16_BusVoltage);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ printf("Read BusVoltage failed\n");
|
|
|
+ }
|
|
|
+ return u16_BusVoltage;
|
|
|
+}
|
|
|
+
|
|
|
+void sgm832_Get_Power(u8 address)
|
|
|
+{
|
|
|
+ u16 u16_Power = 0;
|
|
|
+ u8 ret = SGM832_Read2Bytes(address, POWER_REGISTER, &u16_Power);
|
|
|
+ if(ret)
|
|
|
+ {
|
|
|
+ printf( "POWER_REGISTER: %d\n", u16_Power);
|
|
|
+ //There is a ratio of 25 between the LSB of the power and the Current_LSB
|
|
|
+ //u16_Power = (float)u16_Power * 25 * 0.00015; //W
|
|
|
+ //printf( "Power: 0x%04x\n", u16_Power);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ printf("Read Power failed\n");
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void sgm832_Get_Calibration(u8 address)
|
|
|
+{
|
|
|
+ u16 u16_Calibration = 0;
|
|
|
+ u8 ret = SGM832_Read2Bytes(address, CALIBRATION_REGISTER, &u16_Calibration);
|
|
|
+ if(ret)
|
|
|
+ {
|
|
|
+ printf( "Calibration: 0x%04x\n", u16_Calibration);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ printf("Read Calibration failed\n");
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void sgm832_Get_MaskEnable(u8 address)
|
|
|
+{
|
|
|
+ u16 u16_MaskEnable = 0;
|
|
|
+ u8 ret = SGM832_Read2Bytes(address, MASK_ENABLE_REGISTER, &u16_MaskEnable);
|
|
|
+ if(ret)
|
|
|
+ {
|
|
|
+ printf( "MaskEnable: 0x%04x\n", u16_MaskEnable);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ printf("Read MaskEnable failed\n");
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void sgm832_Get_AlertLimit(u8 address)
|
|
|
+{
|
|
|
+ u16 u16_AlertLimit = 0;
|
|
|
+ u8 ret = SGM832_Read2Bytes(address, ALERT_LIMIT_REGISTER, &u16_AlertLimit);
|
|
|
+ if(ret)
|
|
|
+ {
|
|
|
+ printf( "AlertLimit: 0x%04x\n", u16_AlertLimit);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ printf("Read AlertLimit failed\n");
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void sgm832_Get_Current(u8 address)
|
|
|
+{
|
|
|
+ u16 u16_Current = 0;
|
|
|
+ u8 ret = SGM832_Read2Bytes(address, CURRENT_REGISTER, &u16_Current);
|
|
|
+ if(ret)
|
|
|
+ {
|
|
|
+ if (u16_Current & 0x8000)
|
|
|
+ {
|
|
|
+ // Sign-extend for negative Current
|
|
|
+ u16_Current = ~u16_Current + 1;
|
|
|
+ }
|
|
|
+ printf( "CURRENT_REGISTER: %d\n", u16_Current);
|
|
|
+ //Current_LSB=Max_Expected_Current/(2^15),
|
|
|
+ //Max_Expected_Current = 5A,
|
|
|
+ //Current_LSB=5A/(2^15) ≈ 0.00015A
|
|
|
+ //u16_Current = (float)u16_Current * 0.00015;
|
|
|
+ //printf( "Current: 0x%04x\n", u16_Current);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ printf("Read Current failed\n");
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void sgm832_Get_DieID(u8 address)
|
|
|
+{
|
|
|
+ u16 u16_DieID = 0;
|
|
|
+ u8 ret = SGM832_Read2Bytes(address, DIE_ID_REGISTER, &u16_DieID);
|
|
|
+ if(ret)
|
|
|
+ {
|
|
|
+ printf( "DieID: 0x%04x\n", u16_DieID);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ printf("Read DieID failed\n");
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void sgm832_Get_ManufacturerID(u8 address)
|
|
|
+{
|
|
|
+ u16 u16_ManufacturerID = 0;
|
|
|
+ u8 ret = SGM832_Read2Bytes(address, MANUFACTURER_ID_REGISTER, &u16_ManufacturerID);
|
|
|
+ if(ret)
|
|
|
+ {
|
|
|
+ printf( "ManufacturerID: 0x%04x\n", u16_ManufacturerID);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ printf("Read ManufacturerID failed\n");
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void sgm832_Power_detect_Proc_Bl(void)
|
|
|
+{
|
|
|
+ //u16 rBuf_bl = 0;
|
|
|
+
|
|
|
+ //printf("\n");
|
|
|
+ //printf("[Power]:back light\n");
|
|
|
+ //rBuf_bl = sgm832_Get_ShuntVoltage(SGM832_I2C_ADDR_BL);
|
|
|
+ //g_Power_bl = rBuf_bl;
|
|
|
+ //SendUart5DataPacket(0x20, (u8*)&rBuf_bl, sizeof(rBuf_bl));
|
|
|
+
|
|
|
+ sgm832_Get_BusVoltage(SGM832_I2C_ADDR_BL);
|
|
|
+ //rBuf_bl = sgm832_Get_BusVoltage(SGM832_I2C_ADDR_BL);
|
|
|
+ //g_Bus_bl = rBuf_bl;
|
|
|
+ //SendUart5DataPacket(0x30, (u8*)&rBuf_bl, sizeof(rBuf_bl));
|
|
|
+
|
|
|
+ sgm832_Get_Power(SGM832_I2C_ADDR_BL);
|
|
|
+
|
|
|
+ sgm832_Get_Current(SGM832_I2C_ADDR_BL);
|
|
|
+
|
|
|
+ //sgm832_Get_ConfigReg();
|
|
|
+ //sgm832_Get_ManufacturerID();
|
|
|
+ //sgm832_Get_DieID();
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+void sgm832_Power_detect_Proc_Sys(void)
|
|
|
+{
|
|
|
+ //u16 rBuf_sys = 0;
|
|
|
+
|
|
|
+ //printf("[Power]:sys power\n");
|
|
|
+ //rBuf_sys = sgm832_Get_ShuntVoltage(SGM832_I2C_ADDR_SYS);
|
|
|
+ //g_Power_sys = rBuf_sys;
|
|
|
+ //SendUart5DataPacket(0x21, (u8*)&rBuf_sys, sizeof(rBuf_sys));
|
|
|
+
|
|
|
+ sgm832_Get_BusVoltage(SGM832_I2C_ADDR_SYS);
|
|
|
+ //rBuf_sys = sgm832_Get_BusVoltage(SGM832_I2C_ADDR_SYS);
|
|
|
+ //g_Bus_sys = rBuf_sys;
|
|
|
+ //SendUart5DataPacket(0x31, (u8*)&rBuf_sys, sizeof(rBuf_sys));
|
|
|
+ //printf("\n");
|
|
|
+
|
|
|
+ sgm832_Get_Power(SGM832_I2C_ADDR_SYS);
|
|
|
+
|
|
|
+ sgm832_Get_Current(SGM832_I2C_ADDR_SYS);
|
|
|
+
|
|
|
+ //sgm832_Get_ConfigReg();
|
|
|
+ //sgm832_Get_ManufacturerID();
|
|
|
+ //sgm832_Get_DieID();
|
|
|
+}
|