| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- #include "protocol.h"
- #include <string.h>
- static uint16_t _check_sum(uint8_t*data,uint16_t size);
- /*
- * 调用 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);
- //send from uart
- }
- 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;
- //send from uart
- }
- 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;
- }
|