bms_message.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #include <string.h>
  2. #include "app/sox/soc.h"
  3. #include "app/sox/measure.h"
  4. #include "app/sox/measure_task.h"
  5. #include "app/sox/health.h"
  6. #include "app/sox/state.h"
  7. #include "bsp/gpio.h"
  8. #include "bsp/ml5238.h"
  9. #include "bsp/fmc_flash.h"
  10. #include "bsp/cs1180.h"
  11. #include "app/nv_storage.h"
  12. #include "libs/logger.h"
  13. #include "protocol.h"
  14. #include "bms_message.h"
  15. #include "event_record.h"
  16. extern char* bsp_get_fversion(void);
  17. static uint8_t bms_insert = 0;
  18. static uint8_t bms_insert_ack = 0;
  19. //主要用来告知PSxxx是否刚插入,PSxxx答复后需要清除
  20. void bms_message_update_insert(int is_hall_detect){
  21. if (!is_hall_detect){
  22. bms_insert = 0;
  23. bms_insert_ack = 0;
  24. }else {
  25. if (!bms_insert_ack) {
  26. bms_insert = 1;
  27. }
  28. }
  29. }
  30. void process_bms_message(can_frame_t *frame, int len){
  31. int result = 0;
  32. uint8_t *data = NULL;
  33. int data_len = 0;
  34. // set_log_all(L_debug);
  35. switch(frame->key) {
  36. case CAN_KEY_BMS_SET_POWER:
  37. if (len != sizeof(pwr_cmd_t) || frame->head.can_addr != 0x42){//开关大电必须42发过来
  38. result = 1;
  39. }else {
  40. pwr_cmd_t *cmd = (pwr_cmd_t *)frame->data;
  41. uint32_t user_request = USER_REQUEST_PENDING;
  42. if (cmd->charger_mask) {
  43. if (cmd->charger_fet){
  44. user_request |= USER_REQUEST_CHARGER_ON;
  45. }else {
  46. user_request |= USER_REQUEST_CHARGER_OFF;
  47. }
  48. }
  49. if (cmd->discharger_mask) {
  50. if (cmd->discharger_fet){
  51. user_request |= USER_REQUEST_DISCHARGER_ON;
  52. }else {
  53. user_request |= USER_REQUEST_DISCHARGER_OFF;
  54. }
  55. }
  56. if (cmd->small_mask) {
  57. if (cmd->small_power){
  58. user_request |= USER_REQUEST_SMALLCURRENT_ON;
  59. }else {
  60. user_request |= USER_REQUEST_SMALLCURRENT_OFF;
  61. }
  62. }
  63. bms_state_get()->user_request = user_request;
  64. }
  65. protocol_send_ack(frame->head.can_addr, frame->key, result);
  66. break;
  67. case CAN_KEY_BMS_BASE_INFO:{
  68. binfo_cmd_resp_t bresp;
  69. bresp.capacity = get_soc()->capacity;
  70. if (get_soc()->coulomb_now >= get_soc()->coulomb_min) {
  71. bresp.energy = get_soc()->coulomb_now - get_soc()->coulomb_min;
  72. }else{
  73. bresp.energy = 0;
  74. }
  75. bresp.pack_current = measure_value()->load_current;
  76. bresp.pack_voltage = bms_state_get()->pack_voltage;
  77. bresp.max_temp = -100;
  78. for (int i = 0; i < PACK_TEMPS_NUM; i ++){
  79. if (bresp.max_temp < measure_value()->pack_temp[i]){
  80. bresp.max_temp = measure_value()->pack_temp[i];
  81. }
  82. }
  83. bresp.health = bms_health()->i_status;
  84. if (bms_is_ps_charger_in()) {/*如果在底座或者车上(有充电器),提前置位过高/低温充电标志*/
  85. bresp.health |= (bms_health()->lower_temp_deny_charger << 12 | bms_health()->over_temp_deny_charger << 14);
  86. }
  87. bresp.health &= ~(1 << 8);
  88. bresp.health |= ((get_soc()->flags & SOC_FLAG_CALIBRATED) != 0) << 8;
  89. stat_cmd_resp_t sresp;
  90. sresp.insert = bms_insert;
  91. sresp.is_charging = bms_state_get()->charging;
  92. sresp.discharger_fet = ml5238_is_discharging();
  93. sresp.charger_fet = ml5238_is_charging();
  94. sresp.small_power = AUX_VOL_IS_OPEN();
  95. sresp.is_balancing = bms_state_get()->pack_balancing;
  96. bresp.state = *((uint8_t*)&sresp);
  97. data = (uint8_t *)&bresp;
  98. data_len = sizeof(bresp);
  99. protocol_send_bms_info(frame->head.can_addr, frame->key, data, data_len);
  100. break;
  101. }
  102. case CAN_KEY_BMS_CHARG_INFO:{
  103. cinfo_cmd_resp_t cresp;
  104. cresp.charge_current = measure_value()->load_current;
  105. cresp.charge_remain_time = soc_get_charger_remain_time();
  106. data = (uint8_t *)&cresp;
  107. data_len = sizeof(cresp);
  108. protocol_send_bms_info(frame->head.can_addr, frame->key, data, data_len);
  109. break;
  110. }
  111. case CAN_KEY_BMS_CLEAR:
  112. bms_insert_ack = 1;
  113. bms_insert = 0;
  114. bms_work_mode_set(WORK_MODE_AGING_TEST, 0);
  115. protocol_send_ack(frame->head.can_addr, frame->key, result);
  116. break;
  117. case CAN_KEY_BMS_GET_TIME:{
  118. uint32_t time = shark_get_seconds();
  119. data = (uint8_t *)&time;
  120. data_len = sizeof(time);
  121. protocol_send_bms_info(frame->head.can_addr, frame->key, data, data_len);
  122. break;
  123. }
  124. case CAN_KEY_BMS_GET_STAT: {
  125. stat_cmd_resp_t sresp;
  126. sresp.insert = bms_insert;
  127. sresp.is_charging = bms_state_get()->charging;
  128. sresp.discharger_fet = ml5238_is_discharging();
  129. sresp.charger_fet = ml5238_is_charging();
  130. sresp.small_power = AUX_VOL_IS_OPEN();
  131. sresp.is_balancing = bms_state_get()->pack_balancing;
  132. sresp.health = (((uint16_t )(bms_health()->i_status)) != 0);
  133. data = (uint8_t *)&sresp;
  134. data_len = sizeof(sresp);
  135. protocol_send_bms_info(frame->head.can_addr, frame->key, data, data_len);
  136. break;
  137. }
  138. case CAN_KEY_BMS_TEMPS: {
  139. u8 temps[PACK_TEMPS_NUM * sizeof(int) + 1];
  140. temps[0] = PACK_TEMPS_NUM;
  141. memcpy(temps+1, measure_value()->pack_temp, PACK_TEMPS_NUM * sizeof(int));
  142. data = temps;
  143. data_len = PACK_TEMPS_NUM * sizeof(int) + 1;
  144. protocol_send_bms_info(frame->head.can_addr, frame->key, data, data_len);
  145. break;
  146. }
  147. case CAN_KEY_BMS_GET_CELLS: {
  148. cell_cmd_resp_t cells;
  149. cells.cell_num = CELLS_NUM;
  150. for (int i = 0; i < CELLS_NUM; i++){
  151. cells.voltages[i] = measure_value()->cell_vol[i];
  152. }
  153. data = (uint8_t *)&cells;
  154. data_len = sizeof(cells);
  155. protocol_send_bms_info(frame->head.can_addr, frame->key, data, data_len);
  156. break;
  157. }
  158. case CAN_KEY_GET_SOC_INFO: {
  159. soc_info_t soc;
  160. soc.c_min = get_soc()->coulomb_min;
  161. soc.c_max = get_soc()->coulomb_max;
  162. soc.c_now = get_soc()->coulomb_now;
  163. soc.c_discharger = get_soc()->dischrger_coulomb;
  164. soc.c_charger = get_soc()->charger_coulomb;
  165. soc.cycle = soc_get_cycle();
  166. soc.calibrated = (get_soc()->flags & SOC_FLAG_CALIBRATED) != 0;
  167. data = (uint8_t *)&soc;
  168. data_len = sizeof(soc);
  169. protocol_send_bms_info(frame->head.can_addr, frame->key, data, data_len);
  170. break;
  171. }
  172. case CAN_KEY_BMS_GET_HEALTH_STAT:
  173. data = (uint8_t *)(&bms_health()->i_status);
  174. data_len = sizeof(bms_health()->i_status);
  175. protocol_send_bms_info(frame->head.can_addr, frame->key, data, data_len);
  176. break;
  177. case CAN_KEY_BMS_SET_WORK_MODE:
  178. if (len != 2) {
  179. result = 1;
  180. }else {
  181. result = bms_work_mode_set(frame->data[0], frame->data[1]);
  182. }
  183. protocol_send_ack(frame->head.can_addr, frame->key, result);
  184. break;
  185. case CAN_KEY_SET_SN:
  186. nv_save_sn((uint8_t *)frame->data+1, len-2);
  187. protocol_send_ack(frame->head.can_addr, frame->key, result);
  188. break;
  189. case CAN_KEY_GET_SN: {
  190. uint8_t sn[32];
  191. int sn_len = nv_read_sn(sn, sizeof(sn));
  192. if (sn_len <= 0){
  193. sn[0] = 'B';
  194. memset(sn + 1, '0', sizeof(sn) - 1);
  195. sn_len = 18;
  196. }
  197. data = (u8 *)sn;
  198. data_len = sn_len;
  199. protocol_send_bms_info(frame->head.can_addr, frame->key, data, data_len);
  200. break;
  201. }
  202. case CAN_KEY_GET_VERSION: {
  203. data = (u8*)bsp_get_fversion();
  204. data_len = strlen((char *)data);
  205. protocol_send_bms_info(frame->head.can_addr, frame->key, data, data_len);
  206. break;
  207. }
  208. case CAN_KEY_SET_LOGGER:
  209. if (len < 1) {
  210. set_log_all(L_debug);
  211. ml5238_reg_log(); //just for debug
  212. cs1180_log();
  213. health_log();
  214. soc_log();
  215. bms_state_log();
  216. result = 1;
  217. } else if (len < 2) {
  218. set_log_all(frame->data[0]);
  219. ml5238_reg_log(); //just for debug
  220. cs1180_log();
  221. health_log();
  222. soc_log();
  223. bms_state_log();
  224. } else if (len < 3){
  225. set_log_level(frame->data[0], frame->data[1]);
  226. }
  227. protocol_send_ack(frame->head.can_addr, frame->key, result);
  228. break;
  229. case CAN_KEY_RESTORE_NV: {
  230. restore_nv_cmd_t *nv = (restore_nv_cmd_t *)frame->data;
  231. nv_save_sn(nv->sn, nv->sn_len);
  232. soc_restore_by_iap(nv->flags, nv->capacity);
  233. protocol_send_ack(frame->head.can_addr, frame->key, 1);
  234. break;
  235. }
  236. case CAN_KEY_MIN_SOC:
  237. get_soc()->coulomb_min = (u32)frame->data[0] * 3600.0f;
  238. nv_save_all_soc();
  239. protocol_send_ack(frame->head.can_addr, frame->key, 1);
  240. break;
  241. case CAN_KEY_GET_EVENT: {
  242. u8 event[sizeof(event_record_t) * 8 + 1];
  243. u16 offset = DECODE_U16(frame->data);
  244. int num = get_event(8, offset, event + 1);
  245. *event = (u8)num;
  246. protocol_send_bms_info(frame->head.can_addr, frame->key, event, sizeof(event_record_t) * num + 1);
  247. break;
  248. }
  249. default:
  250. break;
  251. }
  252. }