1 /**
2   *********************************************************************************
3   *
4   * @file    ald_trng.h
5   * @brief   Header file of TRNG module driver.
6   *
7   * @version V1.0
8   * @date    04 Dec 2017
9   * @author  AE Team
10   * @note
11   *
12   * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
13   *
14   * SPDX-License-Identifier: Apache-2.0
15   *
16   * Licensed under the Apache License, Version 2.0 (the License); you may
17   * not use this file except in compliance with the License.
18   * You may obtain a copy of the License at
19   *
20   * www.apache.org/licenses/LICENSE-2.0
21   *
22   * Unless required by applicable law or agreed to in writing, software
23   * distributed under the License is distributed on an AS IS BASIS, WITHOUT
24   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25   * See the License for the specific language governing permissions and
26   * limitations under the License.
27   *
28   ********************************************************************************
29   */
30 
31 #ifndef __ALD_TRNG_H__
32 #define __ALD_TRNG_H__
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 #include "utils.h"
39 
40 
41 /** @addtogroup ES32FXXX_ALD
42   * @{
43   */
44 
45 /** @addtogroup TRNG
46   * @{
47   */
48 
49 /** @defgroup TRNG_Public_Types TRNG Public Types
50   * @{
51   */
52 /**
53   * @brief Data width
54   */
55 typedef enum {
56 	TRNG_DSEL_1B  = 0x0U,	/**< 1-bit */
57 	TRNG_DSEL_8B  = 0x1U,	/**< 8-bit */
58 	TRNG_DSEL_16B = 0x2U,	/**< 16-bit */
59 	TRNG_DSEL_32B = 0x3U,	/**< 32-bit */
60 } trng_data_width_t;
61 
62 /**
63   * @brief seed type
64   */
65 typedef enum {
66 	TRNG_SEED_TYPE_0    = 0x0U,	/**< Using 0 as seed */
67 	TRNG_SEED_TYPE_1    = 0x1U,	/**< Using 1 as seed */
68 	TRNG_SEED_TYPE_LAST = 0x2U,	/**< Using last seed */
69 	TRNG_SEED_TYPE_SEED = 0x3U,	/**< Using value of register */
70 } trng_seed_type_t;
71 
72 /**
73   * @brief TRNG init structure definition
74   */
75 typedef struct {
76 	trng_data_width_t data_width;	/**< The width of data */
77 	trng_seed_type_t seed_type;	/**< The seed type */
78 	uint32_t seed;			/**< The value of seed */
79 	uint16_t t_start;		/**< T(start) = T(trng) * 2 ^ (t_start + 1), T(start) > 1ms */
80 	uint8_t adjc;			/**< Adjust parameter */
81 	type_func_t posten;		/**< Data back handle function  */
82 } trng_init_t;
83 
84 /**
85   * @brief TRNG state structures definition
86   */
87 typedef enum {
88 	TRNG_STATE_RESET = 0x0U,	/**< Peripheral is not initialized */
89 	TRNG_STATE_READY = 0x1U,	/**< Peripheral Initialized and ready for use */
90 	TRNG_STATE_BUSY  = 0x2U,	/**< An internal process is ongoing */
91 	TRNG_STATE_ERROR = 0x4U,	/**< Error */
92 } trng_state_t;
93 
94 /**
95   * @brief State type
96   */
97 typedef enum {
98 	TRNG_STATUS_START = (1U << 0),	/**< Start state */
99 	TRNG_STATUS_DAVLD = (1U << 1),	/**< Data valid state */
100 	TRNG_STATUS_SERR  = (1U << 2),	/**< Error state */
101 } trng_status_t;
102 
103 /**
104   * @brief Interrupt type
105   */
106 typedef enum {
107 	TRNG_IT_START = (1U << 0),	/**< Start */
108 	TRNG_IT_DAVLD = (1U << 1),	/**< Data valid */
109 	TRNG_IT_SERR  = (1U << 2),	/**< Error */
110 } trng_it_t;
111 
112 /**
113   * @brief Interrupt flag type
114   */
115 typedef enum {
116 	TRNG_IF_START = (1U << 0),	/**< Start */
117 	TRNG_IF_DAVLD = (1U << 1),	/**< Data valid */
118 	TRNG_IF_SERR  = (1U << 2),	/**< Error */
119 } trng_flag_t;
120 
121 /**
122   * @brief  TRNG Handle Structure definition
123   */
124 typedef struct trng_handle_s {
125 	TRNG_TypeDef *perh;	/**< Register base address */
126 	trng_init_t init;	/**< TRNG required parameters */
127 	uint32_t data;		/**< result data */
128 	lock_state_t lock; 	/**< Locking object */
129 	trng_state_t state;	/**< TRNG operation state */
130 
131 	void (*trng_cplt_cbk)(struct trng_handle_s *arg); /**< Trng completed callback */
132 	void (*err_cplt_cbk)(struct trng_handle_s *arg);  /**< Trng error callback */
133 	void (*init_cplt_cbk)(struct trng_handle_s *arg); /**< Trng init completed callback */
134 } trng_handle_t;
135 /**
136   * @}
137   */
138 
139 /** @defgroup TRNG_Public_Macros TRNG Public Macros
140   * @{
141   */
142 #define TRNG_ENABLE()		(SET_BIT(TRNG->CR, TRNG_CR_TRNGEN_MSK))
143 #define TRNG_DISABLE()		(CLEAR_BIT(TRNG->CR, TRNG_CR_TRNGEN_MSK))
144 #define TRNG_ADJM_ENABLE()	(SET_BIT(TRNG->CR, TRNG_CR_ADJM_MSK))
145 #define TRNG_ADJM_DISABLE()	(CLEAR_BIT(TRNG->CR, TRNG_CR_ADJM_MSK))
146 /**
147   * @}
148   */
149 
150 /**
151   * @defgroup TRNG_Private_Macros TRNG Private Macros
152   * @{
153   */
154 #define IS_TRNG_DATA_WIDTH(x)	(((x) == TRNG_DSEL_1B)  || \
155                                  ((x) == TRNG_DSEL_8B)  || \
156                                  ((x) == TRNG_DSEL_16B) || \
157                                  ((x) == TRNG_DSEL_32B))
158 #define IS_TRNG_SEED_TYPE(x)	(((x) == TRNG_SEED_TYPE_0)    || \
159                                  ((x) == TRNG_SEED_TYPE_1)    || \
160                                  ((x) == TRNG_SEED_TYPE_LAST) || \
161                                  ((x) == TRNG_SEED_TYPE_SEED))
162 #define IS_TRNG_STATUS(x)	(((x) == TRNG_STATUS_START)  || \
163                                  ((x) == TRNG_STATUS_DAVLD)  || \
164                                  ((x) == TRNG_STATUS_SERR))
165 #define IS_TRNG_IT(x)		(((x) == TRNG_IT_START)  || \
166                                  ((x) == TRNG_IT_DAVLD)  || \
167                                  ((x) == TRNG_IT_SERR))
168 #define IS_TRNG_FLAG(x)		(((x) == TRNG_IF_START)  || \
169                                  ((x) == TRNG_IF_DAVLD)  || \
170                                  ((x) == TRNG_IF_SERR))
171 #define IS_TRNG_ADJC(x)		((x) < 4)
172 #define IS_TRNG_T_START(x)	((x) < 8)
173 /**
174   * @}
175   */
176 
177 /** @addtogroup TRNG_Public_Functions
178   * @{
179   */
180 /** @addtogroup TRNG_Public_Functions_Group1
181   * @{
182   */
183 /* Initialization functions */
184 extern ald_status_t ald_trng_init(trng_handle_t *hperh);
185 /**
186   * @}
187   */
188 /** @addtogroup TRNG_Public_Functions_Group2
189   * @{
190   */
191 /* Control functions */
192 extern uint32_t ald_trng_get_result(trng_handle_t *hperh);
193 extern void ald_trng_interrupt_config(trng_handle_t *hperh, trng_it_t it, type_func_t state);
194 extern flag_status_t ald_trng_get_status(trng_handle_t *hperh, trng_status_t status);
195 extern it_status_t ald_trng_get_it_status(trng_handle_t *hperh, trng_it_t it);
196 extern flag_status_t ald_trng_get_flag_status(trng_handle_t *hperh, trng_flag_t flag);
197 extern void ald_trng_clear_flag_status(trng_handle_t *hperh, trng_flag_t flag);
198 extern void ald_trng_irq_handler(trng_handle_t *hperh);
199 /**
200   * @}
201   */
202 /**
203   * @}
204   */
205 
206 /**
207   * @}
208   */
209 
210 /**
211   * @}
212   */
213 #ifdef __cplusplus
214 }
215 #endif
216 
217 #endif /* __ALD_TRNG_H__ */
218