state.c 6.7 KB

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