Skip to content

Commit 4ebe0a4

Browse files
committed
First Official Release for Arduino Library Manager
1 parent fa5e2cb commit 4ebe0a4

File tree

9 files changed

+310
-0
lines changed

9 files changed

+310
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#include <LoRa.h>
2+
#include <LoRandom.h>
3+
4+
#define SS 18
5+
#define RST 14
6+
#define DI0 26
7+
#define BAND 431E6
8+
#define REG_OCP 0x0B
9+
#define REG_PA_CONFIG 0x09
10+
#define REG_LNA 0x0c
11+
#define REG_OP_MODE 0x01
12+
#define REG_MODEM_CONFIG_1 0x1d
13+
#define REG_MODEM_CONFIG_2 0x1e
14+
#define REG_MODEM_CONFIG_3 0x26
15+
#define REG_PA_DAC 0x4D
16+
#define PA_DAC_HIGH 0x87
17+
#define REG_PA_DAC 0x4D
18+
#define MODE_LONG_RANGE_MODE 0x80
19+
#define MODE_SLEEP 0x00
20+
#define MODE_STDBY 0x01
21+
#define MODE_TX 0x03
22+
#define MODE_RX_CONTINUOUS 0x05
23+
#define MODE_RX_SINGLE 0x06
24+
25+
/*
26+
Note: I have "customized" the LoRa library by moving
27+
uint8_t readRegister(uint8_t address);
28+
void writeRegister(uint8_t address, uint8_t value);
29+
to public: in LoRa.h – as we need access to the registers, obviously.
30+
*/
31+
32+
void writeRegister(uint8_t reg, uint8_t value) {
33+
LoRa.writeRegister(reg, value);
34+
}
35+
uint8_t readRegister(uint8_t reg) {
36+
return LoRa.readRegister(reg);
37+
}
38+
39+
unsigned char pkt[256];
40+
41+
void setup() {
42+
Serial.begin(115200);
43+
delay(1000);
44+
Serial.flush();
45+
Serial.print(F("\n\n\n[SX1278] Initializing ... "));
46+
delay(1000);
47+
SPI.begin(5, 19, 27, 18);
48+
LoRa.setPins(SS, RST, DI0);
49+
if (!LoRa.begin(BAND)) {
50+
Serial.println("Starting LoRa failed!");
51+
while (1);
52+
}
53+
LoRa.setTxPower(20, PA_OUTPUT_PA_BOOST_PIN);
54+
LoRa.setPreambleLength(8);
55+
LoRa.setTxPower(20, PA_OUTPUT_PA_BOOST_PIN);
56+
LoRa.setPreambleLength(8);
57+
LoRa.setSpreadingFactor(12);
58+
LoRa.setSignalBandwidth(250E3);
59+
LoRa.setCodingRate4(5);
60+
61+
// BW = 8: 250 kHz, CR = 1: 4/5, HM = 0
62+
uint8_t reg1 = 0x82;
63+
// SF = 12: 12, CRC = 1
64+
uint8_t reg2 = 0xC4;
65+
// LDRO = 1, AGCAutoOn = 0
66+
uint8_t reg3 = 0x08;
67+
// PaSelect = 1, MaxPower = 7: 15 dBm, OutputPower = 15: 17 dBm
68+
uint8_t regpaconfig = 0xFF;
69+
// 7:5 LnaGain 001 -> G1 = maximum gain
70+
// 1:0 LnaBoostHf 11 -> Boost on, 150% LNA current
71+
uint8_t reglna = 0b00100011;
72+
73+
Serial.print("\nSetting up LoRa for Transmission ");
74+
writeRegister(REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_SLEEP);
75+
delay(10);
76+
writeRegister(REG_PA_CONFIG, 0xFF);
77+
writeRegister(REG_MODEM_CONFIG_1, reg1);
78+
writeRegister(REG_MODEM_CONFIG_2, reg2);
79+
writeRegister(REG_MODEM_CONFIG_3, reg3);
80+
writeRegister(REG_LNA, reglna);
81+
writeRegister(REG_PA_DAC, REG_PA_DAC); // That's for the receiver
82+
writeRegister(REG_OCP, 0b00111111);
83+
delay(10);
84+
writeRegister(REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_STDBY);
85+
delay(10);
86+
Serial.println("LoRa [" + String(BAND / 1E6) + "]");
87+
88+
uint8_t x = readRegister(0x01);
89+
Serial.print("RegOpMode: 0x");
90+
if (x < 16) Serial.write('0');
91+
Serial.println(x, HEX);
92+
x = readRegister(0x1D);
93+
Serial.print("RegModemConfig1: 0x");
94+
if (x < 16) Serial.write('0');
95+
Serial.println(x, HEX);
96+
x = readRegister(0x1E);
97+
Serial.print("RegModemConfig2: 0x");
98+
if (x < 16) Serial.write('0');
99+
Serial.println(x, HEX);
100+
}
101+
102+
void loop() {
103+
Serial.println("\n Generating Random Numbers with LoRandom");
104+
fillRandom(pkt, 256);
105+
hexDump(pkt, 256);
106+
delay(3000);
107+
}

