Skip to main content
DS.4
Senior
March 10, 2025
Solved

When I get connected and paired to peer HAL RNG doesn' work

  • March 10, 2025
  • 2 replies
  • 634 views

Having issues generating random numbers while being connected via BLE.

In general use, HAL RNG works fine, and I get a random number without timeouts.

BUT,

Upon finishing the connection flow,

connection mtu exchange pairing ,

I start to get timeouts when trying to generate a new random number.

Increasing the timeout , initializing,  did not resolve the issue.

I am currently trying to use HCI_RND command instead in the hopes it will get me my random number.

If it helps, 

I am on one of the latest BLE stacks 1.20 or 1.21


Cheers,

and let us all have random numbers when connected!

Best answer by STTwo-32

The HCI_LE_RAND can be used to request the controller to generate eight octets of random data to send to the host. You may call it to generate random data. the Random_Number is generated according to [Vol 2] Part H, Section 2. To be able to use it, the LE feature (LL encryption) should be supported. More details are available on the Bluetooth spec. v.6.0 [Vol 4, Part E, 7.8.23].

Best Regards.

STTwo-32

2 replies

STTwo-32
Technical Moderator
March 10, 2025

Hello @DS.4 

As soon as the CPU2 uses the RNG, it will switch OFF the RNG source clock when
done.

You need to re enable the clock each time you want to use the RNG.
Moreover, when you are done with RNG, you need to restore RNGSEL to CLK48 so that it is ready for BLE.

You can review Section 4.3 Fig 8 of AN5289 Rev 18 for a description about this. I tried this procedure when the device is in connected mode, and it works and allows me to use the RNG from CPU1. You may try this snippet code that was tested before by a colleague on a similar case:

 

 

while( LL_HSEM_1StepLock( HSEM, CFG_HW_RNG_SEMID ) ); //Poll Sem0 until granted
MX_RNG_Init(); //Configure and switch on RNG clock

/* Generate eight 32-bit long random numbers */
for (counter = 0; counter < 8; counter++)
{
if (HAL_RNG_GenerateRandomNumber(&hrng, &aRandom32bit[counter]) != HAL_OK) //RNG process
{
/* Random number generation error */
Error_Handler();
}
}

HAL_RNG_DeInit(&hrng); //Switch off RNG IP
__HAL_RCC_RNG_CLK_DISABLE(); //Switch off RNG clock

LL_HSEM_ReleaseLock( HSEM, CFG_HW_RNG_SEMID, 0 ); //Release Sem0

 

 

Best Regards.

STTwo-32

DS.4
DS.4Author
Senior
March 10, 2025

Should HCI_RND work out of the box? I may even ask Always?

 

I find it could be an easier solution in my case.

 

Thanks again

STTwo-32
STTwo-32Best answer
Technical Moderator
March 10, 2025

The HCI_LE_RAND can be used to request the controller to generate eight octets of random data to send to the host. You may call it to generate random data. the Random_Number is generated according to [Vol 2] Part H, Section 2. To be able to use it, the LE feature (LL encryption) should be supported. More details are available on the Bluetooth spec. v.6.0 [Vol 4, Part E, 7.8.23].

Best Regards.

STTwo-32