📄 mac_support.c
字号:
// Source PAN ID
if (!(temp & INTRA_PAN_BM) && (addrModes & SRC_ADDR_BM)) {
memcpy(pPacket->pHeader + length, &srcPanId, 2);
length += 2;
}
// Source address
if ((addrModes & SRC_ADDR_BM) == SRC_ADDR_SHORT) {
memcpy(pPacket->pHeader + length, (BYTE*) &pSrcAddr->Short, 2);
length += 2;
} else if ((addrModes & SRC_ADDR_BM) == SRC_ADDR_EXT) {
memcpy(pPacket->pHeader + length, (BYTE*) &pSrcAddr->Extended, 8);
length += 8;
}
pPacket->headerLength = length;
#if MAC_OPT_SECURITY
// By default, set cleartextLenght to header length
// Include command frame identifier for MAC command frames
// Note: clearTextLength must be incremented for beacon frames
pPacket->securitySetup.clearTextLength = length + (type == FT_MAC_COMMAND);
// Decode security suite and find setup information
// Set clearTextLength to 0 for CBC-MAC
msecDecodeSecuritySuite(&pPacket->securitySetup, pPacket->securitySuite);
#endif
} // msupPrepareHeader
//-------------------------------------------------------------------------------------------------------
// UINT8 msupCalcPacketDuration(UINT8 length, ZBOOL ackRequest)
//
// DESCRIPTION:
// Calculates the number of backoff slots required to transmit a packet, including (turnaround time,
// acknowledgment and) inter-frame spacing.
//
// PARAMETERS:
// UINT8 length
// The packet length in bytes (PHY frame length field)
// ZBOOL ackRequest
// Acknowledged packet?
//
// RETURN VALUE:
// UINT8
// The number of backoff slots required to transmit the packet
//-------------------------------------------------------------------------------------------------------
UINT8 msupCalcPacketDuration(UINT8 length, ZBOOL ackRequest) {
UINT8 totalByteCount, lastBosByteCount;
// Calculate the number of bytes in the whole packet (not including the preamble, but including
// SFD, length-byte and PSDU)
totalByteCount = 1 + 1 + length;
// Acknowledgment request
if (ackRequest) {
// The number of bytes in the last backoff slot
lastBosByteCount = totalByteCount % (aUnitBackoffPeriod / 2);
// Round up the total count to N * (aUnitBackoffPeriod / 2), and add an extra slot if there isn't enough turnaround time before the ack (12-32 symbols)
if (lastBosByteCount) totalByteCount += (aUnitBackoffPeriod / 2) - lastBosByteCount;
// Add the bytes in the acknowledgment frame
totalByteCount += (aUnitBackoffPeriod / 2) + (1 + 1 + 5);
}
// Add interframe spacing
if (length > aMaxSIFSFrameSize) {
totalByteCount += (aMinLIFSPeriod / 2);
} else {
totalByteCount += (aMinSIFSPeriod / 2);
}
// Round up
return ((BYTE) (totalByteCount + ((aUnitBackoffPeriod / 2) - 1))) / (aUnitBackoffPeriod / 2);
} // msupCalcPacketDuration
/*******************************************************************************************************
*******************************************************************************************************
************************** CC2420 RAM/FIFO ACCESS **************************
*******************************************************************************************************
*******************************************************************************************************/
//-------------------------------------------------------------------------------------------------------
// void msupWriteRam(void *pData, UINT16 address, UINT8 count, ZBOOL disableInterrupts)
//
// DESCRIPTION:
// Write data to the CC2420 RAM-space
// Used for setup of addresses and security
//
// PARAMETERS:
// void *pData
// Pointer to data to be written
// UINT16 address
// CC2420 RAM Address
// UNIT8 count
// Number of bytes to be written
// ZBOOL disableInterrupts
// Disable global interrupts while writing?
//-------------------------------------------------------------------------------------------------------
void msupWriteRam(void *pData, UINT16 address, UINT8 count, ZBOOL disableInterrupts) {
/* if (count) {
do {
if (disableInterrupts) DISABLE_GLOBAL_INT();
SPI_ENABLE();
FASTSPI_TX(0x80 | (address & 0x7F)); \
FASTSPI_TX((address >> 1) & 0xC0); \
do {
FASTSPI_TX(*((BYTE*) pData));
pData++;
} while ((--count) & 0x07);
SPI_DISABLE();
if (disableInterrupts) ENABLE_GLOBAL_INT();
address += 8;
} while (count);
}*/ //--li
} // msupWriteRam
//-------------------------------------------------------------------------------------------------------
// void msupReadFifo(void *pData, UINT8 count)
//
// DESCRIPTION:
// Reads data from the CC2420 RX FIFO
//
// PARAMETERS:
// void *pData
// Pointer to data to be read
// UNIT8 count
// Number of bytes to be read
//-------------------------------------------------------------------------------------------------------
void msupReadFifo(void *pData, UINT8 count) {
//DISABLE_GLOBAL_INT();
//SPI_ENABLE();
//FASTSPI_RX_ADDR(CC2420_RXFIFO);
//if (count) {
// do {
// FASTSPI_RX(*((BYTE*) pData));
// pData++;
// } while (--count);
//}
//SPI_DISABLE();
//ENABLE_GLOBAL_INT(); --li
} // msupReadFifo
//-------------------------------------------------------------------------------------------------------
// void msupWriteFifo(void *pData, UINT8 count)
//
// DESCRIPTION:
// Write data to the CC2420 TX FIFO
//
// PARAMETERS:
// void *pData
// Pointer to data to be written
// UNIT8 count
// Number of bytes to be written
//-------------------------------------------------------------------------------------------------------
void msupWriteFifo(void *pData, UINT8 count) {
/*if (count) {
do {
DISABLE_GLOBAL_INT();
SPI_ENABLE();
FASTSPI_TX_ADDR(CC2420_TXFIFO);
do {
FASTSPI_TX(*((BYTE*) pData));
pData++;
} while ((--count) & 0x07);
SPI_DISABLE();
ENABLE_GLOBAL_INT();
} while (count);
}*/ //--li
} // msupWriteFifo
/*******************************************************************************************************
*******************************************************************************************************
************************** SIMPLE CALCULATIONS **************************
*******************************************************************************************************
*******************************************************************************************************/
//-------------------------------------------------------------------------------------------------------
// UINT32 msupCalcCapDuration(void)
//
// DESCRIPTION:
// Calculates the number off backoff slots in the superframe, minus the CFP. Does not subtract the
// beacon transmission time.
//
// RETURN VALUE:
// UINT32
// See above
//-------------------------------------------------------------------------------------------------------
UINT32 msupCalcCapDuration(void) {
return ((UINT32) (aBaseSlotDuration / aUnitBackoffPeriod) * (UINT32) (mbcnInfo.lastSfSpec.finalCap + (UINT32) 1)) << (UINT32) mpib.macSuperframeOrder;
} // msupCalcCapDuration
//-------------------------------------------------------------------------------------------------------
// UINT32 msupCalcSuperframeDuration(void)
//
// DESCRIPTION:
// Calculates the number off backoff slots in the superframe. Does not subtract the beacon
// transmission time.
//
// RETURN VALUE:
// UINT32
// See above
//-------------------------------------------------------------------------------------------------------
UINT32 msupCalcSuperframeDuration(void) {
return ((UINT32) (aBaseSlotDuration / aUnitBackoffPeriod) * (UINT32) 16) << (UINT32) mpib.macSuperframeOrder;
} // msupCalcCapDuration
//-------------------------------------------------------------------------------------------------------
// UINT32 msupCalcBeaconInterval(void)
//
// DESCRIPTION:
// Calculates the number off backoff slots in the beacon interval.
//
// RETURN VALUE:
// UINT32
// See above
//-------------------------------------------------------------------------------------------------------
UINT32 msupCalcBeaconInterval(void) {
return (UINT32) (aBaseSuperframeDuration / aUnitBackoffPeriod) << (UINT32) mpib.macBeaconOrder;
} // msupCalcBeaconInterval
//-------------------------------------------------------------------------------------------------------
// MAC_ENUM msupTransmitPanConflict(BYTE coordAddrMode, WORD coordPANId, ADDRESS *pCoordAddress, ZBOOL ...)
//
// DESCRIPTION:
// Transmits a PAN ID conflict notification message.
//
// ARGUMENTS:
// BYTE coordAddrMode
// The address mode used by the coordinator
// WORD coordPANId
// The PAN ID used by the coordinator
// ADDRESS *pCoordAddress
// A pointer to the coordinator address (taken from the MAC PIB, short or extended)
// ZBOOL securityEnable
// TBD...
//
// RETURN VALUE:
// MAC_ENUM
// SUCCESS No error (the transmission was initiated)
// RESOURCE_SHORTAGE TX packet or task not available
// UNAVAILABLE_KEY No key
// FAILED_SECURITY_CHECK Failed security check
//-------------------------------------------------------------------------------------------------------
MAC_ENUM msupTransmitPanConflict(BYTE coordAddrMode, WORD coordPANId, ADDRESS *pCoordAddress, ZBOOL securityEnable) {
MAC_TX_PACKET *pPacket;
UINT8 taskNumber;
BYTE txOptions = 0;
UINT8 temp;
BYTE *pPayload;
// Try to reserve a packet
pPacket = mtxpReservePacket();
if (!pPacket) return RESOURCE_SHORTAGE;
// Try to reserve a transmission task
taskNumber = mschReserveTask();
if (taskNumber == NO_TASK) {
mtxpReleasePacket(pPacket);
return RESOURCE_SHORTAGE;
}
#if MAC_OPT_SECURITY
if (securityEnable) {
txOptions |= TX_OPT_SECURITY_ENABLE;
}
#endif
// TX mode
pPacket->txMode = MTX_MODE_USE_CSMACA_BM;
pPacket->retriesLeft = aMaxFrameRetries;
// Generate the packet header (and find security material, if enabled)
if (mpib.macShortAddress >= 0xFFFE) {
msupPrepareHeader(pPacket, FT_MAC_COMMAND, (coordAddrMode << 2) | SRC_ADDR_EXT, mpib.macPANId, (ADDRESS*) &aExtendedAddress, coordPANId, pCoordAddress, txOptions);
} else {
msupPrepareHeader(pPacket, FT_MAC_COMMAND, (coordAddrMode << 2) | SRC_ADDR_SHORT, mpib.macPANId, (ADDRESS*) &mpib.macShortAddress, coordPANId, pCoordAddress, txOptions);
}
// Store the command type
pPacket->commandType = CMD_PAN_ID_CONFLICT_NOTIFICATION;
// Command frame identifier
pPayload = pPacket->pPayload;
*(pPayload++) = CMD_PAN_ID_CONFLICT_NOTIFICATION;
#if MAC_OPT_SECURITY
// #bytes in frame counter + key sequence counter (0 or 5)
temp = msecProcessSecurityCounters(pPacket, pPayload);
// In case of frame counter overflow or missing key
// Generate error with FAILED_SECURITY_CHECK or UNAVAILABLE_KEY
if (pPacket->securitySuite >= 8) {
mtxpReleasePacket(pPacket);
mschReleaseTask(taskNumber);
return pPacket->securitySuite;
}
// Increment payload pointer when counters inserted
pPayload += temp;
// Include command payload length and optional MIC (integrity code) length
temp += CMD_DATA_REQUEST_PAYLOAD_LENGTH + pPacket->securitySetup.micLength;
#else
// No security material included, set MAC payload length
temp = CMD_PAN_ID_CONFLICT_NOTIFICATION_PAYLOAD_LENGTH;
#endif
// Set the packet length
pPacket->length = pPacket->headerLength + temp + MAC_FCS_LENGTH;
// No further packet payload
// Calculate the packet duration (including FCS)
pPacket->duration = msupCalcPacketDuration(pPacket->length, pPacket->txOptions);
// Initiate the transmission
mschAddTask(taskNumber, MAC_TASK_PRI_LOW, mtxScheduleTransmission, (WORD) pPacket);
return SUCCESS;
} // mipTransmitDataRequest
/*******************************************************************************************************
* Revision history:
*
* $Log: mac_support.c,v $
* Revision 1.15 2005/02/04 13:16:06 thl
* Fixed problem with oncounter and CC2420 when disabling RX
*
* Revision 1.14 2005/02/02 15:08:17 thl
* Added functionality for pan id conflict according to IEEE802.15.4-2003
*
* Revision 1.13 2005/01/05 10:45:29 thl
* Added full suport for ppib.phyTransmitPower, will also now adjust the transmit power
* of the radio chip. to include set compile switch MAC_OPT_TRANSMIT_POWER=1 in
* make file.
*
* Revision 1.12 2004/11/10 09:33:15 thl
* Fixed a number of bugs according to: MAC software check lists.xls
*
* Revision 1.11 2004/08/13 13:04:47 jol
* CC2420 MAC Release v0.7
*
*
*******************************************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -