protocol.c 1.4 KB

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