[SX127x] Added TRNG support

This commit is contained in:
jgromes 2020-09-13 17:53:16 +02:00
parent 3cc55806cf
commit 04195ee0d2
2 changed files with 32 additions and 0 deletions

View file

@ -958,6 +958,31 @@ void SX127x::setRfSwitchPins(RADIOLIB_PIN_TYPE rxEn, RADIOLIB_PIN_TYPE txEn) {
_mod->setRfSwitchPins(rxEn, txEn);
}
uint8_t SX127x::random() {
// check active modem
uint8_t rssiValueReg = SX127X_REG_RSSI_WIDEBAND;
if(getActiveModem() == SX127X_FSK_OOK) {
rssiValueReg = SX127X_REG_RSSI_VALUE_FSK;
}
// set mode to Rx
setMode(SX127X_RX);
// wait a bit for the RSSI reading to stabilise
Module::delay(10);
// read RSSI value 8 times, always keep just the least significant bit
uint8_t randByte = 0x00;
for(uint8_t i = 0; i < 8; i++) {
randByte |= ((_mod->SPIreadRegister(rssiValueReg) & 0x01) << i);
}
// set mode to standby
setMode(SX127X_STANDBY);
return(randByte);
}
int8_t SX127x::getTempRaw() {
int8_t temp = 0;
uint8_t previousOpMode;

View file

@ -931,6 +931,13 @@ class SX127x: public PhysicalLayer {
*/
void setRfSwitchPins(RADIOLIB_PIN_TYPE rxEn, RADIOLIB_PIN_TYPE txEn);
/*!
\brief Get one truly random byte from RSSI noise.
\returns TRNG byte.
*/
uint8_t random();
#ifndef RADIOLIB_GODMODE
protected:
#endif