Преглед изворни кода

增加两个SGM832检测功耗功能,一个检测整个系统的功耗,一个检测屏幕的功耗,目前能检测Power、BusVoltage、Current
SGM452和SGM832在同一路IIC上,根据IIC address不同来区分通信
删除一些不需要的代码

robbin пре 6 дана
родитељ
комит
8113a93e70

+ 1 - 0
EVT/EXAM/CodePro/YJD-CH32V30X/.settings/org.eclipse.core.resources.prefs

@@ -8,5 +8,6 @@ encoding//src/src/IIC_hal/IIC_Software.c=UTF-8
 encoding//src/src/TaskProcessCom.c=UTF-8
 encoding//src/src/mculib_common.c=UTF-8
 encoding//src/src/ms933x/ms933x_app.c=UTF-8
+encoding//src/src/sgm832_power/sgm832.c=UTF-8
 encoding//src/src/uart5/uart5.c=UTF-8
 encoding//src/src/uart_ht7315/uart_ht7315.c=UTF-8

Разлика између датотеке није приказан због своје велике величине
+ 884 - 757
EVT/EXAM/CodePro/YJD-CH32V30X/obj/YJD-CH32V30X.map


+ 1 - 0
EVT/EXAM/CodePro/YJD-CH32V30X/obj/makefile

@@ -10,6 +10,7 @@ RM := rm -rf
 -include sources.mk
 -include src/src/uart_ht7315/subdir.mk
 -include src/src/uart5/subdir.mk
+-include src/src/sgm832_power/subdir.mk
 -include src/src/sgm452_temperature/subdir.mk
 -include src/src/ms933x/subdir.mk
 -include src/src/IIC_hal/subdir.mk

+ 1 - 0
EVT/EXAM/CodePro/YJD-CH32V30X/obj/sources.mk

@@ -30,6 +30,7 @@ src/src \
 src/src/IIC_hal \
 src/src/ms933x \
 src/src/sgm452_temperature \
+src/src/sgm832_power \
 src/src/uart5 \
 src/src/uart_ht7315 \
 

+ 20 - 0
EVT/EXAM/CodePro/YJD-CH32V30X/obj/src/src/sgm832_power/subdir.mk

@@ -0,0 +1,20 @@
+################################################################################
+# Automatically-generated file. Do not edit!
+################################################################################
+
+# Add inputs and outputs from these tool invocations to the build variables 
+C_SRCS += \
+../src/src/sgm832_power/sgm832.c 
+
+OBJS += \
+./src/src/sgm832_power/sgm832.o 
+
+C_DEPS += \
+./src/src/sgm832_power/sgm832.d 
+
+
+# Each subdirectory must supply rules for building sources it contributes
+src/src/sgm832_power/%.o: ../src/src/sgm832_power/%.c
+	@	@	riscv-none-embed-gcc -march=rv32imacxw -mabi=ilp32 -msmall-data-limit=8 -msave-restore -Os -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -fno-common -Wunused -Wuninitialized  -g -D__STD_GCC__ -I"D:\Wingcool\WingCool\YJD-CH32V30X\EVT\EXAM\SRC\Debug" -I"D:\Wingcool\WingCool\YJD-CH32V30X\EVT\EXAM\SRC\Core" -I"D:\Wingcool\WingCool\YJD-CH32V30X\EVT\EXAM\SRC\Peripheral\inc" -I"D:\Wingcool\WingCool\YJD-CH32V30X\EVT\EXAM\CodePro\YJD-CH32V30X\src\src" -I"D:\Wingcool\WingCool\YJD-CH32V30X\EVT\EXAM\CodePro\YJD-CH32V30X\src\inc" -I"D:\Wingcool\WingCool\YJD-CH32V30X\EVT\EXAM\CodePro\YJD-CH32V30X\lib" -std=gnu99 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@)" -c -o "$@" "$<"
+	@	@
+

+ 0 - 1
EVT/EXAM/CodePro/YJD-CH32V30X/src/inc/TaskProcessCom.h

