Jelajahi Sumber

fix 5238 imon寄存器偶发变为0,导致电流采集出错的问题

Signed-off-by: huhui <huhui@sharkgulf.com>
huhui 5 tahun lalu
induk
melakukan
e39032b3a4

+ 31 - 4
Application/app/sox/measure.c

@@ -47,10 +47,35 @@ static const float max_gd_adc = 4095.0f;//65536.0f;
 static const float v_cs1180_ref = 1235.0f;//cs1180 vref = 1.235v
 static const float max_cs1180_adc = 0x7FFFF;//
 static const float small_cur_r_sense = 0.360f;//ŷķ
+uint32_t check_gain_error = 0;
+static u64      check_gain_time = 0;
 static least_square_t adc_cali[2]; // y = ax + b
 
 #define GD32_ADC_READ_TIMES 128
 
+static int __inline__ _is_x10_gain(void){
+	return imon_gain_now == imon_gain_10x;
+}
+
+static void __inline__ check_gain(void){
+	int count = 5;
+	while (_is_x10_gain() && !ML5238_IS_10X()){
+		ML5238_IMON_OUT_10X();
+		check_gain_error ++;
+		if (count-- <= 0) {
+			break;
+		}
+	}
+	count = 5;
+	while (!_is_x10_gain() && !ML5238_IS_50X()){
+		ML5238_IMON_OUT_50X();
+		check_gain_error ++;
+		if (count-- <= 0) {
+			break;
+		}
+	}
+}
+
 static void __inline__ select_gain_10x(int select){	
 	if (select){
 		ML5238_IMON_OUT_10X();
@@ -64,10 +89,7 @@ static void __inline__ select_gain_10x(int select){
 		vim0_now = vim0_50x;
 		ml5238_now_ceoff = ml5238_50x_ceoff;
 	}
-}
-
-static int __inline__ _is_x10_gain(void){
-	return imon_gain_now == imon_gain_10x;
+	check_gain();
 }
 
 float get_ml5238_gain(void){
@@ -137,6 +159,11 @@ static float get_current_by_ml5238(void){
 
 /* get battery pack's current (mA) */
 static float get_pack_current_by_gd(void){
+	u64 time_now = shark_get_mseconds();
+	if (time_now - check_gain_time >= 100) {
+		check_gain();
+		check_gain_time = time_now;
+	}
 	float adc = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
 	if (adc >= (max_gd_adc - 255.0f) && (!_is_x10_gain())){//overflow, use 10x gain
 		select_gain_10x(1);

+ 2 - 2
Application/app/sox/measure_task.c

@@ -35,13 +35,13 @@ void measure_task_init(measure_notify cn, measure_notify vn, measure_notify tn){
 measure_value_t * measure_value(void){
 	return &_measure_value;
 }
-
+extern uint32_t check_gain_error;
 void measure_log(void){
 	measure_debug("Current %.4f -- %.4f\n", (float)_measure_value.load_current/1000.0f, (float)_measure_value.current_5238/1000.0f);
 	for (int i = 0; i < CELLS_NUM; i++){
 		//measure_debug("Cell[%d]: %.3fv\n", i, _measure_value.cell_vol[i]/1000.0f);
 	}
-	measure_debug("Gain:%f, Off %f\n", get_ml5238_gain(), get_ml5238_vos());
+	measure_debug("Gain:%f, Off %f, check error = %d\n", get_ml5238_gain(), get_ml5238_vos(), check_gain_error);
 }
 /*
  * 测量电芯电压,计算出总电压, 测量总电流,放电和充电

+ 8 - 0
Application/bsp/ml5238.c

@@ -235,6 +235,14 @@ void ml5238_reg_log(void){
 	}
 }
 
+uint8_t ml5238_read_imon(void){
+	uint8_t data = 0xFF;
+	if (ml5238_read(ML5238_IMON, &data) < 0) {
+		return 0xff;
+	}
+	return data;
+}
+
 void ml5238_power_down(void){
 	do {
 		ml5238_write(ML5238_PSENSE, PSENSE_EPSH|PSENSE_IPSH); //before power down, we must enable charger detect

+ 4 - 0
Application/bsp/ml5238.h

@@ -18,6 +18,7 @@ int ml5238_is_discharging(void);
 int ml5238_is_short_current_enabled(int mode);
 uint8_t ml5238_noop_register_rw(uint8_t data);//ml5238 ¶Áд²âÊÔ
 void ml5238_reg_log(void);
+uint8_t ml5238_read_imon(void);
 
 
 typedef void (*ml5238_notify_hander)(int event);
@@ -66,6 +67,9 @@ int ml5238_write(uint8_t regaddr, uint8_t data);
 #define ML5238_IMON_OUT_10X() {ml5238_write(ML5238_IMON, 0x10);delay_us(1000);};
 #define ML5238_IMON_OUT_50X() {ml5238_write(ML5238_IMON, 0x11);delay_us(5000);};
 
+#define ML5238_IS_10X() (ml5238_read_imon() == 0x10)
+#define ML5238_IS_50X() (ml5238_read_imon() == 0x11)
+
 //vim0 == xxx_OUT_ZERO_xxx, vim1 == xxx_OUT_V2000_xxx, vr == xxx_OUT_V100_xxx
 #define ML5238_GAIN(vim0, vim1, vr) ((vim1-vim0)/vr)