examples/ESP32_Random_Test/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+
74.5 KB
Loading
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# ESP32 Random Test
2+
3+
A sample sketch demonstrating the problem in Sandeep Mistry's excellent LoRa library's `random()` function. See [this issue](https://github.com/sandeepmistry/arduino-LoRa/issues/394) for context.
4+
5+
This was written for ESP32, but is easily adaptable to other platforms. The code runs on TTGO series. A T-Beam in this case, but it'll work as is with basically any LoRa-equipped TTGO or Heltec ESP32 device.
6+
7+
This version moves LoRandom to a [library on its own](https://github.com/Kongduino/LoRandom). I can now update one file and reuse in all my projects...
8+
9+
Note: I have "customized the LoRa library by moving
10+
```c
11+
uint8_t readRegister(uint8_t address);
12+
void writeRegister(uint8_t address, uint8_t value);
13+
```
14+
to `public:` in LoRa.h – as we need access to the registers, obviously.
15+
16+
See:
17+
18+
![Generating Random Numbers with LoRandom](LoRandom.png)
19+
20+
vs
21+
22+
![Generating Random Numbers with LoRaClass::random](SandeepRandom.png)
58 KB
Loading

keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ readRegister KEYWORD2
1212
resetLoRa KEYWORD2
1313
getLoRandomByte KEYWORD2
1414
fillRandom KEYWORD2
15+
fillRandomMultiple KEYWORD2
1516
hexDump KEYWORD2
1617

1718
RegOpMode LITERAL1

library.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "LoRandom",
3+
"keywords": "trng, rng, lora",
4+
"description": "A library using Semtech's sx1276/7/8/9's `RegRssiWideband` register properly to generate random numbers. This was written for Sandeep Mistry's library, but is easily adaptable to other platforms. See [this issue](https://github.com/sandeepmistry/arduino-LoRa/issues/394) for context.",
5+
"homepage": "https://github.com/Kongduino/LoRandom",
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/Kongduino/LoRandom.git"
9+
},
10+
"version": "1.0.0",
11+
"authors": {
12+
"name": "Kongduino",
13+
"url": "https://kongduino.wordpress.com"
14+
},
15+
"exclude": [
16+
".github",
17+
"extras"
18+
],
19+
"frameworks": "arduino",
20+
"platforms": "*"
21+
}

library.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=LoRandom
2+
version=1.0.0
3+
author=Kongduino
4+
maintainer=Kongduino <[email protected]>
5+
sentence=A library using Semtech's sx1276/7/8/9's `RegRssiWideband` register properly to generate random numbers.
6+
paragraph=This was written for Sandeep Mistry's library, but is easily adaptable to other platforms. See [this issue](https://github.com/sandeepmistry/arduino-LoRa/issues/394) for context.
7+
category=Data Processing
8+
url=https://github.com/Kongduino/LoRandom
9+
architectures=*

