| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- #include "protocol.h"
- #include <string.h>
- #include "bsp/uart.h"
- #include "pc_message.h"
- #include "bms_message.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(protocol_head_t *head){
- }
- void protocol_send_debug_info(uint8_t dest, uint8_t *data, int size){
- can_head_t can_head;
- can_head.can_addr = dest;
- shark_uart_frame_start(current_uart, (uint8_t *)&can_head, sizeof(can_head));
- shark_uart_frame_continue(current_uart, data, size);
- 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_head_t)){
- return;
- }
- can_head_t *can_head = (can_head_t *)data;
- if (can_head->can_addr == 0x45){ //pc sent
- process_pc_message(data + sizeof(can_head_t), len - sizeof(can_head_t));
- }else {
- len -= sizeof(can_head_t);
- if (len <sizeof(protocol_head_t)){
- return;
- }
- process_bms_message(data + sizeof(can_head_t), len - sizeof(can_head_t));
- }
- }
- 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;
- }
|