protocol.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include "protocol.h"
  2. #include <string.h>
  3. static uint16_t _check_sum(uint8_t*data,uint16_t size);
  4. /*
  5. * 调用 protocol_send_xxx 接口的时候,需要注意需要多分配can_head_t 大小的内存,并且
  6. * 需要offset掉can_head_t 大小, 这样设计的原因是避免多次拷贝
  7. */
  8. void protocol_send_bms_info(protocol_head_t *head){
  9. can_head_t *can = (can_head_t *)(((uint8_t *)head) - sizeof(can_head_t));
  10. can->can_addr = 0x42;
  11. can->size = head->size;
  12. can->can_key = 0x00;
  13. head->checksum = 0;
  14. head->checksum = _check_sum((uint8_t *)head, head->size);
  15. //send from uart
  16. }
  17. void protocol_send_debug_info(uint8_t dest, char *data, int size){
  18. can_head_t *can = (can_head_t *)(((uint8_t *)data) - sizeof(can_head_t));
  19. can->can_addr = dest;
  20. can->size = size;
  21. can->can_key = 0x00;
  22. //send from uart
  23. }
  24. static uint16_t _check_sum(uint8_t*data,uint16_t size)
  25. {
  26. uint32_t checksum;
  27. if((NULL == data)||(0 == size)){
  28. return 0;
  29. }
  30. checksum = 0;
  31. while(size>1) {
  32. checksum += *(uint16_t*)data;
  33. data += 2;
  34. size -= 2;
  35. }
  36. if(size>0) {
  37. checksum += *data;
  38. }
  39. while(checksum>>16) {
  40. checksum = (checksum&0xFFFF)+(checksum>>16);
  41. }
  42. return (uint16_t)~checksum;
  43. }