protocol_old.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 "app/nv_storage.h"
  11. #include "libs/logger.h"
  12. #include "bsp/uart.h"
  13. #include "protocol_old.h"
  14. #include "pcba_test.h"
  15. static uart_enum_t current_uart = SHARK_UART0;
  16. extern char* bsp_get_fversion(void);
  17. uint16_t _checksum(uint8_t *data, uint16_t size) {
  18. uint32_t checksum;
  19. if((NULL == data) || (0 == size)) {
  20. return 0;
  21. }
  22. checksum = 0;
  23. while(size>1) {
  24. checksum += *(uint16_t*)data;
  25. data += 2;
  26. size -= 2;
  27. }
  28. if(size>0) {
  29. checksum += *data;
  30. }
  31. while(checksum>>16) {
  32. checksum = (checksum&0xFFFF)+(checksum>>16);
  33. }
  34. return (uint16_t)~checksum;
  35. }
  36. static int get_response_data(uint8_t *data, uint8_t operate, uint8_t result){
  37. comm_head_t *head = (comm_head_t *)data;
  38. head->size = sizeof(comm_head_t);
  39. head->type = bms_state_get()->bms_addr;
  40. head->dir = 0x16;
  41. head->bStatus = 1;
  42. head->cmd = 0x10;
  43. head->protocol = 'C';
  44. head->checksum = 0;
  45. comm_response_t *response = (comm_response_t *)head->data;
  46. response->remain_miles= 0; //Ê£ÓàÀï³Ì
  47. response->capacity = get_soc()->capacity;
  48. response->remain_charger_time= soc_get_charger_remain_time()/360; //10·ÖÖ®1Сʱµ¥Î»
  49. response->is_chargering = bms_state_get()->charging;
  50. response->load_current = measure_value()->load_current;
  51. response->pack_voltage = bms_state_get()->pack_voltage;
  52. response->max_temp = -100;
  53. for (int i = 0; i < PACK_TEMPS_NUM; i++){
  54. if (response->max_temp < measure_value()->pack_temp[i]){
  55. response->max_temp = measure_value()->pack_temp[i];
  56. }
  57. }
  58. response->health_state = *((uint16_t *)bms_health());
  59. response->balance_mask = 0x0;
  60. response->misc_status = (ml5238_is_charging() << 1) | (ml5238_is_discharging() << 2) | (AUX_VOL_IS_OPEN() << 3) |(io_state()->aux_lock_detect << 4) | ((get_soc()->capacity==100) << 5) | (((get_soc()->flags & SOC_FLAG_CALIBRATED) == 0) << 6);
  61. response->result = (operate << 4) | result;
  62. head->size += sizeof(comm_response_t);
  63. if (operate == OP_READ_INFO){
  64. uint8_t sn[32];
  65. int sn_len = nv_read_sn(sn, sizeof(sn));
  66. if (sn_len <= 0){
  67. sn[0] = 'B';
  68. memset(sn + 1, '0', sizeof(sn) - 1);
  69. sn_len = 18;
  70. }
  71. memcpy(&data[head->size], sn, sn_len);
  72. head->size += sn_len;
  73. strcpy((char *)&data[head->size], bsp_get_fversion());
  74. head->size += strlen(bsp_get_fversion());
  75. }else if (operate == OP_ALARM_TIMES){
  76. memset(&data[head->size], 0, sizeof(times_response_t));
  77. times_response_t *resp = (times_response_t *)&data[head->size];
  78. resp->charger_cycle = soc_get_cycle();
  79. head->size += sizeof(times_response_t);
  80. }else if (operate == OP_CELL_VOL){
  81. data[head->size ++] = CELLS_NUM;
  82. for (int i = 0; i <CELLS_NUM; i++){
  83. data[head->size ++] = (uint8_t)measure_value()->cell_vol[i];
  84. data[head->size ++] = (uint8_t)(measure_value()->cell_vol[i] >> 8);
  85. }
  86. }else if (operate == OP_TEMP_OTHER){
  87. for (int i = 0; i < PACK_TEMPS_NUM; i++){
  88. data[head->size ++] = measure_value()->pack_temp[i];
  89. }
  90. data[head->size ++] = (ml5238_is_charging() << 0) | (ml5238_is_discharging() << 1);
  91. }
  92. head->checksum = _checksum(data, head->size);
  93. return head->size;
  94. }
  95. static int protocol_old_process_binary(uart_enum_t uart_no, uint8_t *data, int len){
  96. current_uart = uart_no;
  97. comm_head_t *head = (comm_head_t *)data;
  98. if (head->type != bms_state_get()->bms_addr || head->dir != 0x16 || head->bStatus == 1 || head->size < sizeof(comm_head_t)){
  99. return -1;
  100. }
  101. uint16_t checksum = head->checksum;
  102. head->checksum = 0;
  103. if (checksum != _checksum(data, len)){
  104. return -1;
  105. }
  106. data = head->data;
  107. data += 6;
  108. uint8_t operate = data[0];
  109. data += 1;
  110. uint8_t result = 1;
  111. if (operate == OP_OPEN_FET){
  112. if (data[8] == 0x03) {
  113. bms_state_get()->user_request = USER_REQUEST_PENDING | USER_REQUEST_DISCHARGER_ON|USER_REQUEST_CHARGER_ON;
  114. }else if (data[8] == 0x0){
  115. bms_state_get()->user_request = USER_REQUEST_PENDING | USER_REQUEST_SMALLCURRENT_OFF|USER_REQUEST_DISCHARGER_OFF|USER_REQUEST_CHARGER_OFF;
  116. }else if (data[8] == 0x04) {
  117. bms_state_get()->user_request = USER_REQUEST_PENDING | USER_REQUEST_SMALLCURRENT_ON;
  118. }else {
  119. result = 0;
  120. }
  121. }else if (operate == OP_WRITE_SN){
  122. if (data[8] == 1) {
  123. result = nv_save_sn(&data[9], 18) <= 0?1:0;
  124. }
  125. }
  126. uint8_t response_data[256];
  127. uint16_t response_len = get_response_data(response_data, operate, result);
  128. if (response_len <= 0){
  129. return -1;
  130. }
  131. shark_uart_write_bytes(current_uart, response_data, response_len);
  132. return 0;
  133. }
  134. void protocol_old_recv_frame(uart_enum_t uart_no, uint8_t *data, int len){
  135. if (protocol_old_process_binary(uart_no, data, len) == 0){
  136. return;
  137. }
  138. if (memcmp(data, "mode", 4) == 0){
  139. if (memcmp(data + 4, "laohua", 6) == 0){
  140. bms_work_mode_set(WORK_MODE_AGING_TEST, 1);
  141. bms_state_get()->user_request = USER_REQUEST_PENDING | USER_REQUEST_SMALLCURRENT_OFF | USER_REQUEST_DISCHARGER_ON|USER_REQUEST_CHARGER_ON;
  142. printf("laohua OK!\n");
  143. }else if (memcmp(data + 4, "tuichu", 6) == 0){
  144. if (bms_work_is_aging_test()){
  145. bms_work_mode_set(WORK_MODE_AGING_TEST, 1);
  146. bms_state_get()->user_request = USER_REQUEST_PENDING | USER_REQUEST_SMALLCURRENT_ON | USER_REQUEST_DISCHARGER_OFF | USER_REQUEST_CHARGER_OFF;
  147. printf("tuichu OK!\n");
  148. }
  149. }else if (memcmp(data + 4, "ceshi", 5) == 0) {
  150. bms_work_mode_set(WORK_MODE_PCBA_TEST, 1);
  151. printf("pcba ceshi OK!\n");
  152. }
  153. }else if (bms_work_is_pcba_test() && data[0] == 0xFE && data[1] == 0xFE){
  154. uint8_t response[16];
  155. int resp_len = pcba_test(data, len, response);
  156. if (resp_len > 0) {
  157. shark_uart_write_bytes(current_uart, response, resp_len);
  158. }
  159. }
  160. }