Skip to content

Commit df219c4

Browse files
committed
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 9538a7c commit df219c4

File tree

4 files changed

+127
-1
lines changed

4 files changed

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

targets/targets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2456,7 +2456,7 @@
24562456
"extra_labels": ["RENESAS", "RZ_A1XX", "RZA1UL", "MBRZA1LU"],
24572457
"supported_toolchains": ["ARM", "GCC_ARM", "IAR"],
24582458
"inherits": ["Target"],
2459-
"device_has": ["ANALOGIN", "CAN", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES"],
2459+
"device_has": ["ANALOGIN", "CAN", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES", "TRNG"],
24602460
"features": ["LWIP"],
24612461
"release_versions": ["2", "5"]
24622462
},

0 commit comments

Comments
 (0)