| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- #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;
- /*
- * 调用 protocol_send_xxx 接口的时候,需要注意需要多分配can_head_t 大小的内存,并且
- * 需要offset掉can_head_t 大小, 这样设计的原因是避免多次拷贝
- */
- void protocol_send_bms_info(protocol_head_t *head){
- can_head_t *can = (can_head_t *)(((uint8_t *)head) - sizeof(can_head_t));
- can->can_addr = 0x42;
- can->size = head->size;
- can->can_key = 0x00;
- head->checksum = 0;
- head->checksum = _check_sum((uint8_t *)head, head->size);
- shark_uart_write_frame(current_uart, (char *)can, sizeof(can_head_t) + head->size);
- }
- void protocol_send_debug_info(uint8_t dest, char *data, int size){
- can_head_t *can = (can_head_t *)(((uint8_t *)data) - sizeof(can_head_t));
- can->can_addr = dest;
- can->size = size;
- can->can_key = 0x00;
- shark_uart_write_frame(current_uart, (char *)can, sizeof(can_head_t) + size);
- }
- void protocol_recv_frame(uart_enum_t uart_no, char *data, int 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;
- }
|