protocol_old.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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()/6; //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()) | (bms_health()->sigle_cell_lower_voltage << 1);
  59. if (bms_is_ps_charger_in()) {
  60. response->health_state |= (bms_health()->lower_temp_deny_charger << 12 | bms_health()->over_temp_deny_charger << 14);
  61. }
  62. response->balance_mask = 0x0;
  63. response->misc_status = (ml5238_is_discharging() << 1) | (ml5238_is_charging() << 2);
  64. response->misc_status |= (AUX_VOL_IS_OPEN() << 3) |(io_state()->aux_lock_detect << 5);
  65. response->misc_status |= ((get_soc()->capacity==100) << 4) | (((get_soc()->flags & SOC_FLAG_CALIBRATED) == 0) << 6);
  66. response->misc_status |= io_state()->charger_detect_irq;
  67. response->result = (operate << 4) | result;
  68. head->size += sizeof(comm_response_t);
  69. if (operate == OP_READ_INFO){
  70. uint8_t sn[32];
  71. int sn_len = nv_read_sn(sn, sizeof(sn));
  72. if (sn_len <= 0){
  73. sn[0] = 'B';
  74. memset(sn + 1, '0', sizeof(sn) - 1);
  75. sn_len = 18;
  76. }
  77. memcpy(&data[head->size], sn, sn_len);
  78. head->size += sn_len;
  79. strcpy((char *)&data[head->size], bsp_get_fversion());
  80. head->size += strlen(bsp_get_fversion());
  81. }else if (operate == OP_ALARM_TIMES){
  82. memset(&data[head->size], 0, sizeof(times_response_t));
  83. times_response_t *resp = (times_response_t *)&data[head->size];
  84. resp->charger_cycle = soc_get_cycle();
  85. head->size += sizeof(times_response_t);
  86. }else if (operate == OP_CELL_VOL){
  87. data[head->size ++] = CELLS_NUM;
  88. for (int i = 0; i <CELLS_NUM; i++){
  89. data[head->size ++] = (uint8_t)measure_value()->cell_vol[i];
  90. data[head->size ++] = (uint8_t)(measure_value()->cell_vol[i] >> 8);
  91. }
  92. }else if (operate == OP_TEMP_OTHER){
  93. for (int i = 0; i < PACK_TEMPS_NUM; i++){
  94. data[head->size ++] = measure_value()->pack_temp[i];
  95. }
  96. data[head->size ++] = (ml5238_is_charging() << 0) | (ml5238_is_discharging() << 1);
  97. }else if (operate == OP_READ_DETECT){
  98. data[head->size++] = IS_HALL1_DETECTED() | IS_HALL2_DETECTED() << 1 | IS_CHARGER_IN() << 2;
  99. data[head->size++] = IS_DCDC_POWER_GOOD() | AUX_VOL_IS_OPEN() << 1 | IS_AUX_VOL_LOCKED() << 2;
  100. }
  101. head->checksum = _checksum(data, head->size);
  102. return head->size;
  103. }
  104. static int protocol_old_process_binary(uart_enum_t uart_no, uint8_t *data, int len){
  105. current_uart = uart_no;
  106. comm_head_t *head = (comm_head_t *)data;
  107. if (/*head->type != bms_state_get()->bms_addr || */head->dir != 0x16 || head->bStatus == 1 || head->size < sizeof(comm_head_t)){
  108. return -1;
  109. }
  110. uint16_t checksum = head->checksum;
  111. head->checksum = 0;
  112. if (checksum != _checksum(data, len)){
  113. return -1;
  114. }
  115. data = head->data;
  116. data += 6;
  117. uint8_t operate = data[0];
  118. uint8_t ps_charger = data[1];
  119. bms_set_ps_charger_in(1, (ps_charger == CW_CHE_SHANG_CHARGER || ps_charger == CW_CHONG_DIAN_ZUO));
  120. data += 1;
  121. uint8_t result = 1;
  122. if (operate == OP_OPEN_FET){
  123. if (data[8] == 0x01) {
  124. bms_state_get()->user_request = USER_REQUEST_PENDING | USER_REQUEST_DISCHARGER_ON|USER_REQUEST_CHARGER_OFF;
  125. }else if (data[8] == 0x02) {
  126. bms_state_get()->user_request = USER_REQUEST_PENDING | USER_REQUEST_DISCHARGER_OFF|USER_REQUEST_CHARGER_ON;
  127. }else if (data[8] == 0x03) {
  128. bms_state_get()->user_request = USER_REQUEST_PENDING | USER_REQUEST_DISCHARGER_ON|USER_REQUEST_CHARGER_ON;
  129. }else if (data[8] == 0x0){
  130. bms_state_get()->user_request = USER_REQUEST_PENDING | USER_REQUEST_SMALLCURRENT_OFF|USER_REQUEST_DISCHARGER_OFF|USER_REQUEST_CHARGER_OFF;
  131. }else if (data[8] == 0x04) {
  132. bms_state_get()->user_request = USER_REQUEST_PENDING | USER_REQUEST_SMALLCURRENT_ON;
  133. }else {
  134. result = 0;
  135. }
  136. }else if (operate == OP_WRITE_SN){
  137. if (data[8] == 1) {
  138. result = nv_save_sn(&data[9], 18) <= 0?1:0;
  139. }
  140. }
  141. uint8_t response_data[256];
  142. uint16_t response_len = get_response_data(response_data, operate, result);
  143. if (response_len <= 0){
  144. return -1;
  145. }
  146. set_log_all(L_disable);
  147. shark_uart_write_bytes(current_uart, response_data, response_len);
  148. return 0;
  149. }
  150. void protocol_old_recv_frame(uart_enum_t uart_no, uint8_t *data, int len){
  151. if (protocol_old_process_binary(uart_no, data, len) == 0){
  152. return;
  153. }
  154. if (memcmp(data, "mode", 4) == 0){
  155. set_log_all(L_disable);
  156. if (memcmp(data + 4, "laohua", 6) == 0){
  157. bms_work_mode_set(WORK_MODE_AGING_TEST, 1);
  158. bms_state_get()->user_request = USER_REQUEST_PENDING | USER_REQUEST_SMALLCURRENT_OFF | USER_REQUEST_DISCHARGER_ON|USER_REQUEST_CHARGER_ON;
  159. printf("laohua OK!\n");
  160. }else if (memcmp(data + 4, "tuichu", 6) == 0){
  161. if (bms_work_is_aging_test() || bms_work_is_pcba_test() || bms_work_is_pack_test()){
  162. bms_work_mode_set(WORK_MODE_AGING_TEST, 0);
  163. bms_state_get()->user_request = USER_REQUEST_PENDING | USER_REQUEST_SMALLCURRENT_ON | USER_REQUEST_DISCHARGER_OFF | USER_REQUEST_CHARGER_OFF;
  164. printf("tuichu OK!\n");
  165. }
  166. }else if (memcmp(data + 4, "ceshi", 5) == 0) {
  167. bms_work_mode_set(WORK_MODE_PCBA_TEST, 1);
  168. printf("ceshi success!\n");
  169. }
  170. }else if (bms_work_is_pcba_test() && data[0] == 0xFE && data[1] == 0xFE){
  171. uint8_t response[16];
  172. int resp_len = pcba_test(data, len, response);
  173. if (resp_len > 0) {
  174. shark_uart_write_bytes(current_uart, response, resp_len);
  175. }
  176. }
  177. }