forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentropy_b91_trng.c
71 lines (55 loc) · 1.5 KB
/
entropy_b91_trng.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* Copyright (c) 2021 Telink Semiconductor
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT telink_b91_trng
#include <trng.h>
#include <zephyr/drivers/entropy.h>
#include <string.h>
/* API implementation: driver initialization */
static int entropy_b91_trng_init(const struct device *dev)
{
ARG_UNUSED(dev);
trng_init();
return 0;
}
/* API implementation: get_entropy */
static int entropy_b91_trng_get_entropy(const struct device *dev,
uint8_t *buffer, uint16_t length)
{
ARG_UNUSED(dev);
uint32_t value = 0;
while (length) {
value = trng_rand();
if (length >= sizeof(value)) {
memcpy(buffer, &value, sizeof(value));
buffer += sizeof(value);
length -= sizeof(value);
} else {
memcpy(buffer, &value, length);
break;
}
}
return 0;
}
/* API implementation: get_entropy_isr */
static int entropy_b91_trng_get_entropy_isr(const struct device *dev,
uint8_t *buffer, uint16_t length,
uint32_t flags)
{
ARG_UNUSED(flags);
/* No specific handling in case of running from ISR, just call standard API */
entropy_b91_trng_get_entropy(dev, buffer, length);
return length;
}
/* Entropy driver APIs structure */
static DEVICE_API(entropy, entropy_b91_trng_api) = {
.get_entropy = entropy_b91_trng_get_entropy,
.get_entropy_isr = entropy_b91_trng_get_entropy_isr
};
/* Entropy driver registration */
DEVICE_DT_INST_DEFINE(0, entropy_b91_trng_init,
NULL, NULL, NULL,
PRE_KERNEL_1, CONFIG_ENTROPY_INIT_PRIORITY,
&entropy_b91_trng_api);