OpenCores
URL https://opencores.org/ocsvn/neorv32/neorv32/trunk

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [sw/] [lib/] [source/] [neorv32_trng.c] - Blame information for rev 23

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 zero_gravi
// #################################################################################################
2
// # << NEORV32: neorv32_trng.c - True Random Number Generator (TRNG) HW Driver >>                 #
3
// # ********************************************************************************************* #
4
// # BSD 3-Clause License                                                                          #
5
// #                                                                                               #
6
// # Copyright (c) 2020, Stephan Nolting. All rights reserved.                                     #
7
// #                                                                                               #
8
// # Redistribution and use in source and binary forms, with or without modification, are          #
9
// # permitted provided that the following conditions are met:                                     #
10
// #                                                                                               #
11
// # 1. Redistributions of source code must retain the above copyright notice, this list of        #
12
// #    conditions and the following disclaimer.                                                   #
13
// #                                                                                               #
14
// # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
15
// #    conditions and the following disclaimer in the documentation and/or other materials        #
16
// #    provided with the distribution.                                                            #
17
// #                                                                                               #
18
// # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
19
// #    endorse or promote products derived from this software without specific prior written      #
20
// #    permission.                                                                                #
21
// #                                                                                               #
22
// # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
23
// # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
24
// # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
25
// # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
26
// # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
27
// # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
28
// # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
29
// # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
30
// # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
31
// # ********************************************************************************************* #
32
// # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
33
// #################################################################################################
34
 
35
 
36
/**********************************************************************//**
37
 * @file neorv32_trng.c
38
 * @author Stephan Nolting
39
 * @brief True Random Number Generator (TRNG) HW driver source file.
40
 *
41
 * @note These functions should only be used if the TRNG unit was synthesized (IO_TRNG_USE = true).
42
 **************************************************************************/
43
 
44
#include "neorv32.h"
45
#include "neorv32_trng.h"
46
 
47
 
48
/**********************************************************************//**
49
 * Check if TRNG unit was synthesized.
50
 *
51
 * @return 0 if TRNG was not synthesized, 1 if TRNG is available.
52
 **************************************************************************/
53
int neorv32_trng_available(void) {
54
 
55 12 zero_gravi
  if (SYSINFO_FEATURES & (1 << SYSINFO_FEATURES_IO_TRNG)) {
56 2 zero_gravi
    return 1;
57
  }
58
  else {
59
    return 0;
60
  }
61
}
62
 
63
 
64
/**********************************************************************//**
65 23 zero_gravi
 * Enable true random number generator. The TRNG control register bits are listed in #NEORV32_TRNG_CT_enum.
66 2 zero_gravi
 **************************************************************************/
67 23 zero_gravi
void neorv32_trng_enable(void) {
68 2 zero_gravi
 
69 23 zero_gravi
  int i;
70
 
71 2 zero_gravi
  TRNG_CT = 0; // reset
72
 
73 23 zero_gravi
  for (i=0; i<1000; i++) {
74
    asm volatile ("nop");
75
  }
76 2 zero_gravi
 
77 23 zero_gravi
  TRNG_CT = 1 << TRNG_CT_EN; // activate
78 2 zero_gravi
 
79 23 zero_gravi
  for (i=0; i<1000; i++) {
80
    asm volatile ("nop");
81 2 zero_gravi
  }
82
}
83
 
84
 
85
/**********************************************************************//**
86
 * Disable true random number generator.
87
 **************************************************************************/
88
void neorv32_trng_disable(void) {
89
 
90
  TRNG_CT &= ~((uint32_t)(1 << TRNG_CT_EN));
91
}
92
 
93
 
94
/**********************************************************************//**
95
 * Get random data from TRNG.
96
 *
97 23 zero_gravi
 * @note The TRNG is automatically reset if a stuck-at-one/stuck-at-zero error is detected.
98
 *
99
 * @param[in,out] data uint8_t pointer for storing random data word
100
 * @return Data is valid when 0, stuck-at-zero error when 1, stuck-at-one error when 2, data not (yet) valid when 3
101 2 zero_gravi
 **************************************************************************/
102 23 zero_gravi
int neorv32_trng_get(uint8_t *data) {
103 2 zero_gravi
 
104 23 zero_gravi
  uint32_t trng_ct_reg = TRNG_CT;
105 2 zero_gravi
 
106 23 zero_gravi
  if (trng_ct_reg & (1<<TRNG_CT_ERROR_0)) { // stuck at zero error
107
    neorv32_trng_enable(); // reset TRNG
108
    return 1;
109
  }
110 2 zero_gravi
 
111 23 zero_gravi
  if (trng_ct_reg & (1<<TRNG_CT_ERROR_1)) { // stuck at one error
112
    neorv32_trng_enable(); // reset TRNG
113
    return 2;
114 2 zero_gravi
  }
115
 
116 23 zero_gravi
  if ((trng_ct_reg & (1<<TRNG_CT_VALID)) == 0) { // output data valid (yet)?
117
    return 3;
118 2 zero_gravi
  }
119
 
120 23 zero_gravi
  *data = (uint8_t)(trng_ct_reg >> TRNG_CT_DATA_LSB);
121
  return 0; // valid data
122 2 zero_gravi
}
123
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.