#include "hal_ble_client.h" #include "system.h" #include "bsp_time.h" #include "nrf_gpio.h" /************************ 函数声明 ***********************************/ extern unsigned int send_bytes_client(unsigned char *bytes, uint16_t len); void BLE_Client_Tx_Process(void); /************************ 变量 ***********************************/ /********************** 环形缓存区 *************************/ static const int RxLen = 1024; static volatile unsigned char RxBuf[RxLen]; static volatile unsigned char* RxW=RxBuf; static volatile unsigned char* RxR=RxBuf; void BLE_Client_Push(unsigned char* p,int len) { volatile unsigned char *W=RxW; //这里要与上面指针相同 if(len<=0) return; for(int i=0;i=RxBuf+RxLen) W=RxBuf; //取下一位置(到顶转到底) if(W!=RxR){*RxW=*(p+i); RxW=W;} else break; } } static unsigned int CheckLen(void) //检查RX接收了多少数据 { unsigned int Len; //short volatile unsigned char *W=RxW; volatile unsigned char *R=RxR; if(W>=R)Len=W-R;else Len=(W+RxLen)-R; //这样正确(中途中断改变也变不了结果) return Len; } //static unsigned char ReadByte(void) //读RX中数锯,地指加一,和丢弃 //{ // unsigned char R=*RxR; //读数 // if(RxR!=RxW){ if(RxR+1>=(RxBuf+RxLen))RxR =RxBuf; else RxR++;}//下标 // return R; //} static unsigned char CheckByte(unsigned short n) //看RX中数锯,地指不变, { volatile unsigned char *R=RxR+n; if(R>=(RxBuf+RxLen))R-=RxLen; return *R; } static void Discard(unsigned short n) //丢弃RX数据几位 { while(n){ n--; if(RxR==RxW) return; if(RxR+1>=RxBuf+RxLen){RxR=RxBuf;} else RxR++; //下标 } } //********************* 接收 **********************// #define BLE_Client_RX_CMD_LEN 256 static BLE_Client_Rx_t mBLE_Client_Rx[BLE_NUM_OF]; static unsigned char cmdDatBuf[BLE_Client_RX_CMD_LEN]; //存放一条完整命令 static unsigned int rxNum = 0; int BLE_Client_Rx_Regist(BLE_CMD_n cmd,BLE_Client_Callback cb) //注册 { if(rxNum>=BLE_NUM_OF) return -1; mBLE_Client_Rx[rxNum].cb = cb; mBLE_Client_Rx[rxNum].cmd = cmd; mBLE_Client_Rx[rxNum].pDat = cmdDatBuf; rxNum++; return 0; } //********************* 接收协议 **********************// //协议(1位头+ 1位长度+ 1位长度反码+ 1命令+ N数据+ 1效验) static void Protocol(unsigned char len) //协议处理 { #if DEBUG_BLE_Client SEGGER_RTT_printf(0,"Rx_BLE_Client(%d):",CheckByte(1)); for(int i=0;i=5)){ R++;}else { Discard(1);}} } break; // 多收数据 7 = 3位头+ 1位长度负数+ 1位长度+ 2效验 case 1: if( CheckLen() 250)return -1; buf[L]=0xAA; ver+=buf[L++]; // 头 buf[L]=Len; ver+=buf[L++]; //1位长度 buf[L]=~Len; ver+=buf[L++]; // 1位长度反码 buf[L]=cmd; ver+=buf[L++]; //1命令 for(i=0;icmd = cmd; handle->pDat = pDat; handle->datLen = datLen; handle->tcnt = handle->t; if(handle->n>0) handle->ncnt = handle->n-1; else handle->ncnt = 0; handle->holdon = 1; while(target){ //检查是否已经存在 if(target==handle){ return; } target = target->next ; } handle->next = head_handle; head_handle = handle; } BLE_Client_Send(cmd,pDat,datLen); } void BLE_Client_Tx_Clear(BLE_Client_Tx_t* handle) { BLE_Client_Tx_t** curr; for(curr=&head_handle;*curr;){ BLE_Client_Tx_t* entry = *curr; if(entry==handle){ handle->holdon = 0; *curr = entry->next; }else{ curr = &entry->next; } } } void BLE_Client_Tx_Process(void) { BLE_Client_Tx_t* target; uint8_t holdon = 0; static uint32_t tim = 0; uint32_t tim_dif =0; for(target=head_handle; target; target=target->next) { if(target->tcnt > 0){ tim_dif = (TIME_GetTicks()- tim); tim = TIME_GetTicks(); if(tim_dif < HeartTime_Interval)tim_dif = HeartTime_Interval; if(target->tcnt >= tim_dif)target->tcnt -= tim_dif; else target->tcnt =0; if(target->tcnt==0){ if(target->ncnt>0){target->ncnt--; target->tcnt = target->t; BLE_Client_Send(target->cmd,target->pDat,target->datLen); }else{ BLE_Client_Tx_Clear(target); if(target->cb){ target->cb(target); } } } } holdon |= target->holdon; } Process_SetHoldOn(BLE_Client_Tx_Process,holdon); } #include "ble_comm.h" void BLE_Client_Initialize(void) { Process_Start(0,"BLE_Client_Rx",BLE_Client_Rx_Process); Process_Start(HeartTime_Interval,"BLE_Client_Tx",BLE_Client_Tx_Process); #if USEFIFO Process_Start(0,"send_bytes_client_pcs",send_bytes_client_pcs); #endif }