Skip to content

Commit

Permalink
entropy: rpi_pico: implement entropy driver for RP2350
Browse files Browse the repository at this point in the history
Use get_rand_64() from Pico SDK for entropy.

Signed-off-by: Xudong Zheng <[email protected]>
  • Loading branch information
xudongzheng committed Jan 17, 2025
1 parent 334c6be commit 14d6fb1
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions drivers/entropy/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ zephyr_library_sources_ifdef(CONFIG_ENTROPY_MCUX_RNGA entropy_mcux_rnga
zephyr_library_sources_ifdef(CONFIG_ENTROPY_MCUX_TRNG entropy_mcux_trng.c)
zephyr_library_sources_ifdef(CONFIG_ENTROPY_MCUX_CAAM entropy_mcux_caam.c)
zephyr_library_sources_ifdef(CONFIG_ENTROPY_NRF5_RNG entropy_nrf5.c)
zephyr_library_sources_ifdef(CONFIG_ENTROPY_RPI_PICO_RNG entropy_rpi_pico.c)
zephyr_library_sources_ifdef(CONFIG_ENTROPY_SAM_RNG entropy_sam.c)
zephyr_library_sources_ifdef(CONFIG_ENTROPY_SMARTBOND_TRNG entropy_smartbond.c)
zephyr_library_sources_ifdef(CONFIG_ENTROPY_STM32_RNG entropy_stm32.c)
Expand Down
1 change: 1 addition & 0 deletions drivers/entropy/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ source "drivers/entropy/Kconfig.mcux"
source "drivers/entropy/Kconfig.stm32"
source "drivers/entropy/Kconfig.esp32"
source "drivers/entropy/Kconfig.nrf5"
source "drivers/entropy/Kconfig.rpi_pico"
source "drivers/entropy/Kconfig.sam"
source "drivers/entropy/Kconfig.smartbond"
source "drivers/entropy/Kconfig.native_posix"
Expand Down
12 changes: 12 additions & 0 deletions drivers/entropy/Kconfig.rpi_pico
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Raspberry Pi Pico entropy generator driver configuration

# Copyright (c) 2024 Xudong Zheng
# SPDX-License-Identifier: Apache-2.0

config ENTROPY_RPI_PICO_RNG
bool "Raspberry Pi Pico entropy number generator driver"
default y
depends on DT_HAS_RASPBERRYPI_PICO_RNG_ENABLED
select ENTROPY_HAS_DRIVER
select PICOSDK_USE_CLAIM
select PICOSDK_USE_RAND
48 changes: 48 additions & 0 deletions drivers/entropy/entropy_rpi_pico.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2024 Xudong Zheng
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <string.h>
#include <pico/rand.h>
#include <zephyr/drivers/entropy.h>
#include <zephyr/spinlock.h>

#define DT_DRV_COMPAT raspberrypi_pico_rng

static struct k_spinlock entropy_lock;

static int entropy_rpi_pico_get_entropy(const struct device *dev, uint8_t *buf, uint16_t len)
{
k_spinlock_key_t key = k_spin_lock(&entropy_lock);

__ASSERT_NO_MSG(buf != NULL);
uint8_t *buf_bytes = buf;

while (len > 0) {
uint64_t value = get_rand_64();
uint64_t to_copy = MIN(sizeof(value), len);

memcpy(buf_bytes, &value, to_copy);
buf_bytes += to_copy;
len -= to_copy;
}

k_spin_unlock(&entropy_lock, key);
return 0;
}

static DEVICE_API(entropy, entropy_rpi_pico_api_funcs) = {
.get_entropy = entropy_rpi_pico_get_entropy
};

static int entropy_rpi_pico_init(const struct device *dev)
{
return 0;
}

DEVICE_DT_INST_DEFINE(0,
entropy_rpi_pico_init, NULL, NULL, NULL,
PRE_KERNEL_1, CONFIG_ENTROPY_INIT_PRIORITY,
&entropy_rpi_pico_api_funcs);
9 changes: 9 additions & 0 deletions dts/arm/raspberrypi/rpi_pico/rp2350.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
die-temp0 = &die_temp;
};

chosen {
zephyr,entropy = &rng;
};

cpus {
#address-cells = <1>;
#size-cells = <0>;
Expand Down Expand Up @@ -424,6 +428,11 @@
resets = <&reset RPI_PICO_RESETS_RESET_PIO2>;
status = "disabled";
};

rng: rng {
status = "okay";
compatible = "raspberrypi,pico-rng";
};
};

pinctrl: pin-controller {
Expand Down
8 changes: 8 additions & 0 deletions dts/bindings/rng/raspberrypi,pico-rng.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) 2024 Xudong Zheng
# SPDX-License-Identifier: Apache-2.0

description: Raspberry Pi Pico RNG/Entropy

compatible: "raspberrypi,pico-rng"

include: base.yaml
12 changes: 12 additions & 0 deletions modules/hal_rpi_pico/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,18 @@ if(CONFIG_HAS_RPI_PICO)
zephyr_include_directories_ifdef(CONFIG_PICOSDK_USE_CLAIM
${common_dir}/hardware_claim/include)

zephyr_library_sources_ifdef(CONFIG_PICOSDK_USE_RAND
${rp2_common_dir}/hardware_timer/timer.c
${rp2_common_dir}/pico_rand/rand.c
)
zephyr_include_directories_ifdef(CONFIG_PICOSDK_USE_RAND
${common_dir}/pico_sync/include
${common_dir}/pico_time/include
${rp2_common_dir}/pico_rand/include
${rp2_common_dir}/pico_time_adapter/include
${rp2_common_dir}/pico_unique_id/include
)

zephyr_library_sources_ifdef(CONFIG_SOC_SERIES_RP2350
${rp2_common_dir}/pico_runtime_init/runtime_init.c)
zephyr_include_directories_ifdef(CONFIG_SOC_SERIES_RP2350
Expand Down
5 changes: 5 additions & 0 deletions modules/hal_rpi_pico/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,8 @@ config PICOSDK_USE_RTC
bool
help
Use the RTC driver from pico-sdk

config PICOSDK_USE_RAND
bool
help
Use the "rand" driver from pico-sdk

0 comments on commit 14d6fb1

Please sign in to comment.