Line 64... |
Line 64... |
* @return 0 if execution was successful
|
* @return 0 if execution was successful
|
**************************************************************************/
|
**************************************************************************/
|
int main(void) {
|
int main(void) {
|
|
|
// check if UART unit is implemented at all
|
// check if UART unit is implemented at all
|
if (neorv32_uart_available() == 0) {
|
if (neorv32_uart0_available() == 0) {
|
return 1;
|
return 1;
|
}
|
}
|
|
|
// capture all exceptions and give debug info via UART
|
// capture all exceptions and give debug info via UART
|
// this is not required, but keeps us safe
|
// this is not required, but keeps us safe
|
neorv32_rte_setup();
|
neorv32_rte_setup();
|
|
|
|
|
// init UART at default baud rate, no parity bits, ho hw flow control
|
// init UART at default baud rate, no parity bits, ho hw flow control
|
neorv32_uart_setup(BAUD_RATE, PARITY_NONE, FLOW_CONTROL_NONE);
|
neorv32_uart0_setup(BAUD_RATE, PARITY_NONE, FLOW_CONTROL_NONE);
|
|
|
// check available hardware extensions and compare with compiler flags
|
// check available hardware extensions and compare with compiler flags
|
neorv32_rte_check_isa(0); // silent = 0 -> show message if isa mismatch
|
neorv32_rte_check_isa(0); // silent = 0 -> show message if isa mismatch
|
|
|
// intro
|
// intro
|
neorv32_uart_printf("\n--- TRNG Demo ---\n\n");
|
neorv32_uart0_printf("\n--- TRNG Demo ---\n\n");
|
|
|
// check if TRNG unit is implemented at all
|
// check if TRNG unit is implemented at all
|
if (neorv32_trng_available() == 0) {
|
if (neorv32_trng_available() == 0) {
|
neorv32_uart_printf("No TRNG implemented.");
|
neorv32_uart0_printf("No TRNG implemented.");
|
return 1;
|
return 1;
|
}
|
}
|
|
|
// enable TRNG
|
// enable TRNG
|
neorv32_trng_enable();
|
neorv32_trng_enable();
|
|
|
while(1) {
|
while(1) {
|
|
|
// main menu
|
// main menu
|
neorv32_uart_printf("\nCommands:\n"
|
neorv32_uart0_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"
|
" h: Generate and print histogram\n");
|
" h: Generate and print histogram\n");
|
|
|
neorv32_uart_printf("CMD:> ");
|
neorv32_uart0_printf("CMD:> ");
|
char cmd = neorv32_uart_getc();
|
char cmd = neorv32_uart0_getc();
|
neorv32_uart_putc(cmd); // echo
|
neorv32_uart0_putc(cmd); // echo
|
neorv32_uart_printf("\n");
|
neorv32_uart0_printf("\n");
|
|
|
if (cmd == 'n') {
|
if (cmd == 'n') {
|
print_random_data();
|
print_random_data();
|
}
|
}
|
else if (cmd == 'h') {
|
else if (cmd == 'h') {
|
generate_histogram();
|
generate_histogram();
|
}
|
}
|
else {
|
else {
|
neorv32_uart_printf("Invalid command.\n");
|
neorv32_uart0_printf("Invalid command.\n");
|
}
|
}
|
}
|
}
|
|
|
return 0;
|
return 0;
|
}
|
}
|
Line 130... |
Line 130... |
uint8_t trng_data;
|
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!\n");
|
neorv32_uart0_printf("\nTRNG error!\n");
|
break;
|
break;
|
}
|
}
|
neorv32_uart_printf("%u ", (uint32_t)(trng_data));
|
neorv32_uart0_printf("%u ", (uint32_t)(trng_data));
|
num_samples++;
|
num_samples++;
|
if (neorv32_uart_char_received()) { // abort when key pressed
|
if (neorv32_uart0_char_received()) { // abort when key pressed
|
break;
|
break;
|
}
|
}
|
}
|
}
|
neorv32_uart_printf("\nPrinted samples: %u\n", num_samples);
|
neorv32_uart0_printf("\nPrinted samples: %u\n", num_samples);
|
}
|
}
|
|
|
|
|
/**********************************************************************//**
|
/**********************************************************************//**
|
* Generate and print histogram. Samples random data until a key is pressed.
|
* Generate and print histogram. Samples random data until a key is pressed.
|
Line 154... |
Line 154... |
uint32_t i;
|
uint32_t i;
|
uint32_t cnt = 0;
|
uint32_t cnt = 0;
|
int err = 0;
|
int err = 0;
|
uint8_t trng_data;
|
uint8_t trng_data;
|
|
|
neorv32_uart_printf("Press any key to start.\n");
|
neorv32_uart0_printf("Press any key to start.\n");
|
|
|
while(neorv32_uart_char_received() == 0);
|
while(neorv32_uart0_char_received() == 0);
|
neorv32_uart_printf("Sampling... Press any key to stop.\n");
|
neorv32_uart0_printf("Sampling... Press any key to stop.\n");
|
|
|
// clear histogram
|
// clear histogram
|
for (i=0; i<256; i++) {
|
for (i=0; i<256; i++) {
|
hist[i] = 0;
|
hist[i] = 0;
|
}
|
}
|
Line 172... |
Line 172... |
err = neorv32_trng_get(&trng_data);
|
err = neorv32_trng_get(&trng_data);
|
hist[trng_data & 0xff]++;
|
hist[trng_data & 0xff]++;
|
cnt++;
|
cnt++;
|
|
|
if (err) {
|
if (err) {
|
neorv32_uart_printf("\nTRNG error!\n");
|
neorv32_uart0_printf("\nTRNG error!\n");
|
break;
|
break;
|
}
|
}
|
|
|
if (neorv32_uart_char_received()) { // abort when key pressed
|
if (neorv32_uart0_char_received()) { // abort when key pressed
|
break;
|
break;
|
}
|
}
|
|
|
if (cnt & 0x80000000UL) { // to prevent overflow
|
if (cnt & 0x80000000UL) { // to prevent overflow
|
break;
|
break;
|
}
|
}
|
}
|
}
|
|
|
// print histogram
|
// print histogram
|
neorv32_uart_printf("Histogram [random data value] : [# occurences]\n");
|
neorv32_uart0_printf("Histogram [random data value] : [# occurences]\n");
|
for (i=0; i<256; i++) {
|
for (i=0; i<256; i++) {
|
neorv32_uart_printf("%u: %u\n", (uint32_t)i, hist[i]);
|
neorv32_uart0_printf("%u: %u\n", (uint32_t)i, hist[i]);
|
}
|
}
|
|
|
neorv32_uart_printf("\nSamples: %u\n", cnt);
|
neorv32_uart0_printf("\nSamples: %u\n", cnt);
|
|
|
// average
|
// average
|
uint64_t average = 0;
|
uint64_t average = 0;
|
for (i=0; i<256; i++) {
|
for (i=0; i<256; i++) {
|
average += (uint64_t)hist[i] * i;
|
average += (uint64_t)hist[i] * i;
|
}
|
}
|
average = average / ((uint64_t)cnt);
|
average = average / ((uint64_t)cnt);
|
neorv32_uart_printf("Average value: %u\n", (uint32_t)average);
|
neorv32_uart0_printf("Average value: %u ", (uint32_t)average);
|
|
|
|
if (((uint8_t)average) == ((uint8_t)(255/2))) {
|
|
neorv32_uart0_printf("%c[1m[TEST OK]%c[0m\n", 27, 27);
|
|
}
|
|
else {
|
|
neorv32_uart0_printf("%c[1m[TEST FAILED]%c[0m\n", 27, 27);
|
|
}
|
|
|
}
|
}
|
|
|
No newline at end of file
|
No newline at end of file
|