iv_measure.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include "iv_measure.h"
  2. #include "bsp/ml5238.h"
  3. #include "bsp/cs1180.h"
  4. #include "bsp/gd32_adc.h"
  5. #include "bsp/clock.h"
  6. #include "bsp/gpio.h"
  7. #include "bsp/temp_lookup_tab.h"
  8. /* measure the temp & current & voltage for battery pack by using
  9. * ms5238 & cs1180(only used when bms is in small current loading)
  10. */
  11. /* this is the inited gain set to the ms5238, but the really gain is calibrated
  12. * by measure_system_calibrate
  13. */
  14. static float imon_gain_10x = 10.0f;
  15. static float imon_gain_50x = 50.0f;
  16. static float imon_gain_now;
  17. static float vim0_10x = 0.0f;
  18. static float vim0_50x = 0.0f;
  19. static float vim0_now;
  20. #if defined CONFIG_BOARD_SP700
  21. #define r_resistor 1.0f // 1ºÁÅ·
  22. #elif defined CONFIG_BOARD_SP600
  23. #define r_resistor 2.0f
  24. #endif
  25. #define r_pcb_resistor 0.0f // pcb resistor
  26. static const float r_sense = r_resistor + r_pcb_resistor;
  27. static const float v_ref = 3300.0f; //adc ref = 3.3v
  28. static const float max_adc = 65535.0f;
  29. static const float small_cur_r_sense = 360.0f;//mo
  30. static void __inline__ select_gain_10x(int select){
  31. if (select){
  32. ML5238_IMON_OUT_10X();
  33. imon_gain_now = imon_gain_10x;
  34. vim0_now = vim0_10x;
  35. }else {
  36. ML5238_IMON_OUT_50X();
  37. imon_gain_now = imon_gain_50x;
  38. vim0_now = vim0_50x;
  39. }
  40. }
  41. static int __inline__ _is_x10_gain(void){
  42. return imon_gain_now == imon_gain_10x;
  43. }
  44. /*calibrate when startup && temperature is changed more than 5? degree
  45. * calibrate the ms5238's IMON output voltage gain
  46. */
  47. void current_calibrate(void){
  48. /* calibrate the 10x gain */
  49. ML5238_IMON_OUT_ZERO_10X();
  50. vim0_10x = adc_sample(ADC_CHAN_IMON ,TRUE);
  51. ML5238_IMON_OUT_V2000_10X();
  52. float vim1 = adc_sample(ADC_CHAN_IMON, FALSE);
  53. ML5238_IMON_OUT_V100_10X();
  54. float vr = adc_sample(ADC_CHAN_IMON, FALSE);
  55. imon_gain_10x = ML5238_GAIN(vim0_10x, vim1, vr);
  56. /* calibrate the 50x gain */
  57. ML5238_IMON_OUT_ZERO_50X();
  58. vim0_50x = adc_sample(ADC_CHAN_IMON ,TRUE);
  59. ML5238_IMON_OUT_V2000_50X();
  60. vim1 = adc_sample(ADC_CHAN_IMON, FALSE);
  61. ML5238_IMON_OUT_V100_50X();
  62. vr = adc_sample(ADC_CHAN_IMON, FALSE);
  63. imon_gain_50x = ML5238_GAIN(vim0_50x, vim1, vr);
  64. select_gain_10x(0);//default gain is 50x, if 50x overflow, use 10x
  65. }
  66. void measure_system_init(void){
  67. adc_init();
  68. ml5238_init();
  69. //cs1180_init();
  70. current_calibrate();
  71. }
  72. /* get battery pack's current (mA) */
  73. float get_imon_current(void){
  74. float adc = adc_sample(ADC_CHAN_IMON, TRUE);
  75. if (adc >= 0xFFF0 && (!_is_x10_gain())){//overflow, use 10x gain
  76. select_gain_10x(1);
  77. adc = adc_sample(ADC_CHAN_IMON, TRUE);
  78. }else if (adc <= 0x1F && (_is_x10_gain())){// is too small, select 50x gain
  79. select_gain_10x(0);
  80. adc = adc_sample(ADC_CHAN_IMON, TRUE);
  81. }
  82. float cali_adc = ML5238_V_RSENSER(adc, vim0_now, imon_gain_now);
  83. return (cali_adc / max_adc) * v_ref / r_sense * 1000;
  84. }
  85. /* get cell's voltage (mV) */
  86. float get_vmon_voltage(int cell){
  87. ml5238_select_cell_to_vmon(cell);
  88. delay_us(100);
  89. float adc = adc_sample(ADC_CHAN_VMON, TRUE);
  90. return cell_real_vol((adc / max_adc) * v_ref);
  91. }
  92. /* get battery pack's aux current (mA) */
  93. float get_small_current(void){
  94. float adc = adc_sample(ADC_CHAN_AUX_CURR, TRUE);
  95. return (adc / max_adc * v_ref) / small_cur_r_sense * 1000;
  96. }
  97. int get_pcb_temperature(void){
  98. TEMP_OPEN(1);
  99. delay_us(100);
  100. uint16_t adc = adc_sample(ADC_CHAN_TEMPERATURE_4, TRUE);
  101. TEMP_OPEN(0);
  102. return get_temp_by_adc(adc);
  103. }
  104. /*
  105. * index : 0...2
  106. */
  107. int get_pack_temperature(int index){
  108. TEMP_OPEN(1);
  109. delay_us(100);
  110. uint16_t adc = adc_sample(ADC_CHAN_TEMPERATURE_1 + index, TRUE);
  111. TEMP_OPEN(0);
  112. return get_temp_by_adc(adc);
  113. }