Skip to content

Commit 6754159

Browse files
committed
first commit
0 parents  commit 6754159

4 files changed

Lines changed: 167 additions & 0 deletions

File tree

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Kongduino
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+

LoRandom.h

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
SX1276 Register (address) Register bit field (bit #) Values Note
3+
RegOpMode (0x01) LongRangeMode[7] ‘1’ LoRa mode enable
4+
Mode[2:0] ‘101’ Receive Continuous mode
5+
------------------------------------------------------------------------------------------------------------------
6+
RegModemConfig1 (0x1D) Bw[7:4] ‘0111’ ‘0111’ for 125kHz modulation Bandwidth
7+
CodingRate[3:1] ‘001’ 4/5 error coding rate
8+
ImplicitHeaderModeOn[0] ‘0’ Packets have up-front header
9+
------------------------------------------------------------------------------------------------------------------
10+
RegModemConfig2 (0x1E) SpreadingFactor[7:4] ‘0111’ ‘0111’ (SF7) = 6kbit/s
11+
12+
To generate an N bit random number, perform N read operation of the register RegRssiWideband (address 0x2c)
13+
and use the LSB of the fetched value. The value from RegRssiWideband is derived from a wideband (4MHz) signal strength
14+
at the receiver input and the LSB of this value constantly and randomly changes.
15+
*/
16+
17+
#define RegOpMode 0x01
18+
#define RegModemConfig1 0x1D
19+
#define RegModemConfig2 0x1E
20+
#define RegRssiWideband 0x2C
21+
22+
void writeRegister(uint8_t reg, uint8_t value);
23+
uint8_t readRegister(uint8_t reg);
24+
// Provide your own functions, which will depend on your library
25+
26+
uint8_t modemconf1;
27+
uint8_t modemconf2;
28+
29+
void setupLoRandom() {
30+
modemconf1 = readRegister(RegModemConfig1);
31+
modemconf2 = readRegister(RegModemConfig2);
32+
writeRegister(RegOpMode, 0b10001101); // MODE_LONG_RANGE_MODE 0b1xxxxxxx || LowFrequencyModeOn 0bxxxx1xxx || MODE_RX_CONTINUOUS 0bxxxxx101
33+
writeRegister(RegModemConfig1, 0b01110010);
34+
writeRegister(RegModemConfig2, 0b01110000);
35+
}
36+
37+
void resetLoRa() {
38+
writeRegister(RegOpMode, 0b10000001); // MODE_LONG_RANGE_MODE 0b1xxxxxxx || MODE_STDBY 0bxxxxxxx1
39+
writeRegister(RegModemConfig1, modemconf1);
40+
writeRegister(RegModemConfig2, modemconf2);
41+
}
42+
43+
uint8_t getLoRandomByte() {
44+
uint8_t x = 0;
45+
for (uint8_t j = 0; j < 8; j++) {
46+
x += (readRegister(RegRssiWideband) & 0b00000001);
47+
x = x << 1;
48+
delay(1);
49+
}
50+
return x;
51+
}
52+
53+
void fillRandom(unsigned char *x, size_t len) {
54+
setupLoRandom();
55+
size_t i;
56+
for (i = 0; i < len; i++) {
57+
x[i] = getLoRandomByte();
58+
}
59+
resetLoRa();
60+
}
61+
62+
void fillRandom(unsigned char *x, size_t len, uint8_t except) {
63+
// used, for instance, when you want a non-zero value.
64+
// fillRandom(UUID, 16, 0);
65+
// get a UUID with 16 bytes, all non-zero
66+
setupLoRandom();
67+
size_t i;
68+
for (i = 0; i < len; i++) {
69+
unsigned char c = getLoRandomByte();
70+
while (c == except)c = getLoRandomByte();
71+
x[i] = c;
72+
}
73+
resetLoRa();
74+
}
75+
76+
void fillRandom(unsigned char *x, size_t len, uint8_t minValue, uint8_t maxValue) {
77+
// used, for instance, when you want a value within a specified range.
78+
// fillRandom(x, 16, 0, 15);
79+
// get a bank of random numbers, between 0 and 15.
80+
setupLoRandom();
81+
size_t i;
82+
for (i = 0; i < len; i++) {
83+
unsigned char c = getLoRandomByte();
84+
while (c < minValue || c > maxValue)c = getLoRandomByte();
85+
x[i] = c;
86+
}
87+
resetLoRa();
88+
}
89+
90+
void hexDump(unsigned char *buf, uint16_t len) {
91+
String s = "|", t = "| |";
92+
Serial.println(F(" |.0 .1 .2 .3 .4 .5 .6 .7 .8 .9 .a .b .c .d .e .f |"));
93+
Serial.println(F(" +------------------------------------------------+ +----------------+"));
94+
for (uint16_t i = 0; i < len; i += 16) {
95+
for (uint8_t j = 0; j < 16; j++) {
96+
if (i + j >= len) {
97+
s = s + " "; t = t + " ";
98+
} else {
99+
char c = buf[i + j];
100+
if (c < 16) s = s + "0";
101+
s = s + String(c, HEX) + " ";
102+
if (c < 32 || c > 127) t = t + ".";
103+
else t = t + (String(c));
104+
}
105+
}
106+
uint8_t index = i / 16;
107+
Serial.print(index, HEX); Serial.write('.');
108+
Serial.println(s + t + "|");
109+
s = "|"; t = "| |";
110+
}
111+
Serial.println(F(" +------------------------------------------------+ +----------------+"));
112+
}

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# LoRandom
2+
3+
A library using Semtech's sx1276/7/8/9's `RegRssiWideband` register properly to generate random numbers. See [this issue](https://github.com/sandeepmistry/arduino-LoRa/issues/394) for context.
4+
5+
This was written for Sandeep Mistry's library, but is easily adaptable to other platforms.
6+
7+
See:
8+
9+
![Generating Random Numbers with LoRandom](LoRandom.png)
10+
11+
vs
12+
13+
![Generating Random Numbers with LoRaClass::random](SandeepRandom.png)

keywords.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#######################################
2+
# Syntax Coloring Map For LoRandom
3+
#######################################
4+
5+
#######################################
6+
# Methods and Functions (KEYWORD2)
7+
#######################################
8+
9+
setupLoRandom KEYWORD2
10+
writeRegister KEYWORD2
11+
readRegister KEYWORD2
12+
resetLoRa KEYWORD2
13+
getLoRandomByte KEYWORD2
14+
fillRandom KEYWORD2
15+
hexDump KEYWORD2
16+
17+
RegOpMode LITERAL1
18+
RegModemConfig1 LITERAL1
19+
RegModemConfig2 LITERAL1
20+
RegRssiWideband LITERAL1

0 commit comments

Comments
 (0)