📄 halcc2430radiop.nc
字号:
command error_t HALCC2420.addressFilterDisable() { _CC2430_MDMCTRL0H &= ~_BV(CC2430_MDMCTRL0H_ADDR_DECODE); return SUCCESS; } void CC2430SetPanid(uint16_t id) { _CC2430_PANID = id; } void CC2430SetShortaddr(uint16_t shortAddr) { _CC2430_SHORTADDR = shortAddr; } void CC2420SetIEEEAddr(ieee_mac_addr_t extAddress) { _CC2430_IEEE_ADDR7 = extAddress[7]; _CC2430_IEEE_ADDR6 = extAddress[6]; _CC2430_IEEE_ADDR5 = extAddress[5]; _CC2430_IEEE_ADDR4 = extAddress[4]; _CC2430_IEEE_ADDR3 = extAddress[3]; _CC2430_IEEE_ADDR2 = extAddress[2]; _CC2430_IEEE_ADDR1 = extAddress[1]; _CC2430_IEEE_ADDR0 = extAddress[0]; }/**************************************************************************************************** Interrupt handler**************************************************************************************************/ task void receivedPacketTask(); task void flushBufferTask(); task void sendPacketDoneTask(); bool receivedPacketTaskPosted = FALSE, flushBufferTaskPosted = FALSE; /************************************************************************** * TXDone fired **************************************************************************/ async event void InterruptTXDone.fired() { if (transmitInProgress) { transmitInProgress = FALSE; post sendPacketDoneTask(); } else { call StdOut.print("MAC: Unscheduled transmit\n"); } } task void sendPacketDoneTask() { signal HALCC2420.sendPacketDone(transmitPacketPtr, SUCCESS); } /************************************************************************** * FIFOP fired **************************************************************************/ async event void InterruptFIFOP.fired() {// call StdOut.print("MAC: Frame received\r\n"); // Packet reception completed and address completed // Number of packets in fifo can be found in RXFIFOCNT if (!receivedPacketTaskPosted && (_CC2430_RXFIFOCNT > 0) ) { receivedPacketTaskPosted = TRUE; post receivedPacketTask(); } } uint8_t frameLength = 0, receivedPacketTaskRetries = 0; task void receivedPacketTask() { uint8_t correlation, i;// call StdOut.print("MAC: ReceivedPacketTask\n\r"); /* read frame length if not done so already */ if (frameLength == 0) { frameLength = RFD; if (frameLength < 127) receivedPacketPtr[0] = frameLength; else { /* error - frame too big - flush buffer*/ post flushBufferTask(); atomic receivedPacketTaskPosted = FALSE; frameLength = 0; receivedPacketTaskRetries = 0; return; } } /* check if entire frame has been received */ if (frameLength > _CC2430_RXFIFOCNT) { /* frame not complete */ /* repost task if buffer is not overrun */ /* or if this is not the 10th try */ atomic { if (flushBufferTaskPosted || receivedPacketTaskRetries > 10) { post flushBufferTask(); receivedPacketTaskPosted = FALSE; frameLength = 0; receivedPacketTaskRetries = 0; } else { post receivedPacketTask(); receivedPacketTaskRetries++; } } return; } /* reset retry counter */ receivedPacketTaskRetries = 0; /* read frame from receive buffer */ for (i = 0; i < frameLength; i++) { if ( (_CC2430_RXFIFOCNT > 0) && (_CC2430_RXFIFOCNT < 129) ) receivedPacketPtr[i+1] = RFD; else { receivedPacketPtr[frameLength] = 0; break; } } /* read CORRELATION */ correlation = receivedPacketPtr[frameLength]; /* reset frameLength so next frame is read from the top */ frameLength = 0; /*********************************************** ** Process this frame ***********************************************/ /* only signal packet if not corrupt */ if (correlation & FCS_CRC_OK_MASK) { receivedPacketPtr = signal HALCC2420.receivedPacket(receivedPacketPtr); } else { call StdOut.print("MAC: CRC Failed\n\r"); /* corrupt packet might indicate misaligned frame */ atomic receivedPacketTaskPosted = FALSE; post flushBufferTask(); } /*********************************************** ** Check if other frames are available ***********************************************/ atomic { /* if there are more bytes in rxfifo then get them */ if ( (_CC2430_RXFIFOCNT > 0) && (_CC2430_RXFIFOCNT < 129) ) post receivedPacketTask(); else { receivedPacketTaskPosted = FALSE; /* check if buffer has overrun while processing frame */ /* and post flushBufferTask if it has */ if (flushBufferTaskPosted) post flushBufferTask(); } } /* buggy hardware - enable interrupt when frame has been read */ RFIF &= ~_BV(CC2430_RFIF_FIFOP); } /************************************************************************** * RFErr fired **************************************************************************/ async event void InterruptRFErr.fired() { call StdOut.print("MAC: Buffer overrun\r\n"); /* buffer overrun detected * * if no receivedPacketTask is posted, * * then flush buffer * * else flag buffer to be flushed */ if (!receivedPacketTaskPosted && !flushBufferTaskPosted) { post flushBufferTask(); } flushBufferTaskPosted = TRUE; } task void flushBufferTask() { if (rxEnabled) { /* turn off receiver */ CC2430RxDisable(); /* flush buffer - done when enabling/disabling receiver */ RFST = _CC2430_ISFLUSHRX; RFST = _CC2430_ISFLUSHRX; /* enable receiver */ CC2430RxEnable(); } else { /* flush buffer */ RFST = _CC2430_ISFLUSHRX; RFST = _CC2430_ISFLUSHRX; } atomic flushBufferTaskPosted = FALSE; call StdOut.print("MAC: Buffer flushed\r\n"); }/**************************************************************************************************** Internal utility functions**************************************************************************************************/ /********************************************************************** ** ** Internal CC2420 utility functions ** **********************************************************************/ /******************** ********************/ void CC2430Reset() { // Power cycle voltage regulator _CC2430_RFPWR &= ~_BV(CC2430_RFPWR_RREG_RADIO_PD); _CC2430_RFPWR |= _BV(CC2430_RFPWR_RREG_RADIO_PD); // RFPWR contains the register ADI_RADIO_PD as a delayed version // The manual however waits for the IF while ( RFIF & _BV(CC2430_RFIF_RREG_ON) ) { } } /******************** ** Enable radio and wait for boot up ********************/ void CC2430RFEnable() { // Set delay an power up radio _CC2430_RFPWR = 0x04; // Power up radio and set delay // Wait for radio to power up. // Wait by delay time in RFPWR_RREG_DELAY // One or the other should be good enough.. while((_CC2430_RFPWR & 0x10)){} } /******************** ********************/ void CC2430RFDisable() { _CC2430_RFPWR |= _BV(CC2430_RFPWR_RREG_RADIO_PD); } /******************** ********************/ void CC2430InternalRCOscillator() { CLKCON |= _BV(CC2430_CLKCON_CLKSPD); // Select clock source = int. RC osc SLEEP |= _BV(CC2430_SLEEP_OSC_PD); // Power down external osc // Wait for RC osc stable while (!(SLEEP & _BV(CC2430_SLEEP_HFRC_STB))); } /******************** ** Switch clock source to high-power, high-precision crystal ** and wait for it to stable *********************/ void CC2430ExternalOscillator() { SLEEP &= ~_BV(CC2430_SLEEP_OSC_PD); // Power up external osc CLKCON &= ~_BV(CC2430_CLKCON_CLKSPD); // Select clock source = ext osc // Wait for XOSC powered up and stable while (!(SLEEP & _BV(CC2430_SLEEP_XOSC_STB))); } /******************** ** ********************/ void CC2430RxEnable() { /* flush rx buffer */ RFST = _CC2430_ISFLUSHRX; RFST = _CC2430_ISFLUSHRX; /* enable interrupts */ call InterruptFIFOP.enableRisingEdge(); /* Enable receiver */ RFST = _CC2430_ISRXON; rxEnabled = TRUE; } /******************** ** ********************/ void CC2430RxDisable() { RFST = _CC2430_ISRFOFF; /* disable interrupts */ call InterruptFIFOP.disable(); rxEnabled = FALSE; } /******************** ** ********************/ void CC2430ChannelSet(uint8_t channel) { uint16_t freq; /* Channel values: 11-26 */ freq = (uint16_t) channel - 11; freq *= 5; /* channel spacing */ freq += 357; /* correct channel range */ freq |= 0x4000; /* LOCK_THR = 1 */ _CC2430_FSCTRL = freq; } /******************** ** ********************/ void CC2430PALevelSet(uint8_t new_power) { uint16_t power; power = new_power * 0x1F; // 0x1F ~ 100% power level power /= 100; _CC2430_TXCTRL = (_CC2430_TXCTRL & ~0x1F) | (power & 0x1F); } /*********************************************** ** Set the correlation threshold = 20 ** Turn off "Security enable" ** Set the FIFOP threshold to maximum ***********************************************/ void CC2430ControlSet() { /* disable address recognition */ _CC2430_MDMCTRL0H &= ~_BV(CC2430_MDMCTRL0H_ADDR_DECODE);// _CC2430_MDMCTRL0H |= _BV(CC2430_MDMCTRL0H_ADDR_DECODE); /* enable auto-crc */ _CC2430_MDMCTRL0L |= _BV(CC2430_MDMCTRL0L_AUTOCRC);// _CC2430_MDMCTRL0 = mcr0; /* set FIFOP threshold to 0x7F */ _CC2430_IOCFG0 = 0x7F; /* */ _CC2430_TXCTRL = 0x050F; return; } /*********************************************** ** Wait while radio is transmitting ***********************************************/ void CC2430TxWait() { uint8_t i = 0; while ( (_CC2430_RFSTATUS & _BV(CC2430_RFSTATUS_TX_ACTIVE) ) && (i < 50) ) { wait(100); i++; } return; } /********************************************************************** * wait * Guess: loop size is perhaps 10 cycles including load/store of 16 bit value * 32 MHz equals 32 cycles pr. us. *********************************************************************/ inline void wait(uint16_t u) { uint8_t j; uint16_t i; u = u >> 3; // devide by 8 ~ 10 =] // (u*32) cycles / 10 (cyles/loop) for (i = 0; i < u; i++) { for (j = 0; j < 32;) { j++; } } }/**************************************************************************************************** Event stubs**************************************************************************************************/ /********************************************************************** * StdOut *********************************************************************/ async event void StdOut.get(uint8_t data) { } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -