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

📄 mac_power_management.c

📁 zigbee location examples
💻 C
📖 第 1 页 / 共 2 页
字号:
/*******************************************************************************************************
 *                                                                                                     *
 *        **********                                                                                   *
 *       ************                                                                                  *
 *      ***        ***                                                                                 *
 *      ***   +++   ***                                                                                *
 *      ***   + +   ***                                                                                *
 *      ***   +                         CHIPCON CC2430 INTEGRATED 802.15.4 MAC AND PHY                 *
 *      ***   + +   ***                             CC2430 Power Management                            *
 *      ***   +++   ***                                                                                *
 *      ***        ***                                                                                 *
 *       ************                                                                                  *
 *        **********                                                                                   *
 *                                                                                                     *
 *******************************************************************************************************
 * CONFIDENTIAL                                                                                        *
 * The use of this file is restricted by the signed MAC software license agreement.                    *
 *                                                                                                     *
 * Copyright Chipcon AS, 2005                                                                          *
 *******************************************************************************************************
 * This module contains functions to be used by the higher layer to power down the CC2430.             *
 *******************************************************************************************************/
#include <mac_headers.h>

//-------------------------------------------------------------------------------------------------------
// Internal module data
MPM_INFO   mpmInfo;
//-------------------------------------------------------------------------------------------------------
void mpmSetTask(MAC_TASK_INFO *pTask);

static void ResumeCoordinatorAfterPowerDown (RESUME_MODE  resumeMode, BOOL synchronousStart);
static void ResumeDeviceAfterPowerDown (RESUME_MODE resumeMode, BOOL synchronousStart);

//-------------------------------------------------------------------------------------------------------
// Simple power up/down functions
ROOT void mpmTurnOnVregAndReset(void)
{
    DISABLE_GLOBAL_INT ();
    //RFR_MAINH = RESET_RF_CHIP_ON;
    RFPWR = VREG_POWER_UP;
    CLKCON = 0;
    halMacWait (0xFF);
    ENABLE_GLOBAL_INT ();
}

ROOT void mpmTurnOffReset(void)
{
    DISABLE_GLOBAL_INT();
    //RFR_MAINH = RESET_RF_CHIP_OFF;
    ENABLE_GLOBAL_INT();
}

ROOT void mpmTurnOnXosc(void)
{
    DISABLE_GLOBAL_INT();
    ISRXON; // TBD: Must be WRONG
    ENABLE_GLOBAL_INT();
}

ROOT void mpmTurnOffVreg(void)
{
    DISABLE_GLOBAL_INT();
    //RFR_MAINH = RESET_RF_CHIP_ON;
    RFPWR = VREG_POWER_DOWN;
    ENABLE_GLOBAL_INT();
}

ROOT void mpmTurnOffXosc(void)
{
    DISABLE_GLOBAL_INT();
    ISRFOFF;
    ENABLE_GLOBAL_INT();
}

//-------------------------------------------------------------------------------------------------------




//-------------------------------------------------------------------------------------------------------
//  void mpmRestoreRegsAndRam(void)
//
//  DESCRIPTION:
//      Restores all CC2430 registers and RAM, assuming that there was no activity in the MAC layer at
//      power-down. That includes:
//          MDMCTRL0 (register) (PAN coordinator bit)
//          MDMCTRL1 (register) (Correlation threshold, only required by old chip revisions, see data
//              sheets)
//          SECCTRL0 (register)
//          PANID (RAM)
//          IEEEADDR (RAM)
//          SHORTADDR (RAM)
//          FSCTRL (register)
//-------------------------------------------------------------------------------------------------------
ROOT void mpmRestoreRegsAndRam(void) {

    // Write default register values
    DISABLE_GLOBAL_INT();
#if MAC_OPT_FFD
    WRITE_RFR16(MDMCTRL0, GET_MF(MF_PAN_COORDINATOR) ? MDMCTRL0_PAN_COORDINATOR : MDMCTRL0_NO_PAN_COORDINATOR);
#endif
    ENABLE_AUTOACK();
    WRITE_RFR16(MDMCTRL1, 0x1400);
    SET_FIFOP_THRESHOLD(4);
    ENABLE_GLOBAL_INT();

    // PAN ID, extended and short addresses
    WRITE_RFR16(PANID, mpib.macPANId);
    WRITE_RFR16(SHORTADDR, mpib.macShortAddress);
    msupWriteExtendedAddress(aExtendedAddress);

    // Frequency word
    msupSetChannel(ppib.phyCurrentChannel, TRUE);

} // mpmRestoreRegsAndRam




