|
@@ -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 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 = 0.360f;//ŷķ
|
|
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
|
|
static least_square_t adc_cali[2]; // y = ax + b
|
|
|
|
|
|
|
|
#define GD32_ADC_READ_TIMES 128
|
|
#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){
|
|
static void __inline__ select_gain_10x(int select){
|
|
|
if (select){
|
|
if (select){
|
|
|
ML5238_IMON_OUT_10X();
|
|
ML5238_IMON_OUT_10X();
|
|
@@ -64,10 +89,7 @@ static void __inline__ select_gain_10x(int select){
|
|
|
vim0_now = vim0_50x;
|
|
vim0_now = vim0_50x;
|
|
|
ml5238_now_ceoff = ml5238_50x_ceoff;
|
|
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){
|
|
float get_ml5238_gain(void){
|
|
@@ -137,6 +159,11 @@ static float get_current_by_ml5238(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){
|
|
|
|
|
+ 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);
|
|
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
|
|
if (adc >= (max_gd_adc - 255.0f) && (!_is_x10_gain())){//overflow, use 10x gain
|
|
|
select_gain_10x(1);
|
|
select_gain_10x(1);
|