iv_measure.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #define r_resistor 1.0f // 1ºÁÅ·
  21. #define r_pcb_resistor 0.0f // pcb resistor
  22. static const float r_sense = r_resistor + r_pcb_resistor;
  23. static const float v_ref = 3300.0f; //adc ref = 3.3v
  24. static const float max_adc = 65535.0f;
  25. static const float small_cur_r_sense = 360.0f;//mo
  26. static void __inline__ select_gain_10x(int select){
  27. if (select){
  28. ML5238_IMON_OUT_10X();
  29. imon_gain_now = imon_gain_10x;
  30. vim0_now = vim0_10x;
  31. }else {
  32. ML5238_IMON_OUT_50X();
  33. imon_gain_now = imon_gain_50x;
  34. vim0_now = vim0_50x;
  35. }
  36. }
  37. /*calibrate when startup && temperature is changed more than 5? degree
  38. * calibrate the ms5238's IMON output voltage gain
  39. */
  40. void current_calibrate(void){
  41. /* calibrate the 10x gain */
  42. ML5238_IMON_OUT_ZERO_10X();
  43. vim0_10x = adc_sample(ADC_CHAN_IMON ,TRUE);
  44. ML5238_IMON_OUT_V2000_10X();
  45. float vim1 = adc_sample(ADC_CHAN_IMON, FALSE);
  46. ML5238_IMON_OUT_V100_10X();
  47. float vr = adc_sample(ADC_CHAN_IMON, FALSE);
  48. imon_gain_10x = ML5238_GAIN(vim0_10x, vim1, vr);
  49. /* calibrate the 50x gain */
  50. ML5238_IMON_OUT_ZERO_50X();
  51. vim0_50x = adc_sample(ADC_CHAN_IMON ,TRUE);
  52. ML5238_IMON_OUT_V2000_50X();
  53. vim1 = adc_sample(ADC_CHAN_IMON, FALSE);
  54. ML5238_IMON_OUT_V100_50X();
  55. vr = adc_sample(ADC_CHAN_IMON, FALSE);
  56. imon_gain_50x = ML5238_GAIN(vim0_50x, vim1, vr);
  57. select_gain_10x(0);//default gain is 50x, if 50x overflow, use 10x
  58. }
  59. void measure_system_init(void){
  60. adc_init();
  61. ml5238_init();
  62. cs1180_init();
  63. current_calibrate();
  64. }
  65. /* get battery pack's current (mA) */
  66. float get_imon_current(int calibration){
  67. float adc = adc_sample(ADC_CHAN_IMON, calibration);
  68. if (adc >= 0xFFF0){//overflow, use 10x gain
  69. select_gain_10x(1);
  70. adc = adc_sample(ADC_CHAN_IMON, calibration);
  71. }else if (adc <= 0x1F){// is too small, select 50x gain
  72. select_gain_10x(0);
  73. adc = adc_sample(ADC_CHAN_IMON, calibration);
  74. }
  75. float cali_adc = ML5238_V_RSENSER(adc, vim0_now, imon_gain_now);
  76. return (cali_adc / max_adc) * v_ref / r_sense * 1000;
  77. }
  78. /* get cell's voltage (mV) */
  79. float get_vmon_voltage(int cell, int calibration){
  80. ml5238_select_cell_to_vmon(cell);
  81. delay_us(50);
  82. float adc = adc_sample(ADC_CHAN_VMON, calibration);
  83. return (adc / max_adc) * v_ref * 1000;
  84. }
  85. /* get battery pack's aux current (mA) */
  86. float get_small_current(int calibration){
  87. float adc = adc_sample(ADC_CHAN_AUX_CURR, calibration);
  88. return (adc / max_adc * v_ref) / small_cur_r_sense * 1000;
  89. }
  90. int get_pcb_temperature(int calibration){
  91. TEMP_OPEN(1);
  92. delay_us(100);
  93. uint16_t adc = adc_sample(ADC_CHAN_TEMPERATURE_4, calibration);
  94. TEMP_OPEN(0);
  95. return get_temp_by_adc(adc);
  96. }
  97. int get_pack_temperature(int index, int calibration){
  98. TEMP_OPEN(1);
  99. delay_us(100);
  100. uint16_t adc = adc_sample(ADC_CHAN_TEMPERATURE_1 + index, calibration);
  101. TEMP_OPEN(0);
  102. return get_temp_by_adc(adc);
  103. }