blob: d75374fd7f6957c0e1277dae2ad16d31ad46c9c2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
/**
*******************************************************************************
* @file hc32f4a0_trng.c
* @brief This file provides firmware functions to manage the True Random
* Number Generator(TRNG).
@verbatim
Change Logs:
Date Author Notes
2020-06-12 Heqb First version
@endverbatim
*******************************************************************************
* Copyright (C) 2020, Huada Semiconductor Co., Ltd. All rights reserved.
*
* This software component is licensed by HDSC under BSD 3-Clause license
* (the "License"); You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
*******************************************************************************
*/
/*******************************************************************************
* Include files
******************************************************************************/
#include "hc32f4a0_trng.h"
#include "hc32f4a0_utility.h"
/**
* @addtogroup HC32F4A0_DDL_Driver
* @{
*/
/**
* @defgroup DDL_TRNG TRNG
* @brief TRNG Driver Library
* @{
*/
#if (DDL_TRNG_ENABLE == DDL_ON)
/*******************************************************************************
* Local type definitions ('typedef')
******************************************************************************/
/*******************************************************************************
* Local pre-processor symbols/macros ('#define')
******************************************************************************/
/**
* @defgroup TRNG_Local_Macros TRNG Local Macros
* @{
*/
#define TRNG_TIMEOUT (20000UL)
/**
* @defgroup TRNG_Check_Parameters_Validity TRNG Check Parameters Validity
* @{
*/
#define IS_VALID_SHIFT_CNT(x) \
( ((x) == TRNG_SHIFT_COUNT_32) || \
((x) == TRNG_SHIFT_COUNT_64) || \
((x) == TRNG_SHIFT_COUNT_128) || \
((x) == TRNG_SHIFT_COUNT_256))
#define IS_VALID_RELOAD(x) \
( ((x) == TRNG_RELOAD_ENABLE) || \
((x) == TRNG_RELOAD_DISABLE))
/**
* @}
*/
/**
* @}
*/
/*******************************************************************************
* Global variable definitions (declared in header file with 'extern')
******************************************************************************/
/*******************************************************************************
* Local function prototypes ('static')
******************************************************************************/
/*******************************************************************************
* Local variable definitions ('static')
******************************************************************************/
/*******************************************************************************
* Function implementation - global ('extern') and local ('static')
******************************************************************************/
/**
* @defgroup TRNG_Global_Functions TRNG Global Functions
* @{
*/
/**
* @brief Set shift times
* @param [in] u32ShiftCount Config the shift times.
* This parameter can be a value of the following:
* @arg TRNG_SHIFT_COUNT_32 Shift 32 times
* @arg TRNG_SHIFT_COUNT_64 Shift 64 times
* @arg TRNG_SHIFT_COUNT_128 Shift 128 times
* @arg TRNG_SHIFT_COUNT_256 Shift 256 times
* @retval None
*/
void TRNG_SetShiftCnt(uint32_t u32ShiftCount)
{
DDL_ASSERT(IS_VALID_SHIFT_CNT(u32ShiftCount));
MODIFY_REG32(M4_TRNG->MR, TRNG_MR_CNT, u32ShiftCount);
}
/**
* @brief Enable or disable load new value
* @param [in] u32ReloadCmd Enable or disable load new value.
* This parameter can be a value of the following:
* @arg TRNG_RELOAD_ENABLE Enable the function
* @arg TRNG_RELOAD_DISABLE Disable the function
* @retval None
*/
void TRNG_ReloadCmd(uint32_t u32ReloadCmd)
{
DDL_ASSERT(IS_VALID_RELOAD(u32ReloadCmd));
MODIFY_REG32(M4_TRNG->MR, TRNG_MR_LOAD, u32ReloadCmd);
}
/**
* @brief Start TRNG and get random number
* @param [in] au32Random The destination address where the random number will be stored.
* @retval Ok: Success
* ErrorTimeout: Process timeout
* ErrorInvalidParameter: Parameter error
* @note Please pass in an array of 64 bits or more
*/
en_result_t TRNG_Generate(uint32_t au32Random[])
{
en_result_t enRet = ErrorInvalidParameter;
uint32_t u32TimeCount = 0U;
if(au32Random != NULL)
{
enRet = Ok;
/* Enable TRNG circuit. */
SET_REG32_BIT(M4_TRNG->CR, TRNG_CR_EN);
/* Start TRNG */
SET_REG32_BIT(M4_TRNG->CR, TRNG_CR_RUN);
/* Wait for the TRNG to stop */
while(READ_REG32_BIT(M4_TRNG->CR, TRNG_CR_RUN) == 1U)
{
if(u32TimeCount++ > TRNG_TIMEOUT)
{
enRet = ErrorTimeout;
break;
}
}
if(enRet == Ok)
{
/* Get the random number. */
au32Random[0U] = READ_REG32(M4_TRNG->DR0);
au32Random[1U] = READ_REG32(M4_TRNG->DR1);
/* Disable TRNG circuit. */
CLEAR_REG32_BIT(M4_TRNG->CR, TRNG_CR_EN);
}
}
return enRet;
}
/**
* @}
*/
#endif /* DDL_TRNG_ENABLE */
/**
* @}
*/
/**
* @}
*/
/*******************************************************************************
* EOF (not truncated)
******************************************************************************/
|