📄 spp.c
字号:
culDmaFromRadio(dmaRx, 0, TRUE);
// Making sure that none of the channels are armed.
DMA_ABORT_CHANNEL(dmaNumberRx);
DMA_ABORT_CHANNEL(dmaNumberTx);
INT_ENABLE(INUM_DMA, INT_ON);
return status;
} // ends sppInit
//-----------------------------------------------------------------------------
// See cul.h for a description of this function.
//-----------------------------------------------------------------------------
void sppSetAddress(BYTE address){
myAddress = address;
} // Ends sppSetAddress()
// Internal function which enables the timeout when waiting for an ACK.
void waitForAck(void)
{
ackTimerNumber = culTimer4AdmSet(ACK_TIMEOUT, &ackTimeout);
SET_DMA_DEST(dmaRx,pAckBuffer);
SET_DMA_LENGTH(dmaRx,7);
} // Ends waitForAck()
//-----------------------------------------------------------------------------
// See cul.h for a description of this function.
//-----------------------------------------------------------------------------
BYTE sppSend(SPP_TX_STRUCT* pPacketPointer){
BYTE res = TRUE;
// If data is to be transmitted, the DMA is set up.
if(pPacketPointer->payloadLength)
{
if (pPacketPointer->payloadLength > SPP_MAX_PAYLOAD_LENGTH)
{
res = TOO_LONG;
sppTxStatus = TX_IDLE;
}
else
{
// Setting up the DMA
DMA_ABORT_CHANNEL(dmaNumberTx);
SET_DMA_SOURCE(dmaTx,pPacketPointer->payload);
SET_DMA_LENGTH(dmaTx,pPacketPointer->payloadLength);
}
}
// Proceed if the packet length is OK.
if (res == TRUE)
{
// Flipping the sequence bit if the transfer is not a retransmission.
if(!(pPacketPointer->flags & RETRANSMISSION))
{
pPacketPointer->flags ^= SEQUENCE_BIT;
}
// Clearing RF interrupt flags and enabling RF interrupts.
if(FSMSTATE == 6 && RXFIFOCNT > 0)
{
ISFLUSHRX;
ISFLUSHRX;
}
RFIF &= ~IRQ_TXDONE;
RFIM &= ~IRQ_SFD;
INT_SETFLAG(INUM_RF, INT_CLR);
// Writing the total packet length, addresses and flags to Tx FiFo.
// Transferring the payload if any.
RFD = (pPacketPointer->payloadLength + SPP_HEADER_AND_FOOTER_LENGTH);
RFD = pPacketPointer->destAddress;
RFD = myAddress;
RFD = pPacketPointer->flags;
if(pPacketPointer->payloadLength)
{
DMA_ARM_CHANNEL(dmaNumberTx);
DMA_START_CHANNEL(dmaNumberTx);
}
// If the RSSI value is not valid, enable receiver
if(RSSIL == 0x80)
{
ISRXON;
// Turning on Rx and waiting 320u-sec to make the RSSI value become valid.
halWait(1);
}
//Transmitting
ISTXONCCA;
//if(TX_ACTIVE)
if(FSMSTATE > 30)
{
// Asserting the status flag and enabling ACK reception if expected.
sppTxStatus = TX_IN_PROGRESS;
if(pPacketPointer->flags & DO_ACK)
{
pAckData = pPacketPointer;
DMA_ABORT_CHANNEL(dmaNumberRx);
waitForAck();
}
else
{
pAckData = NULL;
}
RFIM |= IRQ_TXDONE;
}
else
{
ISFLUSHTX;
res = CHANNEL_BUSY;
RFIM &= ~IRQ_TXDONE;
// De-flipping the sequence bit.
if(!(pPacketPointer->flags & RETRANSMISSION))
{
pPacketPointer->flags ^= SEQUENCE_BIT;
}
}
}
return res;
} // ends sppSend
// Internal function which is called when an ack is received.
// If the ACK is from the expected node, the retransmission of the packet is cancelled.
BOOL ackReceived(BYTE sourceAddress)
{
BOOL res = FALSE;
if(sourceAddress == pAckData->destAddress)
{
res = TRUE;
culTimer4AdmClear(ackTimerNumber);
sppTxStatus = TX_SUCCESSFUL;
retransmissionCounter = 0;
pAckData = 0;
}
return res;
} //Ends ackReceived()
//-----------------------------------------------------------------------------
// See cul.h for a description of this function.
//-----------------------------------------------------------------------------
#pragma vector=RF_VECTOR
__interrupt void spp_rf_IRQ(void)
{
BYTE enabledAndActiveInterrupt;
INT_GLOBAL_ENABLE(INT_OFF);
enabledAndActiveInterrupt = RFIF;
RFIF = 0x00; // Clear all interrupt flags
INT_SETFLAG(INUM_RF, INT_CLR); // Clear MCU interrupt flag
enabledAndActiveInterrupt &= RFIM;
// Start of frame delimiter (SFD)
if(enabledAndActiveInterrupt & IRQ_SFD)
{
if(sppRxStatus == RX_WAIT)
{
sppRxStatus = RX_IN_PROGRESS;
RFIM &= ~IRQ_SFD;
}
}
// Transmission of a packet is finished. Enabling reception of ACK if required.
if(enabledAndActiveInterrupt & IRQ_TXDONE)
{
if(sppTxStatus == TX_IN_PROGRESS)
{
if(pAckData == NULL)
{
sppTxStatus = TX_SUCCESSFUL;
}
else
{
DMA_ARM_CHANNEL(dmaNumberRx);
}
}
// Clearing the tx done interrupt enable
RFIM &= ~IRQ_TXDONE;
}
INT_GLOBAL_ENABLE(INT_ON);
}
//-----------------------------------------------------------------------------
// See cul.h for a description of this function.
//-----------------------------------------------------------------------------
void sppReceive(SPP_RX_STRUCT* pReceiveData){
sppRxStatus = RX_WAIT;
DMA_ABORT_CHANNEL(dmaNumberRx);
// Setting the address to where the received data are to be written.
SET_DMA_DEST(dmaRx,pReceiveData);
SET_DMA_LENGTH(dmaRx,255);
// Arming the DMA channel. The receiver will initate the transfer when a packet is received.
DMA_ARM_CHANNEL(dmaNumberRx);
if(FSMSTATE == 6 && RXFIFOCNT > 0)
{
ISFLUSHRX;
ISFLUSHRX;
}
// Turning on the receiver
ISRXON;
return;
}
//-----------------------------------------------------------------------------
// See cul.h for a description of this function.
//-----------------------------------------------------------------------------
#pragma vector=RFERR_VECTOR
__interrupt static void rf_error_IRQ(void)
{
INT_GLOBAL_ENABLE(INT_OFF);
// If Rx overflow occurs, the Rx FiFo is reset.
// The Rx DMA is reset and reception is started over.
if(FSMSTATE == 17)
{
STOP_RADIO();
ISFLUSHRX;
ISFLUSHRX;
DMA_ABORT_CHANNEL(dmaNumberRx);
DMA_ARM_CHANNEL(dmaNumberRx);
ISRXON;
}
else if(FSMSTATE == 56)
{
ISFLUSHTX;
}
INT_SETFLAG(INUM_RFERR,INT_CLR);
INT_GLOBAL_ENABLE(INT_ON);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -