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

📄 mac_support.c

📁 zigbee location examples
💻 C
📖 第 1 页 / 共 2 页
字号:
/*******************************************************************************************************
 *                                                                                                     *
 *        **********                                                                                   *
 *       ************                                                                                  *
 *      ***        ***                                                                                 *
 *      ***   +++   ***                                                                                *
 *      ***   + +   ***                                                                                *
 *      ***   +                         CHIPCON CC2430 INTEGRATED 802.15.4 MAC AND PHY                 *
 *      ***   + +   ***                       Various Support/Utility Functions                        *
 *      ***   +++   ***                                                                                *
 *      ***        ***                                                                                 *
 *       ************                                                                                  *
 *        **********                                                                                   *
 *                                                                                                     *
 *******************************************************************************************************
 * CONFIDENTIAL                                                                                        *
 * The use of this file is restricted by the signed MAC software license agreement.                    *
 *                                                                                                     *
 * Copyright Chipcon AS, 2005                                                                          *
 *******************************************************************************************************
 * This module contains support/utility functions for the MAC sublayer.                                *
 *******************************************************************************************************/
#include <mac_headers.h>


UINT8 oldPhyTransmitPower = OUTPUT_POWER_0DBM;

/*******************************************************************************************************
 *******************************************************************************************************
 **************************                 GENERAL FUNCTIONS                 **************************
 *******************************************************************************************************
 *******************************************************************************************************/


//-------------------------------------------------------------------------------------------------------
//  void msupWaitTask(MAC_TASK_INFO *pTask)
//
//  DESCRIPTION:
//      This task is meant to be placed behind another task to make sure that this task has finihed.
//      When the task is run, it will set an 8-bit boolean value, pointed to by the taskData, to FALSE.
//
//  TASK DATA:
//      A BOOL pointer to the flag to be changed to FALSE
//-------------------------------------------------------------------------------------------------------
void msupWaitTask(MAC_TASK_INFO   *pTask) NEAR {
    *((BOOL   *) pTask->taskData) = FALSE;
    mschRemoveTask(pTask->priority, MSCH_KEEP_TASK_IN_PROGRESS_BM);
} // msupWaitTask




//-------------------------------------------------------------------------------------------------------
//  BOOL msupCompareAddress(ADDRESS *pA, ADDRESS *pB)
//
//  DESCRIPTION:
//      Support function for comparing two 64-bit ADDRESS, A and B
//
//  PARAMETERS:
//      ADDRESS *pA
//          pointer to ADDRESS A
//      ADDRESS *pB
//          pointer to ADDRESS B
//
//  RETURN VALUE:
//      BOOL
//          TRUE if A and B are equal, false otherwise
//-------------------------------------------------------------------------------------------------------
ROOT BOOL msupCompareExtendedAddress(ADDRESS   *pA, ADDRESS   *pB) {
    UINT8 n;
    n = sizeof(ADDRESS);
    do {
        if (*((BYTE  *) pA) != *((BYTE  *) pB)) return FALSE;
        pA = (ADDRESS  *) ((BYTE  *) pA + 1);
        pB = (ADDRESS  *) ((BYTE  *) pB + 1);
    } while (--n);
    return TRUE;
} // msupCompareAddress




//-------------------------------------------------------------------------------------------------------
//  void msupReverseCopy(BYTE *pDestination, BYTE *pSource, UINT8 length)
//
//  DESCRIPTION:
//      Copies an array of data in the reverse order, so that the first part of the destination can
//      overlap with the last part of the source:
//
//      Source      F---------------L        (last byte "L" is moved first -> avoids overwriting)
//      Destination        F---------------L
//-------------------------------------------------------------------------------------------------------
ROOT void msupReverseCopy(BYTE   *pDestination, BYTE   *pSource, UINT8 length) {
    pDestination += length;
    pSource += length;
    while (length--) {
        *(--pDestination) = *(--pSource);
    }
} // msupReverseCopy




//-------------------------------------------------------------------------------------------------------
// The random generator polynom
#define RANDOM_POLY 0x1021

// The 16-bit shift register which is used to generate random numbers
WORD random;
//-------------------------------------------------------------------------------------------------------


