bms_message.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include "app/sox/soc.h"
  2. #include "app/sox/measure.h"
  3. #include "app/sox/measure_task.h"
  4. #include "app/sox/health.h"
  5. #include "app/sox/state.h"
  6. #include "bsp/gpio.h"
  7. #include "bsp/ml5238.h"
  8. #include "libs/logger.h"
  9. #include "protocol.h"
  10. #include "bms_message.h"
  11. static uint8_t bms_insert = 0;
  12. static uint8_t bms_insert_ack = 0;
  13. //主要用来告知PSxxx是否刚插入,PSxxx答复后需要清除
  14. void bms_message_update_insert(int is_hall_detect){
  15. if (!is_hall_detect){
  16. bms_insert = 0;
  17. bms_insert_ack = 0;
  18. }else {
  19. if (!bms_insert_ack) {
  20. bms_insert = 1;
  21. }
  22. }
  23. }
  24. void process_bms_message(can_frame_t *frame, int len){
  25. int result = 0;
  26. uint8_t *data = NULL;
  27. int data_len = 0;
  28. switch(frame->key) {
  29. case CAN_KEY_BMS_SET_POWER:
  30. if (len != sizeof(pwr_cmd_t)){
  31. result = 1;
  32. }else {
  33. pwr_cmd_t *cmd = (pwr_cmd_t *)frame->data;
  34. uint32_t user_request = USER_REQUEST_PENDING;
  35. if (cmd->charger_fet){
  36. user_request |= USER_REQUEST_CHARGER;
  37. }
  38. if (cmd->discharger_fet){
  39. user_request |= USER_REQUEST_DISCHARGER;
  40. }
  41. if (cmd->small_power){
  42. user_request |= USER_REQUEST_SMALLCURRENT;
  43. }
  44. bms_state_get()->user_request = user_request;
  45. }
  46. protocol_send_ack(frame->head.can_addr, frame->key, result);
  47. break;
  48. case CAN_KEY_BMS_BASE_INFO:{
  49. binfo_cmd_resp_t bresp;
  50. bresp.capacity = get_soc()->capacity;
  51. bresp.energy = get_soc()->energy;
  52. bresp.pack_current = measure_value()->load_current;
  53. bresp.pack_voltage = bms_state_get()->pack_voltage;
  54. bresp.max_temp = 0;
  55. for (int i = 0; i < PACK_TEMPS_NUM; i ++){
  56. if (bresp.max_temp < measure_value()->pack_temp[i]){
  57. bresp.max_temp = measure_value()->pack_temp[i];
  58. }
  59. }
  60. bresp.cycle = soc_get_cycle();
  61. data = (uint8_t *)&bresp;
  62. data_len = sizeof(bresp);
  63. break;
  64. }
  65. case CAN_KEY_BMS_CHARG_INFO:{
  66. cinfo_cmd_resp_t cresp;
  67. cresp.charge_current = measure_value()->load_current;
  68. cresp.charge_remain_time = soc_get_charger_remain_time();
  69. data = (uint8_t *)&cresp;
  70. data_len = sizeof(cresp);
  71. break;
  72. }
  73. case CAN_KEY_BMS_CLEAR:
  74. bms_insert_ack = 1;
  75. bms_insert = 0;
  76. protocol_send_ack(frame->head.can_addr, frame->key, result);
  77. break;
  78. case CAN_KEY_BMS_GET_TIME:{
  79. uint32_t time = shark_get_seconds();
  80. data = (uint8_t *)&time;
  81. data_len = sizeof(time);
  82. break;
  83. }
  84. case CAN_KEY_BMS_GET_STAT: {
  85. stat_cmd_resp_t sresp;
  86. sresp.insert = bms_insert;
  87. sresp.is_charging = bms_state_get()->charging;
  88. sresp.discharger_fet = ml5238_is_discharging();
  89. sresp.charger_fet = ml5238_is_charging();
  90. sresp.small_power = AUX_VOL_IS_OPEN();
  91. sresp.is_balancing = bms_state_get()->pack_balancing;
  92. uint32_t *h = (uint32_t *)(bms_health());
  93. sresp.health = (*h != 0);
  94. data = (uint8_t *)&sresp;
  95. data_len = sizeof(sresp);
  96. break;
  97. }
  98. case CAN_KEY_BMS_TEMPS:
  99. data = (uint8_t *)measure_value()->pack_temp;
  100. data_len = sizeof(measure_value()->pack_temp);
  101. break;
  102. case CAN_KEY_BMS_GET_CELLS:
  103. data = (uint8_t *)measure_value()->cell_vol;
  104. data_len = sizeof(measure_value()->cell_vol);
  105. break;
  106. case CAN_KEY_BMS_GET_HEALTH_STAT:
  107. data = (uint8_t *)bms_health();
  108. data_len = sizeof(*bms_health());
  109. break;
  110. case CAN_KEY_BMS_SET_WORK_MODE:
  111. if (len != 2) {
  112. result = 1;
  113. }else {
  114. result = bms_work_mode_set(frame->data[0], frame->data[1]);
  115. }
  116. protocol_send_ack(frame->head.can_addr, frame->key, result);
  117. break;
  118. case CAN_KEY_SET_SN:
  119. protocol_send_ack(frame->head.can_addr, frame->key, result);
  120. break;
  121. case CAN_KEY_GET_SN:
  122. break;
  123. case CAN_KEY_GET_VERSION:
  124. break;
  125. case CAN_KEY_SET_LOGGER:
  126. if (len != 2) {
  127. result = 1;
  128. }else {
  129. set_log_level(frame->data[0], frame->data[1]);
  130. }
  131. protocol_send_ack(frame->head.can_addr, frame->key, result);
  132. break;
  133. }
  134. if (data != NULL && data_len > 0){
  135. protocol_send_bms_info(frame->head.can_addr, frame->key, data, data_len);
  136. }
  137. }