//-------------------------------------------------------------------------------------------------------
//  void mpmSetTask(MAC_TASK_INFO *pTask)
//
//  DESCRIPTION:
//      This task is responsible for controlling CC2430 power, as requested by the higher layer through
//      mpmSetRequest. Power-down is currently only supported for non-beacon PANs.
//
//  TASK DATA:
//      The new power mode (MPM_CC2430_ON, MPM_CC2430_XOSC_OFF, or MPM_CC2430_XOSC_AND_VREG_OFF)
//-------------------------------------------------------------------------------------------------------
void mpmSetTask(MAC_TASK_INFO   *pTask) NEAR {

    // Powerdown is not permitted in non-beacon mode when "RX on when idle" is enabled
    if ((mpib.macBeaconOrder == 15) && mpib.macRxOnWhenIdle) {
        mschRemoveTask(pTask->priority, 0);
        mpmSetConfirm(ERR_RX_ON_WHEN_IDLE);
        return;
    }

    // Come back later if we're not in the default MAC state (there's something going on...)
    if (macInfo.state != MAC_STATE_DEFAULT) {
        mschRescheduleTask(pTask, 0);
        return;
    }

    // Switch to the new power mode
    mpmInfo.selectedMode = (BYTE) pTask->taskData;

    // In a beacon-enabled network the beacon handler will control powerup and powerdown
    // In a non-beacon network the changes will be made directly from here:
    switch (mpmInfo.selectedMode) {
    case MPM_CC2430_ON:

        // Turn on VREG?
        if (mpmInfo.currentState == MPM_CC2430_XOSC_AND_VREG_OFF) {
            mpmTurnOnVregAndReset();
        }

        /*
        // Restore registers and RAM if the voltage regulator was turned off
        if (mpmInfo.currentState == MPM_CC2430_XOSC_AND_VREG_OFF) {
            mpmRestoreRegsAndRam();
        }
        */

        // Turn on XOSC?
        if (mpmInfo.currentState != MPM_CC2430_ON) {
            mpmTurnOnXosc();
        }

        mrxInfo.keepFifopIntOff = FALSE;

        mpmInfo.currentState = MPM_CC2430_ON;
        break;

    case MPM_CC2430_XOSC_OFF:
        T2_STOP();
        mrxInfo.keepFifopIntOff = TRUE;
        mpmTurnOffXosc();
        break;

    case MPM_CC2430_XOSC_AND_VREG_OFF:
        T2_STOP();
        mrxInfo.keepFifopIntOff = TRUE;
        mpmTurnOffVreg();
        break;
    }

    // Update with the new transceiver state
    mpmInfo.currentState = mpmInfo.selectedMode;

    // Clean up and make the callback
    mschRemoveTask(pTask->priority, 0);
    mpmSetConfirm(OK_POWER_MODE_CHANGED);

} // mpmSetTask




//-------------------------------------------------------------------------------------------------------
//  void mpmSetRequest(BYTE mode)
//
//  DESCRIPTION:
//      This function allows the higher layer to power down CC2430 to extend battery lifetime. CC2430
//      must be powered up before any MLME or MCPS primitives can be used (both beacon/non-beacon modes).
//      Power-down is currently only supported for non-beacon PANs.
//
//      The change is not likely to happen instantaneously (under normal conditions the delay can be up
//      to 320 us). Use either the mpmSetConfirm callback, or poll the current power state by using
//      mpmGetState() (returns the selected power mode when it has become effective).
//
//  ARGUMENTS:
//      BYTE mode
//          MPM_CC2430_ON:                The CC2430 crystal oscillator is on, ready to receive/transmit
//          MPM_CC2430_XOSC_OFF:          The CC2430 crystal oscillator is off (startup time ~1 ms)
//          MPM_CC2430_XOSC_AND_VREG_OFF: The CC2430 voltage regulator is off (startup time ~1.6 ms)
//
//          Note: Nothing will happen if the current state is MPM_CC2430_XOSC_AND_VREG_OFF, and the new
//          mode is MPM_CC2430_XOSC_OFF.
//-------------------------------------------------------------------------------------------------------
ROOT void mpmSetRequest(BYTE mode) {
    BYTE taskNumber;

    // Any changes?
    if (mode == mpmInfo.selectedMode) {
        mpmSetConfirm(OK_POWER_MODE_UNCHANGED);
    } else {

        // Reserve the task to be used
        do {
            taskNumber = mschReserveTask();
        } while (taskNumber == NO_TASK);

        mschAddTask(taskNumber, MAC_TASK_PRI_LOW, mpmSetTask, (WORD)mode);
    }

} // mpmSetRequest




//-------------------------------------------------------------------------------------------------------
//  BYTE mpmGetState(void)
//
//  DESCRIPTION:
//      Returns the current power state when it has become effective (after a call to mpmSetRequest)
//
//  RETURN VALUE:
//      BYTE
//          MPM_CC2430_ON:                The CC2430 crystal oscillator is on, ready to receive/transmit
//          MPM_CC2430_XOSC_OFF:          The CC2430 crystal oscillator is off
//          MPM_CC2430_XOSC_AND_VREG_OFF: The CC2430 voltage regulator is off
//-------------------------------------------------------------------------------------------------------
ROOT BYTE mpmGetState(void) {
    return mpmInfo.currentState;
} // mpmGetState


//-------------------------------------------------------------------------------------------------------
//  ROOT mpmSetAndResumeMacAndCpuPowerMode (POWER_MODE  powerMode,
//                                          RESUME_MODE resumeMode,
//                                          BOOL        synchronousStart)
//
//  DESCRIPTION:
//      Turns off MAC, radio and sets CPU in correct power.  The CPU will halt in this routine.
//      Processing is resumed when an external interrupt or a sleep timer interrupt occurs. Mac processing

⌨️ 快捷键说明

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