//-------------------------------------------------------------------------------------------------------
//  void msupSetTransmitPower(void)
//
//  DESCRIPTION:
//      Changes the Transiver transmit power.
//      The function checks the ppib.phyTransmitPower to see if it has changed.
//      if the parameter is changed the changes will be writen to the radio chip.
//      Alowed values of ppib.phyTransmitPower are: OUTPUT_POWER_0DBM, OUTPUT_POWER_N1DBM
//      OUTPUT_POWER_N3DBM, OUTPUT_POWER_N5DBM, OUTPUT_POWER_N7DBM, OUTPUT_POWER_N10DBM
//      OUTPUT_POWER_N15DBM and OUTPUT_POWER_N25DBM
//
//  PARAMETERS:
//------------------------------------------------------------------------------------------------------
ROOT void msupSetTransmitPower(void) {
// TBD: Not safe (should disable interrupts or something...)
/*    UINT16 txctrl;

    //If parameter is changed verify and write to chip.
    if (oldPhyTransmitPower != ppib.phyTransmitPower) {
        switch(ppib.phyTransmitPower) {
        case OUTPUT_POWER_0DBM:
            txctrl = CC2430_0DBM;
            break;
        case OUTPUT_POWER_N1DBM:
            txctrl = CC2430_N1DBM;
            break;
        case OUTPUT_POWER_N3DBM:
            txctrl = CC2430_N3DBM;
            break;
        case OUTPUT_POWER_N5DBM:
            txctrl = CC2430_N5DBM;
            break;
        case OUTPUT_POWER_N7DBM:
            txctrl = CC2430_N7DBM;
            break;
        case OUTPUT_POWER_N10DBM:
            txctrl = CC2430_N10DBM;
            break;
        case OUTPUT_POWER_N15DBM:
            txctrl = CC2430_N15DBM;
            break;
        case OUTPUT_POWER_N25DBM:
            txctrl = CC2430_N25DBM;
            break;
        //No valid value reset to old
        default:
            ppib.phyTransmitPower = oldPhyTransmitPower;
            txctrl = CC2430_0DBM;
            break;
        }
        oldPhyTransmitPower = ppib.phyTransmitPower;
        WRITE_RFR16(RFR_TXCTRL, txctrl);
    }
*/
}// msupSetTransmitPower


ROOT BOOL msupIsSfdActive(void) {
    UINT8 rfrFsmstate = FSMSTATE;
    return (((rfrFsmstate >= 7)  && (rfrFsmstate <= 13)) || // RX
            ((rfrFsmstate >= 51) && (rfrFsmstate <= 54)) || // TX ack
            ((rfrFsmstate >= 36) && (rfrFsmstate <= 39)));  // TX
}

ROOT BOOL msupIsTxActive(void) {
    if (FSMSTATE & 0x30) {
        return TRUE;
    } else {
        return FALSE;
    }
}

ROOT BOOL msupIsCspInCriticalSection(void) {
    switch (RFST) {
    case CSP_INSTR_SNOP:
    case CSP_INSTR_WAITX:
        return FALSE;
    default:
        return TRUE;
    }
}



//-------------------------------------------------------------------------------------------------------
//  void msupInitRandomGenerator(void)
//
//  DESCRIPTION:
//      Uses the IEEE extended address to seed the hardware random generator
//-------------------------------------------------------------------------------------------------------
ROOT void msupInitRandomGenerator(BYTE   *pExtendedAddr) {
    UINT8 n;

    // Initialize the LFSR
    RNDL = 0xFF;
    RNDL = 0xFF;

    // Make it "random"
    for (n = 0; n < sizeof(ADDRESS); n++) {
        RNDH = pExtendedAddr[n];
    }

} // msupInitRandomGenerator



//-------------------------------------------------------------------------------------------------------
//  BYTE msupGetRandomByte(void)
//
//  DESCRIPTION:
//      Generates a single pseudo-random byte from the LFSR in the hardware random generator
//      Use msupInitRandomGenerator() to seed the LFSR
//
//  RETURN VALUE:
//      BYTE
//          Random value
//-------------------------------------------------------------------------------------------------------
ROOT BYTE msupGetRandomByte(void) {
    ADCCON1 &= ~0x0C;
    ADCCON1 |= 0x04;
    return RNDH;
} // msupGetRandomByte




