📄 halcc2420m.nc
字号:
mcr0Ptr->adr_decode = FALSE; call HPLCC2420.write(CC_REG_MDMCTRL0, mcr0); /*********************************************** ** Release bus ***********************************************/ taskPosted[TASK_ADDR_DISABLE] = FALSE; call Spi.disable(); call BusArbitration.releaseBus(); } void CC2420_SET_PANID(uint16_t id) { uint16_t tmp; /* reverse bit order - CC2420 not following IEEE address standard */ tmp = id; tmp = (tmp << 8) + (id >> 8); call HPLCC2420RAM.write(CC_ADDR_PANID, 2, (uint8_t *) &id); } void CC2420_SET_SHORTADDR(uint16_t shortAddr) { uint16_t tmp; /* reverse bit order - CC2420 not following IEEE address standard */ tmp = shortAddr; tmp = (tmp << 8) + (shortAddr >> 8); call HPLCC2420RAM.write(CC_ADDR_SHORTADDR, 2, (uint8_t *) &tmp); } void CC2420_SET_IEEEADDR(ieee_mac_addr_t extAddress) { ieee_mac_addr_t buffer; /* reverse bit order - CC2420 not following IEEE address standard */ buffer[0] = extAddress[7]; buffer[1] = extAddress[6]; buffer[2] = extAddress[5]; buffer[3] = extAddress[4]; buffer[4] = extAddress[3]; buffer[5] = extAddress[2]; buffer[6] = extAddress[1]; buffer[7] = extAddress[0]; call HPLCC2420RAM.write(CC_ADDR_IEEEADDR, 8, buffer); }/**************************************************************************************************** Interrupt handler**************************************************************************************************/ /********************************************************************** * MSP430Interrupt *********************************************************************/ task void receivedPacketTask(); task void flushBufferTask(); bool receivedPacketTaskPosted = FALSE, flushBufferTaskPosted = FALSE; uint8_t framesInFifo = 0; async event void MSP430Interrupt.fired() { if (transmitInProgress) { transmitInProgress = FALSE; signal HALCC2420.sendPacketDone(transmitPacketPtr, SUCCESS); } else if (call HPLCC2420Status.FIFOP() && !call HPLCC2420Status.FIFO()) {// 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; } else if (call HPLCC2420Status.FIFOP() && call HPLCC2420Status.FIFO()) { /* frame received */ if (!receivedPacketTaskPosted) { receivedPacketTaskPosted = TRUE; post receivedPacketTask(); } framesInFifo++; } else { if (flushBufferTaskPosted) {// call StdOut.print("RX buffer flushed\r\n"); } else { } } } task void flushBufferTask() { /*********************************************** ** Reserve SPI bus ***********************************************/ if (call BusArbitration.getBus() != SUCCESS) { post flushBufferTask(); return; } /*********************************************** ** select SPI bus and module 1 (micro4) ***********************************************/ call Spi.enable(BUS_STE | BUS_PHASE_INVERT, 1); if (rxEnabled) { /* turn off receiver */ CC2420_RX_DISABLE(); /* flush buffer */ call HPLCC2420.cmd(CC_REG_SFLUSHRX); call HPLCC2420.cmd(CC_REG_SFLUSHRX); /* enable receiver */ CC2420_RX_ENABLE(); } else { /* flush buffer */ call HPLCC2420.cmd(CC_REG_SFLUSHRX); call HPLCC2420.cmd(CC_REG_SFLUSHRX); } atomic flushBufferTaskPosted = FALSE; /*********************************************** ** Release bus ***********************************************/ call Spi.disable(); call BusArbitration.releaseBus(); } task void receivedPacketTask() { uint8_t length, correlation; /*********************************************** ** Reserve SPI bus ***********************************************/ if (call BusArbitration.getBus() != SUCCESS) { post receivedPacketTask(); return; } /*********************************************** ** select SPI bus and module 1 (micro4) ***********************************************/ call Spi.enable(BUS_STE | BUS_PHASE_INVERT, 1); /* read frame from receive buffer */ call HPLCC2420FIFO.readRXFIFO(128, receivedPacketPtr); /*********************************************** ** Release bus ***********************************************/ call Spi.disable(); call BusArbitration.releaseBus(); /*********************************************** ** Process this frame ***********************************************/ /* read length and CORRELATION */ length = receivedPacketPtr[0]; correlation = receivedPacketPtr[length]; /* filter corrupt packets */ if (correlation & FCS_CRC_OK_MASK) { receivedPacketPtr = signal HALCC2420.receivedPacket(receivedPacketPtr); } /*********************************************** ** Check if other frames are available ***********************************************/ atomic { framesInFifo--; if (framesInFifo > 0) { post receivedPacketTask(); } else { receivedPacketTaskPosted = FALSE; /* check if buffer has overrun while processing frame */ /* and post flushBufferTask if it has */ if (flushBufferTaskPosted) { post flushBufferTask(); } } } }/**************************************************************************************************** Internal utility functions**************************************************************************************************/ /********************************************************************** ** ** Internal CC2420 utility functions ** **********************************************************************/ void CC2420_RESET() { /* Reset CC2420 using SPI*/ call HPLCC2420.write(CC_REG_MAIN, 0x0000); call HPLCC2420.write(CC_REG_MAIN, 0xF801); /*bit 1 activates external clock*/ TOSH_uwait(2 * 1024); } /******************** ********************/ bool CC2420_OSCILLATOR_ENABLE() { uint8_t i = 0, status = 0; /* Turn oscillator on */ call HPLCC2420.cmd(CC_REG_SXOSCON); TOSH_uwait(1024); /* Wait until oscillator is stable */ do { /* Read status register */ status = CC2420_READ_STATUS(); TOSH_uwait(128); } while ( !(status & CC2420_XOSC16M_STABLE) && (i++ < 160)); if (i >= 160) { return FALSE; } else { return TRUE; } } /******************** ** ********************/ void CC2420_RX_ENABLE() { /* Enable receiver */ call HPLCC2420.cmd(CC_REG_SRXON); call HPLCC2420.cmd(CC_REG_SFLUSHRX); rxEnabled = TRUE; } /******************** ** ********************/ void CC2420_RX_DISABLE() { call HPLCC2420.cmd(CC_REG_SRFOFF); rxEnabled = FALSE; } /******************** ** ********************/ int8_t CC2420_CHANNEL_SET(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*/ call HPLCC2420.write(CC_REG_FSCTRL, freq); return (int8_t) channel; } /******************** ** ********************/ int8_t CC2420_POWER_SET(uint8_t new_power) { uint16_t power; power = 31 * new_power; power /= 100; power += 0xA0E0; /* Set transmitter power */ call HPLCC2420.write(CC_REG_TXCTRL, power); return new_power; } void CC2420_CONTROL_SET() { /*********************************************** ** Set the correlation threshold = 20 ** Turn off "Security enable" ** Set the FIFOP threshold to maximum ***********************************************/ call HPLCC2420.write(CC_REG_MDMCTRL0, mcr0); call HPLCC2420.write(CC_REG_IOCFG0, iocfg0); call HPLCC2420.write(CC_REG_MDMCTRL1, 0x0500); call HPLCC2420.write(CC_REG_SECCTRL0, 0x01C4); return; } void CC2420_TX_WAIT() { uint8_t i = 0, status; /*********************************************** ** Wait if radio is transmitting ***********************************************/ status = call HPLCC2420.cmd(CC_REG_SNOP); while ( (status & CC2420_TX_ACTIVE) && (i < 50) ) { TOSH_uwait(100); status = call HPLCC2420.cmd(CC_REG_SNOP); i++; } return; }/**************************************************************************************************** Event stubs**************************************************************************************************/ /********************************************************************** * CC2420FIFO *********************************************************************/ async event error_t HPLCC2420FIFO.TXFIFODone(uint8_t length, uint8_t *data) { return SUCCESS; } async event error_t HPLCC2420FIFO.RXFIFODone(uint8_t length, uint8_t *data) { return SUCCESS; } /********************************************************************** * CC2420RAM *********************************************************************/ async event error_t HPLCC2420RAM.writeDone(uint16_t addr, uint8_t length, uint8_t* buf) { return SUCCESS; } async event error_t HPLCC2420RAM.readDone(uint16_t addr, uint8_t length, uint8_t* buf) { return SUCCESS; } /********************************************************************** * StdOut *********************************************************************/ async event void StdOut.get(uint8_t data) { } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -