Skip to content

Commit fc54eeb

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 03fa6a0 commit fc54eeb

File tree

10 files changed

+102
-0
lines changed

10 files changed

+102
-0
lines changed

boards/raspberrypi/rpi_pico2/rpi_pico2.dtsi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@
137137
status = "okay";
138138
};
139139

140+
&rng {
141+
status = "okay";
142+
};
143+
140144
zephyr_udc0: &usbd {
141145
status = "okay";
142146
};

drivers/entropy/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ zephyr_library_sources_ifdef(CONFIG_ENTROPY_MCUX_TRNG entropy_mcux_trng
1313
zephyr_library_sources_ifdef(CONFIG_ENTROPY_MCUX_CAAM entropy_mcux_caam.c)
1414
zephyr_library_sources_ifdef(CONFIG_ENTROPY_NRF5_RNG entropy_nrf5.c)
1515
zephyr_library_sources_ifdef(CONFIG_ENTROPY_NRF_CRACEN_CTR_DRBG entropy_nrf_cracen.c)
16+
zephyr_library_sources_ifdef(CONFIG_ENTROPY_RPI_PICO_RNG entropy_rpi_pico.c)
1617
zephyr_library_sources_ifdef(CONFIG_ENTROPY_SAM_RNG entropy_sam.c)
1718
zephyr_library_sources_ifdef(CONFIG_ENTROPY_SMARTBOND_TRNG entropy_smartbond.c)
1819
zephyr_library_sources_ifdef(CONFIG_ENTROPY_STM32_RNG entropy_stm32.c)

drivers/entropy/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ source "drivers/entropy/Kconfig.stm32"
2727
source "drivers/entropy/Kconfig.esp32"
2828
source "drivers/entropy/Kconfig.nrf5"
2929
source "drivers/entropy/Kconfig.nrf_cracen"
30+
source "drivers/entropy/Kconfig.rpi_pico"
3031
source "drivers/entropy/Kconfig.sam"
3132
source "drivers/entropy/Kconfig.smartbond"
3233
source "drivers/entropy/Kconfig.native_posix"

drivers/entropy/Kconfig.rpi_pico

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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

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);

dts/arm/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>;
@@ -424,6 +428,12 @@
424428
resets = <&reset RPI_PICO_RESETS_RESET_PIO2>;
425429
status = "disabled";
426430
};
431+
432+
rng: rng@400f0000 {
433+
compatible = "raspberrypi,pico-rng";
434+
reg = <0x400f0000 DT_SIZE_K(4)>;
435+
status = "disabled";
436+
};
427437
};
428438

429439
pinctrl: pin-controller {
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

modules/hal_rpi_pico/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,19 @@ if(CONFIG_HAS_RPI_PICO)
151151
zephyr_include_directories_ifdef(CONFIG_PICOSDK_USE_CLAIM
152152
${common_dir}/hardware_claim/include)
153153

154+
zephyr_library_sources_ifdef(CONFIG_PICOSDK_USE_RAND
155+
${rp2_common_dir}/hardware_timer/timer.c
156+
${rp2_common_dir}/pico_rand/rand.c
157+
)
158+
zephyr_include_directories_ifdef(CONFIG_PICOSDK_USE_RAND
159+
${common_dir}/pico_sync/include
160+
${common_dir}/pico_time/include
161+
${rp2_common_dir}/pico_rand/include
162+
${rp2_common_dir}/pico_time_adapter/include
163+
${rp2_common_dir}/pico_unique_id/include
164+
)
165+
zephyr_linker_sources_ifdef(CONFIG_PICOSDK_USE_RAND DATA_SECTIONS uninit_data.ld)
166+
154167
zephyr_library_sources_ifdef(CONFIG_SOC_SERIES_RP2350
155168
${rp2_common_dir}/pico_runtime_init/runtime_init.c)
156169
zephyr_include_directories_ifdef(CONFIG_SOC_SERIES_RP2350

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
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.uninitialized_data (NOLOAD): {
2+
. = ALIGN(4);
3+
*(.uninitialized_data*)
4+
} > RAM

0 commit comments

Comments
 (0)