state.c 7.4 KB

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