state.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. #include "bsp/gpio.h"
  2. #include "bsp/ml5238.h"
  3. #include "bsp/uart.h"
  4. #include "bsp/mcu_power_sleep.h"
  5. #include "app/sox/measure.h"
  6. #include "app/sox/measure_task.h"
  7. #include "libs/shark_task.h"
  8. #include "libs/logger.h"
  9. #include "app/nv_storage.h"
  10. #include "health.h"
  11. #include "soc.h"
  12. #include "state.h"
  13. #include "iostate.h"
  14. #define ALLOW_DEEP_SLEEP 1
  15. #define SLEEP_IGNORE_UNHEALTH 0
  16. #define ALLOW_POWER_DOWN 1 //disable power down for debug
  17. #define ALLOW_5238_BALANCE 1
  18. #define IGNORE_DISCHARGER_LOW_VOL 0 //忽略放电欠压
  19. static void _current_notify(void);
  20. static void _voltage_notify(void);
  21. static void _temperature_notify(void);
  22. static u32 _bms_main_task_handler(void);
  23. static void _debug_timer_handler(shark_timer_t *t);
  24. static bms_state_t _bms_state;
  25. static int pcb_temp = 100;
  26. static int pcb_temp_count = 0;
  27. static int ml5238_cali_count = 0;
  28. static shark_task_t _bms_main_task = {.handler = _bms_main_task_handler};
  29. static shark_timer_t _debug_timer = {.handler = _debug_timer_handler};
  30. void bms_state_init(void){
  31. set_log_level(MOD_STATE, L_debug);
  32. state_debug("BMS System Starting......\n");
  33. _bms_state.cell_index_of_max_vol = 0xff;
  34. _bms_state.bms_addr = 0x30;
  35. measure_task_init(_current_notify, _voltage_notify, _temperature_notify);
  36. io_state_init();
  37. health_init();
  38. soc_init();
  39. _bms_state.user_request = USER_REQUEST_PENDING | USER_REQUEST_SMALLCURRENT_ON;
  40. shark_task_add(&_bms_main_task);
  41. shark_timer_post(&_debug_timer, 2000);
  42. pcb_temp = measure_value()->pack_temp[PCB_TEMP_INDEX];
  43. }
  44. bms_state_t *bms_state_get(void){
  45. return &_bms_state;
  46. }
  47. int bms_work_is_normal(void){
  48. return _bms_state.work_mode == WORK_MODE_NORMAL;
  49. }
  50. int bms_work_is_aging_test(void){
  51. return _bms_state.work_mode == WORK_MODE_AGING_TEST;
  52. }
  53. int bms_work_is_pcba_test(void){
  54. return _bms_state.work_mode == WORK_MODE_PCBA_TEST;
  55. }
  56. int bms_work_is_pack_test(void){
  57. return _bms_state.work_mode == WORK_MODE_PCBA_TEST;
  58. }
  59. int bms_work_is_calibrating(void){
  60. return _bms_state.work_mode == WORK_MODE_CALIBRATE;
  61. }
  62. int bms_work_mode_set(int mode, int start){
  63. if (mode < WORK_MODE_AGING_TEST || mode > WORK_MODE_CALIBRATE){
  64. return 1;
  65. }
  66. if (start){
  67. _bms_state.work_mode = mode;
  68. }else {
  69. _bms_state.work_mode = WORK_MODE_NORMAL;
  70. }
  71. return 0;
  72. }
  73. void bms_set_ps_charger_in(uint16_t mask, uint16_t in){
  74. _bms_state.ps_charger_in = in;
  75. _bms_state.ps_charger_mask = mask;
  76. }
  77. void bms_state_log(void){
  78. state_debug("Life Time: %d\n", shark_get_seconds());
  79. state_debug("Sleep Time: %ds\n", get_system_sleep_time());
  80. state_debug("ml5238 cali: %d\n", ml5238_cali_count);
  81. state_debug("ps charger mask:in %d, %d\n", _bms_state.ps_charger_mask, _bms_state.ps_charger_in);
  82. #if 0
  83. state_debug("Charging: %d\n", _bms_state.charging);
  84. state_debug("WorkMode %d\n", _bms_state.work_mode);
  85. state_debug("DMos: %d\n", ml5238_is_discharging());
  86. state_debug("CMos: %d\n", ml5238_is_charging());
  87. state_debug("AuxPower: %d\n", AUX_VOL_IS_OPEN());
  88. state_debug("WorkMode:0x%x\n", _bms_state.work_mode);l
  89. #endif
  90. }
  91. static void _debug_timer_handler(shark_timer_t *t){
  92. #if 0
  93. static int _log_count = 0;
  94. int mod = _log_count % 4;
  95. if (mod == 0){
  96. bms_state_log();
  97. }
  98. if (mod == 1) {
  99. iostate_log();
  100. }
  101. if (mod == 2) {
  102. soc_log();
  103. }
  104. if (mod == 3) {
  105. measure_log();
  106. }
  107. _log_count ++;
  108. #else
  109. bms_state_log();
  110. measure_log();
  111. #endif
  112. shark_timer_post(&_debug_timer, 2000);
  113. }
  114. /*
  115. 放电mos和充电mos的开关要小心:
  116. 1. 大部分的情况下,尽量能做到同时开关,主要是用来保护被关闭那路mos的体二极管(不能过大电流)
  117. 2. 充电过压的情况下,必须要关闭充电mos,但是这个时候放电mos可能是打开的,这样的情况下,
  118. 需要检测放电电流,超过10A必须强制打开充电mos,防止烧充电mos的体二极管
  119. 3. 收到打开大电的指令后,必须两个mos都要打开,然后再经过2的判断
  120. 。。。。。
  121. */
  122. void discharger_open(int open){
  123. /* 打开大电前,先打开短路保护*/
  124. if (open) {
  125. int mode = SHORT_CURRENT_MODE_100A_200A;
  126. int try_count = 3;
  127. /* 确保短路保护设置成功后才能开大电 */
  128. do {
  129. ml5238_short_current_detect(mode);//SP600:100A, SP700:200A
  130. }while(!ml5238_is_short_current_enabled(mode) && (try_count-- >= 0));
  131. if ((try_count < 0) && !ml5238_is_short_current_enabled(mode)){
  132. state_error("set short current error\n");
  133. return;
  134. }
  135. }else {
  136. ml5238_short_current_detect(SHORT_CURRENT_MODE_DISABLE);
  137. }
  138. ml5238_enable_discharger_mosfet(open);
  139. }
  140. void charger_open(int open) {
  141. ml5238_enable_charger_mosfet(open);
  142. }
  143. void start_aux_power(int start){
  144. if (start){
  145. AUX_VOL_OPEN(1);
  146. }else {
  147. AUX_VOL_OPEN(0);
  148. health_stop_aux_detect();
  149. }
  150. }
  151. #define Health_Success 0
  152. #define Health_Discharger_Failt 1
  153. #define Health_charger_Fault 2
  154. #define Health_aux_Fault 4
  155. #define Health_Fault_Can_Sleep 8
  156. static u64 discharger_close_time = 0;
  157. static s32 _process_unheath(void){
  158. u32 unhealth = Health_Success;
  159. if (bms_health()->load_current_short) {//短路检测后,关闭充放电mos
  160. discharger_open(0);
  161. charger_open(0); //disable charger mosfet
  162. start_aux_power(0);
  163. _bms_state.charging = 0;
  164. unhealth = (Health_Discharger_Failt | Health_charger_Fault);
  165. }
  166. if (!bms_work_is_normal()){
  167. return unhealth; //测试模式只关注短路保护
  168. }
  169. if (bms_health()->charger_over_current || bms_health()->charger_over_temp || bms_health()->charger_lower_temp ||
  170. bms_health()->charger_over_voltage || bms_health()->sigle_cell_over_voltage){
  171. if (_bms_state.ps_charger_mask && !_bms_state.ps_charger_in){
  172. }else {
  173. charger_open(0); //disable charger mosfet
  174. unhealth |= Health_charger_Fault;
  175. }
  176. }
  177. if (bms_health()->over_temp_deny_charger|| bms_health()->lower_temp_deny_charger) {
  178. if (_bms_state.ps_charger_mask && !_bms_state.ps_charger_in){
  179. }else if (_bms_state.charging) {
  180. charger_open(0); //disable charger mosfet
  181. unhealth |= Health_charger_Fault;
  182. }
  183. }
  184. if (bms_health()->discharger_over_temp || bms_health()->discharger_lower_temp){
  185. if (bms_health()->discharger_over_temp){ //放电过高温后,小电流也必须关闭
  186. start_aux_power(0);
  187. unhealth |= Health_aux_Fault;
  188. }
  189. discharger_open(0); //disable charger mosfet
  190. unhealth |= Health_Discharger_Failt;
  191. }
  192. if (bms_health()->sigle_cell_lower_voltage || bms_health()->discharger_lower_voltage) {
  193. unhealth |= Health_Fault_Can_Sleep;
  194. #if IGNORE_DISCHARGER_LOW_VOL==0
  195. if (!(_bms_state.ps_charger_mask && _bms_state.ps_charger_in)) {//PSxxx 告知有充电器插入,忽略欠压
  196. unhealth |= Health_Discharger_Failt;
  197. if (ml5238_is_discharging()) {
  198. if (discharger_close_time == 0) {
  199. discharger_close_time = shark_get_mseconds();
  200. }else if (shark_get_mseconds() >= (discharger_close_time + 10 * 1000)){
  201. discharger_open(0);
  202. discharger_close_time = 0;
  203. }
  204. }
  205. }
  206. #endif
  207. }else {
  208. discharger_close_time = 0;
  209. }
  210. if (bms_health()->over_temp_deny_discharger|| bms_health()->lower_temp_deny_discharger) {
  211. if (!_bms_state.charging) {
  212. discharger_open(0); //disable discharger mosfet
  213. }
  214. unhealth |= (Health_Discharger_Failt | Health_Fault_Can_Sleep);
  215. }
  216. if (io_state()->aux_lock_detect || bms_health()->small_current_short) {
  217. unhealth |= Health_aux_Fault;
  218. if (bms_health()->small_current_real_short) {
  219. unhealth |= Health_Discharger_Failt;
  220. }
  221. }
  222. return unhealth;
  223. }
  224. //处理PS100/310/320/360,充电底座,充电柜的指令或者bms自己发给自己的指令
  225. static void _process_user_request(s32 health){
  226. if (_bms_state.user_request & USER_REQUEST_PENDING){
  227. //开关小电
  228. if (_bms_state.user_request & USER_REQUEST_SMALLCURRENT_OFF){
  229. start_aux_power(0);
  230. }else if (_bms_state.user_request & USER_REQUEST_SMALLCURRENT_ON){
  231. if ((health & Health_aux_Fault) == 0){
  232. start_aux_power(1);
  233. }
  234. }
  235. //开关放电
  236. if (_bms_state.user_request & USER_REQUEST_CHARGER_OFF){
  237. charger_open(0);
  238. }else if (_bms_state.user_request & USER_REQUEST_CHARGER_ON){
  239. if ((health & Health_charger_Fault) == 0){
  240. charger_open(1);
  241. }
  242. }
  243. //开关充电
  244. if (_bms_state.user_request & USER_REQUEST_DISCHARGER_OFF){
  245. discharger_open(0);
  246. }else if (_bms_state.user_request & USER_REQUEST_DISCHARGER_ON) {
  247. if ((health & Health_Discharger_Failt) == 0){
  248. discharger_open(1);
  249. }
  250. }
  251. _bms_state.user_request &= ~USER_REQUEST_PENDING;//clear user request pending
  252. }
  253. }
  254. static void _process_power_down(void){
  255. #if (ALLOW_POWER_DOWN==1)
  256. if (bms_health()->powerdown_lower_voltage){
  257. state_debug("BMS System PowerDown!!\n");
  258. if (soc_update_by_ocv()) {
  259. nv_save_soc();
  260. }
  261. shark_uart_flush();
  262. if (io_state()->charger_detect_irq || io_state()->charger_detect){//have charger, do'nt power down
  263. bms_health()->powerdown_lower_voltage = 0;
  264. return;
  265. }
  266. start_aux_power(0);
  267. discharger_open(0);
  268. charger_open(0);
  269. /*需要等待B-和P-之间的电容放电掉后,才能设置5238 power down,
  270. 否则5238会触发充电器插入检测,导致重新开机,进入powerdown <->开机的无限循环*/
  271. ml5238_enable_charger_detect(AUX_VOL_IS_OPEN(), 1);
  272. delay_us(2* 1000);
  273. u64 wait_start = shark_get_mseconds();
  274. while(!ml5238_charger_is_disconnect(AUX_VOL_IS_OPEN())){
  275. shark_uart_flush();
  276. wdog_reload();
  277. if (shark_get_mseconds() - wait_start >= 2000){
  278. bms_health()->powerdown_lower_voltage = 0;
  279. ml5238_enable_charger_detect(AUX_VOL_IS_OPEN(), 0);
  280. return;
  281. }
  282. }
  283. CS1180_PWR_ENABLE(0);
  284. DCDC_VOL_OPEN(0);
  285. ml5238_power_down();
  286. }
  287. #endif
  288. }
  289. static void _process_deepsleep(s32 health){
  290. #if (ALLOW_DEEP_SLEEP==1)
  291. static u64 _sleep_time = 0;
  292. #if (SLEEP_IGNORE_UNHEALTH==0)
  293. if ((health != Health_Success) && ((health & Health_Fault_Can_Sleep) != Health_Fault_Can_Sleep)){
  294. return;
  295. }
  296. #endif
  297. if (!bms_work_is_normal()){
  298. return; //测试模式下不休眠
  299. }
  300. if (ml5238_is_charging() || ml5238_is_discharging() || io_state()->charger_detect_irq || _bms_state.charging){
  301. return;
  302. }
  303. if(io_state()->hall_detect){
  304. return;
  305. }
  306. if (!shark_uart_timeout()){
  307. return;
  308. }
  309. if (io_state()->aux_lock_detect){
  310. return;
  311. }
  312. if (shark_get_mseconds() < (_sleep_time + 3 * 1000)){
  313. return;
  314. }
  315. nv_save_soc();
  316. mcu_enter_deepsleep();
  317. soc_update_for_deepsleep(mcu_get_sleeptime());//补偿休眠的功耗
  318. _sleep_time = shark_get_mseconds();
  319. #endif
  320. }
  321. /*when work as test mode, we do'n need close the discharger */
  322. static int _can_close_mos_no_hall(void){
  323. if (bms_work_is_normal()){
  324. return 1;
  325. }
  326. return 0;
  327. }
  328. static void _process_iostate_changed(s32 unhealth){
  329. static u64 _hall_no_time = 0;
  330. if (!(io_state()->hall_detect)&& _can_close_mos_no_hall()){
  331. bms_set_ps_charger_in(0, 0);
  332. /* 无hall延时2s关大电,确保仪表等负载快速插拔电池能正常,不闪屏 */
  333. if (shark_get_mseconds() - _hall_no_time >= 2 * 1000) {
  334. if (ml5238_is_discharging() && (!_bms_state.charging)){
  335. discharger_open(0);
  336. }
  337. if (!AUX_VOL_IS_OPEN()){
  338. start_aux_power(1);
  339. }
  340. if (!io_state()->charger_detect_irq && ml5238_is_charging() && (!_bms_state.charging)){
  341. charger_open(0);
  342. }
  343. }
  344. }else {
  345. _hall_no_time = shark_get_mseconds();
  346. }
  347. if (io_state()->charger_detect_irq && ((unhealth & Health_charger_Fault) == 0) && (_bms_state.cell_max_vol < SIGLE_CELL_MAX_CHARGER_VOLTAGE)) {
  348. if (!ml5238_is_charging() && shark_uart_timeout()){//不在车上,底座上,充电柜上,检测到充电器插入,自动打开充电,否则的话,只能通过指令来打开充电mos
  349. if (!(bms_health()->over_temp_deny_charger|| bms_health()->lower_temp_deny_charger)) {
  350. charger_open(1);
  351. }
  352. }
  353. }
  354. if (io_state()->hall_detect){
  355. _bms_state.bms_addr = 0x30 + 1;
  356. }else {
  357. _bms_state.bms_addr = 0x30;
  358. }
  359. }
  360. static u32 _bms_main_task_handler(void){
  361. s32 unhealth = _process_unheath();
  362. _process_user_request(unhealth);
  363. _process_deepsleep(unhealth);
  364. _process_power_down();
  365. _process_iostate_changed(unhealth);
  366. return 0;
  367. }
  368. extern void show_leds_for_charging(uint8_t charging);
  369. static debounce_t _charging_detect = {.count = 0, .max_count = 10, .init_count = 0};
  370. static void check_charging(){
  371. if ((measure_value()->load_current >= MIN_START_CHARGER_CURRENT)) {
  372. if (!_bms_state.charging) {
  373. debounce_inc(_charging_detect);
  374. if (debounce_reach_max(_charging_detect)){
  375. _bms_state.charging = 1;
  376. show_leds_for_charging(1);
  377. debounce_reset(_charging_detect);
  378. }
  379. }else {
  380. debounce_reset(_charging_detect);
  381. }
  382. }else/* if ((measure_value()->load_current < MIN_START_LOADING_CURRENT))*/{
  383. if (_bms_state.charging) {
  384. debounce_inc(_charging_detect);
  385. if (debounce_reach_max(_charging_detect)){
  386. _bms_state.charging = 0;
  387. show_leds_for_charging(0);
  388. debounce_reset(_charging_detect);
  389. }
  390. }else {
  391. debounce_reset(_charging_detect);
  392. }
  393. }
  394. }
  395. /* if discharger mos and charger mos, one is open but other is closed.
  396. we must judage the current: if current is large than 10A(-10A),
  397. we must open the closed mos to avoid the closed mos to be destroyed
  398. */
  399. #define MIN_CURRENT_FOR_BOTH_MOS_OPEN (300)
  400. static int _min_current_for_both_mos_count = 0;
  401. static void _check_mos_stat(void){
  402. if (abs(measure_value()->load_current) >= MIN_CURRENT_FOR_BOTH_MOS_OPEN){
  403. _min_current_for_both_mos_count ++;
  404. if (_min_current_for_both_mos_count >= 2){
  405. int dmos = ml5238_is_discharging();
  406. int cmos = ml5238_is_charging();
  407. if (dmos + cmos == 0){
  408. //state_error("current = %d, but all mos is closed\n", measure_value()->load_current);
  409. return;
  410. }
  411. if (dmos == 1 && cmos == 1){
  412. return;
  413. }
  414. uint32_t request = USER_REQUEST_PENDING;
  415. if (!dmos) {
  416. request |= USER_REQUEST_DISCHARGER_ON;
  417. }else {
  418. request |= USER_REQUEST_CHARGER_ON;
  419. }
  420. _bms_state.user_request = request;
  421. }
  422. }else {
  423. _min_current_for_both_mos_count = 0;
  424. }
  425. }
  426. static void _current_notify(void){
  427. check_charging();
  428. check_current_state(); //check health of current
  429. _check_mos_stat();
  430. soc_update(); //计算soc
  431. }
  432. #if (ALLOW_5238_BALANCE==1)
  433. /* 需要检查电芯的电压,如果发现有电芯电压过高,需要开启被动均衡
  434. * 充电过程中考虑balance,主要是希望cell 电压扩散后,保证1. 单电芯不能过压, 2. 单电芯不能比平均电压过低,导致
  435. * 木桶效应,目标是电压最高的那个cell,尽量压制,不让电压再升高,或者升高的尽量慢一些
  436. */
  437. static void _balance_timer_handler(shark_timer_t *t);
  438. static shark_timer_t _balance_timer = {.handler = _balance_timer_handler};
  439. static debounce_t _cell_balance = {.count = 0, .max_count = 10, .init_count = 0};
  440. static void _balance_timer_handler(shark_timer_t *t){
  441. ml5238_cell_start_balance(0);
  442. _bms_state.pack_balancing = 0;
  443. }
  444. static uint32_t get_balance_mask(uint8_t current_max_index){
  445. return BIT(current_max_index);
  446. }
  447. static void check_cell_balance(uint8_t current_max_index){
  448. if (!_bms_state.charging){ //not charging, need not do balance
  449. if (_bms_state.pack_balancing){
  450. _bms_state.pack_balancing = 0;
  451. _cell_balance.count = 10;
  452. ml5238_cell_start_balance(0);
  453. shark_timer_cancel(&_balance_timer);
  454. }
  455. return;
  456. }
  457. if ((!_bms_state.pack_balancing && _bms_state.cell_max_vol < MAX_CELL_VOLTAGE_FOR_BALACNE) || _bms_state.pack_balancing){
  458. return;
  459. }
  460. if (_bms_state.cell_max_vol >= MAX_CELL_VOLTAGE_FOR_BALACNE){
  461. debounce_inc(_cell_balance);
  462. }else {
  463. debounce_reset(_cell_balance);
  464. }
  465. if (!_bms_state.pack_balancing && debounce_reach_max(_cell_balance)){
  466. _bms_state.pack_balancing = 1;
  467. ml5238_cell_start_balance(get_balance_mask(current_max_index));
  468. shark_timer_post(&_balance_timer, 30 * 1000); //stop balance after 30s
  469. debounce_reset(_cell_balance);
  470. }
  471. _bms_state.cell_index_of_max_vol = current_max_index;
  472. }
  473. #endif
  474. static uint8_t calc_cell_voltage(void){
  475. uint16_t voltage = 0;
  476. uint16_t max_cell = 0;
  477. uint16_t min_cell = 0xf000;
  478. uint8_t max_index = 0;
  479. for (int i = 0; i < CELLS_NUM; i++){
  480. voltage += measure_value()->cell_vol[i];
  481. if (max_cell < measure_value()->cell_vol[i]){
  482. max_cell = measure_value()->cell_vol[i];
  483. max_index = i;
  484. }
  485. if (min_cell > measure_value()->cell_vol[i]){
  486. min_cell = measure_value()->cell_vol[i];
  487. }
  488. }
  489. _bms_state.pack_voltage = voltage;
  490. _bms_state.cell_max_vol = max_cell;
  491. _bms_state.cell_min_vol = min_cell;
  492. return max_index;
  493. }
  494. static void _voltage_notify(void){
  495. uint8_t max_index = calc_cell_voltage();
  496. check_voltage_state(); //check health of cell voltage
  497. #if (ALLOW_5238_BALANCE==1)
  498. check_cell_balance(max_index);
  499. #endif
  500. }
  501. static void _temperature_notify(void){
  502. int pcb_current_temp = measure_value()->pack_temp[PCB_TEMP_INDEX];
  503. if (abs(pcb_temp - pcb_current_temp) >= 5){//pcb温度变化超过5度,需要重新校准ML5238
  504. if (pcb_temp_count ++ >= 5) {
  505. current_calibrate();
  506. state_warning("ML5238 calibrate, %d -> %d!!\n", pcb_temp, pcb_current_temp);
  507. pcb_temp = pcb_current_temp;
  508. pcb_temp_count = 0;
  509. ml5238_cali_count ++;
  510. }
  511. }else {
  512. pcb_temp_count = 0;
  513. }
  514. check_temp_state(); //check health of cell/pcb temperature
  515. }