| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- #include "protocol.h"
- #include <string.h>
- #include "bsp/uart.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){
- can_head_t can_head;
- can_head.can_addr = 0x42;
- can_head.size = head->size;
-
- head->checksum = 0;
- head->checksum = _check_sum((uint8_t *)head, head->size);
- shark_uart_frame_start(current_uart, (uint8_t *)&can_head, sizeof(can_head));
- shark_uart_frame_continue(current_uart, (uint8_t *)head, head->size);
- shark_uart_frame_end(current_uart);
- }
- void protocol_send_debug_info(uint8_t dest, uint8_t *data, int size){
- can_head_t can_head;
- can_head.can_addr = dest;
- can_head.size = size;
- 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_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.checksum = _check_sum((uint8_t *)&head, head.size);
- shark_uart_write_frame(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
- }else {
- len -= sizeof(can_head_t);
- if (len <sizeof(protocol_head_t)){
- return;
- }
- protocol_head_t *head = (protocol_head_t *)(data + sizeof(can_head_t)) ;
- if (len != head->size) {
- return;
- }
- uint16_t check_sum_rx = head->checksum;
- head->checksum = 0;
- if (check_sum_rx != _check_sum(data, head->size)){
- return;
- }
- }
- }
- 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;
- }
|