summaryrefslogtreecommitdiff
path: root/bsp/essemi/es32f0654/libraries/ES32F065x_ALD_StdPeriph_Driver/Source/ald_trng.c
blob: 81a62e612797ebf8943d130475d539059d3a2b6c (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/**
  *********************************************************************************
  *
  * @file    ald_trng.c
  * @brief   TRNG module driver.
  *
  * @version V1.0
  * @date    04 Dec 2017
  * @author  AE Team
  * @note
  *
  * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
  *
  * SPDX-License-Identifier: Apache-2.0
  *
  * Licensed under the Apache License, Version 2.0 (the License); you may
  * not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
  * www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  *********************************************************************************
  */

#include "ald_trng.h"


/** @addtogroup ES32FXXX_ALD
  * @{
  */

/** @defgroup TRNG TRNG
  * @brief TRNG module driver
  * @{
  */
#ifdef ALD_TRNG

/** @addtogroup CRYPT_Private_Functions CRYPT Private Functions
  * @{
  */
void trng_reset(trng_handle_t *hperh);
/**
  * @}
  */ 

/** @defgroup TRNG_Public_Functions TRNG Public Functions
  * @{
  */

/** @addtogroup TRNG_Public_Functions_Group1 Initialization functions
  * @brief Initialization functions
  *
  * @verbatim
  ==============================================================================
              ##### Initialization functions #####
  ==============================================================================
    [..]  This section provides functions allowing to initialize the TRNG:
      (+) This parameters can be configured:
        (++) Word Width
        (++) Seed Type
        (++) Seed
        (++) Start Time
        (++) Adjust parameter

    @endverbatim
  * @{
  */


/**
  * @brief  Initializes the TRNG according to the specified
  *         parameters in the trng_init_t.
  * @param  hperh: Pointer to a trng_handle_t structure that contains
  *         the configuration information for the specified TRNG module.
  * @retval Status, see @ref ald_status_t.
  */
ald_status_t ald_trng_init(trng_handle_t *hperh)
{
	uint32_t tmp = 0;

	if (hperh == NULL)
		return ERROR;

	assert_param(IS_TRNG_DATA_WIDTH(hperh->init.data_width));
	assert_param(IS_TRNG_SEED_TYPE(hperh->init.seed_type));
	assert_param(IS_TRNG_ADJC(hperh->init.adjc));
	assert_param(IS_FUNC_STATE(hperh->init.posten));
	assert_param(IS_TRNG_T_START(hperh->init.t_start));

	__LOCK(hperh);
	trng_reset(hperh);

	if (hperh->state == TRNG_STATE_RESET)
		__UNLOCK(hperh);

	tmp = TRNG->CR;

	if (hperh->init.adjc == 0)
		tmp = (0 << TRNG_CR_ADJM_POS);
	else
		tmp = (1 << TRNG_CR_ADJM_POS);

	tmp |= ((1 << TRNG_CR_TRNGSEL_POS) | (hperh->init.data_width << TRNG_CR_DSEL_POSS) |
	       (hperh->init.seed_type << TRNG_CR_SDSEL_POSS) | (hperh->init.adjc << TRNG_CR_ADJC_POSS) |
	       (hperh->init.posten << TRNG_CR_POSTEN_MSK));

	TRNG->CR = tmp;

	WRITE_REG(TRNG->SEED, hperh->init.seed);
	MODIFY_REG(TRNG->CFGR, TRNG_CFGR_TSTART_MSK, (hperh->init.t_start) << TRNG_CFGR_TSTART_POSS);

	hperh->state = TRNG_STATE_READY;
	__UNLOCK(hperh);
	return OK;
}
/**
  * @}
  */

/** @addtogroup TRNG_Public_Functions_Group2 Peripheral Control functions
  * @brief Peripheral Control functions
  *
  * @verbatim
  ==============================================================================
              ##### Peripheral Control functions #####
  ==============================================================================
  [..]  This section provides functions allowing to:
    (+) ald_trng_get_result() API can Get the result.
    (+) ald_trng_interrupt_config() API can be helpful to configure TRNG interrupt source.
    (+) ald_trng_get_it_status() API can get the status of interrupt source.
    (+) ald_trng_get_status() API can get the status of SR register.
    (+) ald_trng_get_flag_status() API can get the status of interrupt flag.
    (+) ald_trng_clear_flag_status() API can clear interrupt flag.

    @endverbatim
  * @{
  */

/**
  * @brief  Get the result.
  * @param  hperh: Pointer to a trng_handle_t structure that contains
  *         the configuration information for the specified TRNG module.
  * @retval The resultl
  */
uint32_t ald_trng_get_result(trng_handle_t *hperh)
{
	hperh->state = TRNG_STATE_READY;
	hperh->data = hperh->perh->DR;
	return (uint32_t)hperh->perh->DR;
}

/**
  * @brief  Enable/disable the specified interrupts.
  * @param  hperh: Pointer to a trng_handle_t structure that contains
  *         the configuration information for the specified TRNG module.
  * @param  it: Specifies the interrupt sources to be enabled or disabled.
  *         This parameter can be one of the @ref trng_it_t.
  * @param  state: New state of the specified interrupts.
  *         This parameter can be:
  *             @arg ENABLE
  *             @arg DISABLE
  * @retval None
  */
void ald_trng_interrupt_config(trng_handle_t *hperh, trng_it_t it, type_func_t state)
{
	assert_param(IS_TRNG_IT(it));
	assert_param(IS_FUNC_STATE(state));

	if (state)
		SET_BIT(hperh->perh->IER, it);
	else
		CLEAR_BIT(hperh->perh->IER, it);

	return;
}

/**
  * @brief  Get the status of SR register.
  * @param  hperh: Pointer to a trng_handle_t structure that contains
  *         the configuration information for the specified TRNG module.
  * @param  status: Specifies the TRNG status type.
  *         This parameter can be one of the @ref trng_status_t.
  * @retval Status:
  *           - 0: RESET
  *           - 1: SET
  */
flag_status_t ald_trng_get_status(trng_handle_t *hperh, trng_status_t status)
{
	assert_param(IS_TRNG_STATUS(status));

	if (READ_BIT(hperh->perh->SR, status))
		return SET;

	return RESET;
}

/**
  * @brief  Get the status of interrupt source.
  * @param  hperh: Pointer to a trng_handle_t structure that contains
  *         the configuration information for the specified TRNG module.
  * @param  it: Specifies the interrupt source.
  *         This parameter can be one of the @ref trng_it_t.
  * @retval Status:
  *           - 0: RESET
  *           - 1: SET
  */
it_status_t ald_trng_get_it_status(trng_handle_t *hperh, trng_it_t it)
{
	assert_param(IS_TRNG_IT(it));

	if (READ_BIT(hperh->perh->IER, it))
		return SET;

	return RESET;
}

/**
  * @brief  Get the status of interrupt flag.
  * @param  hperh: Pointer to a trng_handle_t structure that contains
  *         the configuration information for the specified TRNG module.
  * @param  flag: Specifies the interrupt flag.
  *         This parameter can be one of the @ref trng_flag_t.
  * @retval Status:
  *           - 0: RESET
  *           - 1: SET
  */
flag_status_t ald_trng_get_flag_status(trng_handle_t *hperh, trng_flag_t flag)
{
	assert_param(IS_TRNG_FLAG(flag));

	if (READ_BIT(hperh->perh->IFR, flag))
		return SET;

	return RESET;
}

/**
  * @brief  Clear the interrupt flag.
  * @param  hperh: Pointer to a trng_handle_t structure that contains
  *         the configuration information for the specified TRNG module.
  * @param  flag: Specifies the interrupt flag.
  *         This parameter can be one of the @ref trng_flag_t.
  * @retval None
  */
void ald_trng_clear_flag_status(trng_handle_t *hperh, trng_flag_t flag)
{
	assert_param(IS_TRNG_FLAG(flag));
	WRITE_REG(hperh->perh->IFCR, flag);

	return;
}

/**
  * @brief  Reset the TRNG peripheral.
  * @param  hperh: Pointer to a trng_handle_t structure that contains
  *         the configuration information for the specified TRNG module.
  * @retval None
  */
void trng_reset(trng_handle_t *hperh)
{
	TRNG->CR = 0;
	TRNG->SEED = 0;
	TRNG->CFGR = 0x1FF0707;
	TRNG->IER = 0;
	TRNG->IFCR = 0xFFFFFFFF;

	hperh->state = TRNG_STATE_READY;
	__UNLOCK(hperh);
	return;
}

/**
  * @brief  This function handles TRNG interrupt request.
  * @param  hperh: Pointer to a trng_handle_t structure that contains
  *         the configuration information for the specified TRNG module.
  * @retval None
  */
void ald_trng_irq_handler(trng_handle_t *hperh)
{
	if (ald_trng_get_flag_status(hperh, TRNG_IF_SERR) == SET) {
		hperh->state = TRNG_STATE_ERROR;
		ald_trng_clear_flag_status(hperh, TRNG_IF_SERR);
		if (hperh->err_cplt_cbk)
			hperh->err_cplt_cbk(hperh);
		return;
	}

	if (ald_trng_get_flag_status(hperh, TRNG_IF_DAVLD) == SET) {
		hperh->data = hperh->perh->DR;
		hperh->state = TRNG_STATE_READY;
		ald_trng_clear_flag_status(hperh, TRNG_IF_DAVLD);
		if (hperh->trng_cplt_cbk)
			hperh->trng_cplt_cbk(hperh);
	}

	if (ald_trng_get_flag_status(hperh, TRNG_IF_START) == SET) {
		hperh->state = TRNG_STATE_BUSY;
		ald_trng_clear_flag_status(hperh, TRNG_IF_START);
		if (hperh->init_cplt_cbk)
			hperh->init_cplt_cbk(hperh);
	}
}

/**
  * @}
  */
/**
  * @}
  */
#endif /* ALD_TRNG */
/**
  * @}
  */

/**
  * @}
  */