[GD32L233C-START Review] 8. TRNG True Random Number Generation
[Copy link]
For previous posts, please refer to:
【GD32L233C-START Review】1. Unboxing
[GD32L233C-START Review] 2. Create a new project step by step
[GD32L233C-START Evaluation] 3. Porting FreeRTOS to GD32L233
【GD32L233C-START Review】4. Porting RT-Thread to GD32L233
【GD32L233C-START Review】5. IIC driving OLED
【GD32L233C-START Review】6. Get RTC time and display it through OLED
【GD32L233C-START Review】7. PWM LED Driver
The acquisition of true random numbers is sometimes very important in microcontroller development, especially in cryptography, data verification, and secure communication. This article will explain how to use the built-in random number generation module TRNG of the microcontroller to generate true random numbers.
1. Check the user manual
Refer to Chapter 9 of the User Manual for a detailed description of the principles of true random number generation.
When it comes to true random numbers, we mainly focus on the operation process, as shown below:
You need to enable the IRC48M clock and enable TRNGEN to turn on TRNG.
2. Code Editing
The code uses printf serial port to print the generated random number. You need to ensure that the serial port is available and the serial cable is connected.
1. TRNG status monitoring
Call trng_flag_get() function to get the status of TRNG
ErrStatus trng_ready_check(void)
{
uint32_t timeout = 0;
FlagStatus trng_flag = RESET;
ErrStatus reval = SUCCESS;
/* check wherther the random data is valid */
do
{
timeout++;
trng_flag = trng_flag_get(TRNG_FLAG_DRDY);
} while((RESET == trng_flag) && (0xFFFF > timeout));
if(RESET == trng_flag) {
/* ready check timeout */
printf("Error: TRNG can't ready \r\n");
trng_flag = trng_flag_get(TRNG_FLAG_CECS);
printf("Clock error current status: %d \r\n", trng_flag);
trng_flag = trng_flag_get(TRNG_FLAG_SECS);
printf("Seed error current status: %d \r\n", trng_flag);
reval = ERROR;
}
/* return check status */
return reval;
}
2. TRNG initialization configuration
Configure the TRNG clock, enable TRNG, and check whether the TRNG status is stable
ErrStatus trng_configuration(void)
{
ErrStatus reval = SUCCESS;
/* enable TRNG module clock */
rcu_periph_clock_enable(RCU_TRNG);
/* reset TRNG registers */
trng_deinit();
trng_enable();
/* check TRNG work status */
reval = trng_ready_check();
return reval;
}
3. Get random numbers
Call trng_get_true_random_data() to get the true random number and return the random value
uint8_t trng_random_range_get(uint8_t min, uint8_t max)
{
if(SUCCESS == trng_ready_check())
{
return (trng_get_true_random_data() % (max - min + 1) + min);
}
else
{
return 0;
}
}
4. main function
1) Initialize the 48M clock and wait for it to stabilize
2) Enable the serial port printf printing function
3) Initialize TRNG
4) Get a random number between 1 and 100, get it once every second and print it
int main(void)
{
uint8_t retry = 0;
/* configure systick */
systick_config();
/* turn on the oscillator */
rcu_osci_on(RCU_IRC48M);
if(ERROR == rcu_osci_stab_wait(RCU_IRC48M))
{
while(1);
}
gd_eval_com_init(EVAL_COM);
/* configure TRNG module */
while(ERROR == trng_configuration())
{
printf("TRNG init fail \r\n");
}
while(1)
{
printf("Generate random num1 is %d \r\n", trng_random_range_get(1, 100));
delay_1ms(1000);
}
}
3. Effect display
The serial port printing effect is as follows:
|