3484 views|0 replies

282

Posts

2

Resources
The OP
 

[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:

This post is from GD32 MCU
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Featured Posts
Studying Things to Gain Knowledge 05: Dimensions and Elementary Functions

What is dimension? It is usually taught in middle school physics courses and again in university physics courses. Howev ...

【McQueen Trial】Comparison of the accuracy of several ultrasonic sensor programs of McQueen

Purpose Compare the test accuracy of several ultrasonic sensors to provide a reference for everyone's use. Methods Write ...

[Project source code] Digital tube companion - binary to BCD

This article and design code were written by FPGA enthusiast Xiao Meige. Without the author's permission, this article ...

Design of real-time image test device based on LVDS technology

Design of real-time image test device based on LVDS technology

C language uses binary tree to parse polynomials and evaluate

It mainly realizes the analysis of polynomial data calculation. If there is a need to make a simple calculator based on ...

LPS27HHTW MEMS Pressure Sensor

The LPS27HHTW MEMS pressure sensor is an ultra-compact piezoresistive absolute pressure sensor that can be used as a dig ...

Raspberry Pi Bluetooth Basics - Use of bluezero library

This post was last edited by lb8820265 on 2021-8-8 22:50 There are many libraries for operating Bluetooth in Linux s ...

CC2500 Migration Instructions

Be applicable : Applicable to TI chip CC2500 full series modules How to transplant ? The parts to be ported are in rf_ ...

Embedded Qt-Make a stopwatch

This post was last edited by DDZZ669 on 2022-8-7 15:55 Previous article: Embedded Qt - Write and run your first ARM-Qt ...

Successfully deployed deep learning gait recognition algorithm on Allwinner V853 platform

Xin Zhe, a student from the Communication Research Group of Beijing Institute of Technology, successfully transplanted t ...

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list