ChibiOS/HAL 9.0.0
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 */
56void trngInit(void) {
57
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 */
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 * @return The operation status.
81 *
82 * @api
83 */
84msg_t trngStart(TRNGDriver *trngp, const TRNGConfig *config) {
85 msg_t msg;
86
87 osalDbgCheck(trngp != NULL);
88
90 osalDbgAssert((trngp->state == TRNG_STOP) || (trngp->state == TRNG_READY),
91 "invalid state");
92
93 trngp->config = config;
94
95#if defined(TRNG_LLD_ENHANCED_API)
96 msg = trng_lld_start(trngp);
97 if (msg == HAL_RET_SUCCESS) {
98 trngp->state = TRNG_READY;
99 }
100 else {
101 trngp->state = TRNG_STOP;
102 }
103#else
104 trng_lld_start(trngp);
105 trngp->state = TRNG_READY;
106 msg = HAL_RET_SUCCESS;
107#endif
108
110
111 return msg;
112}
113
114/**
115 * @brief Deactivates the TRNG peripheral.
116 *
117 * @param[in] trngp pointer to the @p TRNGDriver object
118 *
119 * @api
120 */
121void trngStop(TRNGDriver *trngp) {
122
123 osalDbgCheck(trngp != NULL);
124
125 osalSysLock();
126
127 osalDbgAssert((trngp->state == TRNG_STOP) || (trngp->state == TRNG_READY),
128 "invalid state");
129
130 trng_lld_stop(trngp);
131 trngp->config = NULL;
132 trngp->state = TRNG_STOP;
133
135}
136
137/**
138 * @brief True random numbers generator.
139 * @note The function is blocking and likely performs polled waiting
140 * inside the low level implementation.
141 *
142 * @param[in] trngp pointer to the @p TRNGDriver object
143 * @param[in] size size of output buffer
144 * @param[out] out output buffer
145 * @return The operation status.
146 * @retval false if a random number has been generated.
147 * @retval true if an HW error occurred.
148 *
149 * @api
150 */
151bool trngGenerate(TRNGDriver *trngp, size_t size, uint8_t *out) {
152 bool err;
153
154 osalDbgCheck((trngp != NULL) && (out != NULL));
155
156 osalDbgAssert(trngp->state == TRNG_READY, "not ready");
157
158 trngp->state = TRNG_RUNNING;
159
160 err = trng_lld_generate(trngp, size, out);
161
162 trngp->state = TRNG_READY;
163
164 return err;
165}
166
167#endif /* HAL_USE_TRNG == TRUE */
168
169/** @} */
#define HAL_RET_SUCCESS
Definition hal.h:93
static void osalSysLock(void)
Enters a critical zone from thread context.
Definition osal.h:601
static void osalSysUnlock(void)
Leaves a critical zone from thread context.
Definition osal.h:611
int32_t msg_t
Type of a message.
Definition osal.h:159
#define osalDbgAssert(c, remark)
Condition assertion.
Definition osal.h:264
#define osalDbgCheck(c)
Function parameters check.
Definition osal.h:284
void trng_lld_init(void)
Low level TRNG driver initialization.
bool trng_lld_generate(TRNGDriver *trngp, size_t size, uint8_t *out)
True random numbers generator.
void trngInit(void)
TRNG Driver initialization.
Definition hal_trng.c:56
void trng_lld_start(TRNGDriver *trngp)
Configures and activates the TRNG peripheral.
msg_t trngStart(TRNGDriver *trngp, const TRNGConfig *config)
Configures and activates the TRNG peripheral.
Definition hal_trng.c:84
void trngStop(TRNGDriver *trngp)
Deactivates the TRNG peripheral.
Definition hal_trng.c:121
void trng_lld_stop(TRNGDriver *trngp)
Deactivates the TRNG peripheral.
void trngObjectInit(TRNGDriver *trngp)
Initializes the standard part of a TRNGDriver structure.
Definition hal_trng.c:68
struct hal_trng_driver TRNGDriver
Type of a structure representing a TRNG driver.
Definition hal_trng.h:59
bool trngGenerate(TRNGDriver *trngp, size_t size, uint8_t *out)
True random numbers generator.
Definition hal_trng.c:151
struct hal_trng_config TRNGConfig
Driver configuration structure.
Definition hal_trng.h:65
@ TRNG_READY
Definition hal_trng.h:52
@ TRNG_STOP
Definition hal_trng.h:51
@ TRNG_RUNNING
Definition hal_trng.h:53
HAL subsystem header.
USBOutEndpointState out
OUT EP0 state.
Definition hal_usb_lld.c:61
trngstate_t state
Driver state.
Definition hal_trng.h:86
const TRNGConfig * config
Current configuration data.
Definition hal_trng.h:90