state.c 7.7 KB

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