Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Random function #395

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open

Fix Random function #395

wants to merge 14 commits into from

Conversation

sabas1080
Copy link

Hi everyone

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

Welcome feedback

Thanks

src/LoRa.cpp Outdated Show resolved Hide resolved
Copy link
Collaborator

@wero1414 wero1414 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just add some minor changes, to allow the example to compile in any board

@sabas1080
Copy link
Author

Thanks @wero1414 , I have made the changes

@wero1414
Copy link
Collaborator

wero1414 commented Sep 7, 2020

@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

examples/LoRandom/LoRandom.ino Outdated Show resolved Hide resolved
@sabas1080 sabas1080 requested a review from wero1414 September 7, 2020 19:09
@sabas1080
Copy link
Author

ready @wero1414 @tobozo

@Kongduino
Copy link

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

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

Kongduino commented Sep 8, 2020

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

Copy link
Owner

@sandeepmistry sandeepmistry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @sabas1080, thanks for opening this pull request!

I've made some comments to start some discussions. @wero1414 @Kongduino @tobozo feel free to participate in the discussion as well.

examples/LoRandom/LoRandom.ino Outdated Show resolved Hide resolved
examples/LoRandom/LoRandom.ino Outdated Show resolved Hide resolved
examples/LoRandom/LoRandom.ino Outdated Show resolved Hide resolved
examples/LoRandom/LoRandom.ino Outdated Show resolved Hide resolved
examples/LoRandom/LoRandom.ino Outdated Show resolved Hide resolved
src/LoRa.cpp Outdated Show resolved Hide resolved
examples/LoRandom/LoRandom.ino Outdated Show resolved Hide resolved
examples/LoRandom/LoRandom.ino Outdated Show resolved Hide resolved
examples/LoRandom/LoRandom.ino Outdated Show resolved Hide resolved
src/LoRa.h Outdated Show resolved Hide resolved
@tobozo
Copy link

tobozo commented Sep 9, 2020

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

@Kongduino
Copy link

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

@sabas1080
Copy link
Author

I'm coming back to finish this task

src/LoRa.cpp Outdated Show resolved Hide resolved
src/LoRa.h Outdated Show resolved Hide resolved
@IoTThinks
Copy link
Collaborator

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

@sandeepmistry
Copy link
Owner

@sabas1080 any thoughts on @IoTThinks comment in #395 (comment)?

@plybrd
Copy link

plybrd commented Jul 14, 2021

@IoTThinks comment in #395 (comment)

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

plybrd commented Jul 14, 2021

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 #395 (comment)).

@plybrd plybrd mentioned this pull request Aug 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants