1 /**
2  * @file    trng.h
3  * @brief   Random number generator driver.
4  */
5 
6 /******************************************************************************
7  *
8  * Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by
9  * Analog Devices, Inc.),
10  * Copyright (C) 2023-2024 Analog Devices, Inc.
11  *
12  * Licensed under the Apache License, Version 2.0 (the "License");
13  * you may not use this file except in compliance with the License.
14  * You may obtain a copy of the License at
15  *
16  *     http://www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an "AS IS" BASIS,
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  *
24  ******************************************************************************/
25 
26 #ifndef LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32672_TRNG_H_
27 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32672_TRNG_H_
28 
29 /***** Includes *****/
30 #include "trng_regs.h"
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 /**
37  * @defgroup trng TRNG
38  * @ingroup periphlibs
39  * @{
40  */
41 
42 /***** Function Prototypes *****/
43 typedef void (*mxc_trng_complete_t)(void *req, int result);
44 
45 /* ************************************************************************* */
46 /* Global Control/Configuration functions                                    */
47 /* ************************************************************************* */
48 
49 /**
50  * @brief   Enable portions of the TRNG
51  *
52  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
53  */
54 int MXC_TRNG_Init(void);
55 
56 /**
57  * @brief   Enable TRNG Interrupts
58  *
59  */
60 void MXC_TRNG_EnableInt(void);
61 
62 /**
63  * @brief   Disable TRNG Interrupts
64  *
65  */
66 void MXC_TRNG_DisableInt(void);
67 
68 /**
69  * @brief   Disable and reset portions of the TRNG
70  *
71  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
72  */
73 int MXC_TRNG_Shutdown(void);
74 
75 /**
76  * @brief   This function should be called from the TRNG ISR Handler
77  *          when using Async functions
78  */
79 void MXC_TRNG_Handler(void);
80 
81 /* ************************************************************************* */
82 /* True Random Number Generator (TRNG) functions                             */
83 /* ************************************************************************* */
84 
85 /**
86  * @brief   Get a random number
87  *
88  * @return  A random 32-bit number
89  */
90 int MXC_TRNG_RandomInt(void);
91 
92 /**
93  * @brief   Get a random number of length len
94  *
95  * @param   data    Pointer to a location to store the number
96  * @param   len     Length of random number in bytes
97  *
98  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
99  */
100 int MXC_TRNG_Random(uint8_t *data, uint32_t len);
101 
102 /**
103  * @brief   Get a random number of length len, do not block while generating data
104  * @note    The user must call MXC_TRNG_Handler() in the ISR
105  *
106  * @param   data      Pointer to a location to store the number
107  * @param   len       Length of random number in bytes
108  * @param   callback  Function that will be called when all data has been generated
109  *
110  */
111 void MXC_TRNG_RandomAsync(uint8_t *data, uint32_t len, mxc_trng_complete_t callback);
112 
113 /**
114  * @brief   Perform health test of the TRNG entropy source
115  *
116  * @return  If test fails the function will return E_BAD_STATE (-7), otherwise it will return E_NO_ERROR.
117  */
118 int MXC_TRNG_HealthTest(void);
119 
120 #ifdef __cplusplus
121 }
122 #endif
123 /**@} end of group trng */
124 
125 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32672_TRNG_H_
126