This section describes the programming interface of the TRNG Peripheral driver. The TRNG driver provides an easy way to initialize and configure the TRNG.
TRNG Initialization
-
Define the TRNG user configuration structure. Use TRNG_DRV_InitUserConfigDefault() function to set it to default TRNG configuration values.
-
Initialize the TRNG module, call the TRNG_DRV_Init() function and pass the user configuration structure. This function automatically enables the TRNG module and its clock. After that, the TRNG is enabled and the entropy generation starts working.
-
To disable the TRNG module, call the TRNG_DRV_Deinit() function.
Get random data from TRNG
-
TRNG_DRV_GetRandomData() function gets random data from the TRNG module.
This example code shows how to initialize and get random data from the TRNG driver:
TRNG interrupt
The TRNG interrupt is used only when the TRNG_DRV_RTOS_SLEEP configuration parameter is set to 1. It allows putting the calling RTOS task to "sleep" during the TRNG blocking entropy generation. The TRNG asserts an interrupt either when generated a Frequency Count Fail, Entropy Valid or Error Interrupt. By default it is enabled for RTOS and disabled for Bare Metal. It is optional and may be disabled.
-
Enable the TRNG interrupt with the TRNG_DRV_RTOS_SLEEP set to 1.
-
Configure the TRNG interrupt priority using NVIC_SetPriority(g_trngIrqId[0], <priority>).
-
Define the TRNG IRQ function, adding fsl_trng_irq.c file to the application project using the TRNG driver.
- Note
- If MQX RTOS is used, it requires a special installation of interrupt handlers. Add the following MQX RTOS interrupt handler installation to an application before the RTNG initialization:
|
TRNG_Type *const | g_trngBase [] |
| Table of base addresses for TRNG instances. More...
|
|
const IRQn_Type | g_trngIrqId [] |
| Table to save TRNG IRQ enumeration numbers defined in the CMSIS header file. More...
|
|
struct trng_user_config_t |
This structure initializes the TRNG by calling the the TRNG_DRV_Init() function. It contains all TRNG configurations.
bool trng_user_config_t::lock |
Default is kTRNGClockModeRingOscillator.
Default is kTRNGRingOscDiv0.
Default is kTRNGSampleModeRaw.
uint16_t trng_user_config_t::entropyDelay |
Defines the length (in system clocks) of each Entropy sample taken. Default is 3200.
uint16_t trng_user_config_t::sampleSize |
Defines the total number of Entropy samples that will be taken during Entropy generation. Default is 2500.
uint16_t trng_user_config_t::sparseBitLimit |
This limit is used only for During Von Neumann sampling (enabled by TRNG_HAL_SetSampleMode()). Samples are discarded if two consecutive raw samples are both 0 or both 1. If this discarding occurs for a long period of time, it indicates that there is insufficient Entropy. Default is 63.
uint8_t trng_user_config_t::retryCount |
It defines the number of times a statistical check may fails during the TRNG Entropy Generation before generating an error. Default is 1.
uint8_t trng_user_config_t::longRunMaxLimit |
#define TRNG_DRV_RTOS_MULTI_THREAD (USE_RTOS) |
It is achieved by mutex locking of critical sections. and is enabled by default for RTOS. It is optional and may be disabled if TRNG is used only by one RTOS task.
#define TRNG_DRV_RTOS_SLEEP (USE_RTOS) |
If enabled, it requires using of TRNG IRQ handler. and is enabled by default for RTOS. It is optional and may be disabled.
This function initializes the TRNG. When called, the TRNG runs immediately according to the specifications in the configuration.
- Parameters
-
instance | TRNG instance ID. |
user_config | Pointer to initialize configuration structure. |
- Returns
- If successful, returns the kStatus_TRNG_Success. Otherwise, it returns an error.
This function shuts down the TRNG.
- Parameters
-
- Returns
- If successful, returns the kStatus_TRNG_Success. Otherwise, it returns an error.
This function fills the user configuration structure with default TRNG settings.
- Parameters
-
user_config | User configuration structure. |
- Returns
- If successful, returns the kStatus_TRNG_Success. Otherwise, it returns an error.
trng_status_t TRNG_DRV_GetRandomData |
( |
uint32_t |
instance, |
|
|
void * |
data, |
|
|
uint32_t |
data_size |
|
) |
| |
This function gets random data from the TRNG.
- Parameters
-
instance | TRNG instance ID |
data | Pointer address used to store random data |
data_size | Size of the buffer pointed by the data parameter |
- Returns
- random data
void TRNG_DRV_IRQHandler |
( |
uint32_t |
instance | ) |
|
This function handles the TRNG interrupt.
- Parameters
-
TRNG_Type* const g_trngBase[] |
const IRQn_Type g_trngIrqId[] |