⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mac_beacon_handler.c

📁 ucos在NEC平台下的移植
💻 C
📖 第 1 页 / 共 3 页
字号:
    // Set the total packet length
    pPacket->length = pPacket->headerLength + (pPayload - pPacket->pPayload) + MAC_FCS_LENGTH;
    
    // Calculate the packet duration 
    pPacket->duration = msupCalcPacketDuration(pPacket->length, pPacket->txOptions & TX_OPT_ACK_REQ);

} // mbcnPrepareBeacon

#endif  // MAC_OPT_FFD



/*******************************************************************************************************
 *******************************************************************************************************
 **************************                 UTILITY FUNCTIONS                 **************************
 *******************************************************************************************************
 *******************************************************************************************************/
 
 
 
 
//-------------------------------------------------------------------------------------------------------
//  void mbcnGetBeaconMargin(void)
//
//  DESCRIPTION:
//      Because of the crystal inaccuracy, a margin must be added before the beacon that will
//      be received. The result is that:
//          - RX must be turned on a short while before the beacon is expected to be received.
//          - TX must stop before this margin is reached
//
//      The Excel worksheet called "Beacon reception margin.xls" can be used to calculate these 
//      constants.
//
//  RETURN VALUE:
//      UINT8
//          The margin as a number of backoff periods
//-------------------------------------------------------------------------------------------------------
UINT8 mbcnGetBeaconMargin(void) {
    switch (mpib.macBeaconOrder) {
        case 7:  return 2;
        case 8:  return 3;
        case 9:  return 5;
        case 10: return 10;
        case 11: return 20;
        case 12: return 40;
        case 13: return 79;
        case 14: return 158;
        default: return 1;
    }
} // mbcnGetBeaconMargin




//-------------------------------------------------------------------------------------------------------
//  void mbcnUpdateBufferedAttributes(void)
//
//  DESCRIPTION:
//      Updates category 4 attributes (see mlmeSetRequest()) before the beacon is received or 
//      transmitted. Only attributes indicated in the update mask will be changed.
//-------------------------------------------------------------------------------------------------------
void mbcnUpdateBufferedPibAttributes(void) {
    DISABLE_GLOBAL_INT();
    if (macInfo.pibTempBuffer.updateMask & MPIB_UPD_BATT_LIFE_EXT_BM)    mpib.macBattLifeExt = macInfo.pibTempBuffer.macBattLifeExt;
    if (macInfo.pibTempBuffer.updateMask & MPIB_UPD_BEACON_ORDER_BM)     mpib.macBeaconOrder = macInfo.pibTempBuffer.macBeaconOrder;
    if (macInfo.pibTempBuffer.updateMask & MPIB_UPD_RX_ON_WHEN_IDLE_BM)  mpib.macRxOnWhenIdle = macInfo.pibTempBuffer.macRxOnWhenIdle;
    if (macInfo.pibTempBuffer.updateMask & MPIB_UPD_SUPERFRAME_ORDER_BM) mpib.macSuperframeOrder = macInfo.pibTempBuffer.macSuperframeOrder;
    macInfo.pibTempBuffer.updateMask = 0x00;
    ENABLE_GLOBAL_INT();
} // mbcnUpdateBufferedPibAttributes




//-------------------------------------------------------------------------------------------------------
//  void mbcnHandleBeaconModeChange(BOOL wasNonBeacon)
//
//  DESCRIPTION:
//      Handles the change from a beacon network to a non-beacon network and vice versa.
//
//  ARGUMENTS:
//      BOOL wasNonBeacon
//          Indicates the earlier state (BEFORE the mpib.macBeaconOrder variable was updated)
//-------------------------------------------------------------------------------------------------------
void mbcnHandleBeaconModeChange(BOOL wasNonBeacon) {
    
    // Handle non-beacon -> beacon network
    if (wasNonBeacon && (mpib.macBeaconOrder < 15)) {
        
        // "RX on when idle" should only be active in the superframe (set up by the beacon transmission task
        if (mpib.macRxOnWhenIdle) mrxDecrOnCounter();
        
#if MAC_OPT_FFD
        // The beacon transmission task will handle indirect packet expiration
        mtimCancelCallback(mbcnExpirePacketsNonBeacon);
#endif
        
    // Handle beacon -> non-beacon network
    } else if (!wasNonBeacon && (mpib.macBeaconOrder == 15)) {
    
        // Update buffered PIB attributes before the buffering mechanism is disabled
        macInfo.pibTempBuffer.updateMask &= MPIB_UPD_BATT_LIFE_EXT_BM | MPIB_UPD_RX_ON_WHEN_IDLE_BM;
        mbcnUpdateBufferedPibAttributes(); 
        
        // "RX on when idle" is always valid from now on
        if (mpib.macRxOnWhenIdle) mrxIncrOnCounter();
        
        // Cancel beacon transmission and reception, and turn on indirect packet expiration for non-beacon networks
#if MAC_OPT_FFD
        mtimCancelCallback(mbcnTxPeriodicalBeacon);
#endif
        mtimCancelCallback(mbcnRxPeriodicalBeacon);
#if MAC_OPT_FFD
        mtimSetCallback(mbcnExpirePacketsNonBeacon, aBaseSuperframeDuration / aUnitBackoffPeriod);
#endif
    }
    
} // mbcnHandleBeaconModeChange




