// #################################################################################################
|
// #################################################################################################
|
// # << NEORV32 - TRNG Demo Program >> #
|
// # << NEORV32 - TRNG Demo Program >> #
|
// # ********************************************************************************************* #
|
// # ********************************************************************************************* #
|
// # BSD 3-Clause License #
|
// # BSD 3-Clause License #
|
// # #
|
// # #
|
// # Copyright (c) 2020, Stephan Nolting. All rights reserved. #
|
// # Copyright (c) 2020, Stephan Nolting. All rights reserved. #
|
// # #
|
// # #
|
// # Redistribution and use in source and binary forms, with or without modification, are #
|
// # Redistribution and use in source and binary forms, with or without modification, are #
|
// # permitted provided that the following conditions are met: #
|
// # permitted provided that the following conditions are met: #
|
// # #
|
// # #
|
// # 1. Redistributions of source code must retain the above copyright notice, this list of #
|
// # 1. Redistributions of source code must retain the above copyright notice, this list of #
|
// # conditions and the following disclaimer. #
|
// # conditions and the following disclaimer. #
|
// # #
|
// # #
|
// # 2. Redistributions in binary form must reproduce the above copyright notice, this list of #
|
// # 2. Redistributions in binary form must reproduce the above copyright notice, this list of #
|
// # conditions and the following disclaimer in the documentation and/or other materials #
|
// # conditions and the following disclaimer in the documentation and/or other materials #
|
// # provided with the distribution. #
|
// # provided with the distribution. #
|
// # #
|
// # #
|
// # 3. Neither the name of the copyright holder nor the names of its contributors may be used to #
|
// # 3. Neither the name of the copyright holder nor the names of its contributors may be used to #
|
// # endorse or promote products derived from this software without specific prior written #
|
// # endorse or promote products derived from this software without specific prior written #
|
// # permission. #
|
// # permission. #
|
// # #
|
// # #
|
// # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS #
|
// # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS #
|
// # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF #
|
// # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF #
|
// # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE #
|
// # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE #
|
// # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, #
|
// # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, #
|
// # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
|
// # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
|
// # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED #
|
// # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED #
|
// # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING #
|
// # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING #
|
// # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED #
|
// # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED #
|
// # OF THE POSSIBILITY OF SUCH DAMAGE. #
|
// # OF THE POSSIBILITY OF SUCH DAMAGE. #
|
// # ********************************************************************************************* #
|
// # ********************************************************************************************* #
|
// # The NEORV32 Processor - https://github.com/stnolting/neorv32 (c) Stephan Nolting #
|
// # The NEORV32 Processor - https://github.com/stnolting/neorv32 (c) Stephan Nolting #
|
// #################################################################################################
|
// #################################################################################################
|
|
|
|
|
/**********************************************************************//**
|
/**********************************************************************//**
|
* @file demo_trng/main.c
|
* @file demo_trng/main.c
|
* @author Stephan Nolting
|
* @author Stephan Nolting
|
* @brief TRNG demo program.
|
* @brief TRNG demo program.
|
**************************************************************************/
|
**************************************************************************/
|
|
|
#include <neorv32.h>
|
#include <neorv32.h>
|
|
|
|
|
/**********************************************************************//**
|
/**********************************************************************//**
|
* @name User configuration
|
* @name User configuration
|
**************************************************************************/
|
**************************************************************************/
|
/**@{*/
|
/**@{*/
|
/** UART BAUD rate */
|
/** UART BAUD rate */
|
#define BAUD_RATE 19200
|
#define BAUD_RATE 19200
|
/**@}*/
|
/**@}*/
|
|
|
|
|
|
|
/**********************************************************************//**
|
/**********************************************************************//**
|
* This program generates a simple dimming sequence for PWM channel 0,1,2.
|
* This program generates a simple dimming sequence for PWM channel 0,1,2.
|
*
|
*
|
* @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_5of50[5];
|
uint8_t lucky_numbers_2of10[2];
|
uint8_t lucky_numbers_2of10[2];
|
|
|
uint8_t i, j, tmp;
|
uint8_t i, j, tmp;
|
uint16_t trng_data;
|
uint16_t trng_data;
|
unsigned int num_samples;
|
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;
|
}
|
}
|
|
|
|
|
// 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_enable_debug_mode();
|
neorv32_rte_setup();
|
|
|
|
|
// init UART at default baud rate, no rx interrupt, no tx interrupt
|
// init UART at default baud rate, no rx interrupt, no tx interrupt
|
neorv32_uart_setup(BAUD_RATE, 0, 0);
|
neorv32_uart_setup(BAUD_RATE, 0, 0);
|
|
|
// intro
|
// intro
|
neorv32_uart_printf("\n--- TRNG Demo ---\n\n");
|
neorv32_uart_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_uart_printf("No TRNG implemented.");
|
return 0;
|
return 0;
|
}
|
}
|
|
|
// configure TRNG with defaul GARO tap mask
|
// configure TRNG with defaul GARO tap mask
|
neorv32_uart_printf("Configuring TRNG...\n");
|
neorv32_uart_printf("Configuring TRNG...\n");
|
uint16_t trng_tap_config = neorv32_trng_find_tap_mask();
|
uint16_t trng_tap_config = neorv32_trng_find_tap_mask();
|
neorv32_uart_printf("TRNG: Using tap mask: 0x%x ", (uint32_t)trng_tap_config);
|
neorv32_uart_printf("TRNG: Using tap mask: 0x%x ", (uint32_t)trng_tap_config);
|
neorv32_trng_setup(trng_tap_config);
|
neorv32_trng_setup(trng_tap_config);
|
|
|
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");
|
" l: Print your lucky numbers\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
|
// output RND data
|
if (cmd == 'n') {
|
if (cmd == 'n') {
|
num_samples = 0;
|
num_samples = 0;
|
while(1) {
|
while(1) {
|
if (neorv32_trng_get(&trng_data)) {
|
if (neorv32_trng_get(&trng_data)) {
|
neorv32_uart_printf("\nTRNG error!\n");
|
neorv32_uart_printf("\nTRNG error!\n");
|
break;
|
break;
|
}
|
}
|
neorv32_uart_printf("%u ", (uint32_t)(trng_data & 0xFF));
|
neorv32_uart_printf("%u ", (uint32_t)(trng_data & 0xFF));
|
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);
|
neorv32_uart_printf("\nPrinted samples: %u", num_samples);
|
break;
|
break;
|
}
|
}
|
}
|
}
|
}
|
}
|
|
|
// print lucky numbers
|
// print lucky numbers
|
if (cmd == 'l') {
|
if (cmd == 'l') {
|
// reset array
|
// reset array
|
for (i=0; i<5; i++) {
|
for (i=0; i<5; i++) {
|
lucky_numbers_5of50[i] = 0;
|
lucky_numbers_5of50[i] = 0;
|
}
|
}
|
lucky_numbers_2of10[0] = 0;
|
lucky_numbers_2of10[0] = 0;
|
lucky_numbers_2of10[1] = 0;
|
lucky_numbers_2of10[1] = 0;
|
|
|
// get numbers
|
// get numbers
|
i = 0;
|
i = 0;
|
while (i<5) {
|
while (i<5) {
|
if (neorv32_trng_get(&trng_data)) {
|
if (neorv32_trng_get(&trng_data)) {
|
neorv32_uart_printf("\nTRNG error!\n");
|
neorv32_uart_printf("\nTRNG error!\n");
|
break;
|
break;
|
}
|
}
|
// valid range?
|
// valid range?
|
tmp = (uint8_t)(trng_data & 0xff);
|
tmp = (uint8_t)(trng_data & 0xff);
|
if ((tmp == 0) || (tmp > 50)) {
|
if ((tmp == 0) || (tmp > 50)) {
|
continue;
|
continue;
|
}
|
}
|
// already sampled?
|
// already sampled?
|
for (j=0; j<5; j++) {
|
for (j=0; j<5; j++) {
|
if (lucky_numbers_5of50[j] == tmp) {
|
if (lucky_numbers_5of50[j] == tmp) {
|
continue;
|
continue;
|
}
|
}
|
}
|
}
|
lucky_numbers_5of50[i] = tmp;
|
lucky_numbers_5of50[i] = tmp;
|
i++;
|
i++;
|
}
|
}
|
|
|
// get numbers part 2
|
// get numbers part 2
|
i = 0;
|
i = 0;
|
while (i<2) {
|
while (i<2) {
|
if (neorv32_trng_get(&trng_data)) {
|
if (neorv32_trng_get(&trng_data)) {
|
neorv32_uart_printf("\nTRNG error!\n");
|
neorv32_uart_printf("\nTRNG error!\n");
|
break;
|
break;
|
}
|
}
|
// valid range?
|
// valid range?
|
tmp = (uint8_t)(trng_data & 0xff);
|
tmp = (uint8_t)(trng_data & 0xff);
|
if ((tmp == 0) || (tmp > 10)) {
|
if ((tmp == 0) || (tmp > 10)) {
|
continue;
|
continue;
|
}
|
}
|
// already sampled?
|
// already sampled?
|
for (j=0; j<2; j++) {
|
for (j=0; j<2; j++) {
|
if (lucky_numbers_2of10[j] == tmp) {
|
if (lucky_numbers_2of10[j] == tmp) {
|
continue;
|
continue;
|
}
|
}
|
}
|
}
|
lucky_numbers_2of10[i] = tmp;
|
lucky_numbers_2of10[i] = tmp;
|
i++;
|
i++;
|
}
|
}
|
|
|
// output
|
// output
|
neorv32_uart_printf("\n");
|
neorv32_uart_printf("\n");
|
for (j=0; j<5; j++) {
|
for (j=0; j<5; j++) {
|
if (i==4) {
|
if (i==4) {
|
neorv32_uart_printf("%u", (uint32_t)lucky_numbers_5of50[j]);
|
neorv32_uart_printf("%u", (uint32_t)lucky_numbers_5of50[j]);
|
}
|
}
|
else {
|
else {
|
neorv32_uart_printf("%u, ", (uint32_t)lucky_numbers_5of50[j]);
|
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]);
|
neorv32_uart_printf("\nLucky numbers: %u, %u\n", (uint32_t)lucky_numbers_2of10[0], (uint32_t)lucky_numbers_2of10[1]);
|
|
|
}
|
}
|
}
|
}
|
|
|
return 0;
|
return 0;
|
}
|
}
|
|
|
|
|