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);
void configure_trng_callback(void);
void trng_complete_callback(
struct trng_module *
const module_inst);
void configure_trng(void)
{
trng_init(&trng_instance, TRNG, &config_trng);
}
void trng_complete_callback(
struct trng_module *
const module_inst)
{
trng_read_done = true;
}
void configure_trng_callback(void)
{
}
Add to user application initialization (typically the start of main()
):
configure_trng();
configure_trng_callback();
Workflow
- Create a TRNG device instance struct, which will be associated with a TRNG peripheral hardware instance.
- Note
- Device instance structures shall never go out of scope when in use.
- Create a new function
configure_trng()
, which will be used to configure the overall TRNG peripheral. void configure_trng(void)
- Create a TRNG peripheral configuration structure that will be filled out to set the module configuration.
- Fill the TRNG peripheral configuration structure with the default module configuration values.
- Initialize the TRNG peripheral and associate it with the software instance structure that was defined previously.
trng_init(&trng_instance, TRNG, &config_trng);
- Create a new callback function.
void trng_complete_callback(
struct trng_module *
const module_inst)
{
trng_read_done = true;
}
- Create a callback status software flag.
bool volatile trng_read_done = false;
- Let the callback function set the flag to true when read job done.
- Create a new function
configure_trng_callback()
, which will be used to configure the callbacks. void configure_trng_callback(void)
{
}
- Register callback function.
- Enable the callbacks.
- 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) {
while (!trng_read_done) {
}
trng_read_done = false;
volatile uint32_t delay = 50000;
while(delay--) {
}
}
Workflow
- Make the application loop infinitely.
- Start an asynchronous TRNG read job, to store random data into the global buffer and generate a callback when complete.
- Wait until the asynchronous read job is complete.
while (!trng_read_done) {
}
trng_read_done = false;
- Toggle the board LED to indicate specific size of random data were read.
volatile uint32_t delay = 50000;
while(delay--) {
}