From ff8d9d672df033b52dccf17225611bb600b47eb1 Mon Sep 17 00:00:00 2001 From: jgromes Date: Sun, 13 Sep 2020 17:53:30 +0200 Subject: [PATCH] [RF69] Added TRNG support --- src/modules/RF69/RF69.cpp | 19 +++++++++++++++++++ src/modules/RF69/RF69.h | 7 +++++++ 2 files changed, 26 insertions(+) diff --git a/src/modules/RF69/RF69.cpp b/src/modules/RF69/RF69.cpp index 7875f41b..b47c522e 100644 --- a/src/modules/RF69/RF69.cpp +++ b/src/modules/RF69/RF69.cpp @@ -764,6 +764,25 @@ void RF69::setRfSwitchPins(RADIOLIB_PIN_TYPE rxEn, RADIOLIB_PIN_TYPE txEn) { _mod->setRfSwitchPins(rxEn, txEn); } +uint8_t RF69::random() { + // set mode to Rx + setMode(RF69_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(RF69_REG_RSSI_VALUE) & 0x01) << i); + } + + // set mode to standby + setMode(RF69_STANDBY); + + return(randByte); +} + int16_t RF69::config() { int16_t state = ERR_NONE; diff --git a/src/modules/RF69/RF69.h b/src/modules/RF69/RF69.h index 63b63513..0c2294a2 100644 --- a/src/modules/RF69/RF69.h +++ b/src/modules/RF69/RF69.h @@ -820,6 +820,13 @@ class RF69: 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