mirror of
https://github.com/markqvist/RNode_Firmware.git
synced 2025-06-19 12:14:13 -04:00
Added nRF hardware RNG
This commit is contained in:
parent
16dedc72c2
commit
0a95f6f107
2 changed files with 37 additions and 4 deletions
|
@ -94,14 +94,24 @@ void setup() {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Seed the PRNG for CSMA R-value selection
|
// Seed the PRNG for CSMA R-value selection
|
||||||
# if MCU_VARIANT == MCU_ESP32
|
#if MCU_VARIANT == MCU_ESP32
|
||||||
// On ESP32, get the seed value from the
|
// On ESP32, get the seed value from the
|
||||||
// hardware RNG
|
// hardware RNG
|
||||||
int seed_val = (int)esp_random();
|
unsigned long seed_val = (unsigned long)esp_random();
|
||||||
|
#elif MCU_VARIANT == MCU_NRF52
|
||||||
|
// On nRF, get the seed value from the
|
||||||
|
// hardware RNG
|
||||||
|
unsigned long seed_val = get_rng_seed();
|
||||||
#else
|
#else
|
||||||
// Otherwise, get a pseudo-random seed
|
// Otherwise, get a pseudo-random seed
|
||||||
// value from an unconnected analog pin
|
// value from an unconnected analog pin
|
||||||
int seed_val = analogRead(0);
|
//
|
||||||
|
// CAUTION! If you are implementing the
|
||||||
|
// firmware on a platform that does not
|
||||||
|
// have a hardware RNG, you MUST take
|
||||||
|
// care to get a seed value with enough
|
||||||
|
// entropy at each device reset!
|
||||||
|
unsigned long seed_val = analogRead(0);
|
||||||
#endif
|
#endif
|
||||||
randomSeed(seed_val);
|
randomSeed(seed_val);
|
||||||
|
|
||||||
|
|
25
Utilities.h
25
Utilities.h
|
@ -18,6 +18,7 @@
|
||||||
#if HAS_EEPROM
|
#if HAS_EEPROM
|
||||||
#include <EEPROM.h>
|
#include <EEPROM.h>
|
||||||
#elif PLATFORM == PLATFORM_NRF52
|
#elif PLATFORM == PLATFORM_NRF52
|
||||||
|
#include <hal/nrf_rng.h>
|
||||||
#include <Adafruit_LittleFS.h>
|
#include <Adafruit_LittleFS.h>
|
||||||
#include <InternalFileSystem.h>
|
#include <InternalFileSystem.h>
|
||||||
using namespace Adafruit_LittleFS_Namespace;
|
using namespace Adafruit_LittleFS_Namespace;
|
||||||
|
@ -103,6 +104,28 @@ uint8_t boot_vector = 0x00;
|
||||||
// TODO: Get NRF52 boot flags
|
// TODO: Get NRF52 boot flags
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if MCU_VARIANT == MCU_NRF52
|
||||||
|
unsigned long get_rng_seed() {
|
||||||
|
nrf_rng_error_correction_enable(NRF_RNG);
|
||||||
|
nrf_rng_shorts_disable(NRF_RNG, NRF_RNG_SHORT_VALRDY_STOP_MASK);
|
||||||
|
nrf_rng_task_trigger(NRF_RNG, NRF_RNG_TASK_START);
|
||||||
|
while (!nrf_rng_event_check(NRF_RNG, NRF_RNG_EVENT_VALRDY));
|
||||||
|
uint8_t rb_a = nrf_rng_random_value_get(NRF_RNG);
|
||||||
|
nrf_rng_event_clear(NRF_RNG, NRF_RNG_EVENT_VALRDY);
|
||||||
|
while (!nrf_rng_event_check(NRF_RNG, NRF_RNG_EVENT_VALRDY));
|
||||||
|
uint8_t rb_b = nrf_rng_random_value_get(NRF_RNG);
|
||||||
|
nrf_rng_event_clear(NRF_RNG, NRF_RNG_EVENT_VALRDY);
|
||||||
|
while (!nrf_rng_event_check(NRF_RNG, NRF_RNG_EVENT_VALRDY));
|
||||||
|
uint8_t rb_c = nrf_rng_random_value_get(NRF_RNG);
|
||||||
|
nrf_rng_event_clear(NRF_RNG, NRF_RNG_EVENT_VALRDY);
|
||||||
|
while (!nrf_rng_event_check(NRF_RNG, NRF_RNG_EVENT_VALRDY));
|
||||||
|
uint8_t rb_d = nrf_rng_random_value_get(NRF_RNG);
|
||||||
|
nrf_rng_event_clear(NRF_RNG, NRF_RNG_EVENT_VALRDY);
|
||||||
|
nrf_rng_task_trigger(NRF_RNG, NRF_RNG_TASK_STOP);
|
||||||
|
return rb_a << 24 | rb_b << 16 | rb_c << 8 | rb_d;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
#if HAS_NP == true
|
#if HAS_NP == true
|
||||||
#include <Adafruit_NeoPixel.h>
|
#include <Adafruit_NeoPixel.h>
|
||||||
#define NUMPIXELS 1
|
#define NUMPIXELS 1
|
||||||
|
@ -349,7 +372,7 @@ void hard_reset(void) {
|
||||||
#elif MCU_VARIANT == MCU_ESP32
|
#elif MCU_VARIANT == MCU_ESP32
|
||||||
ESP.restart();
|
ESP.restart();
|
||||||
#elif MCU_VARIANT == MCU_NRF52
|
#elif MCU_VARIANT == MCU_NRF52
|
||||||
NVIC_SystemReset();
|
NVIC_SystemReset();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue