Microchip® Advanced Software Framework

Quick Start Guide for TRNG - Callback

In this use case, the True Random Number Generator (TRNG) module is configured for:

  • The TRNG peripheral will not be stopped in standby sleep mode

This use case will read random data in interrupt mode repeatly. After reading specific size of buffer data, the board LED will be toggled.

Setup

Prerequisites

There are no special setup requirements for this use-case.

Code

Copy-paste the following setup code to your user application:

bool volatile trng_read_done = false;
void configure_trng(void);
/* TRNG module software instance (must not go out of scope while in use) */
static struct trng_module trng_instance;
void configure_trng(void)
{
/* Create a new configuration structure for the TRNG settings
* and fill with the default module settings. */
struct trng_config config_trng;
trng_get_config_defaults(&config_trng);
/* Alter any TRNG configuration settings here if required */
/* Initialize TRNG with the user settings */
trng_init(&trng_instance, TRNG, &config_trng);
}
{
trng_read_done = true;
}
{
}

Add to user application initialization (typically the start of main()):

Workflow

  1. Create a TRNG device instance struct, which will be associated with a TRNG peripheral hardware instance.
    static struct trng_module trng_instance;
    Note
    Device instance structures shall never go out of scope when in use.
  2. Create a new function configure_trng(), which will be used to configure the overall TRNG peripheral.
    void configure_trng(void)
  3. Create a TRNG peripheral configuration structure that will be filled out to set the module configuration.
    struct trng_config config_trng;
  4. Fill the TRNG peripheral configuration structure with the default module configuration values.
    trng_get_config_defaults(&config_trng);
  5. Initialize the TRNG peripheral and associate it with the software instance structure that was defined previously.
    trng_init(&trng_instance, TRNG, &config_trng);
  6. Create a new callback function.
    {
    trng_read_done = true;
    }
  7. Create a callback status software flag.
    bool volatile trng_read_done = false;
  8. Let the callback function set the flag to true when read job done.
    trng_read_done = true;
  9. Create a new function configure_trng_callback(), which will be used to configure the callbacks.
  10. Register callback function.
  11. Enable the callbacks.
  12. Enable the now initialized TRNG peripheral.
    Note
    This should not be done until after the TRNG is set up and ready to be used.

Implementation

Code

Copy-paste the following code to your user application:

uint32_t random_buffer[5];
while (true) {
trng_read_buffer_job(&trng_instance, random_buffer, 5);
while (!trng_read_done) {
}
trng_read_done = false;
/* Add a short delay to see LED toggle */
volatile uint32_t delay = 50000;
while(delay--) {
}
}

Workflow

  1. Make the application loop infinitely.
    while (true) {
  2. Start an asynchronous TRNG read job, to store random data into the global buffer and generate a callback when complete.
    trng_read_buffer_job(&trng_instance, random_buffer, 5);
  3. Wait until the asynchronous read job is complete.
    while (!trng_read_done) {
    }
    trng_read_done = false;
  4. Toggle the board LED to indicate specific size of random data were read.
    /* Add a short delay to see LED toggle */
    volatile uint32_t delay = 50000;
    while(delay--) {
    }