Skip to content

Commit 884d02d

Browse files
TomoYamanakaadbridge
authored andcommitted
Support TRNG function for GR-LYCHEE
I supported the TRNG function when target is GR-LYCHEE. GR-LYCHEE generates TRNG by acquiring the random number of Wifi module(ESP32) incorporated in it using I2C.
1 parent 133f404 commit 884d02d

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2006-2017 ARM Limited
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+
#if defined(DEVICE_TRNG)
18+
#include "drivers/I2C.h"
19+
#include "platform/mbed_wait_api.h"
20+
21+
#define ESP32_I2C_ADDR (0x28<<1)
22+
23+
extern "C" void trng_init_esp32(void)
24+
{
25+
/* P5_3(EN), P3_14(IO0) */
26+
if (((GPIOP5 & 0x0008) == 0)
27+
|| ((GPIOPMC5 & 0x0008) != 0)
28+
|| ((GPIOPM5 & 0x0008) != 0)
29+
|| ((GPIOP3 & 0x4000) == 0)
30+
|| ((GPIOPMC3 & 0x4000) != 0)
31+
|| ((GPIOPM3 & 0x4000) != 0)) {
32+
33+
/* P5_3(EN) */
34+
GPIOP5 &= ~0x0008; /* Outputs low level */
35+
GPIOPMC5 &= ~0x0008; /* Port mode */
36+
GPIOPM5 &= ~0x0008; /* Output mode */
37+
38+
/* P3_14(IO0) */
39+
GPIOP3 &= ~0x4000; /* Outputs low level */
40+
GPIOPMC3 &= ~0x4000; /* Port mode */
41+
GPIOPM3 &= ~0x4000; /* Output mode */
42+
43+
GPIOP3 |= 0x4000; /* Outputs hi level */
44+
wait_ms(5);
45+
GPIOP5 |= 0x0008; /* Outputs hi level */
46+
}
47+
}
48+
49+
extern "C" void trng_free_esp32(void)
50+
{
51+
// do nothing
52+
}
53+
54+
extern "C" int trng_get_bytes_esp32(uint8_t *output, size_t length, size_t *output_length)
55+
{
56+
mbed::I2C mI2c(I2C_SDA, I2C_SCL);
57+
int ret;
58+
char send_data[1];
59+
char recv_data[4];
60+
size_t idx = 0;
61+
int i;
62+
int err_cnt = 0;
63+
64+
while (idx < length) {
65+
send_data[0] = 0;
66+
ret = mI2c.write(ESP32_I2C_ADDR, send_data, 1);
67+
if (ret == 0) {
68+
mI2c.read(ESP32_I2C_ADDR, recv_data, sizeof(recv_data));
69+
for (i = 0; (i < 4) && (idx < length); i++) {
70+
output[idx++] = recv_data[i];
71+
}
72+
} else {
73+
err_cnt++;
74+
if (err_cnt >= 20) {
75+
break;
76+
}
77+
wait_ms(100);
78+
}
79+
}
80+
if (output_length != NULL) {
81+
*output_length = idx;
82+
}
83+
84+
return (idx != 0 ? 0 : -1);
85+
}
86+
#endif

targets/TARGET_RENESAS/TARGET_RZ_A1XX/objects.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ struct can_s {
7777
uint32_t ch;
7878
};
7979

80+
struct trng_s {
81+
uint8_t dummy;
82+
};
83+
8084
#ifdef __cplusplus
8185
}
8286
#endif
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2006-2017 ARM Limited
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+
#if defined(DEVICE_TRNG)
18+
#include "trng_api.h"
19+
20+
#if defined(TARGET_GR_LYCHEE)
21+
22+
extern void trng_init_esp32(void);
23+
extern void trng_free_esp32(void);
24+
extern int trng_get_bytes_esp32(uint8_t *output, size_t length, size_t *output_length);
25+
26+
void trng_init(trng_t *obj)
27+
{
28+
trng_init_esp32();
29+
}
30+
31+
void trng_free(trng_t *obj)
32+
{
33+
trng_free_esp32();
34+
}
35+
36+
int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length)
37+
{
38+
return trng_get_bytes_esp32(output, length, output_length);
39+
}
40+
#else
41+
#error "There is no initialization processing."
42+
#endif
43+
#endif

targets/targets.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2571,6 +2571,7 @@
25712571
"inherits": ["RZ_A1XX"],
25722572
"supported_form_factors": ["ARDUINO"],
25732573
"extra_labels_add": ["RZA1UL", "MBRZA1LU"],
2574+
"device_has_add": ["TRNG"],
25742575
"device_has_remove": ["ETHERNET"],
25752576
"features_remove": ["LWIP"],
25762577
"release_versions": ["2", "5"]

0 commit comments

Comments
 (0)