measure.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include "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. #include "bsp/shark_bsp.h"
  9. /* measure the temp & current & voltage for battery pack by using
  10. * ms5238 & cs1180(only used when bms is in small current loading)
  11. */
  12. /*
  13. 1. СµçÁ÷
  14. 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
  15. 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
  16. 2. ´óµçÁ÷
  17. cs1180(32x):1.861-2.082
  18. ml5238(50x):1.869-1.865-1.890
  19. */
  20. /* this is the inited gain set to the ms5238, but the really gain is calibrated
  21. * by measure_system_calibrate
  22. */
  23. static float imon_gain_10x = 10.0f;
  24. static float imon_gain_50x = 50.0f;
  25. static float imon_gain_now;
  26. static float vim0_10x = 0.0f;
  27. static float vim0_50x = 0.0f;
  28. static float vim0_now;
  29. #define gain_default_50x 1
  30. #define r_pcb_resistor 0.0f // pcb resistor
  31. static const float r_sense = r_resistor + r_pcb_resistor;
  32. static const float v_gd_ref = 3300.0f; //adc ref = 3.3v
  33. static const float max_gd_adc = 65535.0f;
  34. static const float v_cs1180_ref = 1235.0f;//cs1180 vref = 1.235v
  35. static const float max_cs1180_adc = 0x7FFFF;//
  36. static const float small_cur_r_sense = 360.0f;//mo
  37. static uint8_t adc_used = GD32_ADC;////CS1180_ADC;
  38. static void __inline__ select_gain_10x(int select){
  39. if (select){
  40. ML5238_IMON_OUT_10X();
  41. imon_gain_now = imon_gain_10x;
  42. vim0_now = vim0_10x;
  43. }else {
  44. ML5238_IMON_OUT_50X();
  45. imon_gain_now = imon_gain_50x;
  46. vim0_now = vim0_50x;
  47. }
  48. }
  49. static int __inline__ _is_x10_gain(void){
  50. return imon_gain_now == imon_gain_10x;
  51. }
  52. static void current_10x_calibrate(void){
  53. /* calibrate the 10x gain */
  54. ML5238_IMON_OUT_ZERO_10X();
  55. vim0_10x = adc_sample_avg(ADC_CHAN_IMON, 10);
  56. ML5238_IMON_OUT_V2000_10X();
  57. float vim1 = adc_sample_avg(ADC_CHAN_IMON, 10);
  58. ML5238_IMON_OUT_V100_10X();
  59. float vr = adc_sample_avg(ADC_CHAN_IMON, 10);
  60. imon_gain_10x = ML5238_GAIN(vim0_10x, vim1, vr);
  61. }
  62. static void current_50x_calibrate(void){
  63. /* calibrate the 50x gain */
  64. ML5238_IMON_OUT_ZERO_50X();
  65. vim0_50x = adc_sample_avg(ADC_CHAN_IMON, 10);
  66. ML5238_IMON_OUT_V2000_50X();
  67. float vim1 = adc_sample_avg(ADC_CHAN_IMON, 10);
  68. ML5238_IMON_OUT_V100_50X();
  69. float vr = adc_sample_avg(ADC_CHAN_IMON, 10);
  70. imon_gain_50x = ML5238_GAIN(vim0_50x, vim1, vr);
  71. }
  72. /*calibrate when startup && temperature is changed more than 5? degree
  73. * calibrate the ms5238's IMON output voltage gain
  74. */
  75. void current_calibrate(void){
  76. #ifdef gain_default_50x
  77. current_50x_calibrate();
  78. select_gain_10x(0);
  79. #else
  80. current_10x_calibrate();
  81. select_gain_10x(1);
  82. #endif
  83. }
  84. void measure_system_init(void){
  85. ml5238_init();
  86. adc_init();
  87. current_calibrate();
  88. cs1180_adc_init();
  89. //AUX_VOL_OPEN(1);
  90. //ml5238_enable_discharger_mosfet(1);
  91. //ml5238_enable_charger_mosfet(1);
  92. }
  93. /* get battery pack's current (mA) */
  94. static float get_pack_current_by_gd(void){
  95. float adc = adc_sample_avg(ADC_CHAN_IMON, 10);
  96. if (adc >= 0xFFF0 && (!_is_x10_gain())){//overflow, use 10x gain
  97. current_10x_calibrate();
  98. select_gain_10x(1);
  99. adc = adc_sample_avg(ADC_CHAN_IMON, 10);
  100. }else if (adc <= 0x1F && (_is_x10_gain())){// is too small, select 50x gain
  101. current_50x_calibrate();
  102. select_gain_10x(0);
  103. adc = adc_sample_avg(ADC_CHAN_IMON, 10);
  104. }
  105. float cali_adc = ML5238_V_RSENSER(adc, vim0_now, imon_gain_now);
  106. return (cali_adc / max_gd_adc) * v_gd_ref / r_sense * 1000;
  107. }
  108. static float get_pack_current_by_cs1180(void){
  109. float adc = cs1180_adc_sample();
  110. return (adc / max_cs1180_adc) * v_cs1180_ref / r_sense * 1000;
  111. }
  112. float get_pack_current(void){
  113. if (adc_used == CS1180_ADC){
  114. return get_pack_current_by_cs1180();
  115. }
  116. return get_pack_current_by_gd();
  117. }
  118. /*
  119. * select the gd32 internel adc or cs1180 adc
  120. */
  121. void adc_select_for_pack_current(int adc){
  122. if (adc != GD32_ADC && adc != CS1180_ADC){
  123. return;
  124. }
  125. if (adc == GD32_ADC){
  126. select_gain_10x(0);
  127. }else {
  128. ML5238_IMON_DISABLE();
  129. }
  130. adc_used = adc;
  131. }
  132. /* get cell's voltage (mV) */
  133. float get_cell_voltage(int cell){
  134. ML5238_SELECT_CELL(cell);
  135. delay_us(100);
  136. float adc = adc_sample_avg(ADC_CHAN_VMON, 34);
  137. return cell_real_vol((adc / max_gd_adc) * v_gd_ref);
  138. }
  139. /* get battery pack's aux current (mA) */
  140. float get_small_current(void){
  141. float adc = adc_sample(ADC_CHAN_AUX_CURR, TRUE);
  142. return (adc / max_gd_adc * v_gd_ref) / small_cur_r_sense * 1000;
  143. }
  144. int get_pcb_temperature(void){
  145. TEMP_OPEN(1);
  146. delay_us(100);
  147. uint16_t adc = adc_sample(ADC_CHAN_TEMPERATURE_4, TRUE);
  148. TEMP_OPEN(0);
  149. return get_temp_by_adc(adc);
  150. }
  151. /*
  152. * index : 0...2
  153. */
  154. int get_pack_temperature(int index){
  155. TEMP_OPEN(1);
  156. delay_us(100);
  157. uint16_t adc = adc_sample(ADC_CHAN_TEMPERATURE_1 + index, TRUE);
  158. TEMP_OPEN(0);
  159. return get_temp_by_adc(adc);
  160. }