protocol.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include "protocol.h"
  2. #include <string.h>
  3. #include "bsp/uart.h"
  4. #include "pc_message.h"
  5. #include "bms_message.h"
  6. static uint16_t _check_sum(uint8_t*data,uint16_t size);
  7. static uart_enum_t current_uart = SHARK_UART0;
  8. void protocol_send_bms_info(protocol_head_t *head){
  9. }
  10. void protocol_send_debug_info(uint8_t dest, uint8_t *data, int size){
  11. can_head_t can_head;
  12. can_head.can_addr = dest;
  13. shark_uart_frame_start(current_uart, (uint8_t *)&can_head, sizeof(can_head));
  14. shark_uart_frame_continue(current_uart, data, size);
  15. shark_uart_frame_end(current_uart);
  16. }
  17. /*如果接收到的数据frame,无法识别为新的protocol,认为是老协议,这里通过老协议发送给PSxxx,告知
  18. 只支持新协议
  19. */
  20. void protocol_notify_old_frame(uart_enum_t uart_no){
  21. current_uart = uart_no;
  22. protocol_old_head_t head;
  23. memset(&head, 0, sizeof(head));
  24. head.size = sizeof(head);
  25. head.protocol = 'C';
  26. head.type = 0x30;
  27. head.dir = 0x16;
  28. head.cmd = 0x10;
  29. head.bStatus = 1;
  30. head.checksum = _check_sum((uint8_t *)&head, head.size);
  31. shark_uart_write_bytes(current_uart, (uint8_t *)&head, head.size);
  32. }
  33. void protocol_recv_frame(uart_enum_t uart_no, uint8_t *data, int len){
  34. current_uart = uart_no;
  35. if (len < sizeof(can_head_t)){
  36. return;
  37. }
  38. can_head_t *can_head = (can_head_t *)data;
  39. if (can_head->can_addr == 0x45){ //pc sent
  40. process_pc_message(data + sizeof(can_head_t), len - sizeof(can_head_t));
  41. }else {
  42. len -= sizeof(can_head_t);
  43. if (len <sizeof(protocol_head_t)){
  44. return;
  45. }
  46. process_bms_message(data + sizeof(can_head_t), len - sizeof(can_head_t));
  47. }
  48. }
  49. static uint16_t _check_sum(uint8_t*data,uint16_t size)
  50. {
  51. uint32_t checksum;
  52. if((NULL == data)||(0 == size)){
  53. return 0;
  54. }
  55. checksum = 0;
  56. while(size>1) {
  57. checksum += *(uint16_t*)data;
  58. data += 2;
  59. size -= 2;
  60. }
  61. if(size>0) {
  62. checksum += *data;
  63. }
  64. while(checksum>>16) {
  65. checksum = (checksum&0xFFFF)+(checksum>>16);
  66. }
  67. return (uint16_t)~checksum;
  68. }