static uint16_t random_seed(void) { uint16_t seed = 0; // Enable the radio clock HWREG(SYS_CTRL_RCGCRFC) = 1; // Wait until the radio is clocked while (HWREG(SYS_CTRL_RCGCRFC) != 1); // Turn the radio on and receive CC2538_RF_CSP_ISRXON(); // Wait until the RSSI valid signal is high while (!(HWREG(RFCORE_XREG_RSSISTAT) & RFCORE_XREG_RSSISTAT_RSSI_VALID)); // Sample the radio until the seed is valid (0x0000 and 0x8003 are not valid) while (seed == 0x0000 || seed == 0x8003) { for (uint8_t i = 0; i < 16; i++) { seed |= (HWREG(RFCORE_XREG_RFRND) & RFCORE_XREG_RFRND_IRND); seed <<= 1; } } // Turn the radio off CC2538_RF_CSP_ISRFOFF(); return seed; }
void radio_rxNow() { //empty buffer before receiving //CC2538_RF_CSP_ISFLUSHRX(); //enable radio interrupts enable_radio_interrupts(); CC2538_RF_CSP_ISRXON(); // busy wait until radio really listening while(!((HWREG(RFCORE_XREG_FSMSTAT1) & RFCORE_XREG_FSMSTAT1_RX_ACTIVE))); }
/*---------------------------------------------------------------------------*/ static int on(void) { PRINTF("RF: On\n"); if(!(rf_flags & RX_ACTIVE)) { CC2538_RF_CSP_ISFLUSHRX(); CC2538_RF_CSP_ISRXON(); rf_flags |= RX_ACTIVE; } ENERGEST_ON(ENERGEST_TYPE_LISTEN); return 1; }
void RfRnd(unsigned char *seed, size_t seedlen) { int i = 0, j = 0; unsigned char s = 0; int overheated = 0; #if VERBOSE int rssi = 0, freqest = 0; #endif memset(seed, 0, seedlen); /* Make sure the RNG is on */ REG(SOC_ADC_ADCCON1) &= ~(SOC_ADC_ADCCON1_RCTRL1 | SOC_ADC_ADCCON1_RCTRL0); /* Enable clock for the RF Core */ REG(SYS_CTRL_RCGCRFC) = 1; /* Wait for the clock ungating to take effect */ while (REG(SYS_CTRL_RCGCRFC) != 1); /* Infinite RX - FRMCTRL0[3:2] = 10 * * This will mess with radio operation - see note above */ REG(RFCORE_XREG_FRMCTRL0) = 0x00000008; /* Turn RF on */ CC2538_RF_CSP_ISRXON(); #if 0 //This is something evil... //Turn on PD_OVERRIDE REG(RFCORE_XREG_PTEST1) = 0x08; //Turn off LNA and mixer. REG(RFCORE_XREG_PTEST0) = 0x04; #endif /* * * Wait until "the chip has been in RX long enough for the transients to * * have died out. A convenient way to do this is to wait for the RSSI-valid * * signal to go high." * */ while (!(REG(RFCORE_XREG_RSSISTAT) & RFCORE_XREG_RSSISTAT_RSSI_VALID)); /* * * Form the seed by concatenating bits from IF_ADC in the RF receive path. * * Keep sampling until we have read at least 16 bits AND the seed is valid * * * * Invalid seeds are 0x0000 and 0x8003 and should not be used. * */ for (i = 0; i < seedlen; i++) { //Read 8 bits from RFRND. for (j = 0; j < 8; j++) { if (!((REG(RFCORE_XREG_RSSISTAT) & RFCORE_XREG_RSSISTAT_RSSI_VALID))) { //RF core is invalidated. Stop reading. printf("#RFRND invalidated.\n"); overheated = 1; break; } s <<= 1; s |= (REG(RFCORE_XREG_RFRND) & RFCORE_XREG_RFRND_IRND); } if (overheated) { break; } seed[i] = s; s = 0; } #if VERBOSE //Read out RSSI and FREQEST. if(128 <= (rssi = REG(RFCORE_XREG_RSSI))) rssi -= 256; if(128 <= (freqest = REG(RFCORE_XREG_FREQEST))) freqest -= 256; //Print bits read, RSSI and frequency offset. printf("#(NBIT,RSSI,FREQEST) %d %d %d\n", i * 8, rssi - 73, (freqest * 7800 - 15000) / 1000 ); #else printf("#(NBIT) %d\n", i * 8); #endif /* RF Off. NETSTACK_RADIO.init() will sort out normal RF operation */ CC2538_RF_CSP_ISRFOFF(); printseed(seed, i); printf("\n"); return; }
port_INLINE void radio_on(){ CC2538_RF_CSP_ISFLUSHRX(); CC2538_RF_CSP_ISRXON(); }