From ecf795f0f1230ff2b4e0442b666d5be9b2b7ee95 Mon Sep 17 00:00:00 2001 From: Ali Hozhabri Date: Thu, 15 May 2025 11:26:58 +0200 Subject: [PATCH] drivers: bluetooth: hci: Use TRNG peripheral for BLE purpose on WB0x Call entropy APIs to use TRNG peripheral on STM32WB0x devices for BLE purposes. Enable RNG node on Nucleo-WB0x boards. Remove RNG initialization as it's done in the entropy driver. Signed-off-by: Ali Hozhabri --- boards/st/nucleo_wb05kz/nucleo_wb05kz.dts | 5 +++++ boards/st/nucleo_wb07cc/nucleo_wb07cc.dts | 5 +++++ boards/st/nucleo_wb09ke/nucleo_wb09ke.dts | 5 +++++ drivers/bluetooth/hci/hci_stm32wb0.c | 27 +++++++++++++++++++++-- 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/boards/st/nucleo_wb05kz/nucleo_wb05kz.dts b/boards/st/nucleo_wb05kz/nucleo_wb05kz.dts index fa82620367b..63c88eb0881 100644 --- a/boards/st/nucleo_wb05kz/nucleo_wb05kz.dts +++ b/boards/st/nucleo_wb05kz/nucleo_wb05kz.dts @@ -24,6 +24,7 @@ zephyr,sram = &sram0; zephyr,flash = &flash0; zephyr,bt-c2h-uart = &usart1; + zephyr,entropy = &rng; }; leds: leds { @@ -167,6 +168,10 @@ }; }; +&rng { + status = "okay"; +}; + &flash0 { partitions { compatible = "fixed-partitions"; diff --git a/boards/st/nucleo_wb07cc/nucleo_wb07cc.dts b/boards/st/nucleo_wb07cc/nucleo_wb07cc.dts index b8e411b9fca..2a967e83ced 100644 --- a/boards/st/nucleo_wb07cc/nucleo_wb07cc.dts +++ b/boards/st/nucleo_wb07cc/nucleo_wb07cc.dts @@ -24,6 +24,7 @@ zephyr,sram = &sram0; zephyr,flash = &flash0; zephyr,bt-c2h-uart = &usart1; + zephyr,entropy = &rng; }; leds: leds { @@ -164,6 +165,10 @@ }; }; +&rng { + status = "okay"; +}; + &flash0 { partitions { compatible = "fixed-partitions"; diff --git a/boards/st/nucleo_wb09ke/nucleo_wb09ke.dts b/boards/st/nucleo_wb09ke/nucleo_wb09ke.dts index 714ad30d0b3..f9b1b9e274d 100644 --- a/boards/st/nucleo_wb09ke/nucleo_wb09ke.dts +++ b/boards/st/nucleo_wb09ke/nucleo_wb09ke.dts @@ -24,6 +24,7 @@ zephyr,sram = &sram0; zephyr,flash = &flash0; zephyr,bt-c2h-uart = &usart1; + zephyr,entropy = &rng; }; leds: leds { @@ -166,6 +167,10 @@ }; }; +&rng { + status = "okay"; +}; + &flash0 { partitions { compatible = "fixed-partitions"; diff --git a/drivers/bluetooth/hci/hci_stm32wb0.c b/drivers/bluetooth/hci/hci_stm32wb0.c index 49926665d6b..e6d3e16dd32 100644 --- a/drivers/bluetooth/hci/hci_stm32wb0.c +++ b/drivers/bluetooth/hci/hci_stm32wb0.c @@ -10,6 +10,7 @@ #include #include #include +#include #include "bleplat_cntr.h" #include "ble_stack.h" #include "stm32wb0x_hal_radio_timer.h" @@ -20,7 +21,6 @@ #include "dm_alloc.h" #include "aci_adv_nwk.h" #include "app_common.h" -#include "hw_rng.h" #include "hw_aes.h" #include "hw_pka.h" @@ -301,6 +301,30 @@ static void ble_isr_installer(void) IRQ_CONNECT(PKA_IRQn, PKA_PRIO, _PKA_IRQHandler, NULL, PKA_FLAGS); } +static void rng_get_random(void *num, size_t size) +{ + const struct device *dev = DEVICE_DT_GET(DT_DRV_INST(0)); + int res; + + /* try to allocate from pool */ + res = entropy_get_entropy_isr(dev, (uint8_t *)num, size, !ENTROPY_BUSYWAIT); + if (res != size) { + /* Not enough available random numbers, so it falls back to polling */ + entropy_get_entropy_isr(dev, (uint8_t *)num, size, ENTROPY_BUSYWAIT); + } +} + +/* BLEPLAT_RngGetRandomXX definitions are needed for the BLE library. */ +void BLEPLAT_RngGetRandom16(uint16_t *num) +{ + rng_get_random(num, sizeof(*num)); +} + +void BLEPLAT_RngGetRandom32(uint32_t *num) +{ + rng_get_random(num, sizeof(*num)); +} + static struct net_buf *get_rx(uint8_t *msg) { bool discardable = false; @@ -465,7 +489,6 @@ static int bt_hci_stm32wb0_open(const struct device *dev, bt_hci_recv_t recv) HAL_RADIO_Init(&hradio); HAL_RADIO_TIMER_Init(&VTIMER_InitStruct); - HW_RNG_Init(); HW_AES_Init(); hpka.Instance = PKA; HAL_PKA_Init(&hpka);