1
2 /**
3 * @file xmc_prng.h
4 * @date 2015-06-20
5 *
6 * @cond
7 *********************************************************************************************************************
8 * XMClib v2.1.24 - XMC Peripheral Driver Library
9 *
10 * Copyright (c) 2015-2019, Infineon Technologies AG
11 * All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without modification,are permitted provided that the
14 * following conditions are met:
15 *
16 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
19 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials provided with the distribution.
21 *
22 * Neither the name of the copyright holders nor the names of its contributors may be used to endorse or promote
23 * products derived from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
26 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 * WHETHER IN CONTRACT, STRICT LIABILITY,OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 * To improve the quality of the software, users are encouraged to share modifications, enhancements or bug fixes with
34 * Infineon Technologies AG dave@infineon.com).
35 *********************************************************************************************************************
36 *
37 * Change History
38 * --------------
39 *
40 * 2015-02-20:
41 * - Initial <br>
42 * - Documentation updates <br>
43 *
44 * 2015-06-20:
45 * - Removed version macros and declaration of GetDriverVersion API <br>
46 *
47 * @endcond
48 */
49
50 #ifndef XMC_PRNG_H
51 #define XMC_PRNG_H
52
53 /*********************************************************************************************************************
54 * HEADER FILES
55 *********************************************************************************************************************/
56
57 #include "xmc_common.h"
58
59 #if defined (PRNG)
60
61 /**
62 * @addtogroup XMClib XMC Peripheral Library
63 * @{
64 */
65
66 /**
67 * @addtogroup PRNG
68 * @brief Pseudo Random Number Generator (PRNG) driver for XMC1000 microcontroller family
69 *
70 * The pseudo random bit generator (PRNG) provides random data with fast generation times.
71 * PRNG has to be initialized by the user software before use. The initialization consists
72 * of two basic phases: key-loading and warm-up.
73 *
74 * The PRNG low level driver provides functions to configure and initialize the PRNG hardware
75 * peripheral.
76 * @{
77 */
78
79 /*********************************************************************************************************************
80 * MACROS
81 *********************************************************************************************************************/
82
83 /**
84 * Byte mask value for random data block size
85 */
86 #define XMC_PRNG_RDBS_BYTE_READ_MASK (0x00FFU)
87
88 /*********************************************************************************************************************
89 * ENUMS
90 *********************************************************************************************************************/
91
92 /**
93 * PRNG key load operation modes
94 */
95 typedef enum XMC_PRNG_KEY_LOAD_OP_MODE {
96 XMC_PRNG_STRM_MODE = 0U, /**< Streaming mode (default) */
97 XMC_PRNG_KLD_MODE = 1U /**< Loading mode */
98 } XMC_PRNG_KEY_LOAD_OP_MODE_t;
99
100 /**
101 * PRNG data block size
102 */
103 typedef enum XMC_PRNG_DATA_BLOCK_SIZE {
104 XMC_PRNG_RDBS_RESET = 0U, /**< Reset state (no random data block size defined) */
105 XMC_PRNG_RDBS_BYTE = 1U, /**< BYTE (8-bit) */
106 XMC_PRNG_RDBS_WORD = 2U /**< WORD (16-bit) */
107 } XMC_PRNG_DATA_BLOCK_SIZE_t;
108
109 /**
110 * PRNG driver initialization status
111 */
112 typedef enum XMC_PRNG_INIT_STATUS {
113 XMC_PRNG_NOT_INITIALIZED = 0U, /**< Reset state or Non-initialized state (Same as XMC_PRNG_RDBS_RESET) */
114 XMC_PRNG_INITIALIZED = 1U /**< Initialized state */
115 } XMC_PRNG_INIT_STATUS_t;
116
117 /*********************************************************************************************************************
118 * DATA STRUCTURES
119 *********************************************************************************************************************/
120
121 /**
122 * Key words and data block size configuration values of PRNG <br>
123 *
124 * The structure presents a convenient way to set/obtain the key word and data block configuration
125 * values of PRNG.
126 * The XMC_PRNG_Init() can be used to populate the structure with the key word and data block
127 * configuration values of the PRNG module.
128 */
129 typedef struct XMC_PRNG_INIT
130 {
131 uint16_t key_words[5]; /**< Keywords */
132 XMC_PRNG_DATA_BLOCK_SIZE_t block_size; /**< Block size */
133 } XMC_PRNG_INIT_t;
134
135 /*********************************************************************************************************************
136 * API PROTOTYPES
137 *********************************************************************************************************************/
138
139 #ifdef __cplusplus
140 extern "C" {
141 #endif
142
143 /**
144 * @param prng Pointer to a constant instance of ::XMC_PRNG_INIT_t, pointing to
145 * the initialization configuration.
146 * @return XMC_PRNG_INIT_STATUS_t XMC_PRNG_INITIALIZED if initialized,
147 * XMC_PRNG_NOT_INITIALIZED otherwise.
148 *
149 * \par<b>Description: </b><br>
150 * Initialize the PRNG peripheral with the configured key words and block size <br>
151 *
152 * \par
153 * The function configures block size for key loading mode, enables key loading mode,
154 * loads key words (80 bits) and wait till RDV is set, enables the streaming mode and
155 * waits for warmup phase. This function programmes the CTRL and WORD registers.
156 */
157 XMC_PRNG_INIT_STATUS_t XMC_PRNG_Init(const XMC_PRNG_INIT_t *prng);
158
159
160
161 /**
162 * @param block_size Block size of type ::XMC_PRNG_DATA_BLOCK_SIZE_t for read access
163 * @return None
164 *
165 * \par<b>Description: </b><br>
166 * Programming Random Block Size <br>
167 *
168 * \par
169 * The function sets the random data block size as byte or word by programming CTRL.RDBS bitfield.
170 * block_size = 0 for Reset state, block_size = 1 for 'byte' and block_size = 2 for 'word'.
171 */
XMC_PRNG_SetRandomDataBlockSize(XMC_PRNG_DATA_BLOCK_SIZE_t block_size)172 __STATIC_INLINE void XMC_PRNG_SetRandomDataBlockSize(XMC_PRNG_DATA_BLOCK_SIZE_t block_size)
173 {
174 PRNG->CTRL = (uint16_t)((PRNG->CTRL & (uint32_t)~PRNG_CTRL_RDBS_Msk) |
175 ((uint32_t)block_size << (uint32_t)PRNG_CTRL_RDBS_Pos));
176 }
177
178 /**
179 * @return None
180 *
181 * \par<b>Description: </b><br>
182 * Checks the validity (CHK.RDV bit) of the generated random data <br>
183 *
184 * \par
185 * The function checks the validity (CHK.RDV bit) of the generated random data.
186 * In key loading mode, this value indicates if the next partial key word can be written
187 * to PRNG_WORD or not.
188 */
XMC_PRNG_CheckValidStatus(void)189 __STATIC_INLINE uint16_t XMC_PRNG_CheckValidStatus(void)
190 {
191 return (PRNG->CHK & PRNG_CHK_RDV_Msk);
192 }
193
194 /**
195 * @return None
196 *
197 * \par<b>Description: </b><br>
198 * Enables the PRNG key loading mode <br>
199 *
200 * \par
201 * The function initializes the key loading by setting the bit CTRL.KLD. In this mode, Register WORD
202 * acts as always as a 16 bit destination register. After the complete key has been loaded, the CTRL.KLD
203 * must be set to '0' to prepare the following warmup phase.
204 *
205 * \par<b>Related APIs:</b><br>
206 * XMC_PRNG_EnableStreamingMode()
207 */
XMC_PRNG_EnableKeyLoadingMode(void)208 __STATIC_INLINE void XMC_PRNG_EnableKeyLoadingMode(void)
209 {
210 PRNG->CTRL |= (uint16_t)PRNG_CTRL_KLD_Msk;
211 }
212
213 /**
214 * @return None
215 *
216 * \par<b>Description: </b><br>
217 * Enables the Streaming mode <br>
218 *
219 * \par
220 * The function enables the streaming mode and disables the PRNG key loading mode by resetting the
221 * CTRL.KLD bit.
222 *
223 * \par<b>Related APIs:</b><br>
224 * XMC_PRNG_EnableKeyLoadingMode()
225 */
XMC_PRNG_EnableStreamingMode(void)226 __STATIC_INLINE void XMC_PRNG_EnableStreamingMode(void)
227 {
228 PRNG->CTRL &= (uint16_t)~PRNG_CTRL_KLD_Msk;
229 }
230
231 /**
232 * @param key Key word to load into PRNG WORD register
233 * @return None
234 *
235 * \par<b>Description: </b><br>
236 * Loads a partial key word to the PRNG WORD register <br>
237 *
238 * \par
239 * The function loads partial key word to WORD registr. These partial
240 * words are sequentially written and loading a key word will take 16 clock
241 * cycles. The CHK.RDV bit is set to '0' while loading is in progress. '1' indicates
242 * that the next partial key word can be written to WORD register.
243 */
XMC_PRNG_LoadKeyWords(uint16_t key)244 __STATIC_INLINE void XMC_PRNG_LoadKeyWords(uint16_t key)
245 {
246 PRNG->WORD = key;
247 }
248
249 /**
250 * @param None
251 * @return uint16_t Generated random number
252 *
253 * \par<b>Description: </b><br>
254 * Gets the generated random number <br>
255 *
256 * \par
257 * The function gives the generated random number by returning the content of WORD
258 * register. Before reading the WORD register to get the generated random number it is
259 * required to check the bit CHK.RDV is set which indicates that the next random data block
260 * can be read from WORD register. After a word has been read the bit CHK.RDV is reset
261 * by the hardware and generation of new random bits starts.
262 *
263 * \par<b>Related APIs:</b><br>
264 * XMC_PRNG_CheckValidStatus()
265 */
XMC_PRNG_GetPseudoRandomNumber(void)266 __STATIC_INLINE uint16_t XMC_PRNG_GetPseudoRandomNumber(void)
267 {
268 return PRNG->WORD;
269 }
270
271 #ifdef __cplusplus
272 }
273 #endif
274
275 /**
276 * @}
277 */
278
279 /**
280 * @}
281 */
282
283 #endif /* #if defined (PRNG) */
284
285 #endif /* XMC_PRNG_H */
286