-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathESPRNG.cpp
More file actions
executable file
·55 lines (51 loc) · 1.03 KB
/
ESPRNG.cpp
File metadata and controls
executable file
·55 lines (51 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <ESPRNG.h>
#if defined ESP8266 || defined ESP32
/**
* ESP8266 and ESP32 specific hardware true random number generator.
*
* Acording to the ESP32 documentation, you should not call the tRNG
* faster than 5MHz
*
*/
void ESPRNG::fill(uint8_t *dst, unsigned int length)
{
// ESP8266 and ESP32 only
for (int i = 0; i < length; i++)
{
dst[i] = get();
}
#if defined ESP8266
ESP.wdtFeed();
#endif
}
byte ESPRNG::get()
{
#if defined ESP32
// ESP32 only
uint32_t* randReg = (uint32_t*) 0x3FF75144;
return (byte) *randReg;
#elif defined ESP8266
// ESP8266 only
uint32_t* randReg = (uint32_t*) 0x3FF20E44L;
return (byte) *randReg;
#else
// NOT SUPPORTED
return 0;
#endif
}
uint32_t ESPRNG::getLong()
{
#if defined ESP32
// ESP32 only
uint32_t* randReg = (uint32_t*) 0x3FF75144;
return (byte) *randReg;
#elif defined ESP8266
// ESP8266 only
uint32_t* randReg = (uint32_t*) 0x3FF20E44L;
return *randReg;
#else
// NOT SUPPORTED
return 0;
#endif
}
#endif