/************************************************************************************************** * @fn MRFI_Rssi * * @brief Returns "live" RSSI value * * @param none * * @return RSSI value in units of dBm. ************************************************************************************************** */ int8_t MRFI_Rssi(void) { /* Radio must be in RX state to measure rssi. */ MRFI_ASSERT( mrfiRadioState == MRFI_RADIO_STATE_RX ); /* Wait for the RSSI to be valid: * Just having the Radio ON is not enough to read * the correct RSSI value. The Radio must in RX mode for * a certain duration. */ MRFI_RSSI_VALID_WAIT(); /* Read and convert RSSI to decimal and do offset compensation. */ return( Mrfi_CalculateRssi(mrfiSpiReadReg(RSSI)) ); }
/************************************************************************************************** * @fn MRFI_Rssi * * @brief Returns "live" RSSI value * * @param none * * @return RSSI value in units of dBm. ************************************************************************************************** */ int8_t MRFI_Rssi(void) { int8_t rssi; /* Radio must be in RX state to measure rssi. */ MRFI_ASSERT( mrfiRadioState == MRFI_RADIO_STATE_RX ); /* * Assuming that the Radio was just turned on, we must wait for * RSSI to be valid. */ MRFI_RSSI_VALID_WAIT(); /* read RSSI value from hardware */ rssi = RSSI; /* apply offset given in datasheet */ rssi = rssi + MRFI_RSSI_OFFSET; /* return RSSI value */ return( rssi ); }
/************************************************************************************************** * @fn MRFI_Transmit * * @brief Transmit a packet. * * @param pPacket - pointer to packet to transmit * txType - FORCED or CCA * * @return Return code indicates success or failure of transmit: * MRFI_TX_RESULT_SUCCESS - transmit succeeded * MRFI_TX_RESULT_FAILED - transmit failed because CCA failed ************************************************************************************************** */ uint8_t MRFI_Transmit(mrfiPacket_t * pPacket, uint8_t txType) { uint8_t txResult = MRFI_TX_RESULT_SUCCESS; static uint8_t dsn = 0; /* radio must be awake to transmit */ MRFI_ASSERT( mrfiRadioState != MRFI_RADIO_STATE_OFF ); /* TX_DONE line status must be low. If high, some state logic problem. */ MRFI_ASSERT(!MRFI_TX_DONE_STATUS()); /* Turn off reciever. We ignore/drop incoming packets during transmit. */ Mrfi_RxModeOff(); /* -------------------------------------- * Populate the IEEE fields in frame * ------------------------------------ */ /* set the sequence number, also known as DSN (Data Sequence Number) */ pPacket->frame[MRFI_DSN_OFFSET] = dsn++; pPacket->frame[MRFI_FCF_OFFSET] = MRFI_FCF_0_7; pPacket->frame[MRFI_FCF_OFFSET+1] = MRFI_FCF_8_15; /* ------------------------------------------------------------------ * Write packet to transmit FIFO * -------------------------------- */ { uint8_t txBufLen; uint8_t frameLen; uint8_t *p; /* flush FIFO of any previous transmit that did not go out */ MRFI_RADIO_FLUSH_TX_BUFFER(); /* set point at beginning of outgoing frame */ p = &pPacket->frame[MRFI_LENGTH_FIELD_OFFSET]; /* get number of bytes in the packet (does not include the length byte) */ txBufLen = *p; /* * Write the length byte to the FIFO. This length does *not* include the length field * itself but does include the size of the FCS (generically known as RX metrics) which * is generated automatically by the radio. */ frameLen = txBufLen + MRFI_RX_METRICS_SIZE; mrfiSpiWriteTxFifo(&frameLen, 1); /* skip the length field which we already sent to FIFO. */ p++; /* write packet bytes to FIFO */ mrfiSpiWriteTxFifo(p, txBufLen); } /* Forced transmit */ if(txType == MRFI_TX_TYPE_FORCED) { /* NOTE: Bug (#1) described in the errata swrz024.pdf for CC2520: * We never strobe TXON when the radio is in receive state. * If this is changed, must implement the bug workaround as described in the * errata (flush the Rx FIFO). */ /* strobe transmit */ mrfiSpiCmdStrobe(STXON); /* wait for transmit to complete */ while (!MRFI_TX_DONE_STATUS()); /* Clear the TX_FRM_DONE exception flag register in the radio. */ mrfiSpiBitClear(EXCFLAG0, 1); } else /* CCA Transmit */ { /* set number of CCA retries */ uint8_t ccaRetries = MRFI_CCA_RETRIES; MRFI_ASSERT( txType == MRFI_TX_TYPE_CCA ); /* ====================================================================== * CCA Algorithm Loop * ====================================================================== */ while(1) { /* Turn ON the receiver to perform CCA. Can not call Mrfi_RxModeOn(), * since that will enable the rx interrupt, which we do not want. */ mrfiSpiCmdStrobe(SRXON); /* Wait for RSSI to be valid. */ MRFI_RSSI_VALID_WAIT(); /* Request transmit on cca */ mrfiSpiCmdStrobe(STXONCCA); /* If sampled CCA is set, transmit has begun. */ if(MRFI_SAMPLED_CCA()) { /* wait for transmit to complete */ while( !MRFI_TX_DONE_STATUS() ); /* Clear the TX_FRM_DONE exception flag register in the radio. */ mrfiSpiBitClear(EXCFLAG0, 1); /* transmit is done. break out of CCA algorithm loop */ break; } else { /* ------------------------------------------------------------------ * Clear Channel Assessment failed. * ------------------------------------------------------------------ */ /* Retry ? */ if(ccaRetries != 0) { /* turn off reciever to conserve power during backoff */ Mrfi_RxModeOff(); /* delay for a random number of backoffs */ Mrfi_RandomBackoffDelay(); /* decrement CCA retries before loop continues */ ccaRetries--; } else /* No CCA retries left, abort */ { /* set return value for failed transmit and break */ txResult = MRFI_TX_RESULT_FAILED; break; } } } /* End CCA Algorithm Loop */ } /* turn radio back off to put it in a known state */ Mrfi_RxModeOff(); /* If the radio was in RX state when transmit was attempted, * put it back in RX state. */ if(mrfiRadioState == MRFI_RADIO_STATE_RX) { Mrfi_RxModeOn(); } /* return the result of the transmit */ return( txResult ); }
/************************************************************************************************** * @fn MRFI_Init * * @brief Initialize MRFI. * * @param none * * @return none ************************************************************************************************** */ void MRFI_Init(void) { /* Configure Output lines */ MRFI_CONFIG_RESETN_PIN_AS_OUTPUT(); MRFI_CONFIG_VREG_EN_PIN_AS_OUTPUT(); /* Configure Input lines */ MRFI_CONFIG_TX_FRAME_DONE_AS_INPUT(); MRFI_CONFIG_FIFO_AS_INPUT(); MRFI_CONFIG_FIFOP_AS_INPUT(); /* Initialize SPI */ mrfiSpiInit(); /* Power up the radio chip */ Mrfi_TurnOnRadioPower(); /* Confirm that we are talking to the right hardware */ MRFI_ASSERT(mrfiSpiReadReg(CHIPID) == MRFI_RADIO_PARTNUM); /* Random Number Generator: * The seed value for the randon number generator logic * is derived from the radio. */ /* Set radio in rx mode, but with symbol search disabled. Used for RSSI * measurments or when we don't care about the received frames. */ mrfiSpiWriteReg(FRMCTRL0, FRMCTRL0_RESET_VALUE | RX_MODE_RSSI_ONLY); /* Turn on the receiver */ mrfiSpiCmdStrobe(SRXON); /* * Wait for RSSI to be valid. RANDOM command strobe can be used * to generate random number only after this. */ MRFI_RSSI_VALID_WAIT(); /* Get random byte from the radio */ mrfiRndSeed = mrfiSpiRandomByte(); /* * The seed value must not be zero. If it is, the pseudo random sequence * will be always be zero. There is an extremely small chance this seed could * randomly be zero (more likely some type of hardware problem would cause * this). If it is zero, initialize it to something. */ if(mrfiRndSeed == 0) { mrfiRndSeed = 0x80; } /* Random number initialization is done. Turn the radio off */ Mrfi_TurnOffRadioPower(); /* Initial radio state is - OFF state */ mrfiRadioState = MRFI_RADIO_STATE_OFF; /********************************************************************************** * Compute reply delay scalar * * The IEEE radio has a fixed data rate of 250 Kbps. Data rate inference * from radio regsiters is not necessary for this radio. * * The maximum delay needed depends on the MAX_APP_PAYLOAD parameter. Figure * out how many bits that will be when overhead is included. Bits/bits-per-second * is seconds to transmit (or receive) the maximum frame. We multiply this number * by 1000 to find the time in milliseconds. We then additionally multiply by * 10 so we can add 5 and divide by 10 later, thus rounding up to the number of * milliseconds. This last won't matter for slow transmissions but for faster ones * we want to err on the side of being conservative and making sure the radio is on * to receive the reply. The semaphore monitor will shut it down. The delay adds in * a fudge factor that includes processing time on peer plus lags in Rx and processing * time on receiver's side. * * ********************************************************************************** */ #define PLATFORM_FACTOR_CONSTANT 2 #define PHY_PREAMBLE_SYNC_BYTES 8 { uint32_t bits, dataRate = 250000; bits = ((uint32_t)((PHY_PREAMBLE_SYNC_BYTES + MRFI_MAX_FRAME_SIZE)*8))*10000; /* processing on the peer + the Tx/Rx time plus more */ sReplyDelayScalar = PLATFORM_FACTOR_CONSTANT + (((bits/dataRate)+5)/10); } /* Random delay: This prevents devices on the same power source from repeated * transmit collisions on power up. */ Mrfi_RandomBackoffDelay(); BSP_ENABLE_INTERRUPTS(); }
/************************************************************************************************** * @fn MRFI_Transmit * * @brief Transmit a packet using CCA algorithm. * * @param pPacket - pointer to packet to transmit * * @return Return code indicates success or failure of transmit: * MRFI_TX_RESULT_SUCCESS - transmit succeeded * MRFI_TX_RESULT_FAILED - transmit failed because CCA failed ************************************************************************************************** */ uint8_t MRFI_Transmit(mrfiPacket_t * pPacket, uint8_t txType) { static uint8_t dsn = 0; uint8_t txResult = MRFI_TX_RESULT_SUCCESS; /* radio must be awake to transmit */ MRFI_ASSERT( mrfiRadioState != MRFI_RADIO_STATE_OFF ); /* ------------------------------------------------------------------ * Initialize hardware for transmit * ----------------------------------- */ /* turn off reciever */ Mrfi_RxModeOff(); /* clear 'transmit done' interrupt flag, this bit is tested to see when transmit completes */ RFIRQF1 &= ~IRQ_TXDONE; /* ------------------------------------------------------------------ * Populate the IEEE fields in frame * ------------------------------------ */ /* set the sequence number, also known as DSN (Data Sequence Number) */ pPacket->frame[MRFI_DSN_OFFSET] = dsn; /* increment the sequence number, value is retained (static variable) for use in next transmit */ dsn++; /* * Populate the FCF (Frame Control Field) with the following settings. * * bits description setting * -------------------------------------------------------------------------------------- * 0-2 Frame Type 001 - data frame * 3 Security Enabled 0 - security disabled * 4 Frame Pending 0 - no pending data * 5 Ack Request 0 - no Ack request * 6 PAN ID Compression 0 - no PAN ID compression * 7 Reserved 0 - reserved * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - * 8-9 Reserved 00 - reserved * 10-11 Destination Addressing Mode 10 - PAN ID + 16-bit short address * 12-13 Frame Version 00 - IEEE Std 802.15.4-2003 * 14-15 Source Addressing Mode 10 - PAN ID + 16-bit short address * */ pPacket->frame[MRFI_FCF_OFFSET] = MRFI_FCF_0_7; pPacket->frame[MRFI_FCF_OFFSET+1] = MRFI_FCF_8_15; /* ------------------------------------------------------------------ * Write packet to transmit FIFO * -------------------------------- */ { uint8_t txBufLen; uint8_t * p; uint8_t i; /* flush FIFO of any previous transmit that did not go out */ RFST = ISFLUSHTX; /* set point at beginning of outgoing frame */ p = &pPacket->frame[MRFI_LENGTH_FIELD_OFFSET]; /* get number of bytes in the packet (does not include the length byte) */ txBufLen = *p; /* * Write the length byte to the FIFO. This length does *not* include the length field * itself but does include the size of the FCS (generically known as RX metrics) which * is generated automatically by the radio. */ RFD = txBufLen + MRFI_RX_METRICS_SIZE; /* write packet bytes to FIFO */ for (i=0; i<txBufLen; i++) { p++; RFD = *p; } } /* ------------------------------------------------------------------ * Immediate transmit * --------------------- */ if (txType == MRFI_TX_TYPE_FORCED) { /* strobe transmit */ RFST = ISTXON; /* wait for transmit to complete */ while (!(RFIRQF1 & IRQ_TXDONE)); /* transmit is done */ } else { /* ------------------------------------------------------------------ * CCA transmit * --------------- */ MRFI_ASSERT( txType == MRFI_TX_TYPE_CCA ); { bspIState_t s; uint8_t txActive; uint8_t ccaRetries; /* set number of CCA retries */ ccaRetries = MRFI_CCA_RETRIES; /* =============================================================================== * CCA Algorithm Loop * ==================== */ for (;;) { /* Turn ON the receiver to perform CCA. Can not call Mrfi_RxModeOn(), * since that will enable the rx interrupt, which we do not want. */ RFST = ISRXON; /* * Wait for CCA to be valid. */ MRFI_RSSI_VALID_WAIT(); /* * Initiate transmit with CCA. Command is strobed and then status is * immediately checked. If status shows transmit is active, this means * that CCA passed and the transmit has gone out. A critical section * guarantees timing status check happens immediately after strobe. */ BSP_ENTER_CRITICAL_SECTION(s); RFST = ISTXONCCA; txActive = FSMSTAT1 & SAMPLED_CCA; BSP_EXIT_CRITICAL_SECTION(s); /* see transmit went out */ if (txActive) { /* ----------| CCA Passed |---------- */ /* wait for transmit to complete */ while (!(RFIRQF1 & IRQ_TXDONE)); /* transmit is done. break out of CCA algorithm loop */ break; } else { /* ----------| CCA Failed |---------- */ /* if no CCA retries are left, transmit failed so abort */ if (ccaRetries == 0) { /* set return value for failed transmit */ txResult = MRFI_TX_RESULT_FAILED; /* break out of CCA algorithm loop */ break; } /* decrement CCA retries before loop continues */ ccaRetries--; /* turn off reciever to conserve power during backoff */ Mrfi_RxModeOff(); /* delay for a random number of backoffs */ Mrfi_RandomBackoffDelay(); } } /* * --- end CCA Algorithm Loop --- * =============================================================================== */ } } /* turn radio back off to put it in a known state */ Mrfi_RxModeOff(); /* If the radio was in RX state when transmit was attempted, * put it back in RX state. */ if(mrfiRadioState == MRFI_RADIO_STATE_RX) { Mrfi_RxModeOn(); } /* return the result of the transmit */ return( txResult ); }
/************************************************************************************************** * @fn MRFI_Init * * @brief Initialize MRFI. * * @param none * * @return none ************************************************************************************************** */ void MRFI_Init(void) { /* ------------------------------------------------------------------ * Run-time integrity checks * --------------------------- */ memset(&mrfiIncomingPacket, 0x0, sizeof(mrfiIncomingPacket)); /* verify the correct radio is installed */ MRFI_ASSERT( CHIPID == MRFI_RADIO_PARTNUM ); /* wrong radio */ MRFI_ASSERT( CHVER >= MRFI_RADIO_MIN_VERSION ); /* obsolete radio version */ /* ------------------------------------------------------------------ * Configure IO ports * --------------------------- */ #if defined(MRFI_PA_LNA_ENABLED) && defined(BSP_BOARD_SRF04EB) MRFI_BOARD_PA_LNA_CONFIG_PORTS(); MRFI_BOARD_PA_LNA_HGM(); #endif /* ------------------------------------------------------------------ * Configure clock to use XOSC * ----------------------------- */ SLEEPCMD &= ~OSC_PD; /* turn on 16MHz RC and 32MHz XOSC */ while (!(SLEEPSTA & XOSC_STB)); /* wait for 32MHz XOSC stable */ asm("NOP"); /* chip bug workaround */ { uint16_t i; /* Require 63us delay for all revs */ for (i=0; i<504; i++) { asm("NOP"); } } CLKCONCMD = (0x00 | OSC_32KHZ); /* 32MHz XOSC */ while (CLKCONSTA != (0x00 | OSC_32KHZ)); SLEEPCMD |= OSC_PD; /* turn off 16MHz RC */ /* Configure radio registers that should be different from reset values. */ Mrfi_RadioRegConfig(); /* ------------------------------------------------------------------ * Variable Initialization * ------------------------- */ #ifdef MRFI_ASSERTS_ARE_ON PAN_ID0 = 0xFF; PAN_ID1 = 0xFF; #endif /* ------------------------------------------------------------------ * Initialize Random Seed Value * ------------------------------- */ /* * Set radio for infinite reception. Once radio reaches this state, * it will stay in receive mode regardless RF activity. */ FRMCTRL0 = (FRMCTRL0 & ~RX_MODE_MASK) | RX_MODE_INFINITE_RX; /* turn on the receiver */ RFST = ISRXON; /* Wait for RSSI to be valid. Once valid, radio is stable and random bits * can be read. */ MRFI_RSSI_VALID_WAIT(); /* put 16 random bits into the seed value */ { uint16_t rndSeed; uint8_t i; rndSeed = 0; for(i=0; i<16; i++) { /* read random bit to populate the random seed */ rndSeed = (rndSeed << 1) | (RFRND & 0x01); } /* * The seed value must not be zero. If it is, the pseudo random sequence will be always be zero. * There is an extremely small chance this seed could randomly be zero (more likely some type of * hardware problem would cause this). To solve this, a single bit is forced to be one. This * slightly reduces the randomness but guarantees a good seed value. */ rndSeed |= 0x0080; /* * Two writes to RNDL will set the random seed. A write to RNDL copies current contents * of RNDL to RNDH before writing new the value to RNDL. */ RNDL = rndSeed & 0xFF; RNDL = rndSeed >> 8; } /* turn off the receiver, flush RX FIFO just in case something got in there */ RFST = ISRFOFF; /* flush the rx buffer */ MRFI_RADIO_FLUSH_RX_BUFFER(); /* take receiver out of infinite reception mode; set back to normal operation */ FRMCTRL0 = (FRMCTRL0 & ~RX_MODE_MASK) | RX_MODE_NORMAL; /* Initial radio state is OFF state */ mrfiRadioState = MRFI_RADIO_STATE_OFF; /* ------------------------------------------------------------------ * Configure Radio Registers * --------------------------- */ /* disable address filtering */ FRMFILT0 &= ~FRAME_FILTER_EN; /* reject beacon/ack/cmd frames and accept only data frames, * when filtering is enabled. */ FRMFILT1 &= ~(ACCEPT_BEACON | ACCEPT_ACK | ACCEPT_CMD); /* don't enable rx after tx is done. */ FRMCTRL1 &= ~RX_ENABLE_ON_TX; /* set FIFOP threshold to maximum */ FIFOPCTRL = 127; /* set default channel */ MRFI_SetLogicalChannel( 0 ); /* set default output power level */ MRFI_SetRFPwr(MRFI_NUM_POWER_SETTINGS - 1); /* enable general RF interrupts */ IEN2 |= RFIE; /* ------------------------------------------------------------------ * Final Initialization * ----------------------- */ /********************************************************************************** * Compute reply delay scalar * * The IEEE radio has a fixed data rate of 250 Kbps. Data rate inference * from radio regsiters is not necessary for this radio. * * The maximum delay needed depends on the MAX_APP_PAYLOAD parameter. Figure * out how many bits that will be when overhead is included. Bits/bits-per-second * is seconds to transmit (or receive) the maximum frame. We multiply this number * by 1000 to find the time in milliseconds. We then additionally multiply by * 10 so we can add 5 and divide by 10 later, thus rounding up to the number of * milliseconds. This last won't matter for slow transmissions but for faster ones * we want to err on the side of being conservative and making sure the radio is on * to receive the reply. The semaphore monitor will shut it down. The delay adds in * a platform fudge factor that includes processing time on peer plus lags in Rx and * processing time on receiver's side. Also includes round trip delays from CCA * retries. This portion is included in PLATFORM_FACTOR_CONSTANT defined in mrfi.h. * * ********************************************************************************** */ #define PHY_PREAMBLE_SYNC_BYTES 8 { uint32_t bits, dataRate = 250000; bits = ((uint32_t)((PHY_PREAMBLE_SYNC_BYTES + MRFI_MAX_FRAME_SIZE)*8))*10000; /* processing on the peer + the Tx/Rx time plus more */ sReplyDelayScalar = PLATFORM_FACTOR_CONSTANT + (((bits/dataRate)+5)/10); } /* * Random delay - This prevents devices on the same power source from repeated * transmit collisions on power up. */ Mrfi_RandomBackoffDelay(); /* enable global interrupts */ BSP_ENABLE_INTERRUPTS(); }