📄 bt_arm.c
字号:
}while(WInqScan_F!=1&n<3);
n=0;
do
{
HCI_BTWriteScanEnable() ;//<= Command Complete event
DelayNS(30);
GetEvent();
n++;
}while(WScanEn_F!=1&n<3);
/*Command Complete OpCode 0x0E
parameter:Num_HCI_Command_Packets,Command_Opcode, Return_Parameters
*//*
//If it is a slave device ,wait a connection_complete_event
do
{
GetEvent();
if((ConReq_F==1)&(connection_req.Linktype==0x01))
HCI_AcceptConnectionReq();
}while(ConCplt_F!=1);//等待连接建立
//////////////////////////////
//If it is a master device ,inquiry
/* HCI_BTInquiry();
GetEvent();
GetEvent();
GetEvent();
HCI_BTCreateConnection(&inquiry_result);
GetEvent();
GetEvent();
GetEvent();
GetEvent();*/
return TRUE;
/////////////////////////////
}
int HCI_BTDataput(unsigned char *phandle,unsigned char length,unsigned char *pbuffer)
{
unsigned int i;
unsigned char tbyte;
SendByte(0x02);//HCI数据分组
//Connection_Handle(2)
SendByte(*phandle++);
SendByte((*phandle) | 0x20);//第一个数据包
SendByte(length);
SendByte(0x00); //可以在此设置断点,在开发工具软件中,将聊天室打开,就可以看见发送的字符
for(i=1;i<=length;i++)
{
tbyte=*pbuffer;
SendByte(tbyte);
pbuffer++;
}
return(1);
}
/****************************************************************************
*数据发送函数
****************************************************************************/
void ProcessData(void)
{
HCI_BTDataput(creat_connection_cfm.handle,0x11, data_packet.data_buffer);
GetEvent();
}
/****************************************************************************/
/*读取数据队列中的数据*/
/*****************************************************************************/
int BTCOM_GetByte(unsigned char *tbyte)
{
if (BTRxBuf.cnt==0) return FALSE;
*tbyte=BTRxBuf.buf[BTRxBuf.out];
BTRxBuf.buf[BTRxBuf.out]=0x00;
if (BTRxBuf.out>=(BT_BufSize-1))
{
BTRxBuf.out=0;
}
else
{
BTRxBuf.out++;
}
BTRxBuf.cnt--;
return(1);
}
/****************************************************************************/
/*读取发送数据队列中的数据*/
/*****************************************************************************/
int GetData(void)
{
if (ADCDataBuf.cnt==0) return FALSE;
C1Data[0]=ADCDataBuf.buf[ADCDataBuf.out];//C1Data[2],C2Data[2];
ADCDataBuf.buf[ADCDataBuf.out]=0x00;
ADCDataBuf.out++;
ADCDataBuf.cnt--;
C1Data[1]=ADCDataBuf.buf[ADCDataBuf.out];
ADCDataBuf.buf[ADCDataBuf.out]=0x00;
ADCDataBuf.out++;
ADCDataBuf.cnt--;
C2Data[0]=ADCDataBuf.buf[ADCDataBuf.out];
ADCDataBuf.buf[ADCDataBuf.out]=0x00;
ADCDataBuf.out++;
ADCDataBuf.cnt--;
C2Data[1]=ADCDataBuf.buf[ADCDataBuf.out];
ADCDataBuf.buf[ADCDataBuf.out]=0x00;
//ADCDataBuf.out++;
ADCDataBuf.cnt--;
//ADCDataBuf.buf[ADCDataBuf.out]=0x00;
if (ADCDataBuf.out>=(BT_BufSize-1))
{
ADCDataBuf.out=0;
}
else
{
ADCDataBuf.out++;
}
//ADCDataBuf.cnt--;
return(1);
}
/****************************************************************************
* 名称:ISendBuf()
* 功能:将缓冲区的数据发送回主机(使用FIFO),并等待发送完毕。
* 入口参数:无
* 出口参数:无
****************************************************************************/
void ISendBuf(void)
{ //uint8 i;
uint8 tbyte;
//for(i=0; i<8; i++)
BTCOM_GetByte(&tbyte);
SendByte(tbyte);
//while( (U0LSR&0x20)==0 ); // 等待数据发送
}
/***************************************************************************
*
****************************************************************************/
/****************************************************************************
* 名称:UART0_Ini()
* 功能:初始化串口0。设置其工作模式及波特率。
* 入口参数:baud 波特率
* set 模式设置(UARTMODE数据结构)
* 出口参数:返回值为1时表示初始化成功,为0表示参数出错
****************************************************************************/
uint8 UART0_Ini(uint32 baud, UARTMODE set)
{ uint32 bak;
/* 参数过滤 */
if( (0==baud)||(baud>115200) ) return(0);
if( (set.datab<5)||(set.datab>8) ) return(0);
if( (0==set.stopb)||(set.stopb>2) ) return(0);
if( set.parity>4 ) return(0);
/* 设置串口波特率 */
U0LCR = 0x80; // DLAB位置1
bak = (Fpclk>>4)/baud;
U0DLM = bak>>8;
U0DLL = bak&0xff;
/* 设置串口模式 */
bak = set.datab-5; // 设置字长度
if(2==set.stopb) bak |= 0x04; // 判断是否为2位停止位
if(0!=set.parity) {set.parity = set.parity-1; bak |= 0x08;}
bak |= set.parity<<4; // 设置奇偶校验
U0LCR = bak;
return(1);
}
/**************************************************************************
*初始化接收缓冲区
***************************************************************************/
void BTCOM_InitBuffer(void)
{
int i;
BTRxBuf.cnt=0;
BTRxBuf.in=0;
BTRxBuf.out=0;
for(i=0;i<BT_BufSize;i++)
{
BTRxBuf.buf[i]=0x00;
}
}
/**************************************************************************
*初始化ADCDataBuf,即初始化ADC数据缓冲区
***************************************************************************/
void InitADCDataBuf(void)
{
int i;
ADCDataBuf.cnt=0;
ADCDataBuf.in=0;
ADCDataBuf.out=0;
for(i=0;i<BT_BufSize;i++)
{
ADCDataBuf.buf[i]=0x00;
}
}
/**************************************************************************
*
***************************************************************************/
int RecvACLData(void)
{
uint16 datalength;
uint8 datalengthH,datalengthL,dataFlag;
uint8 tbyte;
uint8 mdata;
uint8 i;
uint8 devnum;
BTCOM_GetByte(&tbyte);
//Acl_Packet_handle=tbyte;
RcvData_packet.handle[0]=tbyte;
ACL_hl=tbyte;
BTCOM_GetByte(&tbyte);
RcvData_packet.handle[1]=(tbyte&0x0f);
ACL_hh=tbyte&0x0f;
BTCOM_GetByte(&tbyte);
datalengthL=tbyte;
BTCOM_GetByte(&tbyte);
datalengthH=tbyte;
datalength=(datalengthH<<8);
datalength=datalength+datalengthL;
//OPC_Insp=datalength;
BTCOM_GetByte(&tbyte);
mdata=tbyte;
dataFlag=mdata&0x0f;
devnum=(mdata>>4)&0x0f;
if(dataFlag==COMMAND_DATA)
{
BTCOM_GetByte(&tbyte);
//CommandBehave=tbyte;
HostCommandState=tbyte; //SAMP_PAUSE;
}
else
for(i=0;i<datalength-1;i++)
{
BTCOM_GetByte(&tbyte);
RcvData_packet.data_buffer[i]=tbyte;
}
return 0;
}
int SendADCData(void)
{
//unsigned int i=50;
//unsigned int tbyte=60;
unsigned char *phandle;
SendByte(0x02); //HCI数据分组
//Connection_Handle(2)
phandle=creat_connection_cfm.handle;
SendByte(*phandle++);
SendByte((*phandle) | 0x20);//第一个数据包
SendByte(0x05); //两个字节的数据长度
SendByte(0x00);
SendByte(SAMPLE_DATA|(DEVICE_NUM<<4));//SAMPLE_DATA SAMPLE_DATA|(DEVICE_NUM<<4) //数据包标志
SendByte(C1Data[0]);
SendByte(C1Data[1]);
SendByte(C2Data[0]);
SendByte(C2Data[1]);
return(1);
}
/*************************************************************************
*
**************************************************************************/
void SendTData(void)//SendTemp()
{
unsigned char *phandle;
SendByte(0x02); //HCI数据分组
//Connection_Handle(2)
phandle=creat_connection_cfm.handle;
SendByte(*phandle++);
SendByte((*phandle) | 0x20);//第一个数据包
SendByte(0x03); //两个字节的数据长度
SendByte(0x00);
SendByte(TEMP_DATA|(DEVICE_NUM<<4));//SAMPLE_DATA SAMPLE_DATA|(DEVICE_NUM<<4)
SendByte(Tdata[0]);
SendByte(Tdata[1]);
}
/*************************************************************************
*获得数据包类型,并作相应处理
**************************************************************************/
int GetPacket(void)
{
uint8 GroupType;
uint8 tbyte;
uint8 DataLength;
uint8 i;
if(BTRxBuf.cnt==0) return 0;
DelayNS(30);
GetPacket_F=1;
BTCOM_GetByte(&tbyte);
GroupType=tbyte;
switch( GroupType)
{
case HCI_ACL_Packet:
RecvACLData();
break;
case HCI_Event_Packet:
GetEvent();
break;
default :
BTCOM_GetByte(&tbyte);
BTCOM_GetByte(&tbyte);
BTCOM_GetByte(&tbyte);
DataLength=tbyte;
for(i=0;i<DataLength;i++)
BTCOM_GetByte(&tbyte);
break;
}
return 1;
}
/*************************************************************************
*获得事件码
**************************************************************************/
int GetEvent(void)
{
uint8 OPCodeL=0;
uint8 OPCodeH=0;
uint8 Ret_Parameter,Ret_Sta;
unsigned char eventcode;
unsigned char eventlength;
unsigned char tbyte;
unsigned char i=0;
if(BTRxBuf.cnt==0)
return 0;/**/
// do{
DelayNS(1);
//if( 0x01==(U0LSR&0x0F) ) rcv_new = 1;//
//i++;
// }while ((BTRxBuf.cnt==0)&&(i<10));
if(GetPacket_F!=1)
{
BTCOM_GetByte(&tbyte);
if (tbyte!=0x04)
return 0;
}
BTCOM_GetByte(&tbyte);
eventcode=tbyte; //事件码
BTCOM_GetByte(&tbyte);
eventlength=tbyte; //参数总长
switch(eventcode)
{
case HCI_Command_Complete_Event://不处理
BTCOM_GetByte(&tbyte);
BTCOM_GetByte(&tbyte);
OPCodeL=tbyte;
BTCOM_GetByte(&tbyte);
OPCodeH=tbyte;
BTCOM_GetByte(&tbyte);
Ret_Parameter=tbyte;
if(OPCodeL==0x03&OPCodeH==0x0C&Ret_Parameter==0) Rst_F=1;
if(OPCodeL==0x05&OPCodeH==0x0C&Ret_Parameter==0) EvtFil_F=1;//OPCodeL==0x05&OPCodeH==0x0C&
if(OPCodeL==0x1C&OPCodeH==0x0C&Ret_Parameter==0) WPgScan_F=1;
if(OPCodeL==0x1E&OPCodeH==0x0C&Ret_Parameter==0) WInqScan_F=1;
if(OPCodeL==0x1A&OPCodeH==0x0C&Ret_Parameter==0) WScanEn_F=1;
break;
/* case HCI_Connection_Req_Event://自己加的
BTCOM_GetByte(&tbyte);
connection_req.bd_addr.BD_ADDR[0]=tbyte;
BTCOM_GetByte(&tbyte);
connection_req.bd_addr.BD_ADDR[1]=tbyte;
BTCOM_GetByte(&tbyte);
connection_req.bd_addr.BD_ADDR[2]=tbyte;
BTCOM_GetByte(&tbyte);
connection_req.bd_addr.BD_ADDR[3]=tbyte;
BTCOM_GetByte(&tbyte);
connection_req.bd_addr.BD_ADDR[4]=tbyte;
BTCOM_GetByte(&tbyte);
connection_req.bd_addr.BD_ADDR[5]=tbyte;
BTCOM_GetByte(&tbyte);//Class_of_Device=tbyte;
BTCOM_GetByte(&tbyte);
BTCOM_GetByte(&tbyte);
BTCOM_GetByte(&tbyte);
connection_req.Linktype=tbyte;
ConReq_F=1;
break;*/
case HCI_Connection_Complete_Event://连接已建立
BTCOM_GetByte(&tbyte);
creat_connection_cfm.status=tbyte;//"."还是"->"
BTCOM_GetByte(&tbyte);
creat_connection_cfm.handle[0]=tbyte;
BTCOM_GetByte(&tbyte);
creat_connection_cfm.handle[1]=tbyte;
BTCOM_GetByte(&tbyte);
creat_connection_cfm.bd_addr.BD_ADDR[0]=tbyte;
BTCOM_GetByte(&tbyte);
creat_connection_cfm.bd_addr.BD_ADDR[1]=tbyte;
BTCOM_GetByte(&tbyte);
creat_connection_cfm.bd_addr.BD_ADDR[2]=tbyte;
BTCOM_GetByte(&tbyte);
creat_connection_cfm.bd_addr.BD_ADDR[3]=tbyte;
BTCOM_GetByte(&tbyte);
creat_connection_cfm.bd_addr.BD_ADDR[4]=tbyte;
BTCOM_GetByte(&tbyte);
creat_connection_cfm.bd_addr.BD_ADDR[5]=tbyte;
BTCOM_GetByte(&tbyte);//encription mode; 00-不需加密;01-全部连接需加密
BTCOM_GetByte(&tbyte);
if(creat_connection_cfm.status==0)ConCplt_F=1;
break;
case HCI_Disconnect_Complete_Event:
BTCOM_GetByte(&tbyte);
Ret_Sta=tbyte;
BTCOM_GetByte(&tbyte);
BTCOM_GetByte(&tbyte);
BTCOM_GetByte(&tbyte);
if(Ret_Sta==0) ConnectionState=DISCONNECTED;
break;
case HCI_Mode_Change_Event:
BTCOM_GetByte(&tbyte);
Mode_sta=tbyte;
BTCOM_GetByte(&tbyte);
Mode_handle[0]=tbyte;
BTCOM_GetByte(&tbyte);
Mode_handle[1]=tbyte;
BTCOM_GetByte(&tbyte);
Mode_current=tbyte;
BTCOM_GetByte(&tbyte);
Mode_Interval[0]=tbyte;
BTCOM_GetByte(&tbyte);
Mode_Interval[1]=tbyte;
break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -