bms_message.c 7.5 KB

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