bms_message.c 9.3 KB

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