|
| 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