-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
entropy: rpi_pico: implement entropy driver for RP2350
Use get_rand_64() from Pico SDK for entropy. Signed-off-by: Xudong Zheng <[email protected]>
- Loading branch information
1 parent
334c6be
commit 14d6fb1
Showing
8 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters