Jump to main content

##

# 3.26.22 Random Number Generator (RNG)

The RNG peripheral contains two internal blocks namely TRNG (True Random Number Generator) and PRNG (Pseudo-Random Number Generator).

TRNG can be used for generating seed for the PRNG. The peripheral can generate random numbers of up to 64-bits length. This peripheral does not have interrupt generation capability.

## Using The Library

RNG Peripheral library provides API's that can be used to perform below functionalities on the RNG peripheral.

- Initialize the RNG

- Enable/Disable the TRNG and PRNG blocks inside the RNG peripheral

- Read/Write the seed, polynomial and the generated random number.

- To check whether the number of valid bits in RNGSEEDx reached the required bit length for PRNG seed.

The example code below demonstrates how to generate True Random Number and Pseudo Random Number using RNG peripheral.

    uint32_t true_rand_num[2];
    uint32_t pseudo_rand_num[2];

    int main ( void )
    {
        /* Initialize all modules */
        SYS_Initialize ( NULL );

        /* Enable TRNG */
        RNG_TrngEnable();

        RNG_WaitForTrngCnt();

        true_rand_num[0] = RNG_Seed1Get();
        true_rand_num[1] = RNG_Seed2Get();

        printf("\r\nSeed available in TRNG 0x%x%x", true_rand_num[1], true_rand_num[0]);

        /* Prepare PRNG to get seed from TRNG */
        RNG_LoadSet();
        RNG_Poly1Set(0x00C00003);
        RNG_PrngEnable();

        /* Wait for at least 64 clock cycles */
        CORETIMER_DelayUs(1);

        pseudo_rand_num[0] = RNG_NumGen1Get();
        pseudo_rand_num[1] = RNG_NumGen2Get();

        RNG_PrngDisable();

        printf("\r\nGenerated 64-bit Pseudo-Random Number 0x%x%x\r\n", pseudo_rand_num[1], pseudo_rand_num[0]);

        while(true)
        {
        }

        /* Execution should not come here during normal operation */

        return ( EXIT_FAILURE );
    }

## Library Interface

Random Number Generator peripheral library provides the following interfaces:

Functions

Name | Description
--- | ---
RNG_Initialize | Initializes RNG module of the device
RNG_TrngEnable | Enables the TRNG block inside the RNG peripheral
RNG_TrngDisable | Disables the TRNG block inside the RNG peripheral
RNG_WaitForTrngCnt | Waits for RNGCNT to reach the number of bits required for the PRNG seed
RNG_Seed1Get | Returns the content of RNGSEED1 register
RNG_Seed2Get | Returns the content of RNGSEED2 register
RNG_PrngEnable | Enables the PRNG block inside the RNG peripheral
RNG_PrngDisable | Disables the PRNG block inside the RNG peripheral
RNG_LoadSet | Sets the LOAD bit in RNGCON register
RNG_LoadGet | Returns the LOAD bit value in RNGCON register
RNG_Poly1Set | Loads a value into the RNGPOLY1 register
RNG_Poly2Set | Loads a value into the RNGPOLY2 register
RNG_Poly1Get | Returns the value of the RNGPOLY1 register
RNG_Poly2Get | Returns the value of the RNGPOLY2 register
RNG_NumGen1Set | Loads a value into the RNGNUMGEN1 register
RNG_NumGen2Set | Loads a value into the RNGNUMGEN2 register
RNG_NumGen1Get | Returns the value in the RNGNUMGEN1 register
RNG_NumGen2Get | Returns the value in the RNGNUMGEN2 register