arduino-LoRa icon indicating copy to clipboard operation
arduino-LoRa copied to clipboard

Fix Random function

Open sabas1080 opened this issue 5 years ago • 13 comments

Hi everyone

I am doing this pull request based in the work of @Kongduino in the issue #394

Welcome feedback

Thanks

sabas1080 avatar Sep 07 '20 14:09 sabas1080

Thanks @wero1414 , I have made the changes

sabas1080 avatar Sep 07 '20 15:09 sabas1080

@morganrallen @sandeepmistry this seems to fix the problems with getting random numbers with the radio, could you check it out?

It would close the #150 PR and also the issue #394

wero1414 avatar Sep 07 '20 16:09 wero1414

ready @wero1414 @tobozo

sabas1080 avatar Sep 07 '20 20:09 sabas1080

I will do some tests, but we need to remember that the LoRa chip must be set in a specific state (continuous receive, SF 7, BW 125 KHz, C/R 4/5) before reading register 0x2c. We need to warn the user, and possibly provide a way to switch ti the proper settings, and then switch back to the user's preferred settings.

Kongduino avatar Sep 07 '20 21:09 Kongduino

See https://github.com/Kongduino/ESP32_Random_Test for an example. This version of LoRandom.h seamlessly sets up LoRa for rng, and resets LoRa to the previous settings when done.

Kongduino avatar Sep 08 '20 00:09 Kongduino

Thanks @wero1414 , I have made the changes

There was a reason (not that important but still) to put the settings in binary: these registers have several settings within a byte, so the binary representation helps seeing what is set up.

Example:

  writeRegister(RegOpMode, 0b10001101);
  // MODE_LONG_RANGE_MODE 0b1xxxxxxx || LowFrequencyModeOn 0bxxxx1xxx || MODE_RX_CONTINUOUS 0bxxxxx101

Kongduino avatar Sep 08 '20 00:09 Kongduino

Speculation :

Use a double buffer random pool with implicit update, when the first buffer is consumed it is marked as such and updated on next opportunity. Both buffers are filled when LoRa.begin() is called, and can be separately re-filled by the driver when marked as consumed.

Benefits:

  • Lora.random() actually acts as initially expected with reduced communication overhead (bonus backwards compat).
  • New features to manipulate randomness (buffer size, pool size).

tobozo avatar Sep 09 '20 10:09 tobozo

LoRa.setPins(SS, RFM_RST, RFM_DIO0);

This is specific to a board, BastWAN, that I used originally to create LoRandom. This should definitely be removed from the example code...

Kongduino avatar Sep 23 '20 05:09 Kongduino

I'm coming back to finish this task

sabas1080 avatar Oct 09 '20 04:10 sabas1080

Hi, How is the new method continuousRxMode related to the RX_CONTINOUS mode in receive() metthod? Will the receive() use continuousRxMode? Thanks a lot.

image

IoTThinks avatar Oct 12 '20 15:10 IoTThinks

@sabas1080 any thoughts on @IoTThinks comment in https://github.com/sandeepmistry/arduino-LoRa/pull/395#issuecomment-707183118?

sandeepmistry avatar Nov 11 '20 00:11 sandeepmistry

@IoTThinks comment in https://github.com/sandeepmistry/arduino-LoRa/pull/395#issuecomment-707183118

Hi, How is the new method continuousRxMode related to the RX_CONTINOUS mode in receive() metthod? Will the receive() use continuousRxMode? Thanks a lot.

void LoRaClass::continuousMode()
{
  writeRegister(REG_OP_MODE, 0x72); // -> 0b10001101
}

writeRegister(REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_RX_CONTINUOUS); // -> 0b10000101

There is an description how to generate random numbers in Application Note AN1200.24 from Semtech. See Chapter 4 of Random Number Generation for Cryptography. It said for RegOpMode: Bit 7 -> 1 Bit 2-0 -> 101 Nothing is said about Bit 3

Datasheet says: REGOpMode Bit 3 LowFrequencyModeOn, after Reset it is set to 1. But the setting is only important for access to some registers for FSK/OOK Mode.

Is it not much more clear what is going on by writing

writeRegister(REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_RX_CONTINUOUS);

plybrd avatar Jul 14 '21 06:07 plybrd

Hi, there are many application which needs only few random bytes. A big buffer of random bytes is nice only for applications which needs much of them. Switching between sending/receiving and generating random numbers should work on the fly.

Why not start by adding just a function byte LoRaClass::random(uint8_t *buffer, size_t size) which gives the user just how many bytes he needs and sets the register back to the state before calling this function?

For later on this function can be used to fill some buffer or double buffer of random bytes, ...(see https://github.com/sandeepmistry/arduino-LoRa/pull/395#issuecomment-689470996).

plybrd avatar Jul 14 '21 07:07 plybrd