From 8c74734591b973e1b3e79b6048d95050dd4650ff Mon Sep 17 00:00:00 2001 From: gaoxu Date: Thu, 22 Jan 2026 16:38:02 +0800 Subject: [PATCH] feat(rng): basic rng support on esp32h4 --- .../src/bootloader_random_esp32h4.c | 40 +++++++++++++++++++ .../esp32h4/include/soc/Kconfig.soc_caps.in | 4 ++ components/soc/esp32h4/include/soc/soc_caps.h | 1 + 3 files changed, 45 insertions(+) create mode 100644 components/bootloader_support/src/bootloader_random_esp32h4.c diff --git a/components/bootloader_support/src/bootloader_random_esp32h4.c b/components/bootloader_support/src/bootloader_random_esp32h4.c new file mode 100644 index 00000000000..7ff1b309cf9 --- /dev/null +++ b/components/bootloader_support/src/bootloader_random_esp32h4.c @@ -0,0 +1,40 @@ +/* + * SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD + + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include "sdkconfig.h" +#include "bootloader_random.h" +#include "soc/soc.h" +#include "soc/rng_reg.h" +#include "esp_rom_sys.h" + + + +void bootloader_random_enable(void) +{ + REG_SET_BIT(RNG_DATE_REG, RNG_CLK_EN); + REG_SET_BIT(RNG_CFG_REG, RNG_TIMER_EN); + REG_SET_FIELD(RNG_CFG_REG, RNG_RTC_TIMER_EN, 0x3); + REG_SET_BIT(RNG_CFG_REG, RNG_SAMPLE_ENABLE); + + + for (int i = 0; i < 10; i++) { + uint32_t random_sync = REG_READ(RNG_DATA_SYNC_REG); + uint32_t random_unsync = REG_READ(RNG_DATA_REG); + uint32_t rng_cfg = REG_READ(RNG_CFG_REG); + uint32_t sample_cnt = (rng_cfg >> RNG_SAMPLE_CNT_S) & 0xFF; + esp_rom_printf(" [%d] sync: 0x%08x, unsync: 0x%08x, sample_cnt: 0x%02x\r\n", + i + 1, random_sync, random_unsync, sample_cnt); + esp_rom_delay_us(1000); + } +} + +void bootloader_random_disable(void) +{ + REG_CLR_BIT(RNG_CFG_REG, RNG_SAMPLE_ENABLE); + REG_SET_FIELD(RNG_CFG_REG, RNG_RTC_TIMER_EN, 0x0); + REG_CLR_BIT(RNG_CFG_REG, RNG_TIMER_EN); + REG_CLR_BIT(RNG_DATE_REG, RNG_CLK_EN); +} diff --git a/components/soc/esp32h4/include/soc/Kconfig.soc_caps.in b/components/soc/esp32h4/include/soc/Kconfig.soc_caps.in index 4ce6694398b..2208ead0946 100644 --- a/components/soc/esp32h4/include/soc/Kconfig.soc_caps.in +++ b/components/soc/esp32h4/include/soc/Kconfig.soc_caps.in @@ -123,6 +123,10 @@ config SOC_I2C_SUPPORTED bool default y +config SOC_RNG_SUPPORTED + bool + default y + config SOC_SYSTIMER_SUPPORTED bool default y diff --git a/components/soc/esp32h4/include/soc/soc_caps.h b/components/soc/esp32h4/include/soc/soc_caps.h index 4fd46b63e48..32dd2f8c0f1 100644 --- a/components/soc/esp32h4/include/soc/soc_caps.h +++ b/components/soc/esp32h4/include/soc/soc_caps.h @@ -64,6 +64,7 @@ #define SOC_GPSPI_SUPPORTED 1 #define SOC_LEDC_SUPPORTED 1 #define SOC_I2C_SUPPORTED 1 +#define SOC_RNG_SUPPORTED 1 #define SOC_SYSTIMER_SUPPORTED 1 // #define SOC_SUPPORT_COEXISTENCE 1 // TODO: [ESP32H4] IDF-12251 IDF-12252 IDF-12253 #define SOC_AES_SUPPORTED 1