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

📄 mac.c

📁 该演示程序实现温度的采集与显示功能
💻 C
📖 第 1 页 / 共 2 页
字号:

/*
  V0.1 Initial Release   10/July/2006  RBR

*/


#include "compiler.h"
#include "lrwpan_config.h"         //user configurations
#include "lrwpan_common_types.h"   //types common acrosss most files
#include "ieee_lrwpan_defs.h"
#include "console.h"
#include "hal.h"
#include "halstack.h"
#include "phy.h"
#include "mac.h"

#include "sendpack.h"
//#include "nwk.h"

//#include "neighbor.h"
SUB_MAC_FRAME submacCurrentFrame;
/*
typedef enum _MAC_RXSTATE_ENUM {
  MAC_RXSTATE_IDLE,
  MAC_RXSTATE_NWK_HANDOFF,
  MAC_RXSTATE_CMD_PENDING
} MAC_RXSTATE_ENUM;*/
//
//static MAC_RXSTATE_ENUM macRxState;

MAC_PIB mac_pib;
//MAC_SERVICE a_mac_service;
//MAC_STATE_ENUM macState;

NODE_INFO   macInfo;


//there can only be one TX in progress at a time, so
//a_mac_tx_data contains the arguments for that TX.
MAC_TX_DATA a_mac_tx_data;

//this is used for parsing of current packet.
MAC_RX_DATA a_mac_rx_data;

LRWPAN_STATUS_ENUM macTxFSM_status;


//locals
//static UINT32 mac_utility_timer;   //utility timer

//local functions
//static void macTxData(void);
static void macParseHdr(void);
static void macRxFSM(void);


//does not turn on radio.
void macInit(void){

  BYTE *p;
  //macState = MAC_STATE_IDLE;
  //macRxState = MAC_RXSTATE_IDLE;
  mac_pib.flags.val = 0;
  mac_pib.rxTail = 0;
  mac_pib.rxHead = 0;
  mac_pib.macPANID = LRWPAN_DEFAULT_PANID;
  //mac_pib.macMaxAckRetries = aMaxFrameRetries;

  macInfo.panID.Val=0xffff;
  macInfo.panID.Val=0xffff;

  p = (BYTE*) &macInfo.longAddr;// Initialize the MAC address
  *p++ = (DEFAULTID >>8)&0xFF; //MAC_LONG_ADDR_BYTE1;
  *p++ = DEFAULTID & 0xFF; //MAC_LONG_ADDR_BYTE0;
  	
  //ntInitAddressMap();  //init the address map
#ifdef LRWPAN_COORDINATOR
  mac_pib.depth = 0;
#else
  mac_pib.depth = 1; //depth will be at least one
#endif
  mac_pib.bcnDepth = 0xFF; //remembers depth of node that responded to beacon
  //other capability information
  mac_pib.macCapInfo = 0;
#ifdef LRWPAN_ALT_COORDINATOR     //not supported, included for completeness
  LRWPAN_SET_CAPINFO_ALTPAN(mac_pib.macCapInfo);
#endif
#ifdef LRWPAN_FFD
  LRWPAN_SET_CAPINFO_DEVTYPE(mac_pib.macCapInfo);
#endif
#ifdef LRWPAN_ACMAIN_POWERED
  LRWPAN_SET_CAPINFO_PWRSRC(mac_pib.macCapInfo);
#endif
#ifdef LRWPAN_RCVR_ON_WHEN_IDLE
  LRWPAN_SET_CAPINFO_RONIDLE(mac_pib.macCapInfo);
#endif
#ifdef LRWPAN_SECURITY_CAPABLE
  LRWPAN_SET_CAPINFO_SECURITY(mac_pib.macCapInfo);
#endif
  //always allocate a short address
  LRWPAN_SET_CAPINFO_ALLOCADDR(mac_pib.macCapInfo);
}

//this assumes that phyInit, macInit has previously been called.
//turns on the radio

