ChibiOS/HAL  7.0.3
hal_trng.c
Go to the documentation of this file.
1 /*
2  ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 
17 /**
18  * @file hal_trng.c
19  * @brief TRNG Driver code.
20  *
21  * @addtogroup TRNG
22  * @{
23  */
24 
25 #include "hal.h"
26 
27 #if (HAL_USE_TRNG == TRUE) || defined(__DOXYGEN__)
28 
29 /*===========================================================================*/
30 /* Driver local definitions. */
31 /*===========================================================================*/
32 
33 /*===========================================================================*/
34 /* Driver exported variables. */
35 /*===========================================================================*/
36 
37 /*===========================================================================*/
38 /* Driver local variables and types. */
39 /*===========================================================================*/
40 
41 /*===========================================================================*/
42 /* Driver local functions. */
43 /*===========================================================================*/
44 
45 /*===========================================================================*/
46 /* Driver exported functions. */
47 /*===========================================================================*/
48 
49 /**
50  * @brief TRNG Driver initialization.
51  * @note This function is implicitly invoked by @p halInit(), there is
52  * no need to explicitly initialize the driver.
53  *
54  * @init
55  */
56 void trngInit(void) {
57 
58  trng_lld_init();
59 }
60 
61 /**
62  * @brief Initializes the standard part of a @p TRNGDriver structure.
63  *
64  * @param[out] trngp pointer to the @p TRNGDriver object
65  *
66  * @init
67  */
68 void trngObjectInit(TRNGDriver *trngp) {
69 
70  trngp->state = TRNG_STOP;
71  trngp->config = NULL;
72 }
73 
74 /**
75  * @brief Configures and activates the TRNG peripheral.
76  *
77  * @param[in] trngp pointer to the @p TRNGDriver object
78  * @param[in] config pointer to the @p TRNGConfig object or @p NULL for
79  * default configuration
80  *
81  * @api
82  */
83 void trngStart(TRNGDriver *trngp, const TRNGConfig *config) {
84 
85  osalDbgCheck(trngp != NULL);
86 
87  osalSysLock();
88  osalDbgAssert((trngp->state == TRNG_STOP) || (trngp->state == TRNG_READY),
89  "invalid state");
90  trngp->config = config;
91  trng_lld_start(trngp);
92  trngp->state = TRNG_READY;
93  osalSysUnlock();
94 }
95 
96 /**
97  * @brief Deactivates the TRNG peripheral.
98  *
99  * @param[in] trngp pointer to the @p TRNGDriver object
100  *
101  * @api
102  */
103 void trngStop(TRNGDriver *trngp) {
104 
105  osalDbgCheck(trngp != NULL);
106 
107  osalSysLock();
108 
109  osalDbgAssert((trngp->state == TRNG_STOP) || (trngp->state == TRNG_READY),
110  "invalid state");
111 
112  trng_lld_stop(trngp);
113  trngp->config = NULL;
114  trngp->state = TRNG_STOP;
115 
116  osalSysUnlock();
117 }
118 
119 /**
120  * @brief True random numbers generator.
121  * @note The function is blocking and likely performs polled waiting
122  * inside the low level implementation.
123  *
124  * @param[in] trngp pointer to the @p TRNGDriver object
125  * @param[in] size size of output buffer
126  * @param[out] out output buffer
127  * @return The operation status.
128  * @retval false if a random number has been generated.
129  * @retval true if an HW error occurred.
130  *
131  * @api
132  */
133 bool trngGenerate(TRNGDriver *trngp, size_t size, uint8_t *out) {
134  bool err;
135 
136  osalDbgCheck((trngp != NULL) && (out != NULL));
137 
138  osalDbgAssert(trngp->state == TRNG_READY, "not ready");
139 
140  trngp->state = TRNG_RUNNING;
141 
142  err = trng_lld_generate(trngp, size, out);
143 
144  trngp->state = TRNG_READY;
145 
146  return err;
147 }
148 
149 #endif /* HAL_USE_TRNG == TRUE */
150 
151 /** @} */
void trng_lld_init(void)
Low level TRNG driver initialization.
Definition: hal_trng_lld.c:65
void trng_lld_stop(TRNGDriver *trngp)
Deactivates the TRNG peripheral.
Definition: hal_trng_lld.c:101
void trng_lld_start(TRNGDriver *trngp)
Configures and activates the TRNG peripheral.
Definition: hal_trng_lld.c:80
HAL subsystem header.
bool trng_lld_generate(TRNGDriver *trngp, size_t size, uint8_t *out)
True random numbers generator.
Definition: hal_trng_lld.c:129
void trngStop(TRNGDriver *trngp)
Deactivates the TRNG peripheral.
Definition: hal_trng.c:103
bool trngGenerate(TRNGDriver *trngp, size_t size, uint8_t *out)
True random numbers generator.
Definition: hal_trng.c:133
static void osalSysUnlock(void)
Leaves a critical zone from thread context.
Definition: osal.h:540
void trngObjectInit(TRNGDriver *trngp)
Initializes the standard part of a TRNGDriver structure.
Definition: hal_trng.c:68
void trngStart(TRNGDriver *trngp, const TRNGConfig *config)
Configures and activates the TRNG peripheral.
Definition: hal_trng.c:83
Driver configuration structure.
Definition: hal_trng.h:75
void trngInit(void)
TRNG Driver initialization.
Definition: hal_trng.c:56
#define osalDbgCheck(c)
Function parameters check.
Definition: osal.h:278
const TRNGConfig * config
Current configuration data.
Definition: hal_trng.h:91
static void osalSysLock(void)
Enters a critical zone from thread context.
Definition: osal.h:530
#define osalDbgAssert(c, remark)
Condition assertion.
Definition: osal.h:258
USBOutEndpointState out
OUT EP0 state.
Definition: hal_usb_lld.c:61
Structure representing a TRNG driver.
Definition: hal_trng.h:83
trngstate_t state
Driver state.
Definition: hal_trng.h:87