From b40ad5c2f77f6d691a8e922404b67e996f99d134 Mon Sep 17 00:00:00 2001 From: jgromes Date: Sun, 13 Sep 2020 17:53:23 +0200 Subject: [PATCH] [SX126x] Added TRNG support --- src/modules/SX126x/SX126x.cpp | 21 +++++++++++++++++++++ src/modules/SX126x/SX126x.h | 9 ++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/modules/SX126x/SX126x.cpp b/src/modules/SX126x/SX126x.cpp index edeaae73..203133eb 100644 --- a/src/modules/SX126x/SX126x.cpp +++ b/src/modules/SX126x/SX126x.cpp @@ -1173,6 +1173,27 @@ int16_t SX126x::autoLDRO() { return(ERR_NONE); } +uint8_t SX126x::random() { + // set mode to Rx + setRx(SX126X_RX_TIMEOUT_INF); + + // 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++) { + uint8_t val = 0x00; + readRegister(SX126X_REG_RANDOM_NUMBER_0, &val, sizeof(uint8_t)); + randByte |= ((val & 0x01) << i); + } + + // set mode to standby + standby(); + + return(randByte); +} + int16_t SX126x::setTCXO(float voltage, uint32_t delay) { // set mode to standby standby(); diff --git a/src/modules/SX126x/SX126x.h b/src/modules/SX126x/SX126x.h index 3d45266a..0778ea6e 100644 --- a/src/modules/SX126x/SX126x.h +++ b/src/modules/SX126x/SX126x.h @@ -854,7 +854,7 @@ class SX126x: public PhysicalLayer { /*! \brief Forces LoRa low data rate optimization. Only available in LoRa mode. After calling this method, LDRO will always be set to - the provided value, regardless of symbol length. To re-enable automatic LDRO configuration, call SX1278::autoLDRO() + the provided value, regardless of symbol length. To re-enable automatic LDRO configuration, call SX126x::autoLDRO() \param enable Force LDRO to be always enabled (true) or disabled (false). @@ -870,6 +870,13 @@ class SX126x: public PhysicalLayer { */ int16_t autoLDRO(); + /*! + \brief Get one truly random byte from RSSI noise. + + \returns TRNG byte. + */ + uint8_t random(); + #ifndef RADIOLIB_GODMODE protected: #endif