发送代码/******************************************************************************
* @fn cc2420Send
*
* @brief
* 设置Address函数
*
* Parameters:
*
* @param uint ShortAddress
*
* @return void
*
******************************************************************************/
void SetAddress(unsigned char Number,unsigned int PanID,unsigned int DestAddress,unsigned int MyAddress)
{
rfSettings.txSeqNumber=Number;
rfSettings.panId=PanID;
rfSettings.destAddr=DestAddress;
rfSettings.myAddr=MyAddress;
}
/******************************************************************************
* @fn cc2420Send
*
* @brief
* 发送数据
*
* Parameters:
*
* @param uint ShortAddress
*
* @return void
*
******************************************************************************/
void cc2420SendData(unsigned char ModeType)
{
unsigned char PackLenght,i;
unsigned int frameControlField;
// Wait until the transceiver is idle
while (FIFOP_IN|| SFD_IN);
// Turn off global interrupts to avoid interference on the SPI interface
DISABLE_GLOBAL_INT();
WriteStrobeReg_spi(CC2420_SFLUSHTX);
WriteStrobeReg_spi(CC2420_SFLUSHTX);
PackLenght=0;
switch(ModeType)
{
case 0x01:
{
DataTxFIFO[PackLenght++]=PackLenght; //第一字节为帧长度,不包括长度域本身的字节,但包含帧校验序列2字节
//帧控制域,无安全,确认要求,数据帧,包含16位的目标地址,源地址,目标PAN标识符,源PAN标识符
frameControlField = CC2420_FCF_FRAMETYPE_DATA;
frameControlField |= 0 ? CC2420_FCF_ACK_REQUEST : CC2420_FCF_NO_ACK_REQUEST;
frameControlField |= CC2420_FCF_INTRAPAN;
frameControlField |= CC2420_FCF_DESTADDR_16BIT;
frameControlField |= CC2420_FCF_SOURCEADDR_16BIT;
DataTxFIFO[PackLenght++]=frameControlField >> 8; ; //高字节
DataTxFIFO[PackLenght++]=frameControlField & 0x00FF; //低字节
DataTxFIFO[PackLenght++]=rfSettings.txSeqNumber;; //Sequence number
DataTxFIFO[PackLenght++]=rfSettings.panId >> 8;
DataTxFIFO[PackLenght++]=rfSettings.panId & 0x00FF; //目的PAN标识符
DataTxFIFO[PackLenght++]=rfSettings.destAddr >> 8;
DataTxFIFO[PackLenght++]=rfSettings.destAddr & 0x00FF; //目的地址
DataTxFIFO[PackLenght++]=rfSettings.myAddr >> 8;
DataTxFIFO[PackLenght++]=rfSettings.myAddr & 0x00FF; //源PAN标识符
for (; PackLenght < 20 ;PackLenght++) //有效负载
{
DataTxFIFO[PackLenght]=20;
}
PackLenght++; //CRC
DataTxFIFO[0]=PackLenght;
break;
}
case 0x02:
{
DataTxFIFO[PackLenght++]=PackLenght; //第一字节为帧长度,不包括长度域本身的字节,但包含帧校验序列2字节
//帧控制域,无安全,确认要求,数据帧,包含16位的目标地址,源地址,目标PAN标识符,源PAN标识符
frameControlField = CC2420_FCF_FRAMETYPE_DATA;
frameControlField |= 0 ? CC2420_FCF_ACK_REQUEST : CC2420_FCF_NO_ACK_REQUEST;
frameControlField |= CC2420_FCF_INTRAPAN;
frameControlField |= CC2420_FCF_DESTADDR_16BIT;
frameControlField |= CC2420_FCF_SOURCEADDR_64BIT;
DataTxFIFO[PackLenght++]=frameControlField >> 8; ; //高字节
DataTxFIFO[PackLenght++]=frameControlField & 0x00FF; //低字节
DataTxFIFO[PackLenght++]=rfSettings.txSeqNumber;; //Sequence number
DataTxFIFO[PackLenght++]=rfSettings.panId >> 8;
DataTxFIFO[PackLenght++]=rfSettings.panId & 0x00FF; //目的PAN标识符
DataTxFIFO[PackLenght++]=rfSettings.destAddr >> 8;
DataTxFIFO[PackLenght++]=rfSettings.destAddr & 0x00FF; //目的地址
for( ; PackLenght < PackLenght + 8 ;PackLenght++) // Source address
{
DataTxFIFO[PackLenght] = rfSettings.myIEEE[PackLenght-8];
}
for (; PackLenght < 20 ;PackLenght++) //有效负载
{
DataTxFIFO[PackLenght]=20;
}
PackLenght++; //CRC
DataTxFIFO[0]=PackLenght;
break;
}
case 0x03:
{
DataTxFIFO[PackLenght++]=PackLenght; //第一字节为帧长度,不包括长度域本身的字节,但包含帧校验序列2字节
//帧控制域,无安全,确认要求,数据帧,包含16位的目标地址,源地址,目标PAN标识符,源PAN标识符
frameControlField = CC2420_FCF_FRAMETYPE_DATA;
frameControlField |= 1 ? CC2420_FCF_ACK_REQUEST : CC2420_FCF_NO_ACK_REQUEST;
frameControlField |= CC2420_FCF_INTRAPAN;
frameControlField |= CC2420_FCF_DESTADDR_64BIT;
frameControlField |= CC2420_FCF_SOURCEADDR_16BIT;
DataTxFIFO[PackLenght++]=frameControlField >> 8; ; //高字节
DataTxFIFO[PackLenght++]=frameControlField & 0x00FF; //低字节
DataTxFIFO[PackLenght++]=rfSettings.txSeqNumber;; //Sequence number
DataTxFIFO[PackLenght++]=rfSettings.panId >> 8;
DataTxFIFO[PackLenght++]=rfSettings.panId & 0x00FF; //目的PAN标识符
for( ; PackLenght < PackLenght + 8 ;PackLenght++) //目的地址
{
DataTxFIFO[PackLenght] = rfSettings.destIEEE[PackLenght-8];
}
DataTxFIFO[PackLenght++]=rfSettings.myAddr >> 8;
DataTxFIFO[PackLenght++]=rfSettings.myAddr & 0x00FF; //源PAN标识符
for (; PackLenght < 20 ;PackLenght++) //有效负载
{
DataTxFIFO[PackLenght]=20;
}
PackLenght++; //CRC
DataTxFIFO[0]=PackLenght;
break;
}
}
Write_TXFIFO(DataTxFIFO,PackLenght);
WriteStrobeReg_spi(CC2420_STXON);
while (!SFD_IN);
while (SFD_IN);
// Turn interrupts back on
ENABLE_GLOBAL_INT();
}
|