LRWPAN_STATUS_ENUM macInitRadio(void) {

  phy_pib.phyCurrentFrequency = LRWPAN_DEFAULT_FREQUENCY;
  phy_pib.phyCurrentChannel = LRWPAN_DEFAULT_START_CHANNEL;
  if (phy_pib.phyCurrentChannel < 11){
    mac_pib.macAckWaitDuration = SYMBOLS_TO_MACTICKS(120);
  }
  else {
    mac_pib.macAckWaitDuration = SYMBOLS_TO_MACTICKS(54);
  }

  a_phy_service.args.phy_init_radio_args.radio_flags.bits.listen_mode = 0;
#ifdef LRWPAN_COORDINATOR
  a_phy_service.args.phy_init_radio_args.radio_flags.bits.pan_coordinator = 1;
#else
  a_phy_service.args.phy_init_radio_args.radio_flags.bits.pan_coordinator = 0;
#endif

halInitRadio(phy_pib.phyCurrentFrequency,
	      phy_pib.phyCurrentChannel,
	      a_phy_service.args.phy_init_radio_args.radio_flags);



#ifdef LRWPAN_USE_STATIC_PANID
  halSetRadioPANID(LRWPAN_DEFAULT_PANID); //listen on this PANID
#else
  halSetRadioPANID(0xFFFF);      //broadcast
#endif
  halSetRadioShortAddr(0xFFFE);  //non-broadcast, reserved
  return(a_phy_service.status);
}

void macSetPANID(UINT16 panid){
  mac_pib.macPANID = panid;
  halSetRadioPANID(mac_pib.macPANID);
}


void macSetChannel(BYTE channel){
  phy_pib.phyCurrentChannel = channel;
  halSetChannel(channel);
}

BYTE macGetChannel()
{
  return phy_pib.phyCurrentChannel;
}

void macSetShortAddr(UINT16 saddr) {

  halSetRadioShortAddr(saddr);
}

