bms_message.c 9.8 KB

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