measure.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. #include "libs/logger.h"
  10. /* measure the temp & current & voltage for battery pack by using
  11. * ms5238 & cs1180(only used when bms is in small current loading)
  12. */
  13. /*
  14. 1. 小电流
  15. 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
  16. 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
  17. 2. 大电流
  18. cs1180(32x):1.861-2.082
  19. ml5238(50x):1.869-1.865-1.890
  20. */
  21. /* this is the inited gain set to the ms5238, but the really gain is calibrated
  22. * by measure_system_calibrate
  23. */
  24. static float imon_gain_10x = 10.0f;
  25. static float imon_gain_50x = 50.0f;
  26. static float imon_gain_now;
  27. static float vim0_10x = 0.0f;
  28. static float vim0_50x = 0.0f;
  29. static float vim0_now;
  30. #define gain_default_50x 1
  31. #define CS1180_MAX_CURRENT 4500 //MA, cs1180的最大电流,超过这个使用ML5238
  32. #define r_pcb_resistor 0.0f // pcb resistor
  33. static const float r_sense = r_resistor + r_pcb_resistor;
  34. static const float v_gd_ref = 3300.0f; //adc ref = 3.3v
  35. static const float max_gd_adc = 4095.0f;//65536.0f;
  36. static const float v_cs1180_ref = 1235.0f;//cs1180 vref = 1.235v
  37. static const float max_cs1180_adc = 0x7FFFF;//
  38. static const float small_cur_r_sense = 360.0f;//欧姆
  39. #define GD32_ADC_READ_TIMES 128
  40. static void __inline__ select_gain_10x(int select){
  41. if (select){
  42. ML5238_IMON_OUT_10X();
  43. imon_gain_now = imon_gain_10x;
  44. vim0_now = vim0_10x;
  45. }else {
  46. ML5238_IMON_OUT_50X();
  47. imon_gain_now = imon_gain_50x;
  48. vim0_now = vim0_50x;
  49. }
  50. }
  51. static int __inline__ _is_x10_gain(void){
  52. return imon_gain_now == imon_gain_10x;
  53. }
  54. float get_ml5238_gain(void){
  55. return imon_gain_now;
  56. }
  57. float get_ml5238_vos(void){
  58. return vim0_now;
  59. }
  60. static void current_10x_calibrate(void){
  61. /* calibrate the 10x gain */
  62. ML5238_IMON_OUT_ZERO_10X();
  63. vim0_10x = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
  64. #if 0
  65. ML5238_IMON_OUT_V2000_10X();
  66. float vim1 = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
  67. ML5238_IMON_OUT_V100_10X();
  68. float vr = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
  69. imon_gain_10x = ML5238_GAIN(vim0_10x, vim1, vr);
  70. #else
  71. imon_gain_10x = 10.0f;
  72. #endif
  73. }
  74. static void current_50x_calibrate(void){
  75. /* calibrate the 50x gain */
  76. ML5238_IMON_OUT_ZERO_50X();
  77. vim0_50x = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
  78. #if 0
  79. ML5238_IMON_OUT_V2000_50X();
  80. float vim1 = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
  81. ML5238_IMON_OUT_V20_50X();
  82. float vr = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
  83. imon_gain_50x = ML5238_GAIN(vim0_50x, vim1, vr);
  84. #else
  85. imon_gain_50x = 50.0f;
  86. #endif
  87. }
  88. /*calibrate when startup && temperature is changed more than 5? degree
  89. * calibrate the ms5238's IMON output voltage gain
  90. */
  91. void current_calibrate(void){
  92. current_10x_calibrate();
  93. current_50x_calibrate();
  94. #ifdef gain_default_50x
  95. select_gain_10x(0);
  96. #else
  97. select_gain_10x(1);
  98. #endif
  99. }
  100. void measure_adc_init(void){
  101. ml5238_init();
  102. gd32_adc_init();
  103. current_calibrate();
  104. cs1180_adc_init();
  105. set_log_level(MOD_SYSTEM, L_debug);
  106. }
  107. /* get battery pack's current (mA) */
  108. static float get_pack_current_by_gd(void){
  109. float adc = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
  110. if (adc >= (max_gd_adc - 255.0f) && (!_is_x10_gain())){//overflow, use 10x gain
  111. select_gain_10x(1);
  112. adc = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
  113. }else if (abs(adc - vim0_now) <= 255.0f && (_is_x10_gain())){// is too small, select 50x gain
  114. select_gain_10x(0);
  115. adc = adc_sample_avg(ADC_CHAN_IMON, GD32_ADC_READ_TIMES);
  116. }
  117. float cali_adc = ML5238_V_RSENSER(adc, vim0_now, imon_gain_now);
  118. return (int)((cali_adc / max_gd_adc) * v_gd_ref / r_sense * 1000);
  119. }
  120. static float get_pack_current_by_cs1180(int *valid){
  121. float adc = cs1180_adc_sample(valid);
  122. float vol = (adc / max_cs1180_adc) * v_cs1180_ref;
  123. return (vol / r_sense) * 1000 - 5.0f;//板子固定5MA,cs1180无法测量到
  124. }
  125. float get_pack_current(int *current_5238){
  126. float current = get_pack_current_by_gd();
  127. if (current_5238 != NULL){
  128. *current_5238 = (int)current;
  129. }
  130. if (cs1180_change_gain(current) == 0) {
  131. int valid = 1;
  132. float cs1180_current = get_pack_current_by_cs1180(&valid);
  133. if (valid == 1) {
  134. current = cs1180_current;
  135. }
  136. }
  137. return current;
  138. }
  139. /* get cell's voltage (mV) */
  140. float get_cell_voltage(int cell){
  141. ML5238_SELECT_CELL(cell);
  142. delay_us(100);
  143. float adc = adc_sample_avg(ADC_CHAN_VMON, GD32_ADC_READ_TIMES);
  144. return cell_real_vol((adc / max_gd_adc) * v_gd_ref);
  145. }
  146. /* get battery pack's aux current (MA) */
  147. float get_small_current(void){
  148. float adc = adc_sample_avg(ADC_CHAN_AUX_CURR, GD32_ADC_READ_TIMES);
  149. return ((adc / max_gd_adc * v_gd_ref)) / small_cur_r_sense;
  150. }
  151. /* 用来判断小电流的情况下,电压小于某一个值认为小电流真正短路,比如16v*/
  152. float get_small_current_voltage(void){
  153. float s_current_a = get_small_current();//MA
  154. return s_current_a * (small_cur_r_sense + 28.0f);//28欧姆是mos的D极两个56的并联
  155. }
  156. int get_pcb_temperature(void){
  157. TEMP_OPEN(1);
  158. delay_us(100);
  159. uint16_t adc = adc_sample_avg(ADC_CHAN_TEMPERATURE_4, 1);
  160. TEMP_OPEN(0);
  161. return get_temp_by_adc(adc);
  162. }
  163. /*
  164. * index : 0...3, 3 indicat pcb temp
  165. */
  166. int get_pack_temperature(int index){
  167. TEMP_OPEN(1);
  168. delay_us(100);
  169. uint16_t adc = adc_sample_avg(ADC_CHAN_TEMPERATURE_1 + index, 1);
  170. TEMP_OPEN(0);
  171. return get_temp_by_adc((adc<<4)&0xFFFF);
  172. }