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

📄 mac_support.c

📁 ucos在NEC平台下的移植
💻 C
📖 第 1 页 / 共 2 页
字号:
#include "includes.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 ZBOOLean value, pointed to by the taskData, to FALSE.
//
//  TASK DATA:
//      A ZBOOL pointer to the flag to be changed to FALSE
//-------------------------------------------------------------------------------------------------------
void msupWaitTask(MAC_TASK_INFO *pTask) {
  ///  ENABLE_FIFOP_INT();
    *((ZBOOL*) pTask->taskData) = FALSE;
    mschRemoveTask(pTask->priority, MSCH_KEEP_TASK_IN_PROGRESS_BM);
} // msupWaitTask




//-------------------------------------------------------------------------------------------------------
//  ZBOOL msupCompareQword(QWORD *pA, QWORD *pB)
//
//  DESCRIPTION:
//      Support function for comparing two 64-bit QWORDs, A and B
//
//  PARAMETERS:
//      QWORD *pA
//          pointer to QWORD A
//      QWORD *pB
//          pointer to QWORD B
//
//  RETURN VALUE:
//      ZBOOL
//          TRUE if A and B are equal, false otherwise
//-------------------------------------------------------------------------------------------------------
ZBOOL msupCompareQword(QWORD *pA, QWORD *pB) {
    return (*pA == *pB);
} // msupCompareQword




//-------------------------------------------------------------------------------------------------------
//  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
//-------------------------------------------------------------------------------------------------------
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
UINT16 random;
//-------------------------------------------------------------------------------------------------------


//-------------------------------------------------------------------------------------------------------
//  void msupInitRandom(void)
//
//  DESCRIPTION:
//      Initialise 16-bit random word from CC2420 receiver chain. Note that this function assumes that
//      the crystal oscillator is running, and that the receiver is not currently in use.
//-------------------------------------------------------------------------------------------------------
void msupInitRandom(void) {
    //UINT8 i;

    //// Enable RXBPF, ADC and VGA
    //FASTSPI_SETREG(CC2420_MANAND, 0xFFF1);
    //
    //// Output ADC_Q[0] to CCA pin
    //FASTSPI_SETREG(CC2420_IOCFG1, 0x0001);
    //
    //random = 0;
    //
    //// If random == 0, the pseudo-random values will remain 0 (forever) and must therefore be avoided!
    //while (random == 0) {
    //    for (i = 0; i < 16; i++) {
    //        // Sample ADC_Q[0] 16 times and shift into random register
    //        random = (random << 1) | (!CCA_IS_1());
    //    }
    //}
    //
    //// Set CC2420 registers back to default
    //FASTSPI_SETREG(CC2420_MANAND, 0xFFFF);
    //FASTSPI_SETREG(CC2420_IOCFG1, 0x0000); --li
    
} // msupInitRandom




//-------------------------------------------------------------------------------------------------------
//  BYTE msupGetRandomByte(void)
//
//  DESCRIPTION:
//      Get a single return pseudo-random byte, initialized from msupInitRandom()
//
//  RETURN VALUE:
//      BYTE
//          The low part of the random word
//-------------------------------------------------------------------------------------------------------
BYTE msupGetRandomByte(void) {
    UINT8 n = 8;

    do {
        if ((random >> 8) & 0x80) {
            random = (random << 1) ^ RANDOM_POLY;
        } else {
            random = (random << 1);
        }
    } while (--n);

    return (BYTE) random;
} // msupGetRandomByte




//-------------------------------------------------------------------------------------------------------
//  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:
//-------------------------------------------------------------------------------------------------------
#if MAC_OPT_TRANSMIT_POWER           
void msupSetTransmitPower(void)
{    
    UINT16 cc2420Value;
    //If parameter is changed verify and write to chip.
    if (oldPhyTransmitPower != ppib.phyTransmitPower)
    {
        switch(ppib.phyTransmitPower)
        {
         case OUTPUT_POWER_0DBM:
            cc2420Value = CC2420_0DBM;
            break;
         case OUTPUT_POWER_N1DBM: 
            cc2420Value = CC2420_N1DBM;
            break;
         case OUTPUT_POWER_N3DBM:
            cc2420Value = CC2420_N3DBM;
            break;
         case OUTPUT_POWER_N5DBM:
            cc2420Value = CC2420_N5DBM;
            break;
         case OUTPUT_POWER_N7DBM:
            cc2420Value = CC2420_N7DBM;
            break;
         case OUTPUT_POWER_N10DBM:
            cc2420Value = CC2420_N10DBM;
            break;
         case OUTPUT_POWER_N15DBM:
            cc2420Value = CC2420_N15DBM;
            break;
         case OUTPUT_POWER_N25DBM:
            cc2420Value = CC2420_N25DBM;
            break;
           //No valid value reset to old
          default:
            ppib.phyTransmitPower = oldPhyTransmitPower;
            cc2420Value = CC2420_0DBM;
            break;
        } 
        oldPhyTransmitPower = ppib.phyTransmitPower;          
        FASTSPI_SETREG(CC2420_TXCTRL, cc2420Value);       
    }

}// msupSetTransmitPower
#endif            


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




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



