|
@@ -33,12 +33,12 @@ static float vim0_now;
|
|
|
#define r_pcb_resistor 0.0f // pcb resistor
|
|
#define r_pcb_resistor 0.0f // pcb resistor
|
|
|
static const float r_sense = r_resistor + r_pcb_resistor;
|
|
static const float r_sense = r_resistor + r_pcb_resistor;
|
|
|
static const float v_gd_ref = 3300.0f; //adc ref = 3.3v
|
|
static const float v_gd_ref = 3300.0f; //adc ref = 3.3v
|
|
|
-static const float max_gd_adc = 65535.0f;
|
|
|
|
|
|
|
+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 v_cs1180_ref = 1235.0f;//cs1180 vref = 1.235v
|
|
|
static const float max_cs1180_adc = 0x7FFFF;//
|
|
static const float max_cs1180_adc = 0x7FFFF;//
|
|
|
static const float small_cur_r_sense = 360.0f;//ŷķ
|
|
static const float small_cur_r_sense = 360.0f;//ŷķ
|
|
|
|
|
|
|
|
-#define GD32_ADC_READ_TIMES 4
|
|
|
|
|
|
|
+#define GD32_ADC_READ_TIMES 128
|
|
|
|
|
|
|
|
static void __inline__ select_gain_10x(int select){
|
|
static void __inline__ select_gain_10x(int select){
|
|
|
if (select){
|
|
if (select){
|
|
@@ -56,6 +56,13 @@ static int __inline__ _is_x10_gain(void){
|
|
|
return imon_gain_now == imon_gain_10x;
|
|
return imon_gain_now == imon_gain_10x;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+float get_ml5238_gain(void){
|
|
|
|
|
+ return imon_gain_now;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+float get_ml5238_vos(void){
|
|
|
|
|
+ return vim0_now;
|
|
|
|
|
+}
|
|
|
static void current_10x_calibrate(void){
|
|
static void current_10x_calibrate(void){
|
|
|
/* calibrate the 10x gain */
|
|
/* calibrate the 10x gain */
|
|
|
ML5238_IMON_OUT_ZERO_10X();
|
|
ML5238_IMON_OUT_ZERO_10X();
|
|
@@ -73,20 +80,20 @@ static void current_50x_calibrate(void){
|
|
|
vim0_50x = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
|
|
vim0_50x = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
|
|
|
ML5238_IMON_OUT_V2000_50X();
|
|
ML5238_IMON_OUT_V2000_50X();
|
|
|
float vim1 = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
|
|
float vim1 = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
|
|
|
- ML5238_IMON_OUT_V100_50X();
|
|
|
|
|
|
|
+ ML5238_IMON_OUT_V20_50X();
|
|
|
float vr = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
|
|
float vr = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
|
|
|
- imon_gain_50x = ML5238_GAIN(vim0_50x, vim1, vr);
|
|
|
|
|
|
|
+ imon_gain_50x = 50.0f;//ML5238_GAIN(vim0_50x, vim1, vr);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/*calibrate when startup && temperature is changed more than 5? degree
|
|
/*calibrate when startup && temperature is changed more than 5? degree
|
|
|
* calibrate the ms5238's IMON output voltage gain
|
|
* calibrate the ms5238's IMON output voltage gain
|
|
|
*/
|
|
*/
|
|
|
void current_calibrate(void){
|
|
void current_calibrate(void){
|
|
|
-#ifdef gain_default_50x
|
|
|
|
|
|
|
+ current_10x_calibrate();
|
|
|
current_50x_calibrate();
|
|
current_50x_calibrate();
|
|
|
|
|
+#ifdef gain_default_50x
|
|
|
select_gain_10x(0);
|
|
select_gain_10x(0);
|
|
|
#else
|
|
#else
|
|
|
- current_10x_calibrate();
|
|
|
|
|
select_gain_10x(1);
|
|
select_gain_10x(1);
|
|
|
#endif
|
|
#endif
|
|
|
}
|
|
}
|
|
@@ -103,15 +110,14 @@ void measure_adc_init(void){
|
|
|
/* get battery pack's current (mA) */
|
|
/* get battery pack's current (mA) */
|
|
|
static float get_pack_current_by_gd(void){
|
|
static float get_pack_current_by_gd(void){
|
|
|
float adc = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
|
|
float adc = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
|
|
|
- if (adc >= 0xFFF0 && (!_is_x10_gain())){//overflow, use 10x gain
|
|
|
|
|
- current_10x_calibrate();
|
|
|
|
|
|
|
+ if (adc >= (max_gd_adc - 0xF) && (!_is_x10_gain())){//overflow, use 10x gain
|
|
|
select_gain_10x(1);
|
|
select_gain_10x(1);
|
|
|
adc = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
|
|
adc = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
|
|
|
}else if (adc <= 0x1F && (_is_x10_gain())){// is too small, select 50x gain
|
|
}else if (adc <= 0x1F && (_is_x10_gain())){// is too small, select 50x gain
|
|
|
- current_50x_calibrate();
|
|
|
|
|
select_gain_10x(0);
|
|
select_gain_10x(0);
|
|
|
adc = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
|
|
adc = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
float cali_adc = ML5238_V_RSENSER(adc, vim0_now, imon_gain_now);
|
|
float cali_adc = ML5238_V_RSENSER(adc, vim0_now, imon_gain_now);
|
|
|
|
|
|
|
|
return (int)((cali_adc / max_gd_adc) * v_gd_ref / r_sense * 1000);
|
|
return (int)((cali_adc / max_gd_adc) * v_gd_ref / r_sense * 1000);
|
|
@@ -126,7 +132,7 @@ static float get_pack_current_by_cs1180(void){
|
|
|
|
|
|
|
|
float get_pack_current(void){
|
|
float get_pack_current(void){
|
|
|
float current = get_pack_current_by_gd();
|
|
float current = get_pack_current_by_gd();
|
|
|
- if (abs(current) < CS1180_MAX_CURRENT && cs1180_is_ready()){
|
|
|
|
|
|
|
+ if (cs1180_change_gain(current) == 0) {
|
|
|
current = get_pack_current_by_cs1180();
|
|
current = get_pack_current_by_cs1180();
|
|
|
}
|
|
}
|
|
|
return current;
|
|
return current;
|
|
@@ -159,7 +165,7 @@ float get_small_current_voltage(void){
|
|
|
int get_pcb_temperature(void){
|
|
int get_pcb_temperature(void){
|
|
|
TEMP_OPEN(1);
|
|
TEMP_OPEN(1);
|
|
|
delay_us(100);
|
|
delay_us(100);
|
|
|
- uint16_t adc = adc_sample(ADC_CHAN_TEMPERATURE_4, TRUE);
|
|
|
|
|
|
|
+ uint16_t adc = adc_sample_avg(ADC_CHAN_TEMPERATURE_4, 1);
|
|
|
TEMP_OPEN(0);
|
|
TEMP_OPEN(0);
|
|
|
return get_temp_by_adc(adc);
|
|
return get_temp_by_adc(adc);
|
|
|
}
|
|
}
|
|
@@ -170,7 +176,7 @@ int get_pcb_temperature(void){
|
|
|
int get_pack_temperature(int index){
|
|
int get_pack_temperature(int index){
|
|
|
TEMP_OPEN(1);
|
|
TEMP_OPEN(1);
|
|
|
delay_us(100);
|
|
delay_us(100);
|
|
|
- uint16_t adc = adc_sample(ADC_CHAN_TEMPERATURE_1 + index, TRUE);
|
|
|
|
|
|
|
+ uint16_t adc = adc_sample_avg(ADC_CHAN_TEMPERATURE_1 + index, 1);
|
|
|
TEMP_OPEN(0);
|
|
TEMP_OPEN(0);
|
|
|
return get_temp_by_adc(adc);
|
|
return get_temp_by_adc(adc);
|
|
|
}
|
|
}
|