bms_message.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. data = (uint8_t *)&bresp;
  61. data_len = sizeof(bresp);
  62. break;
  63. }
  64. case CAN_KEY_BMS_CHARG_INFO:{
  65. cinfo_cmd_resp_t cresp;
  66. cresp.charge_current = measure_value()->load_current;
  67. cresp.charge_remain_time = 0;
  68. data = (uint8_t *)&cresp;
  69. data_len = sizeof(cresp);
  70. break;
  71. }
  72. case CAN_KEY_BMS_CLEAR:
  73. bms_insert_ack = 1;
  74. bms_insert = 0;
  75. protocol_send_ack(frame->head.can_addr, frame->key, result);
  76. break;
  77. case CAN_KEY_BMS_GET_TIME:{
  78. uint32_t time = shark_get_seconds();
  79. data = (uint8_t *)&time;
  80. data_len = sizeof(time);
  81. break;
  82. }
  83. case CAN_KEY_BMS_GET_STAT: {
  84. stat_cmd_resp_t sresp;
  85. sresp.insert = bms_insert;
  86. sresp.is_charging = bms_state_get()->charging;
  87. sresp.discharger_fet = ml5238_is_discharging();
  88. sresp.charger_fet = ml5238_is_charging();
  89. sresp.small_power = AUX_VOL_IS_OPEN();
  90. sresp.is_balancing = bms_state_get()->pack_balancing;
  91. uint32_t *h = (uint32_t *)(bms_health());
  92. sresp.health = (*h != 0);
  93. data = (uint8_t *)&sresp;
  94. data_len = sizeof(sresp);
  95. break;
  96. }
  97. case CAN_KEY_BMS_TEMPS:
  98. data = (uint8_t *)measure_value()->pack_temp;
  99. data_len = sizeof(measure_value()->pack_temp);
  100. break;
  101. case CAN_KEY_BMS_GET_CELLS:
  102. data = (uint8_t *)measure_value()->cell_vol;
  103. data_len = sizeof(measure_value()->cell_vol);
  104. break;
  105. case CAN_KEY_BMS_GET_HEALTH_STAT:
  106. data = (uint8_t *)bms_health();
  107. data_len = sizeof(*bms_health());
  108. break;
  109. case CAN_KEY_BMS_SET_WORK_MODE:
  110. if (len != 2) {
  111. result = 1;
  112. }else {
  113. result = bms_work_mode_set(frame->data[0], frame->data[1]);
  114. }
  115. protocol_send_ack(frame->head.can_addr, frame->key, result);
  116. break;
  117. case CAN_KEY_SET_SN:
  118. protocol_send_ack(frame->head.can_addr, frame->key, result);
  119. break;
  120. case CAN_KEY_GET_SN:
  121. break;
  122. case CAN_KEY_GET_VERSION:
  123. break;
  124. case CAN_KEY_SET_LOGGER:
  125. if (len != 2) {
  126. result = 1;
  127. }else {
  128. set_log_level(frame->data[0], frame->data[1]);
  129. }
  130. protocol_send_ack(frame->head.can_addr, frame->key, result);
  131. break;
  132. }
  133. if (data != NULL && data_len > 0){
  134. protocol_send_bms_info(frame->head.can_addr, frame->key, data, data_len);
  135. }
  136. }