hal_esb.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include "hal_esb.h"
  2. #include "bsp_esb.h"
  3. #include "bsp_time.h"
  4. #include "main.h"
  5. //********************* 选择串口 接口 **********************//
  6. static unsigned char CheckLen(void) //查看收到多少数据
  7. {
  8. return ESB_CheckLen();
  9. }
  10. static unsigned char CheckByte(int dex) //查看数据
  11. {
  12. return ESB_CheckByte(dex);
  13. }
  14. static void Discard(unsigned int dex) //丢弃RX数据几位
  15. {
  16. ESB_Discard(dex);
  17. }
  18. static void SendBuf(unsigned char *p,int L) //发送
  19. {
  20. ESB_SendBuff(p,L);
  21. }
  22. //********************* 接收 **********************//
  23. #define ESB_RX_CMD_LEN 256
  24. static ESB_Rx_t mESB_Rx[ESB_NUM_OF_R];
  25. static unsigned char cmdDatBuf[ESB_RX_CMD_LEN]; //存放一条完整命令
  26. int ESB_Rx_Regist(unsigned char cmd,ESB_Callback cb) //注册
  27. {
  28. if(cmd>=ESB_NUM_OF_R) return -1;
  29. mESB_Rx[cmd].cb = cb;
  30. mESB_Rx[cmd].cmd = cmd;
  31. mESB_Rx[cmd].pDat = cmdDatBuf;
  32. return 0;
  33. }
  34. //********************* 接收协议 **********************//
  35. //协议(1位头+ 1位长度+ 1位长度反码+ 1命令+ N数据+ 1效验)
  36. static void Protocol(unsigned char len) //协议处理
  37. {
  38. #if DEBUG_ESB
  39. SEGGER_RTT_printf(0,"Rx_ESB(%d):",CheckByte(1)); for(int i=0;i<len;i++){SEGGER_RTT_printf(0,"%02X ",CheckByte(i));} SEGGER_RTT_printf(0,"\r\n");
  40. #endif
  41. uint8_t cmd = CheckByte(3);
  42. if(len<5||cmd>=ESB_NUM_OF_R) return;
  43. if(mESB_Rx[cmd].cb){
  44. for(int i=0;i<len-5;i++) mESB_Rx[cmd].pDat[i] = CheckByte(i+4);
  45. mESB_Rx[cmd].datLen = len-5;
  46. mESB_Rx[cmd].cb((ESB_Rx_t*)&mESB_Rx[cmd]);
  47. }
  48. }
  49. //协议(1位头+ 1位长度+ 1位长度反码+ 1命令+ N数据+ 1效验)
  50. void ESB_Rx_Process(void)
  51. {
  52. static unsigned char R=0;
  53. static unsigned char L=0;
  54. //接收缓存有数据,就全部处理
  55. while(1){
  56. switch( R ){
  57. case 0: if( CheckLen()<3 )return; else{
  58. if(CheckByte(0)!=0xAA){ Discard(1);}
  59. else{unsigned char LF=CheckByte(2);LF=~LF; L=CheckByte(1); if((LF==L)&&(L>=5)){ R++;}else { Discard(1);}}
  60. } break;
  61. // 多收数据 7 = 3位头+ 1位长度负数+ 1位长度+ 2效验
  62. case 1: if( CheckLen()<L) { return; }else{ //SEGGER_RTT_printf(0,"Rx:"); for(int i=0;i<L;i++){SEGGER_RTT_printf(0,"%02X ",CheckByte(i));} SEGGER_RTT_printf(0,"\r\n");
  63. unsigned char i,ver=0;
  64. for(i=0;i<L-1;i++){ ver += CheckByte(i); }
  65. if(CheckByte(L-1)==ver){ Protocol(L);Discard(L);//丢弃整条命令
  66. } else Discard(1);
  67. R=0;
  68. } break;
  69. default: R=0;break; }
  70. }
  71. }
  72. //********************* 发送协议 **********************//
  73. //协议(1位头+ 1位长度+ 1位长度反码+ 1命令+ N数据+ 1效验)
  74. void ESB_Send(unsigned char cmd,unsigned char *pDat,unsigned char datLen) //协议发送
  75. {
  76. unsigned char buf[255];
  77. unsigned char ver=0;
  78. unsigned char i,L=0,Len=datLen+5;
  79. buf[L]=0xAA; ver+=buf[L++]; // 头
  80. buf[L]=Len; ver+=buf[L++]; //1位长度
  81. buf[L]=~Len; ver+=buf[L++]; // 1位长度反码
  82. buf[L]=cmd; ver+=buf[L++]; //1命令
  83. for(i=0;i<datLen; i++){ buf[L]=pDat[i];ver+=buf[L++];} //数据
  84. buf[L++]=ver; //效验
  85. #if DEBUG_ESB
  86. SEGGER_RTT_printf(0,"Tx_ESB:"); for(int i=0;i<L;i++){SEGGER_RTT_printf(0,"%02X ",buf[i]);} SEGGER_RTT_printf(0,"\r\n");
  87. #endif
  88. SendBuf(buf,L); //压入发送缓存
  89. }
  90. static ESB_Tx_t mESB_Tx[ESB_NUM_OF_T];
  91. void ESB_Tx_Process(void)
  92. {
  93. static uint32_t tim=0;
  94. uint32_t temp = TIME_GetTicks()-tim;
  95. tim = TIME_GetTicks();
  96. for(int i=0;i<ESB_NUM_OF_T;i++){
  97. if(temp>=1){
  98. if(mESB_Tx[i].periodTimecnt>0){mESB_Tx[i].periodTimecnt--;
  99. if(mESB_Tx[i].nTimes==0&&mESB_Tx[i].periodTimecnt==0&&mESB_Tx[i].cb){
  100. mESB_Tx[i].cb((ESB_Tx_t*)&mESB_Tx[i]);
  101. }
  102. }
  103. }
  104. if(mESB_Tx[i].nTimes==1&&mESB_Tx[i].periodTimecnt==0){
  105. ESB_Send(i,mESB_Tx[i].pDat,mESB_Tx[i].datLen);
  106. mESB_Tx[i].periodTimecnt = mESB_Tx[i].waitTime;
  107. mESB_Tx[i].nTimes--;
  108. }else if(mESB_Tx[i].nTimes>1&&mESB_Tx[i].periodTimecnt==0){
  109. ESB_Send(i,mESB_Tx[i].pDat,mESB_Tx[i].datLen);
  110. mESB_Tx[i].periodTimecnt = mESB_Tx[i].periodTime;
  111. mESB_Tx[i].nTimes--;
  112. }
  113. }
  114. }
  115. int ESB_Tx_Send(unsigned char cmd,unsigned char *pDat,unsigned char datLen,uint8_t nTimes,uint32_t periodTime,uint32_t afterTime,uint32_t waitTime)
  116. {
  117. if(cmd>=ESB_NUM_OF_T) return -1;
  118. mESB_Tx[cmd].cmd = cmd;
  119. mESB_Tx[cmd].pDat = pDat;
  120. mESB_Tx[cmd].nTimes = nTimes;
  121. mESB_Tx[cmd].datLen = datLen;
  122. mESB_Tx[cmd].periodTime = periodTime;
  123. mESB_Tx[cmd].periodTimecnt = afterTime;
  124. mESB_Tx[cmd].waitTime = waitTime;
  125. return 0;
  126. }
  127. int ESB_Tx_Regist(unsigned char cmd,ESB_Callback timeout_cb)
  128. {
  129. if(cmd>=ESB_NUM_OF_T) return -1;
  130. mESB_Tx[cmd].cb = timeout_cb;
  131. return 0;
  132. }
  133. void ESB_Tx_Clear(unsigned char cmd)
  134. {
  135. if(cmd>=ESB_NUM_OF_T) return;
  136. mESB_Tx[cmd].nTimes = 0;
  137. mESB_Tx[cmd].periodTimecnt = 0;
  138. }
  139. void ESB_Initialize(void)
  140. {
  141. ESB_Init();
  142. Process_Regist(ESB_Rx_Process);
  143. Process_Regist(ESB_Tx_Process);
  144. }