#include "protocol.h" #include #include "bsp/uart.h" #include "app/pc_message.h" #include "app/bms_message.h" #include "app/iap.h" static uint16_t _check_sum(uint8_t*data,uint16_t size); static uart_enum_t current_uart = SHARK_UART0; void protocol_send_bms_info(uint8_t dest, uint8_t key, uint8_t *data, int len){ can_frame_t can_frame; CAN_OUT(&(can_frame.head), dest); can_frame.key = key; shark_uart_frame_start(current_uart, (uint8_t *)&can_frame, sizeof(can_frame)); shark_uart_frame_continue(current_uart, data, len); shark_uart_frame_end(current_uart); } void protocol_send_ack(uint8_t dest, uint8_t key, int result) { uint8_t data[sizeof(can_frame_t) + 1]; can_frame_t *frame = (can_frame_t *)data; CAN_OUT(&(frame->head), dest); frame->key = key; data[sizeof(can_frame_t)] = result; shark_uart_write_frame(current_uart, data, sizeof(data)); } void protocol_send_debug_info(uint8_t dest, uint8_t *data, int len){ if (bms_work_is_aging_test() || bms_work_is_pcba_test()){ shark_uart_write_bytes(current_uart, data, len); return; } can_head_t can_head; CAN_OUT(&can_head, dest); shark_uart_frame_start(current_uart, (uint8_t *)&can_head, sizeof(can_head)); shark_uart_frame_continue(current_uart, data, len); shark_uart_frame_end(current_uart); } /*如果接收到的数据frame,无法识别为新的protocol,认为是老协议,这里通过老协议发送给PSxxx,告知 只支持新协议 */ void protocol_notify_old_frame(uart_enum_t uart_no){ current_uart = uart_no; protocol_old_head_t head; memset(&head, 0, sizeof(head)); head.size = sizeof(head); head.protocol = 'C'; head.type = 0x30; head.dir = 0x16; head.cmd = 0x10; head.bStatus = 1; head.checksum = _check_sum((uint8_t *)&head, head.size); shark_uart_write_bytes(current_uart, (uint8_t *)&head, head.size); } void protocol_recv_frame(uart_enum_t uart_no, uint8_t *data, int len){ current_uart = uart_no; if (len < sizeof(can_frame_t)){ return; } can_frame_t *can_frame = (can_frame_t *)data; if (!CAN_IN(&(can_frame->head))) {//data is sent by myself, drop return; } len -= sizeof(can_frame_t); if (can_frame->key >= CAN_KEY_IAP_ENTER){ process_iap_message(can_frame, len); }else { if (!process_pc_message(can_frame, len)) { process_bms_message(can_frame, len); } } } static uint16_t _check_sum(uint8_t*data,uint16_t size) { uint32_t checksum; if((NULL == data)||(0 == size)){ return 0; } checksum = 0; while(size>1) { checksum += *(uint16_t*)data; data += 2; size -= 2; } if(size>0) { checksum += *data; } while(checksum>>16) { checksum = (checksum&0xFFFF)+(checksum>>16); } return (uint16_t)~checksum; }