2.90 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

NameDescription
RNG_InitializeInitializes RNG module of the device
RNG_TrngEnableEnables the TRNG block inside the RNG peripheral
RNG_TrngDisableDisables the TRNG block inside the RNG peripheral
RNG_WaitForTrngCntWaits for RNGCNT to reach the number of bits required for the PRNG seed
RNG_Seed1GetReturns the content of RNGSEED1 register
RNG_Seed2GetReturns the content of RNGSEED2 register
RNG_PrngEnableEnables the PRNG block inside the RNG peripheral
RNG_PrngDisableDisables the PRNG block inside the RNG peripheral
RNG_LoadSetSets the LOAD bit in RNGCON register
RNG_LoadGetReturns the LOAD bit value in RNGCON register
RNG_Poly1SetLoads a value into the RNGPOLY1 register
RNG_Poly2SetLoads a value into the RNGPOLY2 register
RNG_Poly1GetReturns the value of the RNGPOLY1 register
RNG_Poly2GetReturns the value of the RNGPOLY2 register
RNG_NumGen1SetLoads a value into the RNGNUMGEN1 register
RNG_NumGen2SetLoads a value into the RNGNUMGEN2 register
RNG_NumGen1GetReturns the value in the RNGNUMGEN1 register
RNG_NumGen2GetReturns the value in the RNGNUMGEN2 register
Note: Not all APIs maybe implemented. See the specific device family section for available APIs.