#include "measure.h" #include "bsp/ml5238.h" #include "bsp/cs1180.h" #include "bsp/gd32_adc.h" #include "bsp/clock.h" #include "bsp/gpio.h" #include "bsp/temp_lookup_tab.h" #include "bsp/shark_bsp.h" /* measure the temp & current & voltage for battery pack by using * ms5238 & cs1180(only used when bms is in small current loading) */ /* 1. 小电流 cs1180(32x):0.305-0.315, 0.277-0.286, 0.255-0.264, 0.220-0.228, 0.184-0.190, 0.137-0.142, 0.062-0.064, 0.011-0.009:0.012 ml5289(50x):0.307-0.313:0.330,0.277-0.275:0.290,0.254-0.251:0.271,0.221-0.208:0.253,0.183-0.170:0.196,0.136-0.121:0.155,0.061-0.072:0.096, 0.011-0.033:0.060 2. 大电流 cs1180(32x):1.861-2.082 ml5238(50x):1.869-1.865-1.890 */ /* this is the inited gain set to the ms5238, but the really gain is calibrated * by measure_system_calibrate */ static float imon_gain_10x = 10.0f; static float imon_gain_50x = 50.0f; static float imon_gain_now; static float vim0_10x = 0.0f; static float vim0_50x = 0.0f; static float vim0_now; #define gain_default_50x 1 #define r_pcb_resistor 0.0f // 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 max_gd_adc = 65535.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 = 360.0f;//mo static uint8_t adc_used = GD32_ADC;////CS1180_ADC; static void __inline__ select_gain_10x(int select){ if (select){ ML5238_IMON_OUT_10X(); imon_gain_now = imon_gain_10x; vim0_now = vim0_10x; }else { ML5238_IMON_OUT_50X(); imon_gain_now = imon_gain_50x; vim0_now = vim0_50x; } } static int __inline__ _is_x10_gain(void){ return imon_gain_now == imon_gain_10x; } static void current_10x_calibrate(void){ /* calibrate the 10x gain */ ML5238_IMON_OUT_ZERO_10X(); vim0_10x = adc_sample_avg(ADC_CHAN_IMON, 10); ML5238_IMON_OUT_V2000_10X(); float vim1 = adc_sample_avg(ADC_CHAN_IMON, 10); ML5238_IMON_OUT_V100_10X(); float vr = adc_sample_avg(ADC_CHAN_IMON, 10); imon_gain_10x = ML5238_GAIN(vim0_10x, vim1, vr); } static void current_50x_calibrate(void){ /* calibrate the 50x gain */ ML5238_IMON_OUT_ZERO_50X(); vim0_50x = adc_sample_avg(ADC_CHAN_IMON, 10); ML5238_IMON_OUT_V2000_50X(); float vim1 = adc_sample_avg(ADC_CHAN_IMON, 10); ML5238_IMON_OUT_V100_50X(); float vr = adc_sample_avg(ADC_CHAN_IMON, 10); imon_gain_50x = ML5238_GAIN(vim0_50x, vim1, vr); } /*calibrate when startup && temperature is changed more than 5? degree * calibrate the ms5238's IMON output voltage gain */ void current_calibrate(void){ #ifdef gain_default_50x current_50x_calibrate(); select_gain_10x(0); #else current_10x_calibrate(); select_gain_10x(1); #endif } void measure_adc_init(void){ ml5238_init(); adc_init(); current_calibrate(); cs1180_adc_init(); //AUX_VOL_OPEN(1); //ml5238_enable_discharger_mosfet(1); //ml5238_enable_charger_mosfet(1); } /* get battery pack's current (mA) */ static float get_pack_current_by_gd(void){ float adc = adc_sample_avg(ADC_CHAN_IMON, 10); if (adc >= 0xFFF0 && (!_is_x10_gain())){//overflow, use 10x gain current_10x_calibrate(); select_gain_10x(1); adc = adc_sample_avg(ADC_CHAN_IMON, 10); }else if (adc <= 0x1F && (_is_x10_gain())){// is too small, select 50x gain current_50x_calibrate(); select_gain_10x(0); adc = adc_sample_avg(ADC_CHAN_IMON, 10); } float cali_adc = ML5238_V_RSENSER(adc, vim0_now, imon_gain_now); return (cali_adc / max_gd_adc) * v_gd_ref / r_sense * 1000; } static float get_pack_current_by_cs1180(void){ float adc = cs1180_adc_sample(); return (adc / max_cs1180_adc) * v_cs1180_ref / r_sense * 1000; } float get_pack_current(void){ if (adc_used == CS1180_ADC){ return get_pack_current_by_cs1180(); } return get_pack_current_by_gd(); } /* * select the gd32 internel adc or cs1180 adc */ void adc_select_for_pack_current(int adc){ if (adc != GD32_ADC && adc != CS1180_ADC){ return; } if (adc == GD32_ADC){ select_gain_10x(0); }else { ML5238_IMON_DISABLE(); } adc_used = adc; } /* get cell's voltage (mV) */ float get_cell_voltage(int cell){ ML5238_SELECT_CELL(cell); delay_us(100); float adc = adc_sample_avg(ADC_CHAN_VMON, 34); return cell_real_vol((adc / max_gd_adc) * v_gd_ref); } /* get battery pack's aux current (mA) */ float get_small_current(void){ float adc = adc_sample(ADC_CHAN_AUX_CURR, TRUE); return (adc / max_gd_adc * v_gd_ref) / small_cur_r_sense * 1000; } int get_pcb_temperature(void){ TEMP_OPEN(1); delay_us(100); uint16_t adc = adc_sample(ADC_CHAN_TEMPERATURE_4, TRUE); TEMP_OPEN(0); return get_temp_by_adc(adc); } /* * index : 0...3, 3 indicat pcb temp */ int get_pack_temperature(int index){ TEMP_OPEN(1); delay_us(100); uint16_t adc = adc_sample(ADC_CHAN_TEMPERATURE_1 + index, TRUE); TEMP_OPEN(0); return get_temp_by_adc(adc); }