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

📄 zphy_cc2420.cpp

📁 一个Zigbee开放协议栈移植到VC环境下的版本.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*********************************************************************
 *
 *                  PHY for the Chipcon 2420
 *
 *********************************************************************
 * FileName:        zPHY_CC2420.c
 * Dependencies:
 * Processor:       PIC18F
 * Complier:        MCC18 v3.00 or higher
 * Company:         Microchip Technology, Inc.
 *
 * Software License Agreement
 *
 * The software supplied herewith by Microchip Technology Incorporated
 * (the 揅ompany? for its PIC?microcontroller is intended and
 * supplied to you, the Company抯 customer, for use solely and
 * exclusively on Microchip PIC microcontroller products.
 *
 * You may not modify or create derivatives works of the Software.
 *
 * If you intend to use the software in a product for sale, then you must
 * be a member of the ZigBee Alliance and obtain any licenses required by
 * them.  For more information, go to www.zigbee.org.
 *
 * The software is owned by the Company and/or its licensor, and is
 * protected under applicable copyright laws. All rights are reserved.
 *
 * Any use in violation of the foregoing restrictions may subject the
 * user to criminal sanctions under applicable laws, as well as to
 * civil liability for the breach of the terms and conditions of this
 * license.
 *
 * THIS SOFTWARE IS PROVIDED IN AN 揂S IS?CONDITION. NO WARRANTIES,
 * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
 * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
 * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
 *
 *
 * Author               Date    Comment
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * DF/KO                01/09/06 Microchip ZigBee Stack v1.0-3.5
 * DF/KO                08/31/06 Microchip ZigBee Stack v1.0-3.6
 ********************************************************************/

#include "ZigbeeTasks.h"
//#include "zigbee.def"
#include "zigbee_cc.def"
#include "generic.h"
#include "zPHY.h"
#include "zPHY_CC2420.h"
#include "zMAC.h"
//#include "zMAC_CC2420.h"
#include "MSPI.h"
#include "sralloc.h"
#include "Compiler.h"
//#include "console.h"
#define RF_CHIP CC2420

#if(RF_CHIP == CC2420)

// ******************************************************************************
// Data Structures
/*
typedef union _PHYPendingTasks
{
    struct
    {
        unsigned int PLME_SET_TRX_STATE_request_task :1;
        unsigned int PHY_RX :1;
    } bits;
    BYTE Val;
} PHY_PENDING_TASKS;
*/
typedef struct _RX_DATA
{
    unsigned int size :7;
    unsigned int inUse :1;
} RX_DATA;


// ******************************************************************************
// Variable Definitions

extern CURRENT_PACKET       currentPacket;
static unsigned char        GIEH_backup;
extern MAC_TASKS_PENDING    macTasksPending;
MAC_FRAME_CONTROL  pendingAckFrameControl;
PHY_PIB                     phyPIB;
volatile PHY_PENDING_TASKS  PHYTasksPending;
extern BYTE                 RxBuffer[RX_BUFFER_SIZE];
volatile RX_DATA            RxData;
#if (RX_BUFFER_SIZE > 256)
    extern WORD             RxRead;
    extern WORD    RxWrite;
#else
    extern BYTE             RxRead;
    extern BYTE    RxWrite;
#endif
SAVED_BITS                  savedBits;
BYTE                        TRXCurrentState;
PHY_ERROR                   phyError;


//----------------------------------------self defines by lq---------
extern _LATABITS  LATAbits;
extern _INTCONBITS INTCONbits;
extern _RCONBITS   RCONbits;
extern _SSPCON1BITS SSPCON1bits;
extern _SSPCON2BITS SSPCON2bits;
extern _PIR1BITS    PIR1bits;
extern _LATCBITS    LATCbits;
extern _TRISCBITS   TRISCbits;
extern _INTCON2BITS INTCON2bits;
extern _SSPSTATBITS SSPSTATbits;
extern _TRISBBITS   TRISBbits;
extern _PORTBBITS   PORTBbits;
extern _PIE2BITS    PIE2bits;
extern _PIR2BITS    PIR2bits;
extern _T3CONBITS   T3CONbits;
extern _EECON1BITS  EECON1bits;
extern _RCSTABITS   RCSTAbits;
extern int SSPSTAT;
extern int SSPCON1;
extern int ADCON1;
extern int LATA;
extern int TRISA;
extern int LATB;
extern int PORTB;
extern int TMR0L;
extern int T3CON;
extern int TMR3L;
extern int TMR3H;
extern int CCP2CON;
extern int EECON1;

extern int PHY_VREG_EN;
extern int SSPBUF;
extern int PHY_FIFO;
extern int PHY_FIFOP;
extern int PHY_SFD;


extern void CLRWDT();
//----------------------------------------------------------------

// ******************************************************************************
// Function Prototypes

void UserInterruptHandler(void);


/*********************************************************************
 * Function:        BYTE PHYGet(void)
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          Returns one buffered data byte that was received
 *                  by the transceiver.
 *
 * Side Effects:    None
 *
 * Overview:        This routine returns one byte that was previously
 *                  received and buffered.
 *
 * Note:            None
 ********************************************************************/
BYTE PHYGet(void)
{
    BYTE toReturn;

    toReturn = RxBuffer[RxRead++];  //RxBuffer-接收缓冲区

    if(RxRead == RX_BUFFER_SIZE)
    {
        RxRead = 0;
    }

    return toReturn;
}

/*********************************************************************
 * Function:        BYTE PHYHasBackgroundTasks(void)
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          If the PHY layer has background tasks in progress.
 *
 * Side Effects:    None
 *
 * Overview:        This routine returns an indication of whether or
 *                  not the layer has background tasks in progress.
 *
 * Note:            None
 ********************************************************************/
BYTE PHYHasBackgroundTasks(void)
{
    //PHY might want to check to verify that we are not in the middle of
    // a transmission before allowing the user to turn off the TRX
    return PHYTasksPending.Val;
}


/*********************************************************************
 * Function:        void PHYSetOutputPower( BYTE power)
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        This routine sets the output power of the transceiver.
 *                  Refer to the transceiver datasheet for the value
 *                  to pass to this function for the desired power level.
 *
 * Note:            None
 ********************************************************************/
void PHYSetOutputPower( BYTE power)
{
/*          According to Table 9 of CC2420 datasheet
 *
 *          PA_LEVEL (TXCTRL.LSB)       Output Power (dBm)  Current Consumption
 *          ====================================================================
 *          0xFF                        0                   17.4 mA
 *          0xFB                        -1                  16.5 mA
 *          0xF7                        -3                  15.2 mA
 *          0xF3                        -5                  13.9 mA
 *          0xEF                        -7                  12.5 mA
 *          0xEB                        -10                 11.2 mA
 *          0xE7                        -15                 9.9 mA
 *          0xE3                        -25                 8.5 mA
 */

    // Select desired TX output power level
    SPIPut(REG_TXCTRL);   //cc2420 pdf page 60. 控制寄存器 TXCTRL:0x15
    // As defined by Table 9 of CC2420 datasheet.
    SPIPut(0xA0);             // MSB
    SPIPut( power );          // LSB
}

/*********************************************************************
 * Function:        void PHYInit(void)
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        This routine initializes the PHY layer of the stack.
 *
 * Note:            None
 ********************************************************************/
void PHYInit(void)
{
    WORD i;
    CC2420_STATUS d;

    RxData.inUse = 0;
    PHYTasksPending.Val = 0;

    PHY_RESETn = 0;

    CLRWDT();  //清除看门狗
    i = 0xE000;
    PHY_VREG_EN = 1;
    while(++i){}
    CLRWDT();

    PHY_RESETn = 1;
    i = 500;
    while(i--);
    CLRWDT();

    i=0;

    while(1)
    {
        PHY_CSn_0();
        SPIPut(STROBE_SXOSCON);
        PHY_CSn_1();

        d.Val = SSPBUF;

		d.bits.XOSC16M_STABLE=1;//add by lq
        if(d.bits.XOSC16M_STABLE == 1)
        {
            break;
        }
        i++;
    }

    PHY_CSn_0();

    /* set up REG_MDMCTRL0 */
    // Set AUTOACK bit in MDMCTRL0 register.
    // 15:14 = '00' = Reserved
    // 13    = '0'  = Reserved frames are rejected
    // 12    = '?'  = '1' if this is coordinator, '0' if otherwise
    // 11    = '1'  = Address decoding is enabled
    // 10:8  = '010'    = CCA Hysteresis in DB - default value
    // 7:6   = '11' = CCA = 1, when RSSI_VAL < CCA_THR - CCA_HYST and not receiving valid IEEE 802.15.4 data
    // 5     = '1'  = Auto CRC
    // 4     = '1'  = Auto ACK
    // 3:0   = '0010' = 3 leading zero bytes - IEEE compliant.


    SPIPut(REG_MDMCTRL0);
#if defined(I_AM_COORDINATOR)
    // MSB = 0b0001 1010
    SPIPut(0x1A);
    // LSB = 0b1111 0010
    SPIPut(0xF2);
#else
    // MSB = 0b0000 1010
    SPIPut(0x0A);
    // LSB = 0b1111 0010
    SPIPut(0xF2);
#endif

    SPIPut(REG_MDMCTRL1);   
    SPIPut(0x05);            // MSB  //CORR_THR=20 建议值 
    SPIPut(0x00);            // LSB

    /* disable the MAC level security */
    SPIPut(REG_SECCTRL0);
    SPIPut(0x01);            // MSB
    SPIPut(0xC4);            // LSB

    PHYSetOutputPower( PA_LEVEL );

    // Set FIFOP threshold to full RXFIFO buffer
    SPIPut(REG_IOCFG0);
    SPIPut(0x00);
    SPIPut(0x7F);
    PHY_CSn_1();

    // Flush the TX FIFO
    PHY_CSn_0();
    SPIPut(STROBE_SFLUSHTX);
    PHY_CSn_1();

#if !defined(I_AM_END_DEVICE)
    // set the data pending bit
    PHY_CSn_0();
    SPIPut(STROBE_SACKPEND);
    PHY_CSn_1();
#endif

    // Flush the RX FIFO
    PHY_CSn_0();
    SPIPut(REG_RXFIFO | CMD_READ);
    SPIGet();
    PHY_CSn_1();
    PHY_CSn_0();
    SPIPut(STROBE_SFLUSHRX);
    SPIPut(STROBE_SFLUSHRX);
    PHY_CSn_1();

    return;
}

ZIGBEE_PRIMITIVE PHYTasks(ZIGBEE_PRIMITIVE inputPrimitive)
{
    if(inputPrimitive == NO_PRIMITIVE)  //没有原语时
    {
        if(RxRead == RxWrite)  //无新数据
        {
            if(PHY_FIFO == 0)
            {
                if(PHY_FIFOP == 1)  //FIFOP高且FIFO低,说明RXFIFO溢出,应该清空RXFIFO
                {
                    // Flush the RX FIFO
                    iPHY_CSn_0();
                    SPIPut(REG_RXFIFO | CMD_READ);
                    SPIGet();
                    iPHY_CSn_1();
                    iPHY_CSn_0();
                    SPIPut(STROBE_SFLUSHRX);
                    SPIPut(STROBE_SFLUSHRX);
                    iPHY_CSn_1();
                    RxData.inUse = 0;
                    ZigBeeStatus.flags.bits.bRxBufferOverflow = 0;
                }
            }
        }

        /* manage background tasks here */
        /* phy background tasks */
        if(ZigBeeTxUnblocked)   //(TxFIFOControl.bFIFOInUse==0)
        {
            if(PHYTasksPending.bits.PHY_RX)
            {
                BYTE packetSize;

                if(CurrentRxPacket != NULL)
                {
                    return NO_PRIMITIVE;

⌨️ 快捷键说明

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