| M480 BSP V3.05.001 The Board Support Package for M480 Series

trng.c
Go to the documentation of this file.
1 /**************************************************************************/
10 #include <stdio.h>
11
12 #include "NuMicro.h"
13
14
33 void TRNG_Open(void)
34 {
35 SYS->IPRST1 |= SYS_IPRST1_TRNGRST_Msk;
36 SYS->IPRST1 ^= SYS_IPRST1_TRNGRST_Msk;
37
38 TRNG->CTL |= TRNG_CTL_TRNGEN_Msk;
39
40 TRNG->ACT |= TRNG_ACT_ACT_Msk;
41
42 /* Waiting for ready */
43 while ((TRNG->CTL & TRNG_CTL_READY_Msk) == 0);
44 }
45
46
55 int32_t TRNG_GenWord(uint32_t *u32RndNum)
56 {
57 uint32_t i, u32Reg, timeout;
58
59 *u32RndNum = 0;
60 u32Reg = TRNG->CTL;
61
62 for (i = 0; i < 4; i++)
63 {
64 TRNG->CTL = TRNG_CTL_TRNGEN_Msk | u32Reg;
65
66 /* TRNG should generate one byte per 125*8 us */
67 for (timeout = (CLK_GetHCLKFreq() / 100); timeout > 0; timeout--)
68 {
69 if (TRNG->CTL & TRNG_CTL_DVIF_Msk)
70 break;
71 }
72
73 if (timeout == 0)
74 return -1;
75
76 *u32RndNum |= ((TRNG->DATA & 0xff) << i*8);
77
78 }
79 return 0;
80 }
81
91 int32_t TRNG_GenBignum(uint8_t u8BigNum[], int32_t i32Len)
92 {
93 uint32_t i, u32Reg, timeout;
94
95 u32Reg = TRNG->CTL;
96
97 for (i = 0; i < i32Len/8; i++)
98 {
99 TRNG->CTL = TRNG_CTL_TRNGEN_Msk | u32Reg;
100
101 /* TRNG should generate one byte per 125*8 us */
102 for (timeout = (CLK_GetHCLKFreq() / 100); timeout > 0; timeout--)
103 {
104 if (TRNG->CTL & TRNG_CTL_DVIF_Msk)
105 break;
106 }
107
108 if (timeout == 0)
109 return -1;
110
111 u8BigNum[i] = (TRNG->DATA & 0xff);
112 }
113 return 0;
114 }
115
125 int32_t TRNG_GenBignumHex(char cBigNumHex[], int32_t i32Len)
126 {
127 uint32_t i, idx, u32Reg, timeout;
128 uint32_t data;
129
130 u32Reg = TRNG->CTL;
131 idx = 0;
132 for (i = 0; i < i32Len/8; i++)
133 {
134 TRNG->CTL = TRNG_CTL_TRNGEN_Msk | u32Reg;
135
136 /* TRNG should generate one byte per 125*8 us */
137 for (timeout = (CLK_GetHCLKFreq() / 100); timeout > 0; timeout--)
138 {
139 if (TRNG->CTL & TRNG_CTL_DVIF_Msk)
140 break;
141 }
142
143 if (timeout == 0)
144 return -1;
145
146 data = (TRNG->DATA & 0xff);
147
148 if (data >= 0xA0)
149 cBigNumHex[idx++] = ((data >> 4) & 0xf) - 10 + 'A';
150 else
151 cBigNumHex[idx++] = ((data >> 4) & 0xf) + '0';
152
153 data &= 0xf;
154 if (data >= 0xA)
155 cBigNumHex[idx++] = data - 10 + 'A';
156 else
157 cBigNumHex[idx++] = data + '0';
158 }
159 cBigNumHex[idx] = 0;
160 return 0;
161 }
162
163 /* end of group TRNG_EXPORTED_FUNCTIONS */
165 /* end of group TRNG_Driver */
167 /* end of group Standard_Driver */
169
170 /*** (C) COPYRIGHT 2019 Nuvoton Technology Corp. ***/
171
172
TRNG_CTL_READY_Msk
#define TRNG_CTL_READY_Msk
Definition: trng_reg.h:200
SYS_IPRST1_TRNGRST_Msk
#define SYS_IPRST1_TRNGRST_Msk
Definition: sys_reg.h:4978
TRNG_Open
void TRNG_Open(void)
Initialize TRNG hardware.
Definition: trng.c:33
NuMicro.h
NuMicro peripheral access layer header file.
SYS
#define SYS
Definition: M480.h:367
TRNG_CTL_DVIF_Msk
#define TRNG_CTL_DVIF_Msk
Definition: trng_reg.h:191
TRNG_ACT_ACT_Msk
#define TRNG_ACT_ACT_Msk
Definition: trng_reg.h:212
TRNG_GenWord
int32_t TRNG_GenWord(uint32_t *u32RndNum)
Generate a 32-bits random number word.
Definition: trng.c:55
TRNG_GenBignumHex
int32_t TRNG_GenBignumHex(char cBigNumHex[], int32_t i32Len)
Generate a big number in hex format.
Definition: trng.c:125
TRNG_CTL_TRNGEN_Msk
#define TRNG_CTL_TRNGEN_Msk
Definition: trng_reg.h:188
CLK_GetHCLKFreq
uint32_t CLK_GetHCLKFreq(void)
Get HCLK frequency.
Definition: clk.c:244
TRNG_GenBignum
int32_t TRNG_GenBignum(uint8_t u8BigNum[], int32_t i32Len)
Generate a big number in binary format.
Definition: trng.c:91
TRNG
#define TRNG
Definition: M480.h:447