unsigned char gMacDSN=0;
void MACPutHeader(NODE_INFO *dest, BYTE frameCON)
{
    BYTE c;
    BYTE dstmode, srcmode;

    //DEBUG_STRING(DBG_TX,"macTxData()-header\n");

    a_mac_tx_data.fcflsb = frameCON;
    a_mac_tx_data.fcfmsb = LRWPAN_FCF_DSTMODE_SADDR|LRWPAN_FCF_SRCMODE_NOADDR;
    a_mac_tx_data.DestAddr.saddr = LRWPAN_BCAST_PANID;
    a_mac_tx_data.DestPANID = LRWPAN_BCAST_PANID;
    a_mac_tx_data.SrcPANID = LRWPAN_BCAST_PANID;
					
    dstmode = LRWPAN_GET_DST_ADDR(a_mac_tx_data.fcfmsb);
    srcmode = LRWPAN_GET_SRC_ADDR(a_mac_tx_data.fcfmsb);

    if (mac_pib.macPANID == 0xFFFE && srcmode == LRWPAN_ADDRMODE_SADDR)
    {
      srcmode = LRWPAN_ADDRMODE_LADDR;
      a_mac_tx_data.fcfmsb = a_mac_tx_data.fcfmsb & ~LRWPAN_FCF_SRCMODE_MASK;

      LRWPAN_SET_SRC_ADDR(a_mac_tx_data.fcfmsb,LRWPAN_ADDRMODE_LADDR);
    }

    phy_pib.currentTxFrm = tmpTxBuff;
    phy_pib.currentTxFlen=0;	

    //format LSB Fcontrol
    //phy_pib.currentTxFrm++;
    *phy_pib.currentTxFrm = a_mac_tx_data.fcflsb;	

    //format MSB Fcontrol
    phy_pib.currentTxFrm++;
    *phy_pib.currentTxFrm = a_mac_tx_data.fcfmsb;


    //format dsn
    mac_pib.macDSN = gMacDSN++;
    phy_pib.currentTxFrm++;
    *phy_pib.currentTxFrm = mac_pib.macDSN; //set DSN	

    phy_pib.currentTxFlen = phy_pib.currentTxFlen + 3; //DSN, FCFLSB, FCFMSB

    //format dst PANID, will be present if both dst is nonzero
    if (dstmode != LRWPAN_ADDRMODE_NOADDR)
	{
      phy_pib.currentTxFrm++;
      *phy_pib.currentTxFrm = (BYTE)a_mac_tx_data.DestPANID;	

      phy_pib.currentTxFrm++;
      *phy_pib.currentTxFrm = (BYTE) (a_mac_tx_data.DestPANID >> 8);
					
      phy_pib.currentTxFlen=phy_pib.currentTxFlen+2;
    }

    //format dst Address
    switch(dstmode)
    {
        case LRWPAN_ADDRMODE_NOADDR:
          break;
        case LRWPAN_ADDRMODE_SADDR:
            phy_pib.currentTxFrm++;
            *phy_pib.currentTxFrm = (BYTE)a_mac_tx_data.DestAddr.saddr;
            phy_pib.currentTxFrm++;
            *phy_pib.currentTxFrm = (BYTE)(a_mac_tx_data.DestAddr.saddr >> 8);
            phy_pib.currentTxFlen=phy_pib.currentTxFlen+2;
            break;
        case LRWPAN_ADDRMODE_LADDR:
            for(c=0;c<8;c++) {
               phy_pib.currentTxFrm++;
               *phy_pib.currentTxFrm = a_mac_tx_data.DestAddr.laddr.bytes[c];
            }
        phy_pib.currentTxFlen=phy_pib.currentTxFlen+8;
            break;
	default:
            break;
    }

    //format src PANID
    if ( !LRWPAN_GET_INTRAPAN(a_mac_tx_data.fcflsb) &&
        srcmode != LRWPAN_ADDRMODE_NOADDR
          )
    {
            phy_pib.currentTxFrm++;			
            *phy_pib.currentTxFrm = (BYTE)a_mac_tx_data.SrcPANID;
            phy_pib.currentTxFrm++;
            *phy_pib.currentTxFrm = (BYTE) (a_mac_tx_data.SrcPANID >> 8);
            phy_pib.currentTxFlen=phy_pib.currentTxFlen+2;
    }

    //format src Address
    switch(srcmode)
    {
	case LRWPAN_ADDRMODE_NOADDR:
	    break;
        case LRWPAN_ADDRMODE_SADDR:
	    phy_pib.currentTxFrm++;
	    *phy_pib.currentTxFrm = (BYTE)a_mac_tx_data.SrcAddr;	
	     phy_pib.currentTxFrm++;
	    *phy_pib.currentTxFrm = (BYTE)(a_mac_tx_data.SrcAddr >> 8);					
	     phy_pib.currentTxFlen=phy_pib.currentTxFlen+2;
	     break;
	case LRWPAN_ADDRMODE_LADDR:
	  //this has to be our own long address, get it
	    halGetProcessorIEEEAddress_ASC(phy_pib.currentTxFrm-8);
	    phy_pib.currentTxFlen=phy_pib.currentTxFlen+8;
	    phy_pib.currentTxFrm = phy_pib.currentTxFrm+8;
	    break;
        default:
	    break;
    }	
}

void MACPutArray(BYTE *data,BYTE len) //bhj 本函数在数组的最后追加了一个校验字节
{
   BYTE i;
   BYTE cs;
   for(int i=0;i<len;i++)
   {
   	 phy_pib.currentTxFrm++;
      *phy_pib.currentTxFrm = data[i];
    }
    phy_pib.currentTxFlen += len;	
	
    cs=0;
    for(i=7;i<len;i++)
	cs+=data[i];	
    phy_pib.currentTxFrm++;
    *phy_pib.currentTxFrm =cs;
    phy_pib.currentTxFlen ++;
	
}
void macFormatUserData(BYTE frame) //bhj
{
   int i;
   BYTE testdata[10];
   //unsigned char datalen=0;
   testdata[0]=1;
   testdata[1]=0xff;
   testdata[2]=0xff;
   testdata[3]=0xff;
   testdata[4]=0xff;
   testdata[5]=1;  //len  =data + cs;

   testdata[6]=0;  //header checksum (test[0]~test[5])
   for(i=0;i<=5;i++) testdata[6]+=testdata[i];

   MACPutArray(testdata,7);

}
BYTE MACFlush()
{

    // at this point, we will attempt a TX
    mac_pib.flags.bits.ackPending = 0;

    //now send the data, ignore the GTS and INDIRECT bits for now
    //DEBUG_STRING(DBG_TX,"TX DSN: ");
    //DEBUG_UINT8(DBG_TX,mac_pib.macDSN);
    //DEBUG_STRING(DBG_TX,"\n");

⌨️ 快捷键说明

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