state.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #include "bsp/gpio.h"
  2. #include "bsp/ml5238.h"
  3. #include "bsp/mcu_power_sleep.h"
  4. #include "app/sox/measure.h"
  5. #include "app/sox/measure_task.h"
  6. #include "libs/shark_task.h"
  7. #include "health.h"
  8. #include "state.h"
  9. #include "iostate.h"
  10. #define ALLOW_DEEP_SLEEP 0
  11. #define ALLOW_POWER_DOWN 0
  12. #define ALLOW_5238_BALANCE 1
  13. static bms_state_t _bms_state;
  14. static shark_task_t _bms_main_task;
  15. static void _current_notify(void);
  16. static void _voltage_notify(void);
  17. static void _temperature_notify(void);
  18. static u32 _bms_main_task_handler(void);
  19. void bms_state_init(void){
  20. _bms_state.cell_index_of_max_vol = 0xff;
  21. _bms_main_task.handler = _bms_main_task_handler;
  22. measure_task_init(_current_notify, _voltage_notify, _temperature_notify);
  23. io_state_init();
  24. health_init();
  25. shark_task_add(&_bms_main_task);
  26. }
  27. bms_state_t *bms_state_get(void){
  28. return &_bms_state;
  29. }
  30. #define Health_Success 0
  31. #define Health_Discharger_Failt 1
  32. #define Health_charger_Fault 2
  33. #define Health_aux_Fault 4
  34. static s32 _process_unheath(void){
  35. if (bms_health()->load_current_short) {//短路检测后,关闭充放电mos
  36. ml5238_enable_discharger_mosfet(0);
  37. ml5238_enable_charger_mosfet(0); //disable charger mosfet
  38. _bms_state.charging = 0;
  39. _bms_state.discharging = 0;
  40. return (Health_Discharger_Failt | Health_charger_Fault);
  41. }
  42. if (bms_health()->charger_over_current || bms_health()->charger_over_temp || bms_health()->charger_lower_temp){
  43. ml5238_enable_charger_mosfet(0); //disable charger mosfet
  44. _bms_state.charging = 0;
  45. return Health_charger_Fault;
  46. }
  47. if (bms_health()->discharger_over_temp || bms_health()->discharger_lower_temp || bms_health()->discharger_lower_voltage){
  48. ml5238_enable_discharger_mosfet(0); //disable charger mosfet
  49. _bms_state.discharging = 0;
  50. return Health_Discharger_Failt;
  51. }
  52. if (io_state()->aux_lock_detect && AUX_VOL_IS_OPEN()) {
  53. AUX_VOL_OPEN(0);
  54. return Health_aux_Fault;
  55. }
  56. return Health_Success;
  57. }
  58. //处理PS100/310/320/360,充电底座,充电柜的指令或者bms自己发给自己的指令
  59. static void _process_user_request(s32 health){
  60. if (_bms_state.user_request & USER_REQUEST_PENDING){
  61. if ((health & Health_charger_Fault) == 0){
  62. ml5238_enable_charger_mosfet(!!(_bms_state.user_request & USER_REQUEST_CHARGER));
  63. }
  64. if ((health & Health_Discharger_Failt) == 0){
  65. ml5238_enable_discharger_mosfet(!!(_bms_state.user_request & USER_REQUEST_DISCHARGER));
  66. }
  67. if ((health & Health_aux_Fault) == 0){
  68. AUX_VOL_OPEN(!!(_bms_state.user_request & USER_REQUEST_SMALLCURRENT));
  69. }
  70. _bms_state.user_request = 0;//clear user request
  71. }
  72. }
  73. static void _process_power_down(void){
  74. #if (ALLOW_POWER_DOWN==1)
  75. if (bms_health()->powerdown_lower_voltage){
  76. ml5238_enable_charger_detect(1);
  77. delay_us(2* 1000);
  78. if (!ml5238_charger_is_disconnect()){//have charger, do'nt power down
  79. bms_health()->powerdown_lower_voltage = 0;
  80. return;
  81. }
  82. AUX_VOL_OPEN(0);
  83. CS1180_PWR_ENABLE(0);
  84. ml5238_enable_discharger_mosfet(0);
  85. ml5238_enable_charger_mosfet(0);
  86. ml5238_power_down();
  87. }
  88. #endif
  89. }
  90. static void _process_deepsleep(s32 health){
  91. #if (ALLOW_DEEP_SLEEP==1)
  92. if (health != Health_Success){
  93. return;
  94. }
  95. if (ml5238_is_charging() || ml5238_is_discharging() || IS_CHARGER_IN()){
  96. return;
  97. }
  98. if (!(io_state()->hall_detect)){
  99. mcu_enter_deepsleep();
  100. }
  101. #endif
  102. }
  103. static void _process_iostate_changed(void){
  104. if (!(io_state()->hall_detect)){
  105. if (ml5238_is_discharging()){
  106. ml5238_enable_discharger_mosfet(0);
  107. }
  108. if (!AUX_VOL_IS_OPEN()){
  109. AUX_VOL_OPEN(1);
  110. }
  111. }
  112. if (IS_CHARGER_IN()) {
  113. if (!ml5238_is_charging()){
  114. ml5238_enable_charger_mosfet(1);
  115. }
  116. }
  117. }
  118. static u32 _bms_main_task_handler(void){
  119. s32 unhealth = _process_unheath();
  120. _process_user_request(unhealth);
  121. _process_deepsleep(unhealth);
  122. _process_power_down();
  123. _process_iostate_changed();
  124. return 0;
  125. }
  126. static debounce_t _charging_detect = {.count = 0, .max_count = 10};
  127. static void check_charging(){
  128. if (!_bms_state.charging) {
  129. if (measure_value()->load_current >= MIN_START_CHARGER_CURRENT) {
  130. debounce_inc(_charging_detect);
  131. }else {
  132. debounce_reset(_charging_detect);
  133. }
  134. if (debounce_reach_max(&_charging_detect)){
  135. _bms_state.charging = 1;
  136. _bms_state.discharging = 0;
  137. debounce_reset(_charging_detect);
  138. }
  139. }else {
  140. if (measure_value()->load_current <= MIN_START_LOADING_CURRENT) {
  141. debounce_inc(_charging_detect);
  142. }else {
  143. debounce_reset(_charging_detect);
  144. }
  145. if (debounce_reach_max(_charging_detect)){
  146. _bms_state.charging = 0;
  147. _bms_state.discharging = 1;
  148. debounce_reset(_charging_detect);
  149. }
  150. }
  151. }
  152. static void _current_notify(void){
  153. check_current_state(); //check health of current
  154. check_charging();
  155. }
  156. /* 需要检查电芯的电压,如果发现有电芯电压过高,需要开启被动均衡
  157. * 充电过程中考虑balance,主要是希望cell 电压扩散后,保证1. 单电芯不能过压, 2. 单电芯不能比平均电压过低,导致
  158. * 木桶效应,目标是电压最高的那个cell,尽量压制,不让电压再升高,或者升高的尽量慢一些
  159. */
  160. static debounce_t _cell_balance = {.count = 10, .max_count = 20};
  161. static void check_cell_balance(uint8_t current_max_index){
  162. if (!_bms_state.charging){ //not charging, need not do balance
  163. if (_bms_state.pack_balancing){
  164. _bms_state.pack_balancing = 0;
  165. _cell_balance.count = 10;
  166. ml5238_cell_start_balance(0);
  167. }
  168. return;
  169. }
  170. if (!_bms_state.pack_balancing && _bms_state.cell_min_vol < CELL_FUSION_VOLTAGE){
  171. return;
  172. }
  173. if (_bms_state.cell_max_vol - _bms_state.cell_min_vol >= MAX_DIFF_BETWEEN_MIN_MAX_CELL){
  174. debounce_inc(_cell_balance);
  175. }else {
  176. debounce_dec(_cell_balance);
  177. }
  178. if (!_bms_state.pack_balancing && debounce_reach_max(_cell_balance)){
  179. _bms_state.pack_balancing = 1;
  180. }else if (_bms_state.pack_balancing && debounce_reach_zero(_cell_balance)){
  181. _bms_state.pack_balancing = 0;
  182. ml5238_cell_start_balance(0);
  183. }
  184. if (_bms_state.pack_balancing && (current_max_index != _bms_state.cell_index_of_max_vol)){
  185. ml5238_cell_start_balance(BIT(current_max_index));
  186. }
  187. _bms_state.cell_index_of_max_vol = current_max_index;
  188. }
  189. static uint8_t calc_cell_voltage(void){
  190. uint16_t voltage = 0;
  191. uint16_t max_cell = 0;
  192. uint16_t min_cell = 0xf000;
  193. uint8_t max_index = 0;
  194. for (int i = 0; i < CELLS_NUM; i++){
  195. voltage += measure_value()->cell_vol[i];
  196. if (max_cell > measure_value()->cell_vol[i]){
  197. max_cell = measure_value()->cell_vol[i];
  198. max_index = i;
  199. }
  200. if (min_cell < measure_value()->cell_vol[i]){
  201. min_cell = measure_value()->cell_vol[i];
  202. }
  203. }
  204. _bms_state.pack_voltage = voltage;
  205. _bms_state.cell_max_vol = max_cell;
  206. _bms_state.cell_min_vol = min_cell;
  207. return max_index;
  208. }
  209. static void _voltage_notify(void){
  210. uint8_t max_index = calc_cell_voltage();
  211. check_voltage_state(); //check health of cell voltage
  212. #if (ALLOW_5238_BALANCE==1)
  213. check_cell_balance(max_index);
  214. #endif
  215. }
  216. static void _temperature_notify(void){
  217. check_temp_state(); //check health of cell/pcb temperature
  218. }