//-------------------------------------------------------------------------------------------------------
//  void msupSetChannel(UINT8 logicalChannel, ZBOOL 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
//      ZBOOL changePib
//          Set ppib.phyCurrentChannel = logicalChannel?
//-------------------------------------------------------------------------------------------------------
void msupSetChannel(UINT8 logicalChannel, ZBOOL changePib) {
    //UINT16 newFreq, oldFreq;
    //ZBOOL sfdWasActiveBeforeStrobe;
    //    
    //// Derive frequency programming from the given channel number
    //newFreq = (UINT16) (logicalChannel - 11); // Subtract the base channel 
    //newFreq = newFreq + (newFreq << 2);       // Multiply with 5, which is the channel spacing
    //newFreq = newFreq + 357 + 0x4000;         // 357 is 2405-2048, 0x4000 is LOCK_THR = 1

    //// Do we need to change the frequency at all?
    //DISABLE_GLOBAL_INT();
    //FASTSPI_GETREG(CC2420_FSCTRL, oldFreq);
    //ENABLE_GLOBAL_INT();
    //if ((newFreq ^ oldFreq) & 0x03FF) {
    //    
    //    // Force RX off
    //    DISABLE_GLOBAL_INT();
    //    sfdWasActiveBeforeStrobe = SFD_IS_ACTIVE();                
    //    FASTSPI_STROBE(CC2420_SRFOFF);
    //    FASTSPI_STROBE(CC2420_SFLUSHRX);
    //    FASTSPI_STROBE(CC2420_SFLUSHRX);        
    //    // Clean up if we interrupted the receiver
    //    DISABLE_FIFOP_INT();
    //    ENABLE_GLOBAL_INT();
    //    if (sfdWasActiveBeforeStrobe) mrxResetRxEngine();
    //    ENABLE_FIFOP_INT();
    //    
    //    // Change the PIB attribute (?) and update the frequency register 
    //    DISABLE_GLOBAL_INT();
    //    if (changePib) ppib.phyCurrentChannel = logicalChannel;
    //    FASTSPI_SETREG(CC2420_FSCTRL, newFreq);
    //    
    //    // Return to the previous RX state
    //    if (mrxInfo.onCounter) {
    //        FASTSPI_STROBE(CC2420_SRXON);
    //    }
    //    ENABLE_GLOBAL_INT();
    //}                    --li
    
} // 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
//      ADDRESS *pSrcAddr
//          Pointer to the source address (short or extended)
//      WORD destPanId, 
//          Destination PAN identifier
//      ADDRESS *pDestAddr, 
//          Pointer to the destination address (short or extended)
//      BYTE txOptions
//          (TX_OPT_SECURITY_ENABLE | TX_OPT_INDIRECT | TX_OPT_GTS | TX_OPT_ACK_REQ) or TX_OPT_NONE
//-------------------------------------------------------------------------------------------------------
void msupPrepareHeader(MAC_TX_PACKET *pPacket, BYTE type, BYTE addrModes, WORD srcPanId, ADDRESS *pSrcAddr, WORD destPanId, ADDRESS *pDestAddr, BYTE txOptions) {
    BYTE temp;
    UINT8 length = 0;

    // Packet type and options
    pPacket->type = type;
    pPacket->txOptions = txOptions;
    
    // Frame control field
    temp = type & FRAME_TYPE_BM;
#if MAC_OPT_SECURITY
    pPacket->securitySetup.micLength = 0;

    if (txOptions & TX_OPT_SECURITY_ENABLE) {
        temp |= SECURITY_ENABLED_BM;
        // Set securitySuite or securityStatus (UNAVAILABLE_KEY)
        msecFindTxSecurityMaterial(pPacket, TRUE, ((addrModes & DEST_ADDR_BM) >> 2), destPanId, pDestAddr);

    } else {
        pPacket->securitySuite = MAC_SECURITY_NONE;
    }
#endif
    if (txOptions & TX_OPT_ACK_REQ) temp |= ACK_REQ_BM;
    if (((addrModes & BOTH_ADDR_USED) == BOTH_ADDR_USED) && (srcPanId == destPanId)) temp |= INTRA_PAN_BM;
    pPacket->pHeader[length++] = temp;
    pPacket->pHeader[length++] = addrModes;
    
    // Sequence number
    if (type == FT_BEACON) {
        pPacket->pHeader[length++] = mpib.macBSN++;
    } else {
        pPacket->pHeader[length++] = mpib.macDSN++;
    }

    // Destination PAN ID
    if (addrModes & DEST_ADDR_BM) {
        memcpy(pPacket->pHeader + length, &destPanId, 2);
        length += 2;
        pPacket->toCoord = (mpib.macPANId == destPanId);
    } else {
        pPacket->toCoord = FALSE;
    }

    // Destination address
    if ((addrModes & DEST_ADDR_BM) == DEST_ADDR_SHORT) {
        memcpy(pPacket->pHeader + length, (BYTE*) &pDestAddr->Short, 2);
        length += 2;
        pPacket->toCoord &= (mpib.macCoordShortAddress == pDestAddr->Short);
    } else if ((addrModes & DEST_ADDR_BM) == DEST_ADDR_EXT) {
        memcpy(pPacket->pHeader + length, (BYTE*) &pDestAddr->Extended, 8);
        length += 8;
        pPacket->toCoord &= msupCompareQword(&mpib.macCoordExtendedAddress, &pDestAddr->Extended);
    }
    

⌨️ 快捷键说明

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