CMSIS-Driver_PIC32CZ-CA70  
Peripheral Library (PLIB) Documentation
 
Loading...
Searching...
No Matches
TRNG Interface

Driver API for TRNG (plib_trng.h) More...

Typedefs

typedef void(* TRNG_CALLBACK) (uint32_t random_value)
 Function pointer type for the TRNG Data Ready callback.
 

Functions

void TRNG_Enable (void)
 Enables the TRNG peripheral.
 
void TRNG_Disable (void)
 Disables the TRNG peripheral.
 
bool TRNG_DataIsReady (void)
 Checks whether a new random value is available.
 
bool TRNG_GetRandomNumber (uint32_t *data)
 Reads a random 32-bit value from the TRNG peripheral.
 
void TRNG_EnableInterrupt (void)
 Enables the TRNG Data Ready interrupt.
 
void TRNG_DisableInterrupt (void)
 Disables the TRNG Data Ready interrupt.
 
bool TRNG_InterruptIsEnabled (void)
 Checks whether the Data Ready interrupt is enabled.
 
void TRNG_SetCallbackHandler (TRNG_CALLBACK callback)
 Registers the callback invoked from TRNG_Handler() on Data Ready.
 

Description

Driver API for TRNG (plib_trng.h)

Usage Example

The following example demonstrates typical usage of the TRNG peripheral:

/*******************************************************************************
* @file configure_trng.c
* @brief TRNG interrupt-driven configuration template
*
* @details
* The TRNG is a hardware entropy source with no seed and no configurable
* parameters: software only controls whether the peripheral is running
* (TRNG_CR.ENABLE) and whether Data Ready raises an interrupt (TRNG_IER).
*
* In this template:
* - The callback is registered and the Data Ready interrupt is armed
* before the peripheral is enabled, so no word can be missed.
* - No warm-up delay is added: every read path gates on TRNG_ISR.DATRDY,
* which cannot assert before TRNGWUP has elapsed.
* - Data Ready re-asserts every ~84 clock cycles until disabled. The
* callback only stores the word; the application calls
* TRNG_DisableInterrupt() when it has collected enough.
* - The NVIC line is left masked; the application enables it (see
* app_plib.c) so it owns the TRNG interrupt priority.
******************************************************************************/
#include "plib_trng.h"
#include <stdint.h>
#include <stdbool.h>
/* Raised by the callback for the application to consume. */
volatile bool trng_data_ready = false;
volatile uint32_t trng_last_value = 0U;
/* Data Ready callback, invoked from TRNG_Handler() in interrupt context */
void TRNG_DataReadyCallback(uint32_t random_value)
{
trng_last_value = random_value;
trng_data_ready = true;
}
int32_t configure_trng(void)
{
/* Start from a known state. */
/* Arm the callback and Data Ready before the peripheral runs. */
TRNG_SetCallbackHandler(&TRNG_DataReadyCallback);
/* Start the entropy source. */
return 0;
}
void TRNG_SetCallbackHandler(TRNG_CALLBACK callback)
Registers the callback invoked from TRNG_Handler() on Data Ready.
void TRNG_DisableInterrupt(void)
Disables the TRNG Data Ready interrupt.
void TRNG_Enable(void)
Enables the TRNG peripheral.
void TRNG_Disable(void)
Disables the TRNG peripheral.
void TRNG_EnableInterrupt(void)
Enables the TRNG Data Ready interrupt.

Typedef Documentation

◆ TRNG_CALLBACK

typedef void(* TRNG_CALLBACK) (uint32_t random_value)

Function pointer type for the TRNG Data Ready callback.

Parameters
random_valueThe 32-bit random value read from TRNG_ODATA.

Function Documentation

◆ TRNG_DataIsReady()

bool TRNG_DataIsReady ( void )

Checks whether a new random value is available.

Returns
true if a random value is ready to be read, false otherwise.
Note
TRNG_ISR.DATRDY is cleared by hardware on read. TRNG_DataIsReady() and TRNG_GetRandomNumber() share an internal latch so a pending value is never lost between them.

◆ TRNG_Disable()

void TRNG_Disable ( void )

Disables the TRNG peripheral.

Returns
None.

◆ TRNG_DisableInterrupt()

void TRNG_DisableInterrupt ( void )

Disables the TRNG Data Ready interrupt.

Returns
None.

◆ TRNG_Enable()

void TRNG_Enable ( void )

Enables the TRNG peripheral.

Returns
None.
Note
Wait for the TRNG Warm-Up Time (TRNGWUP), per the AC electrical timings, before reading random data.

◆ TRNG_EnableInterrupt()

void TRNG_EnableInterrupt ( void )

Enables the TRNG Data Ready interrupt.

Returns
None.
Note
Fires every 84 clock cycles until disabled; TRNG_Handler() does not disable it automatically. Register a callback first, or each word is discarded. Do not call TRNG_GetRandomNumber() while enabled; both read TRNG_ISR destructively.

◆ TRNG_GetRandomNumber()

bool TRNG_GetRandomNumber ( uint32_t * data)

Reads a random 32-bit value from the TRNG peripheral.

Parameters
[out]dataPointer to store the random value.
Returns
true if a value was read into *data, false if data is NULL or no value was ready.

◆ TRNG_InterruptIsEnabled()

bool TRNG_InterruptIsEnabled ( void )

Checks whether the Data Ready interrupt is enabled.

Returns
true if TRNG_IMR.DATRDY is set, false otherwise.

◆ TRNG_SetCallbackHandler()

void TRNG_SetCallbackHandler ( TRNG_CALLBACK callback)

Registers the callback invoked from TRNG_Handler() on Data Ready.

Parameters
callbackPointer to the callback function (NULL to clear).
Returns
None.