/*******************************************************************************************************
 *******************************************************************************************************
 **************************                  BEACON TRACKING                  **************************
 *******************************************************************************************************
 *******************************************************************************************************/




//-------------------------------------------------------------------------------------------------------
//  void mbcnSyncToBeacons(MAC_TASK_INFO *pTask)
//
//  DESCRIPTION:
//      This task starts synchronization to beacons
//
//  TASK DATA:
//      0
//-------------------------------------------------------------------------------------------------------
void mbcnSyncToBeacons(MAC_TASK_INFO *pTask) {
    ENABLE_FIFOP_INT();

    // Reserve the beacon tracking task if necessary
    if (mbcnInfo.rxTaskNumber == NO_TASK) mbcnInfo.rxTaskNumber = mschReserveTask();

    // Set track status variables
    mbcnInfo.findBeacon = TRUE;
    mbcnInfo.noBcnCountdown = aMaxLostBeacons;
    mbcnInfo.trackBeacon = (BOOL) pTask->taskData;
    
    // Switch radio channel if necessary
    msupSetChannel(pTask->taskData >> 8, TRUE);
    mrxIncrOnCounter();
    
    // Start receiving beacons
    mbcnRxPeriodicalBeacon();
    
    // Remove this task
    mschRemoveTask(pTask->priority, MSCH_KEEP_TASK_IN_PROGRESS_BM);
    
} // mbcnSyncToBeacons




/*******************************************************************************************************
 *******************************************************************************************************
 **************************          NON-BEACON INDIRECT EXPIRATION           **************************
 *******************************************************************************************************
 *******************************************************************************************************/



#if MAC_OPT_FFD

//-------------------------------------------------------------------------------------------------------
//  void mbcnExpirePacketsNonBeacon(void)
//
//  DESCRIPTION:
//      Schedules the next expiration "tick", and starts the expiration task
//-------------------------------------------------------------------------------------------------------
void mbcnExpirePacketsNonBeacon(void) {
    
    // Schedule the next round (48 backoff periods from now)
    mtimSetCallback(mbcnExpirePacketsNonBeacon, aBaseSuperframeDuration / aUnitBackoffPeriod);
    
    // Create the task that will expire old packets
    mschAddTask(mschReserveTask(), MAC_TASK_PRI_MEDIUM, mbcnExpirePacketsNonBeaconTask, 0);
    
} // mbcnExpirePacketsNonBeacon




//-------------------------------------------------------------------------------------------------------
//  void mbcnExpirePacketsNonBeaconTask(MAC_TASK_INFO *pTask)
//
//  DESCRIPTION:
//      Expires indirect packets in a non-beacon network by decrementing the "time to live" counter, and
//      then transforming the task into an miqExpireIndirectPacketsTask().
//
//  TASK DATA:
//      0
//-------------------------------------------------------------------------------------------------------
void mbcnExpirePacketsNonBeaconTask(MAC_TASK_INFO *pTask) {
    ENABLE_FIFOP_INT();

    // Decrement "time to live"
    miqDecrTimeToLive();
            
    // Continue in the miqExpireIndirectPacketsTask() function
    pTask->pTaskFunc = miqExpireIndirectPacketsTask;
    
} // mbcnExpirePacketsNonBeaconTask


#endif // MAC_OPT_FFD




/*******************************************************************************************************
 * Revision history:                                                                                   
 *
 * $Log: mac_beacon_handler.c,v $
 * Revision 1.10  2005/03/01 12:42:50  thl
 * Inn CC2420_RXCTRL1 the RXBPF_LOCUR is now enabled,
 * result is about 1,2-1,5mA reduction in RX current for the chip.
 *
 * Revision 1.9  2004/11/18 15:07:57  thl
 * Added support for pending frame bit in outgoing data frames.
 * Only apply to outgoing data frames and wherer the address matches
 * one or more packet in the indirect packet quee.
 *
 * Revision 1.8  2004/11/10 09:33:13  thl
 * Fixed a number of bugs according to: MAC software check lists.xls
 *
 * Revision 1.7  2004/08/13 13:04:46  jol
 * CC2420 MAC Release v0.7
 *
 *
 *******************************************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -