possible to direct access RNG registers of 32F411 via arduino-ide?

Post here first, or if you can't find a relevant section!
keepitsimple
Posts: 3
Joined: Sun Nov 20, 2022 1:27 am

possible to direct access RNG registers of 32F411 via arduino-ide?

Post by keepitsimple »

Hello, I'm using a Nucleo-64 (STM32F411) and the arduino-ide. The 32F411 datasheet says it has a RNG, and the ST RM0090 datasheet (pages 767-771) clearly explain how the RNG works. From a bitwise perspective setting up the registers, monitoring, and retrieving the random number seems straight forward, but trying to make this happen from the arduino-ide is where I get lost.

My thought process is if I can read/write the control and status registers directly, then I can figure out how to monitor for a valid number and read it.
The following lines are my thought process to just get started:

RNG_CR = 0x00000004; // write to the RNG control register to enable rngen no interrupt
delay(10); // wait a little (hopefully 40 clock cycles of the RNG_CLK occur in under 10mS)
unsigned long numberstatus = RNG_SR; // read the status register
serial.println(numberstatus); // send the contents out the uart

I can't seem to figure out how to read/write to the registers directly, is it even possible using the arduino-ide?
THanks
dannyf
Posts: 217
Joined: Sat Jul 04, 2020 7:46 pm

Re: possible to direct access RNG registers of 32F411 via arduino-ide?

Post by dannyf »

the simplest would be to go through the header files. they typically define a struct and anchor it to the address of the module. if that's not present, rolling your own isn't that difficult.

the 2nd best solution would be for you to define a set of macros / registers.

and the absolute worst would be to use pointers to hard-coded addresses.

I would start by figuring out what header files you are using and go from there.
ozcar
Posts: 127
Joined: Wed Apr 29, 2020 9:07 pm
Answers: 4

Re: possible to direct access RNG registers of 32F411 via arduino-ide?

Post by ozcar »

keepitsimple wrote: Sun Nov 20, 2022 2:51 am The 32F411 datasheet says it has a RNG, and the ST RM0090 datasheet (pages 767-771) clearly explain how the RNG works.
The datasheet I'm looking at does not mention RNG, and RM0090 does not appear to be for F411??
ag123
Posts: 1421
Joined: Thu Dec 19, 2019 5:30 am
Answers: 19

Re: possible to direct access RNG registers of 32F411 via arduino-ide?

Post by ag123 »

it'd need a stm32f407xx or stm32f405xx to pair with RM0009 ref manual.
stm32f401, f411 is probably a little less 'feature rich' vs the larger and more complex chips, deemed 'value' line? after all?
for a 'simplier' RNG, connect a wire of some length say 30cm and measure ADC voltages, you would pick up the random radio waves.
stm32 is a SDR (software defined radio), all it takes is a wire connected to the ADC, some kind of 'grounding', 'termination' etc is probably needed ,
e.g. to terminate with a 10k resistor divider at the other end say at 1/2 of 3v VDD

perhaps this is sufficiently 'random' for your purpose?
viewtopic.php?p=8455#p8455
Image
User avatar
Bakisha
Posts: 116
Joined: Fri Dec 20, 2019 6:50 pm
Answers: 4
Contact:

Re: possible to direct access RNG registers of 32F411 via arduino-ide?

Post by Bakisha »

CMSIS definition from https://github.com/stm32duino/Arduino_C ... 32f407xx.h

I think this should be enough:

Code: Select all


// Write to register
RNG -> CR = 0;
RNG -> CR = (RNG -> CR  ) | RNG_CR_RNGEN;   // enable bit
RNG -> CR |= RNG_CR_RNGEN; 			// enable bit
RNG -> CR &= ~(RNG_CR_RNGEN); 			// disable bit

// Read data register
uint32_t my_rnd = RNG -> DR ; 

compare:
if ( (RNG -> SR) & RNG_SR_DRDY) { /* this bit is set */   } ;

Maybe enable clock is needed too?

Code: Select all

RCC-> AHB2ENR |= RCC_AHB2ENR_RNGEN;
ozcar
Posts: 127
Joined: Wed Apr 29, 2020 9:07 pm
Answers: 4

Re: possible to direct access RNG registers of 32F411 via arduino-ide?

Post by ozcar »

Bakisha wrote: Mon Nov 21, 2022 4:05 pm CMSIS definition from https://github.com/stm32duino/Arduino_C ... 32f407xx.h

I think this should be enough:

Code: Select all


// Write to register
RNG -> CR = 0;
RNG -> CR = (RNG -> CR  ) | RNG_CR_RNGEN;   // enable bit
RNG -> CR |= RNG_CR_RNGEN; 			// enable bit
RNG -> CR &= ~(RNG_CR_RNGEN); 			// disable bit

// Read data register
uint32_t my_rnd = RNG -> DR ; 

compare:
if ( (RNG -> SR) & RNG_SR_DRDY) { /* this bit is set */   } ;

Maybe enable clock is needed too?

Code: Select all

RCC-> AHB2ENR |= RCC_AHB2ENR_RNGEN;
Well, it could work if the processor actually has the feature.

I see that if I select say a F407 board then that code compiles OK (I did not try to run it).

However if I select a F411 board it does not even compile, it throws out errors like:

Code: Select all

rng:12:20: error: 'RCC_AHB2ENR_RNGEN' was not declared in this scope; did you mean 'RCC_AHB2ENR_OTGFSEN'?

rng:18:18: error: 'RNG' was not declared in this scope
That seems reasonable, good even, if the processor does not have RNG.

Unless you think maybe, like some processors have more flash than is documented, perhaps the RNG might really be there? I guess if the processor turned out to be a fake maybe anything could be possible, but I don't know if there are any fake F411 processors, let alone ones on Nucleo boards. If you wanted to test that theory, you'd have to do some fancy footwork to get the code to compile in the first place.
User avatar
Bakisha
Posts: 116
Joined: Fri Dec 20, 2019 6:50 pm
Answers: 4
Contact:

Re: possible to direct access RNG registers of 32F411 via arduino-ide?

Post by Bakisha »

I don't think F411 have that feature.
Quick search for "RNG" in CMSIS:
RNG.jpg
RNG.jpg (94.65 KiB) Viewed 248 times
19 out of 24 do have it, but F411 is not one of them.

Either use noise from analog input, as ag123 suggested, or implement some LFSR in timer interrupt.
keepitsimple
Posts: 3
Joined: Sun Nov 20, 2022 1:27 am

Re: possible to direct access RNG registers of 32F411 via arduino-ide?

Post by keepitsimple »

Everyone, thank you for your input. I've got egg on my face and I'm embarrassed that I was trying to use a core that doesn't support RNG. I'm sorry I quoted the 32F411 as having an RNG, not sure what I was thinking. I've amazon'ed a 32F407 to experiment with and I'll report back once I've made progress. Once again thanks.
dannyf
Posts: 217
Joined: Sat Jul 04, 2020 7:46 pm

Re: possible to direct access RNG registers of 32F411 via arduino-ide?

Post by dannyf »

generating random numbers isn't that big of a deal. the RC phase shift is one, the adc is another.

and the W801/W806 has a TRNG / RNG peripheral, for $2.
dannyf
Posts: 217
Joined: Sat Jul 04, 2020 7:46 pm

Re: possible to direct access RNG registers of 32F411 via arduino-ide?

Post by dannyf »

all it takes is a wire connected to the ADC
no pin needed, actually.

My standard code pieces for random numbers using adc.

Code: Select all

//generate 4-bit random numbers
uint8_t arand4(void) {
	uint8_t tmp=arand1();
	
	//tmp = (tmp << 1) | arand1();
	tmp = (tmp << 1) | arand1();
	tmp = (tmp << 1) | arand1();
	tmp = (tmp << 1) | arand1();
	return tmp;
}

//generate 8-bit random number
uint8_t arand8(void) {
	return (arand4() << 4) | arand4();
}

//return 16-bit random number
uint16_t arand16(void) {
	return ((uint16_t) arand8() << 8) | arand8();
}


//return 32-bit random number
uint32_t arand32(void) {
	return ((uint32_t) arand16() << 16) | arand16();
}
all built on the arand1() macro. its implementation is platform dependent and on this chip (W801), it goes like this:

Code: Select all

#define ADC_ARAND	ADC_VREF			//read Vref for arand1()
#define arand1()	(analogRead(ADC_ARAND) & 0x01)	//the lsb for random number generation
Post Reply

Return to “General discussion”