Jump to main content

##

# 3.31.24 True Random Number Generator (TRNG)

As soon as the TRNG is enabled, the module provides a new 32-bit random data, for every 84 peripheral clock cycles.

The TRNG can optionally generate an interrupt when a new random value is available.

## Using The Library

The library provides both blocking and callback methods to generate and read the random number.

- With blocking API, it enables the TRNG module, waits for new random number to be available and then returns the newly generated random number.

- With callback, it enables the TRNG module and the registered callback function will be called once the new random value is available.

Blocking API

The example code to generate 32-bit random number using blocking API is given below.

    uint32_t trueRandomNumber

    trueRandomNumber = TRNG_ReadData();

Callback method

This example demonstrates how to use callback to generate 32-bit random number.

    uint32_t trueRandomNumber;

    /* Random number is provided as part of callback */
    void TRNG_EventHandler(uint32_t randomNumber, uintptr_t context)
    {
        trueRandomNumber = randomNumber;
    }

    int main(void)
    {
        /* Register Callback */
        TRNG_CallbackRegister(TRNG_EventHandler, (uintptr_t) NULL);

        /* Enable TRNG */
        TRNG_RandomNumberGenerate();
    }

## Library Interface

True Random Number Generator peripheral library provides the following interfaces:

Functions

Name | Description
--- | ---
TRNG_RandomNumberGenerate | Initiates generation of random number using the TRNG peripheral in interrupt mode
TRNG_ReadData | Generates and returns a 32-bit random number in blocking mode
TRNG_CallbackRegister | Sets the pointer to the function (and it is context) to be called when the random number is ready to be read

Data types and constants

Name | Type | Description
--- | --- | ---
TRNG_CALLBACK | Typedef | Defines the data type and function signature for the TRNG peripheral callback function