Skip to content

Commit 29e0630

Browse files
committed
entropy: rpi_pico: implement entropy driver for RP2350
Use get_rand_64() from Pico SDK for entropy. Signed-off-by: Xudong Zheng <[email protected]>
1 parent ec3bcd3 commit 29e0630

File tree

11 files changed

+103
-0
lines changed

11 files changed

+103
-0
lines changed

boards/raspberrypi/rpi_pico2/rpi_pico2.dtsi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ gpio0_lo: &gpio0 {
113113
status = "okay";
114114
};
115115

116+
&rng {
117+
status = "okay";
118+
};
119+
116120
zephyr_udc0: &usbd {
117121
status = "okay";
118122
};

boards/raspberrypi/rpi_pico2/rpi_pico2_rp2350a_m33.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ supported:
1212
- clock
1313
- counter
1414
- dma
15+
- entropy
1516
- gpio
1617
- hwinfo
1718
- i2c

drivers/entropy/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ zephyr_library_sources_ifdef(CONFIG_ENTROPY_NXP_ELE_TRNG entropy_nxp_ele.
3434
zephyr_library_sources_ifdef(CONFIG_ENTROPY_NXP_ELS_TRNG entropy_nxp_els_trng.c)
3535
zephyr_library_sources_ifdef(CONFIG_ENTROPY_PSA_CRYPTO_RNG entropy_psa_crypto.c)
3636
zephyr_library_sources_ifdef(CONFIG_ENTROPY_RENESAS_RA entropy_renesas_ra.c)
37+
zephyr_library_sources_ifdef(CONFIG_ENTROPY_RPI_PICO_RNG entropy_rpi_pico.c)
3738
zephyr_library_sources_ifdef(CONFIG_ENTROPY_RV32M1_TRNG entropy_rv32m1_trng.c)
3839
zephyr_library_sources_ifdef(CONFIG_ENTROPY_SAM_RNG entropy_sam.c)
3940
zephyr_library_sources_ifdef(CONFIG_ENTROPY_SILABS_SIWX91X entropy_silabs_siwx91x.c)

drivers/entropy/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ source "drivers/entropy/Kconfig.nrf_cracen"
4040
source "drivers/entropy/Kconfig.nxp"
4141
source "drivers/entropy/Kconfig.psa_crypto"
4242
source "drivers/entropy/Kconfig.renesas_ra"
43+
source "drivers/entropy/Kconfig.rpi_pico"
4344
source "drivers/entropy/Kconfig.rv32m1"
4445
source "drivers/entropy/Kconfig.sam"
4546
source "drivers/entropy/Kconfig.siwx91x"

drivers/entropy/Kconfig.rpi_pico

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Raspberry Pi Pico entropy generator driver configuration
2+
3+
# Copyright (c) 2024 Xudong Zheng
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
config ENTROPY_RPI_PICO_RNG
7+
bool "Raspberry Pi Pico entropy number generator driver"
8+
default y
9+
depends on DT_HAS_RASPBERRYPI_PICO_RNG_ENABLED
10+
depends on SOC_SERIES_RP2350
11+
select ENTROPY_HAS_DRIVER
12+
select PICOSDK_USE_CLAIM
13+
select PICOSDK_USE_RAND
14+
select PICOSDK_USE_TIMER

drivers/entropy/entropy_rpi_pico.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (c) 2024 Xudong Zheng
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <string.h>
8+
#include <pico/rand.h>
9+
#include <zephyr/drivers/entropy.h>
10+
#include <zephyr/spinlock.h>
11+
12+
#define DT_DRV_COMPAT raspberrypi_pico_rng
13+
14+
static struct k_spinlock entropy_lock;
15+
16+
static int entropy_rpi_pico_get_entropy(const struct device *dev, uint8_t *buf, uint16_t len)
17+
{
18+
__ASSERT_NO_MSG(buf != NULL);
19+
uint8_t *buf_bytes = buf;
20+
21+
while (len > 0) {
22+
uint64_t value;
23+
size_t to_copy = MIN(sizeof(value), len);
24+
25+
K_SPINLOCK(&entropy_lock) {
26+
value = get_rand_64();
27+
}
28+
memcpy(buf_bytes, &value, to_copy);
29+
buf_bytes += to_copy;
30+
len -= to_copy;
31+
}
32+
33+
return 0;
34+
}
35+
36+
static DEVICE_API(entropy, entropy_rpi_pico_api_funcs) = {
37+
.get_entropy = entropy_rpi_pico_get_entropy
38+
};
39+
40+
DEVICE_DT_INST_DEFINE(0,
41+
NULL, NULL, NULL, NULL,
42+
PRE_KERNEL_1, CONFIG_ENTROPY_INIT_PRIORITY,
43+
&entropy_rpi_pico_api_funcs);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright (c) 2024 Xudong Zheng
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: Raspberry Pi Pico RNG/Entropy
5+
6+
compatible: "raspberrypi,pico-rng"
7+
8+
include: base.yaml

dts/vendor/raspberrypi/rpi_pico/rp2350.dtsi

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
die-temp0 = &die_temp;
2222
};
2323

24+
chosen {
25+
zephyr,entropy = &rng;
26+
};
27+
2428
cpus {
2529
#address-cells = <1>;
2630
#size-cells = <0>;
@@ -453,6 +457,12 @@
453457
interrupt-names = "irq0", "irq1";
454458
status = "disabled";
455459
};
460+
461+
rng: rng@400f0000 {
462+
compatible = "raspberrypi,pico-rng";
463+
reg = <0x400f0000 DT_SIZE_K(4)>;
464+
status = "disabled";
465+
};
456466
};
457467

458468
pinctrl: pin-controller {

modules/hal_rpi_pico/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,18 @@ if(CONFIG_HAS_RPI_PICO)
157157
zephyr_include_directories_ifdef(CONFIG_PICOSDK_USE_CLAIM
158158
${common_dir}/hardware_claim/include)
159159

160+
zephyr_library_sources_ifdef(CONFIG_PICOSDK_USE_RAND
161+
${rp2_common_dir}/pico_rand/rand.c
162+
)
163+
zephyr_include_directories_ifdef(CONFIG_PICOSDK_USE_RAND
164+
${common_dir}/pico_sync/include
165+
${common_dir}/pico_time/include
166+
${rp2_common_dir}/pico_rand/include
167+
${rp2_common_dir}/pico_time_adapter/include
168+
${rp2_common_dir}/pico_unique_id/include
169+
)
170+
zephyr_linker_sources_ifdef(CONFIG_PICOSDK_USE_RAND DATA_SECTIONS uninit_data.ld)
171+
160172
zephyr_include_directories_ifdef(CONFIG_RISCV
161173
${common_dir}/pico_sync/include
162174
${common_dir}/pico_time/include

modules/hal_rpi_pico/Kconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,8 @@ config PICOSDK_USE_RTC
5454
bool
5555
help
5656
Use the RTC driver from pico-sdk
57+
58+
config PICOSDK_USE_RAND
59+
bool
60+
help
61+
Use the "rand" driver from pico-sdk

0 commit comments

Comments
 (0)