📄 halcc2420m.nc
字号:
/****************************************** ** Register related ** ******************************************/ /* Set control registers */ CC2420_CONTROL_SET(); /* Set channel */ CC2420_CHANNEL_SET(CC2420_DEFAULT_CHANNEL); /* Set transmitter power */ CC2420_POWER_SET(CC2420_DEFAULT_POWER); /*********************************************** ** Release bus ***********************************************/ taskPosted[TASK_INIT] = FALSE;debug_fsm(); call Spi.disable(); call BusArbitration.releaseBus(); } /********************************************************************** * Start *********************************************************************/ command error_t HALCC2420Control.start() { if (!taskPosted[TASK_START]) { taskPosted[TASK_START] = TRUE; if (queueEmpty()) { post startTask(); } else { queuePut(TASK_START); } return SUCCESS; } else { return FAIL; } } task void startTask() { /*********************************************** ** Reserve SPI bus ***********************************************/ if (call BusArbitration.getBus() != SUCCESS) { queuePut(TASK_START); return; } /* Turn on crystal */ CRYSTAL_OSCILLATOR_ENABLE(); /*********************************************** ** select SPI bus, module 1 (micro4) and radio ***********************************************/ call Spi.enable(BUS_STE | BUS_PHASE_INVERT, 1); TOSH_uwait(100);debug_fsm(); /* Turn on oscillator */ CC2420_OSCILLATOR_ENABLE();debug_fsm(); /* Enable receiver */ CC2420_RX_ENABLE();debug_fsm(); /* Enable interrupts */ MCU_ENABLE_IRQ(); radioOn = TRUE;debug_fsm(); /*********************************************** ** Release bus ***********************************************/ taskPosted[TASK_START] = FALSE; call Spi.disable(); call BusArbitration.releaseBus(); return; } /********************************************************************** * Stop *********************************************************************/ command error_t HALCC2420Control.stop() { if (!taskPosted[TASK_STOP]) { taskPosted[TASK_STOP] = TRUE; if (queueEmpty()) { post stopTask(); } else { queuePut(TASK_STOP); } return SUCCESS; } else { return FAIL; } } task void stopTask() { /*********************************************** ** Reserve SPI bus ***********************************************/ if (call BusArbitration.getBus() != SUCCESS) { queuePut(TASK_STOP); return; } /*********************************************** ** select SPI bus, module 1 (micro4) and radio ***********************************************/ call Spi.enable(BUS_STE | BUS_PHASE_INVERT, 1); TOSH_uwait(100); /* Wait if radio is transmitting */ CC2420_TX_WAIT(); /* disable oscillator */ CC2420_OSCILLATOR_DISABLE(); /* Turn off crystal */ CRYSTAL_OSCILLATOR_DISABLE(); /* disable interrupts */ MCU_DISABLE_IRQ(); radioOn = FALSE; /*********************************************** ** Release bus ***********************************************/ taskPosted[TASK_STOP] = FALSE; call Spi.disable(); call BusArbitration.releaseBus(); return; }/**************************************************************************************************** Transmit/receive related**************************************************************************************************/ /********************************************************************** * sendPacket *********************************************************************/ uint8_t * transmitPacketPtr; bool transmitInProgress = FALSE; command error_t HALCC2420.sendPacket(uint8_t * packet) { if (!taskPosted[TASK_TRANSMIT]) { taskPosted[TASK_TRANSMIT] = TRUE; atomic transmitPacketPtr = packet; if (queueEmpty()) { post transmitTask(); } else { queuePut(TASK_TRANSMIT); } return SUCCESS; } else { return FAIL; } } task void transmitTask() { bool oldRxEnabled; uint8_t i, status, counter; /*********************************************** ** Reserve SPI bus ***********************************************/ if (call BusArbitration.getBus() != SUCCESS) { queuePut(TASK_TRANSMIT); return; } /*********************************************** ** select SPI bus and module 1 (micro4) ***********************************************/ call Spi.enable(BUS_STE | BUS_PHASE_INVERT, 1);debug_fsm(); /*********************************************** ** check if radio is on ***********************************************/ if (!radioOn) { signal HALCC2420.sendPacketDone(transmitPacketPtr, CC2420_ERROR_RADIO_OFF); /*********************************************** ** Release bus ***********************************************/ taskPosted[TASK_TRANSMIT] = FALSE; call Spi.disable(); call BusArbitration.releaseBus(); return; } /*********************************************** ** 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 */ // call HPLCC2420.cmd(CC_REG_SRXON); CC2420_RX_ENABLE(); } /* check if RSSI is valid */ status = call HPLCC2420.cmd(CC_REG_SNOP); if (!(status & CC2420_RSSI_VALID)) { /* wait 8 symbol periods (128 us) */ TOSH_uwait(128); }debug_fsm(); /* check if channel is clear */ if (!call HPLCC2420Status.CCA()) { /* restore previous state before returning call */ if (!oldRxEnabled) { /* turn off receiver */ // call HPLCC2420.cmd(CC_REG_SRFOFF); CC2420_RX_DISABLE(); } signal HALCC2420.sendPacketDone(transmitPacketPtr, CC2420_ERROR_CCA); /*********************************************** ** Release bus ***********************************************/ taskPosted[TASK_TRANSMIT] = FALSE; call Spi.disable(); call BusArbitration.releaseBus(); return; }debug_fsm(); /*********************************************** ** Clear to send packet ***********************************************/ /* put packet in transmit buffer */ call HPLCC2420.cmd(CC_REG_SFLUSHTX);debug_fsm(); call HPLCC2420FIFO.writeTXFIFO(transmitPacketPtr[0], transmitPacketPtr);debug_fsm(); /* Wait for it to send the packet */ i= 0; while (i++ < 3) {debug_fsm(); call HPLCC2420.cmd(CC_REG_STXONCCA); counter = 0; do { status = call HPLCC2420.cmd(CC_REG_SNOP); status = call HPLCC2420.cmd(CC_REG_SNOP); }while ( !(status & CC2420_TX_ACTIVE) && (counter++ < 200));debug_fsm(); if (status & CC2420_TX_ACTIVE) break; } /*********************************************** ** Check if packet is being transmitted ***********************************************/ if (!(status & CC2420_TX_ACTIVE)) { /* flush transmit buffer */ call HPLCC2420.cmd(CC_REG_SFLUSHTX); signal HALCC2420.sendPacketDone(transmitPacketPtr, CC2420_ERROR_TX); /*********************************************** ** Release bus ***********************************************/ taskPosted[TASK_TRANSMIT] = FALSE; call Spi.disable(); call BusArbitration.releaseBus(); return; } atomic transmitInProgress = TRUE; /*********************************************** ** Release bus ***********************************************/ taskPosted[TASK_TRANSMIT] = FALSE; call Spi.disable(); call BusArbitration.releaseBus(); return; } /********************************************************************** * setChannel *********************************************************************/ uint8_t currentChannel; command error_t HALCC2420.setChannel(uint8_t channel) { if ( (channel < 11) || (channel > 26) ) return FAIL; else { currentChannel = channel; if (!taskPosted[TASK_SET_CHANNEL]) { taskPosted[TASK_SET_CHANNEL] = TRUE; if (queueEmpty()) { post setChannelTask(); } else { queuePut(TASK_SET_CHANNEL); } return SUCCESS; } else { return FAIL; } } return SUCCESS; } task void setChannelTask() { /*********************************************** ** Reserve SPI bus ***********************************************/ if (call BusArbitration.getBus() != SUCCESS) { queuePut(TASK_SET_CHANNEL); return; } /*********************************************** ** select SPI bus and module 1 (micro4) ***********************************************/ call Spi.enable(BUS_STE | BUS_PHASE_INVERT, 1); TOSH_uwait(100); /* Wait if radio is transmitting */ CC2420_TX_WAIT(); if (rxEnabled) { CC2420_RX_DISABLE(); /* set channel */ CC2420_CHANNEL_SET(currentChannel); CC2420_RX_ENABLE(); } else /* set channel */ CC2420_CHANNEL_SET(currentChannel); /*********************************************** ** Release bus
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -