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

📄 halcc2430radiop.nc

📁 Develop Zigbee network real-time Os
💻 NC
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (c) 2007 University of Copenhagen * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * - Redistributions of source code must retain the above copyright *   notice, this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright *   notice, this list of conditions and the following disclaimer in the *   documentation and/or other materials provided with the *   distribution. * - Neither the name of University of Copenhagen nor the names of *   its contributors may be used to endorse or promote products derived *   from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL STANFORD * UNIVERSITY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. *//** * * @author Marcus Chang <marcus@diku.dk> * @author Klaus S. Madsen <klaussm@diku.dk> * @author Martin Leopold <leopold@diku.dk> */#include <CC2430_CSP.h>module HalCC2430RadioP {    provides {      interface HALCC2420;      interface StdControl as HALCC2420Control;      interface Init;    }    uses {      interface GpioInterrupt as InterruptTXDone;      interface GpioInterrupt as InterruptFIFOP;      interface GpioInterrupt as InterruptRFErr;      interface StdOut;    }}implementation {#include "hplcc2420.h"#include "packet.h"#define debug_fsm(x) //call StdOut.print("FSM: "); call StdOut.printHexword(call HPLCC2420.read(CC_REG_FSMSTATE)); call StdOut.print("\r\n");norace uint8_t volatile RFIF  __attribute((sfrAT0xE9)); /*  RF Interrupt Flags          */  void CC2430SetPanid(uint16_t id);  void CC2430SetShortaddr(uint16_t shortAddr);  void CC2420SetIEEEAddr(ieee_mac_addr_t extAddress);  void CC2430Reset();  void CC2430RFEnable();  void CC2430RFDisable();  void CC2430ExternalOscillator();  void CC2430RxEnable();  void CC2430RxDisable();  void CC2430ChannelSet(uint8_t channel);  void CC2430PALevelSet(uint8_t new_power);  void CC2430ControlSet();  void CC2430TxWait();  void wait(uint16_t u);  task void initTask();  task void transmitTask();  task void setChannelTask();/**************************************************************************************************** StdControl**************************************************************************************************/    // uint16_t mcr0 = 0x0AE2, iocfg0 = 0x007F;    uint16_t mcr0 = 0x0000, iocfg0 = 0x007F;    MDMCTRL0_t * mcr0Ptr;    ieee_mac_addr_t ieeeAddress;    mac_addr_t shortAddress, panid;    bool rxEnabled = FALSE;    uint8_t receivedPacket[128];    uint8_t * receivedPacketPtr;    uint8_t packet_buf_real[PACKET_MAX_PAYLOAD];    /**********************************************************************     * Init     *********************************************************************/    command error_t Init.init()     {        receivedPacketPtr = receivedPacket;                //mcr0Ptr = (MDMCTRL0_t *) &mcr0;//        mcr0Ptr =  (MDMCTRL0_t *) (&mcr0);        /* No auto Ack. Preamble length: 3 zero bytes. *///        mcr0Ptr->preamble_length = LEADING_ZERO_BYTES_3;//        mcr0Ptr->autoack = FALSE;//        mcr0Ptr->autocrc = TRUE;//        mcr0Ptr->cca_mode = 3;//        mcr0Ptr->cca_hyst = CCA_HYST_2DB;//        mcr0Ptr->adr_decode = TRUE;//        mcr0Ptr->pan_coordinator = FALSE;//        mcr0Ptr->reserved_frame_mode = FALSE;                post initTask();                return SUCCESS;    }    task void initTask()    {        /* Chipcons manufacture ID */        ieeeAddress[0] = 0x10;        ieeeAddress[1] = 0x3d;        ieeeAddress[2] = 0x23;        /* no unique ID use TOS_NODE_ID instead */        ieeeAddress[3] = 0;        ieeeAddress[4] = 0;        ieeeAddress[5] = 0;        ieeeAddress[6] = TOS_NODE_ID >> 8;        ieeeAddress[7] = TOS_NODE_ID;                /* Power cycle radio*/        CC2430Reset();                /* turn on oscillator, enabling RAM access */        CC2430ExternalOscillator();        /******************************************        ** RAM related                           **        ******************************************/        /* Write IEEE adress to radio */        CC2420SetIEEEAddr(ieeeAddress);                /* Write lowest bits as default shortAdr */             shortAddress = ((uint16_t) ieeeAddress[6]) << 8 | ieeeAddress[7];        CC2430SetShortaddr(shortAddress);        /* Write scan address in PAN ID */        panid = shortAddress;        CC2430SetPanid(panid);        /******************************************        ** Register related                      **        ******************************************/        /* Set control registers */        CC2430ControlSet();        /* Set channel */        CC2430ChannelSet(CC2420_DEFAULT_CHANNEL);        /* Set transmitter power */        CC2430PALevelSet(CC2420_DEFAULT_POWER);        /******************************************        ** Turn off receiver as default          **        ******************************************/        CC2430RFDisable();        call InterruptTXDone.enableRisingEdge();        call InterruptRFErr.enableRisingEdge();    }    /**********************************************************************    * Start    *********************************************************************/    command error_t HALCC2420Control.start()    {             /* Turn radio on */      CC2430RFEnable();      /* Enable receiver */      CC2430RxEnable();      return SUCCESS;    }         /**********************************************************************   * Stop   *********************************************************************/  command error_t HALCC2420Control.stop()  {    /* Wait if radio is transmitting */    CC2430TxWait();    CC2430RFDisable();    return SUCCESS;  }    /**************************************************************************************************** Transmit/receive related**************************************************************************************************/    /**********************************************************************    * sendPacket    *********************************************************************/    uint8_t * transmitPacketPtr;    bool transmitInProgress = FALSE;    command error_t HALCC2420.sendPacket(uint8_t * packet)     {        transmitPacketPtr = packet;        post transmitTask();            return SUCCESS;    }    task void transmitTask()    {        bool oldRxEnabled;        uint8_t i, status, counter;        /***********************************************        * Check if channel is clear - If radio is not in         * receive mode, turn on receiver to gather RSSI        ***********************************************/        oldRxEnabled = rxEnabled;        if (!rxEnabled)        {            /* turn on receiver */            CC2430RxEnable();        }        /* wait 8 symbol periods (128 us) */        wait(128);                /* check if channel is busy */        if ( !(_CC2430_RFSTATUS & _BV(CC2430_RFSTATUS_CCA)) ) {            /* restore previous state before returning call */            if (!rxEnabled)            {                /* turn off receiver */                CC2430RxDisable();            }            signal HALCC2420.sendPacketDone(transmitPacketPtr, CC2420_ERROR_CCA);            return;        }        /***********************************************        ** Clear to send packet        ***********************************************/        /* put packet in transmit buffer */        RFST = _CC2430_ISFLUSHTX;        /* Even 0x7F is a valid length */        RFD = transmitPacketPtr[0];        for (i = 0; i < transmitPacketPtr[0]; i++) {            RFD = transmitPacketPtr[i+1];          }        /* Wait for it to send the packet */        i = 0;        while (i++ < 3)         {            RFST = _CC2430_ISTXON;            counter = 0;            do {                status = _CC2430_RFSTATUS;            }while ( !(status & _BV(CC2430_RFSTATUS_TX_ACTIVE))  && (counter++ < 200));            if (status & _BV(CC2430_RFSTATUS_TX_ACTIVE))                 break;        }        /***********************************************        ** Check if packet is being transmitted        ***********************************************/            if (!(status & _BV(CC2430_RFSTATUS_TX_ACTIVE))) {            /* flush transmit buffer */            RFST = _CC2430_ISFLUSHTX;            signal HALCC2420.sendPacketDone(transmitPacketPtr, CC2420_ERROR_TX);            return;        }        atomic transmitInProgress = TRUE;            return;    }    /**********************************************************************    * setChannel    *********************************************************************/    uint8_t currentChannel;        command error_t HALCC2420.setChannel(uint8_t channel)     {        if ( (channel < 11) || (channel > 26) )             return FAIL;        else {            currentChannel = channel;            post setChannelTask();        }        return SUCCESS;    }        task void setChannelTask()    {        /* Wait if radio is transmitting */            CC2430TxWait();        if (rxEnabled)         {            CC2430RxDisable();            /* set channel */            CC2430ChannelSet(currentChannel);            CC2430RxEnable();        } else            /* set channel */            CC2430ChannelSet(currentChannel);    }        /**********************************************************************    * setTransmitPower    *********************************************************************/    command error_t HALCC2420.setTransmitPower(uint8_t power)    {        if (power > 100)             return FAIL;        else             CC2430PALevelSet(power);                    return SUCCESS;    }        /**********************************************************************    * rxEnable    *********************************************************************/    command error_t HALCC2420.rxEnable()    {        /* turn on receiver */        CC2430RxEnable();                return SUCCESS;    }        /**********************************************************************    * rxDisable    *********************************************************************/    command error_t HALCC2420.rxDisable()    {        /* ignore call if radio is already off */        if (rxEnabled)         {            CC2430RxDisable();        }                return SUCCESS;    }/**************************************************************************************************** Adressing**************************************************************************************************/    /**********************************************************************     * setShortAddress     *********************************************************************/    command error_t HALCC2420.setAddress(mac_addr_t *addr)    {        shortAddress = *addr;        _CC2430_SHORTADDR = (*addr);        return SUCCESS;    }    /**********************************************************************     * getShortAddress     *********************************************************************/    command const mac_addr_t * HALCC2420.getAddress()    {        return &shortAddress;    }        /**********************************************************************     * setPANAddress     *********************************************************************/    command error_t HALCC2420.setPanAddress(mac_addr_t *addr)    {        panid = *addr;        _CC2430_PANID = panid;        return SUCCESS;    }        /**********************************************************************     * getShortAddress     *********************************************************************/    command const mac_addr_t * HALCC2420.getPanAddress()    {        return &panid;    }        /**********************************************************************     * getExtAddress     *********************************************************************/    command const ieee_mac_addr_t * HALCC2420.getExtAddress()    {        return (const ieee_mac_addr_t *) &ieeeAddress;    }    /**********************************************************************     * Hardware Address Filtering     *********************************************************************/    command error_t HALCC2420.addressFilterEnable()    {        _CC2430_MDMCTRL0H |= _BV(CC2430_MDMCTRL0H_ADDR_DECODE);                return SUCCESS;    }    

⌨️ 快捷键说明

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