src/LoRandom.h

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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);
33+
// MODE_LONG_RANGE_MODE 0b1xxxxxxx || LowFrequencyModeOn 0bxxxx1xxx || MODE_RX_CONTINUOUS 0bxxxxx101
34+
writeRegister(RegModemConfig1, 0b01110010);
35+
writeRegister(RegModemConfig2, 0b01110000);
36+
}
37+
38+
void resetLoRa() {
39+
writeRegister(RegOpMode, 0b10000001);
40+
// MODE_LONG_RANGE_MODE 0b1xxxxxxx || MODE_STDBY 0bxxxxxxx1
41+
writeRegister(RegModemConfig1, modemconf1);
42+
writeRegister(RegModemConfig2, modemconf2);
43+
}
44+
45+
uint8_t getLoRandomByte() {
46+
uint8_t x = 0;
47+
for (uint8_t j = 0; j < 8; j++) {
48+
x += (readRegister(RegRssiWideband) & 0b00000001);
49+
x = x << 1;
50+
//delay(1);
51+
}
52+
return x;
53+
}
54+
55+
void fillRandomMultiple(unsigned char *x, size_t len, uint8_t multiple) {
56+
// Fill up a buffer with multiple instances of the same random byte.
57+
// Yes, I have a specific use for this.
58+
setupLoRandom();
59+
size_t i;
60+
for (i = 0; i < len; i += multiple) {
61+
uint8_t rng = getLoRandomByte();
62+
for (uint8_t j = 0; j < multiple; j++) {
63+
x[i + j] = rng;
64+
}
65+
}
66+
resetLoRa();
67+
}
68+
69+
void fillRandom(unsigned char *x, size_t len) {
70+
setupLoRandom();
71+
size_t i;
72+
for (i = 0; i < len; i++) {
73+
x[i] = getLoRandomByte();
74+
}
75+
resetLoRa();
76+
}
77+
78+
void fillRandom(unsigned char *x, size_t len, uint8_t except) {
79+
// used, for instance, when you want a non-zero value.
80+
// fillRandom(UUID, 16, 0);
81+
// get a UUID with 16 bytes, all non-zero
82+
setupLoRandom();
83+
size_t i;
84+
for (i = 0; i < len; i++) {
85+
unsigned char c = getLoRandomByte();
86+
while (c == except)c = getLoRandomByte();
87+
x[i] = c;
88+
}
89+
resetLoRa();
90+
}
91+
92+
void fillRandom(unsigned char *x, size_t len, uint8_t minValue, uint8_t maxValue) {
93+
// used, for instance, when you want a value within a specified range.
94+
// fillRandom(x, 16, 0, 15);
95+
// get a bank of random numbers, between 0 and 15.
96+
setupLoRandom();
97+
size_t i;
98+
for (i = 0; i < len; i++) {
99+
unsigned char c = getLoRandomByte();
100+
while (c < minValue || c > maxValue)c = getLoRandomByte();
101+
x[i] = c;
102+
}
103+
resetLoRa();
104+
}
105+
106+
void hexDump(unsigned char *buf, uint16_t len) {
107+
String s = "|", t = "| |";
108+
Serial.println(F(" |.0 .1 .2 .3 .4 .5 .6 .7 .8 .9 .a .b .c .d .e .f |"));
109+
Serial.println(F(" +------------------------------------------------+ +----------------+"));
110+
for (uint16_t i = 0; i < len; i += 16) {
111+
for (uint8_t j = 0; j < 16; j++) {
112+
if (i + j >= len) {
113+
s = s + " "; t = t + " ";
114+
} else {
115+
char c = buf[i + j];
116+
if (c < 16) s = s + "0";
117+
s = s + String(c, HEX) + " ";
118+
if (c < 32 || c > 127) t = t + ".";
119+
else t = t + (String(c));
120+
}
121+
}
122+
uint8_t index = i / 16;
123+
Serial.print(index, HEX); Serial.write('.');
124+
Serial.println(s + t + "|");
125+
s = "|"; t = "| |";
126+
}
127+
Serial.println(F(" +------------------------------------------------+ +----------------+"));
128+
}

0 commit comments

Comments
 (0)