Line 34... |
Line 34... |
|
|
|
|
/**********************************************************************//**
|
/**********************************************************************//**
|
* @file demo_trng/main.c
|
* @file demo_trng/main.c
|
* @author Stephan Nolting
|
* @author Stephan Nolting
|
* @brief TRNG demo program.
|
* @brief True random number generator demo program.
|
**************************************************************************/
|
**************************************************************************/
|
|
|
#include <neorv32.h>
|
#include <neorv32.h>
|
|
|
|
|
Line 49... |
Line 49... |
/** UART BAUD rate */
|
/** UART BAUD rate */
|
#define BAUD_RATE 19200
|
#define BAUD_RATE 19200
|
/**@}*/
|
/**@}*/
|
|
|
|
|
|
// prototypes
|
|
void print_random_data(void);
|
|
void generate_histogram(void);
|
|
|
|
|
/**********************************************************************//**
|
/**********************************************************************//**
|
* This program generates a simple dimming sequence for PWM channel 0,1,2.
|
* Simple true random number test/demo program.
|
*
|
*
|
* @note This program requires the UART and the TRNG to be synthesized.
|
* @note This program requires the UART and the TRNG to be synthesized.
|
*
|
*
|
* @return Irrelevant.
|
* @return Irrelevant.
|
**************************************************************************/
|
**************************************************************************/
|
int main(void) {
|
int main(void) {
|
|
|
uint8_t lucky_numbers_5of50[5];
|
|
uint8_t lucky_numbers_2of10[2];
|
|
|
|
int err;
|
|
uint8_t i, j, probe;
|
|
uint8_t trng_data;
|
|
unsigned int num_samples;
|
|
|
|
// check if UART unit is implemented at all
|
// check if UART unit is implemented at all
|
if (neorv32_uart_available() == 0) {
|
if (neorv32_uart_available() == 0) {
|
return 0;
|
return 0;
|
}
|
}
|
|
|
Line 100... |
Line 96... |
while(1) {
|
while(1) {
|
|
|
// main menu
|
// main menu
|
neorv32_uart_printf("\nCommands:\n"
|
neorv32_uart_printf("\nCommands:\n"
|
" n: Print 8-bit random numbers (abort by pressing any key)\n"
|
" n: Print 8-bit random numbers (abort by pressing any key)\n"
|
" l: Print your lucky numbers\n");
|
" h: Generate and print histogram\n");
|
|
|
neorv32_uart_printf("CMD:> ");
|
neorv32_uart_printf("CMD:> ");
|
char cmd = neorv32_uart_getc();
|
char cmd = neorv32_uart_getc();
|
neorv32_uart_putc(cmd); // echo
|
neorv32_uart_putc(cmd); // echo
|
neorv32_uart_printf("\n");
|
neorv32_uart_printf("\n");
|
|
|
// output RND data
|
|
if (cmd == 'n') {
|
if (cmd == 'n') {
|
num_samples = 0;
|
print_random_data();
|
|
}
|
|
else if (cmd == 'h') {
|
|
generate_histogram();
|
|
}
|
|
else {
|
|
neorv32_uart_printf("Invalid command.\n");
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
/**********************************************************************//**
|
|
* Print random numbers until a key is pressed.
|
|
**************************************************************************/
|
|
void print_random_data(void) {
|
|
|
|
uint32_t num_samples = 0;
|
|
int err = 0;
|
|
uint8_t trng_data;
|
|
|
while(1) {
|
while(1) {
|
err = neorv32_trng_get(&trng_data);
|
err = neorv32_trng_get(&trng_data);
|
if (err) {
|
if (err) {
|
neorv32_uart_printf("\nTRNG error (%i)!\n", err);
|
neorv32_uart_printf("\nTRNG error!\n");
|
break;
|
break;
|
}
|
}
|
neorv32_uart_printf("%u ", (uint32_t)(trng_data));
|
neorv32_uart_printf("%u ", (uint32_t)(trng_data));
|
num_samples++;
|
num_samples++;
|
if (neorv32_uart_char_received()) { // abort when key pressed
|
if (neorv32_uart_char_received()) { // abort when key pressed
|
neorv32_uart_printf("\nPrinted samples: %u", num_samples);
|
|
break;
|
break;
|
}
|
}
|
}
|
}
|
|
neorv32_uart_printf("\nPrinted samples: %u\n", num_samples);
|
}
|
}
|
|
|
// print lucky numbers
|
|
if (cmd == 'l') {
|
/**********************************************************************//**
|
// reset arrays
|
* Generate and print histogram. Samples random data until a key is pressed.
|
for (i=0; i<5; i++) {
|
**************************************************************************/
|
lucky_numbers_5of50[i] = 0;
|
void generate_histogram(void) {
|
}
|
|
lucky_numbers_2of10[0] = 0;
|
uint32_t hist[256];
|
lucky_numbers_2of10[1] = 0;
|
uint32_t i;
|
|
uint32_t cnt = 0;
|
// get numbers
|
int err = 0;
|
i = 0;
|
uint8_t trng_data;
|
while (i<5) {
|
|
err = neorv32_trng_get(&trng_data);
|
neorv32_uart_printf("Press any key to start.\n");
|
if (err) {
|
|
neorv32_uart_printf("\nTRNG error (%i)!\n", err);
|
while(neorv32_uart_char_received() == 0);
|
break;
|
neorv32_uart_printf("Sampling... Press any key to stop.\n");
|
}
|
|
// valid range?
|
// clear histogram
|
if ((trng_data == 0) || (trng_data > 50)) {
|
for (i=0; i<256; i++) {
|
continue;
|
hist[i] = 0;
|
}
|
|
// already sampled?
|
|
probe = 0;
|
|
for (j=0; j<5; j++) {
|
|
if (lucky_numbers_5of50[j] == trng_data) {
|
|
probe++;
|
|
}
|
|
}
|
|
if (probe) {
|
|
continue;
|
|
}
|
|
else {
|
|
lucky_numbers_5of50[i] = trng_data;
|
|
i++;
|
|
}
|
|
}
|
}
|
|
|
// get numbers part 2
|
// sample random data
|
i = 0;
|
while(1) {
|
while (i<2) {
|
|
err = neorv32_trng_get(&trng_data);
|
err = neorv32_trng_get(&trng_data);
|
|
hist[trng_data & 0xff]++;
|
|
cnt++;
|
|
|
if (err) {
|
if (err) {
|
neorv32_uart_printf("\nTRNG error (%i)!\n", err);
|
neorv32_uart_printf("\nTRNG error!\n");
|
break;
|
break;
|
}
|
}
|
// valid range?
|
|
if ((trng_data == 0) || (trng_data > 10)) {
|
|
continue;
|
|
}
|
|
// already sampled?
|
|
probe = 0;
|
|
for (j=0; j<2; j++) {
|
|
if (lucky_numbers_2of10[j] == trng_data) {
|
|
probe++;
|
|
}
|
|
}
|
|
if (probe) {
|
|
continue;
|
|
}
|
|
else {
|
|
lucky_numbers_2of10[i] = trng_data;
|
|
i++;
|
|
}
|
|
}
|
|
|
|
// output
|
if (neorv32_uart_char_received()) { // abort when key pressed
|
neorv32_uart_printf("\n");
|
break;
|
for (j=0; j<5; j++) {
|
|
if (i==4) {
|
|
neorv32_uart_printf("%u", (uint32_t)lucky_numbers_5of50[j]);
|
|
}
|
|
else {
|
|
neorv32_uart_printf("%u, ", (uint32_t)lucky_numbers_5of50[j]);
|
|
}
|
|
}
|
}
|
neorv32_uart_printf("\nLucky numbers: %u, %u\n", (uint32_t)lucky_numbers_2of10[0], (uint32_t)lucky_numbers_2of10[1]);
|
|
|
|
|
if (cnt & 0x80000000UL) { // to prevent overflow
|
|
break;
|
}
|
}
|
}
|
}
|
|
|
return 0;
|
// print histogram
|
|
neorv32_uart_printf("Histogram [random data value] : [# occurences]\n");
|
|
for (i=0; i<256; i++) {
|
|
neorv32_uart_printf("%u: %u\n", (uint32_t)i, hist[i]);
|
}
|
}
|
|
|
|
neorv32_uart_printf("\nSamples: %u\n", cnt);
|
|
|
|
// average
|
|
uint64_t average = 0;
|
|
for (i=0; i<256; i++) {
|
|
average += (uint64_t)hist[i] * i;
|
|
}
|
|
average = average / ((uint64_t)cnt);
|
|
neorv32_uart_printf("Average value: %u\n", (uint32_t)average);
|
|
}
|
|
|
No newline at end of file
|
No newline at end of file
|