@@ -41,7 +41,6 @@ typedef enum {
 #define   BIT6                    (0X40)
 #define   BIT7                    (0X80)
 
-extern CHIP_TYPE ChipType;
 extern uint16_t UsbInitTimer;
 extern void (*TouchProc)(void);
 

+ 0 - 1
EVT/EXAM/CodePro/YJD-CH32V30X/src/inc/sgm452.h

@@ -12,7 +12,6 @@
 //      1 0 0 1  1  1  1 rw  ==> 0x9E
 #define  SGM452_I2C_ADDR  0x9E
 
-void sgm452_GPIO_I2cChannel_Init(void);
 void sgm452_Proc(void);
 
 #endif // SGM452_H

+ 40 - 0
EVT/EXAM/CodePro/YJD-CH32V30X/src/inc/sgm832.h

@@ -0,0 +1,40 @@
+#ifndef SGM832_H
+#define SGM832_H
+
+#include "ch32v30x.h"
+#include "IIC3_Software.h"
+//#include "IIC2_Software.h"
+#include "uart5.h"
+
+//sgm832 I2C address
+//7bit: 1 0 0 0 1 0 1 rw   [8A] ==> (A0->Vss, A1->Vss) for (system power detect)
+#define SGM832_I2C_ADDR_SYS    0x8A
+
+//7bit: 1 0 0 0 1 0 0 rw   [8A] ==> (A0->Gnd, A1->Vss) for (back light power detect)
+#define SGM832_I2C_ADDR_BL     0x88
+
+//sgm832 register address
+#define CONFIGURATION_REGISTER    0x00
+#define SHUNT_VOLTAGE_REGISTER    0x01
+#define BUS_VOLTAGE_REGISTER      0x02
+#define POWER_REGISTER            0x03
+#define CURRENT_REGISTER          0x04
+#define CALIBRATION_REGISTER      0x05
+#define MASK_ENABLE_REGISTER      0x06
+#define ALERT_LIMIT_REGISTER      0x07
+#define MANUFACTURER_ID_REGISTER  0xFE
+#define DIE_ID_REGISTER           0xFF
+
+u8 SGM832_WriteData(u8 address, u8 reg, u8 *data, u8 num);
+void Sgm832_Calibration(u8 address);
+void sgm832_Power_detect_Proc_Bl(void);
+void sgm832_Power_detect_Proc_Sys(void);
+
+//定义全局变量供 IIC_Slave 调用
+extern  u16 g_Power_bl;
+extern  u16 g_Power_sys;
+extern  u16 g_Bus_bl;
+extern  u16 g_Bus_sys;
+
+
+#endif // SGM832_H

+ 10 - 16
EVT/EXAM/CodePro/YJD-CH32V30X/src/src/TaskProcessCom.c

@@ -5,8 +5,7 @@
 #include "ch32x035_usbpd.h"
 #include "tmos.h"
 #include "sgm452.h"
-//#include "sgm832.h"
-
+#include "sgm832.h"
 
 
 #define KEY_ADC_DET                           0x0001
@@ -22,13 +21,8 @@
 static UINT16 Task_ProcessEvent( UINT8 task_id, UINT16 events );
 static UINT8 TaskID = INVALID_TASK_ID;
 
-CHIP_TYPE ChipType;
-
-UINT8 ShareBuf[10];
-
 void (*TouchProc)(void);
 
-
 void TIM4_Init( uint16_t arr, uint16_t psc )
 {
     TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure = { 0 };
@@ -150,8 +144,8 @@ static UINT16 Task_ProcessEvent( UINT8 task_id, UINT16 events )
     if(events & POWER_DET)
     {
       //printf("[EVENT]: POWER_DET\n");
-      //sgm832_Power_detect_Proc_Bl();
-      //sgm832_Power_detect_Proc_Sys();
+      sgm832_Power_detect_Proc_Bl();
+      sgm832_Power_detect_Proc_Sys();
       events ^= POWER_DET;
       tmos_start_task(task_id, POWER_DET, PERIO_800ms );
       goto OUT_FUMC;
@@ -176,21 +170,21 @@ OUT_FUMC:
 
 void InitChipStartTask()
 {
-//ms933x chip setting
+  //ms933x chip setting
   //libs auto write iic address 0xB2, no need set it
   ms933x_init();
   tmos_start_task(TaskID, HDMI_RX_DET, PERIO_10ms );
   printf("[task]: ms933x start.\n");
 
-//sgm452 chip setting
-  sgm452_GPIO_I2cChannel_Init();
+  //sgm452 chip setting
   tmos_start_task(TaskID, TEMPTURE_DET, PERIO_500ms );
   printf("[task]: SGM452 start.\n");
 
-//sgm832 chip setting
-  //sgm832_GPIO_I2cChannel_Init();
-  //tmos_start_task(TaskID, POWER_DET, PERIO_800ms );
-  //printf("[task]: SGM832 start.\n");
+  //sgm832 chip setting
+  Sgm832_Calibration(SGM832_I2C_ADDR_BL);
+  Sgm832_Calibration(SGM832_I2C_ADDR_SYS);
+  tmos_start_task(TaskID, POWER_DET, PERIO_800ms );
+  printf("[task]: SGM832 start.\n");
 
   Delay_Ms(80);
 }

+ 3 - 10
EVT/EXAM/CodePro/YJD-CH32V30X/src/src/main.c

@@ -15,6 +15,7 @@
 //#include "sgm832.h"
 #include "uart_ht7315.h"
 #include "uart5.h"
+#include "IIC3_Software.h"
 
 // void GPIOInit()
 // {
@@ -106,25 +107,17 @@ int main(void)
     //printf("2.[Usb Init]: Ok! \n");
 
 //**--[Process Event Init]--//
+    IIC3_sf_Init();
     ProcessEventInit();
     printf("3.[Process Event Init]: Ok! \n");
 
-//**--[SGM452 Init]--//
-    //sgm452_GPIO_I2cChannel_Init();
-    //sgm452_Proc();
-    //printf("5.[SGM452 Init]: Ok! \n");
-
-//**--[sgm832 Init]--//
-    //sgm832_GPIO_I2cChannel_Init();
-    //printf("6.[SGM832 Init]: Ok! \n");
-
     //IWDG_Feed_Init( IWDG_Prescaler_16, 3000 );
 
 //**--[GG]--//
 
     //printf("8.[IIC_Slave Init]: Ok! \n");
     //IIC_Slave_Init( 400000, I2C1Adderss);
-    //printf("[End]: <========== \n");
+    printf("[End]: <========== \n");
 
     while(1)
     {

+ 0 - 5
EVT/EXAM/CodePro/YJD-CH32V30X/src/src/sgm452_temperature/sgm452.c

@@ -43,11 +43,6 @@ u16 SGM452_ReadTemperature(void)
     }
 }
 
-void sgm452_GPIO_I2cChannel_Init(void)
-{
-    IIC3_sf_Init();
-}
-
 /**********************************************************************************/
 void sgm452_Proc(void)
 {

+ 291 - 0
EVT/EXAM/CodePro/YJD-CH32V30X/src/src/sgm832_power/sgm832.c

@@ -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();
+}

Неке датотеке нису приказане због велике количине промена