/*******************************************************************************************************
 *******************************************************************************************************
 **************************                RF CHANNEL SETTINGS                **************************
 *******************************************************************************************************
 *******************************************************************************************************/

//-------------------------------------------------------------------------------------------------------
//  BOOL msupChannelValid(UINT8 logicalChannel)
//
//  DESCRIPTION:
//      Support function for verifying that the logicalChannel is within the valid range for the 2.4 GHz
//      channels supported by CC2430, which are channels 11 (2405 MHz) through 26 (2480 MHz).
//
//  PARAMETERS:
//      UINT8 logicalChannel
//          The channel number
//
//  RETURN VALUE:
//      BOOL
//          TRUE for channels >= 11 and <= 26, otherwise FALSE
//-------------------------------------------------------------------------------------------------------
ROOT BOOL msupChannelValid(UINT8 logicalChannel) {
    return ((BOOL)((logicalChannel >= 11) && (logicalChannel <= 26)));
} // msupChannelValid




//-------------------------------------------------------------------------------------------------------
//  void msupSetChannel(UINT8 logicalChannel, BOOL changePib)
//
//  DESCRIPTION:
//      Changes the radio channel if necessary. The function calculates the new value for the FSCTRL
//      register, and compares it to the current value. When different, the frequency will be changed.
//      The function will clean up the RX engine if we're interrupting a reception. When finished, it
//      will restore the previous RX state.
//
//  PARAMETERS:
//      UINT8 logicalChannel
//          The channel number, 11 - 26
//      BOOL changePib
//          Set ppib.phyCurrentChannel = logicalChannel?
//-------------------------------------------------------------------------------------------------------
ROOT void msupSetChannel(UINT8 logicalChannel, BOOL changePib) {
    UINT16 newFreq, oldFreq;

    // Derive frequency programming from the given channel number
    newFreq = (UINT16) (logicalChannel - 11);      // Subtract the base channel
    newFreq = (UINT16) (newFreq + (newFreq << 2)); // Multiply with 5, which is the channel spacing
    newFreq = (UINT16) (newFreq + 357 + 0x4000);   // 357 is 2405-2048, 0x4000 is LOCK_THR = 1

    // Do we need to change the frequency at all?
    #pragma diag_suppress=PA082
    oldFreq = READ_RFR16(FSCTRL);
    #pragma diag_default=PA082

    if ((newFreq ^ oldFreq) & 0x03FF) {

        // Force RX off
        DISABLE_GLOBAL_INT();
        ISRFOFF;

        // Clean up if we interrupted the receiver
        DisableRfInterrupts();
        ENABLE_GLOBAL_INT();
        mrxResetRxEngine();
        EnableRfInterrupts();

        // Change the PIB attribute (?) and update the frequency register
        DISABLE_GLOBAL_INT();
        if (changePib) ppib.phyCurrentChannel = logicalChannel;
        WRITE_RFR16(FSCTRL, newFreq);

        // Return to the previous RX state
        if (mrxInfo.onCounter) {
            ISRXON;
        }
        ENABLE_GLOBAL_INT();
    }

} // msupSetChannel




/*******************************************************************************************************
 *******************************************************************************************************
 **************************                TX PACKET GENERATION               **************************
 *******************************************************************************************************
 *******************************************************************************************************/




//-------------------------------------------------------------------------------------------------------
//  msupPrepareHeader(MAC_TX_PACKET *pPacket, BYTE type, BYTE addrModes, WORD srcPanId, ADDRESS ...)
//
//  DESCRIPTION:
//      Packet header assembly function, used for beacon, data and command frames.
//
//  PARAMETERS:
//      MAC_TX_PACKET *pPacket
//          The packet structure to update (this function affects the header part)
//      BYTE type
//          FT_BEACON, FT_DATA or FT_COMMAND
//      BYTE addrModes
//          Address mode for source and destination
//          (SRC_ADDR_SHORT, SRC_ADDR_EXT or 0) | (DEST_ADDR_SHORT, DEST_ADDR_EXT or 0)
//      WORD srcPanId
//          Source PAN identifier

⌨️ 快捷键说明

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