From 3d7a643ff701af9e28aa116ffcaca20f1b8f53e2 Mon Sep 17 00:00:00 2001 From: gaoxu Date: Wed, 15 Apr 2026 11:34:37 +0800 Subject: [PATCH] feat(rng): trng refactor to ll functions on esp32s31 --- .../src/bootloader_random_esp32s31.c | 28 +---- components/hal/esp32s31/include/hal/trng_ll.h | 104 ++++++++++++++++++ .../soc/esp32s31/include/soc/wdev_reg.h | 9 +- .../soc/esp32s31/register/soc/trng_struct.h | 1 + .../peripherals/adc/adc_continuous.rst | 2 +- .../peripherals/adc/adc_oneshot.rst | 2 +- .../peripherals/adc/adc_continuous.rst | 2 +- .../peripherals/adc/adc_oneshot.rst | 2 +- 8 files changed, 116 insertions(+), 34 deletions(-) create mode 100644 components/hal/esp32s31/include/hal/trng_ll.h diff --git a/components/bootloader_support/src/bootloader_random_esp32s31.c b/components/bootloader_support/src/bootloader_random_esp32s31.c index 3b70082b018..3dda3bc86ed 100644 --- a/components/bootloader_support/src/bootloader_random_esp32s31.c +++ b/components/bootloader_support/src/bootloader_random_esp32s31.c @@ -5,36 +5,14 @@ */ #include "sdkconfig.h" #include "bootloader_random.h" -#include "soc/trng_reg.h" -#include "rom/ets_sys.h" -#include "rom/uart.h" - +#include "hal/trng_ll.h" void bootloader_random_enable(void) { - // Enable entropy source of ring - SET_PERI_REG_MASK(TRNG_CONF_REG, TRNG_SAMPLE_ENABLE); - - // Enable entropy source of crc - SET_PERI_REG_MASK(TRNG_CONF_REG, TRNG_NOISE_CRC_EN); - - // //For dieharder test - // ets_printf("S31: Random bytes (%s) follow:\n","test"); - // while (1) - // { - // uint32_t w = READ_PERI_REG(TRNG_CRC_SYNC_DATA_REG); - // // ets_printf("rng_data:%d\n", w); - // // uart_tx_wait_idle(0); - - // uart_tx_one_char(w >> 24); - // uart_tx_one_char(w >> 16); - // uart_tx_one_char(w >> 8); - // uart_tx_one_char(w); - // } + trng_ll_enable(); } void bootloader_random_disable(void) { - CLEAR_PERI_REG_MASK(TRNG_CONF_REG, TRNG_SAMPLE_ENABLE); - CLEAR_PERI_REG_MASK(TRNG_CONF_REG, TRNG_NOISE_CRC_EN); + trng_ll_disable(); } diff --git a/components/hal/esp32s31/include/hal/trng_ll.h b/components/hal/esp32s31/include/hal/trng_ll.h new file mode 100644 index 00000000000..2925a857d3d --- /dev/null +++ b/components/hal/esp32s31/include/hal/trng_ll.h @@ -0,0 +1,104 @@ +/* + * SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include +#include +#include "soc/soc.h" +#include "soc/trng_struct.h" +#include "soc/lp_peri_clkrst_struct.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Enable LP TRNG bus clock + */ +static inline void trng_ll_enable_bus_clock(bool enable) +{ + LP_PERI_CLKRST.rng_ctrl.lp_rng_clk_en = enable; +} + +/** + * @brief Reset LP TRNG bus clock + * + */ +static inline void trng_ll_reset(void) +{ + LP_PERI_CLKRST.rng_ctrl.lp_rng_rst_en = 1; + LP_PERI_CLKRST.rng_ctrl.lp_rng_rst_en = 0; +} + +/** + * @brief Enable or disable TRNG module clock + * + * @param enable true to enable, false to disable + */ +static inline void trng_ll_enable_clock(bool enable) +{ + LP_TRNG.date.clk_en = enable; +} + +/** + * @brief Enable or disable TRNG sampling + * + * @param enable true to enable, false to disable + */ +static inline void trng_ll_enable_sample(bool enable) +{ + LP_TRNG.conf.sample_enable = enable; +} + +/** + * @brief Enable or disable CRC RNG source + * + * @param enable true to enable CRC RNG, false to disable + */ +static inline void trng_ll_enable_noise_crc(bool enable) +{ + LP_TRNG.conf.noise_crc_en = enable; +} + +/** + * @brief Read random data from TRNG + */ +static inline uint32_t trng_ll_read_data(void) +{ + return LP_TRNG.crc_sync_data.sw_crc_random_data_sync; +} + +/** + * @brief Enable TRNG module + * + * TODO: unify in trng_hal.c + */ +static inline void trng_ll_enable(void) +{ + trng_ll_enable_bus_clock(true); + trng_ll_reset(); + trng_ll_enable_clock(true); + trng_ll_enable_sample(true); + trng_ll_enable_noise_crc(true); +} + +/** + * @brief Disable TRNG module + * + * TODO: unify in trng_hal.c + */ +static inline void trng_ll_disable(void) +{ + trng_ll_enable_sample(false); + trng_ll_enable_noise_crc(false); + trng_ll_enable_clock(false); + trng_ll_enable_bus_clock(false); +} + +#ifdef __cplusplus +} +#endif diff --git a/components/soc/esp32s31/include/soc/wdev_reg.h b/components/soc/esp32s31/include/soc/wdev_reg.h index 9b8dfd7fc36..1bd1619d4be 100644 --- a/components/soc/esp32s31/include/soc/wdev_reg.h +++ b/components/soc/esp32s31/include/soc/wdev_reg.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2025-2026 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 OR MIT */ @@ -7,8 +7,7 @@ #pragma once #include "soc.h" +#include "soc/trng_reg.h" -// TODO: to be checked IDF-14632 - -/* Hardware random number generator register */ -#define WDEV_RND_REG 0x20814000 +/* Hardware random number generator register (CRC-synchronized TRNG output) */ +#define WDEV_RND_REG TRNG_CRC_SYNC_DATA_REG diff --git a/components/soc/esp32s31/register/soc/trng_struct.h b/components/soc/esp32s31/register/soc/trng_struct.h index ab6af8e036a..8831e3ef1f7 100644 --- a/components/soc/esp32s31/register/soc/trng_struct.h +++ b/components/soc/esp32s31/register/soc/trng_struct.h @@ -380,6 +380,7 @@ typedef struct { volatile trng_date_reg_t date; } trng_dev_t; +extern trng_dev_t LP_TRNG; #ifndef __cplusplus _Static_assert(sizeof(trng_dev_t) == 0x100, "Invalid size of trng_dev_t structure"); diff --git a/docs/en/api-reference/peripherals/adc/adc_continuous.rst b/docs/en/api-reference/peripherals/adc/adc_continuous.rst index f7b80889275..46681f97e53 100644 --- a/docs/en/api-reference/peripherals/adc/adc_continuous.rst +++ b/docs/en/api-reference/peripherals/adc/adc_continuous.rst @@ -388,7 +388,7 @@ Hardware Limitations .. list:: - A specific ADC unit can only work under one operating mode at any one time, either continuous mode or one-shot mode. :cpp:func:`adc_continuous_start` has provided the protection. - - Random Number Generator (RNG) uses ADC as an input source. When ADC continuous mode driver works, the random number generated from RNG will be less random. + :not esp32s31: - Random Number Generator (RNG) uses ADC as an input source. When ADC continuous mode driver works, the random number generated from RNG will be less random. :esp32 or esp32s2: - ADC2 is also used by Wi-Fi. :cpp:func:`adc_continuous_start` has provided the protection between Wi-Fi driver and ADC continuous mode driver. :esp32: - ADC continuous mode driver uses I2S0 peripheral as hardware DMA FIFO. Therefore, if I2S0 is in use already, the :cpp:func:`adc_continuous_new_handle` will return :c:macro:`ESP_ERR_NOT_FOUND`. :esp32: - ESP32 DevKitC: GPIO 0 cannot be used due to external auto program circuits. diff --git a/docs/en/api-reference/peripherals/adc/adc_oneshot.rst b/docs/en/api-reference/peripherals/adc/adc_oneshot.rst index c8ff7160c73..6336dccba98 100644 --- a/docs/en/api-reference/peripherals/adc/adc_oneshot.rst +++ b/docs/en/api-reference/peripherals/adc/adc_oneshot.rst @@ -159,7 +159,7 @@ Hardware Limitations .. list:: - - Random Number Generator (RNG) uses ADC as an input source. When ADC :cpp:func:`adc_oneshot_read` works, the random number generated from RNG will be less random. + :not esp32s31: - Random Number Generator (RNG) uses ADC as an input source. When ADC :cpp:func:`adc_oneshot_read` works, the random number generated from RNG will be less random. :SOC_ADC_DMA_SUPPORTED: - A specific ADC unit can only work under one operating mode at any one time, either continuous mode or oneshot mode. :cpp:func:`adc_oneshot_read` has provided the protection. :esp32 or esp32s2 or esp32s3: - ADC2 is also used by Wi-Fi. :cpp:func:`adc_oneshot_read` has provided protection between the Wi-Fi driver and ADC oneshot mode driver. :esp32c3: - ADC2 oneshot mode is no longer supported, due to hardware limitations. The results are not stable. This issue can be found in `ESP32-C3 Series SoC Errata `_. For compatibility, you can enable :ref:`CONFIG_ADC_ONESHOT_FORCE_USE_ADC2_ON_C3` to force use ADC2. diff --git a/docs/zh_CN/api-reference/peripherals/adc/adc_continuous.rst b/docs/zh_CN/api-reference/peripherals/adc/adc_continuous.rst index 4bab568dfeb..4deeb2fb27d 100644 --- a/docs/zh_CN/api-reference/peripherals/adc/adc_continuous.rst +++ b/docs/zh_CN/api-reference/peripherals/adc/adc_continuous.rst @@ -388,7 +388,7 @@ ADC 连续转换模式读取的原始数据需要进一步解析才能获得可 .. list:: - 一个 ADC 单元一次只能运行一种操作模式,即连续模式或单次模式。:cpp:func:`adc_continuous_start` 提供了保护措施。 - - 随机数生成器 (RNG) 以 ADC 为输入源。使用 ADC 连续转换模式驱动从 RNG 生成随机数时,随机性会减弱。 + :not esp32s31: - 随机数生成器 (RNG) 以 ADC 为输入源。使用 ADC 连续转换模式驱动从 RNG 生成随机数时,随机性会减弱。 :esp32 or esp32s2: - Wi-Fi 也使用 ADC2,:cpp:func:`adc_continuous_start` 提供了 Wi-Fi 驱动和 ADC 连续转换模式驱动之间的保护。 :esp32: - ADC 连续转换模式驱动使用 I2S0 外设作为硬件 DMA FIFO。因此,如果 I2S0 已在使用中,:cpp:func:`adc_continuous_new_handle` 将返回 :c:macro:`ESP_ERR_NOT_FOUND`。 :esp32: - ESP32 DevKitC:由于存在外部自动烧录电路,GPIO 0 不能用于 ADC 连续转换模式。 diff --git a/docs/zh_CN/api-reference/peripherals/adc/adc_oneshot.rst b/docs/zh_CN/api-reference/peripherals/adc/adc_oneshot.rst index 6fbbce9adce..3b8d9a63831 100644 --- a/docs/zh_CN/api-reference/peripherals/adc/adc_oneshot.rst +++ b/docs/zh_CN/api-reference/peripherals/adc/adc_oneshot.rst @@ -159,7 +159,7 @@ ADC 单次转换模式驱动基于 {IDF_TARGET_NAME} SAR ADC 模块实现,不 .. list:: - - 随机数生成器 (RNG) 以 ADC 为输入源。使用 ADC 单次转换模式驱动从 RNG 生成随机数时,随机性会减弱。 + :not esp32s31: - 随机数生成器 (RNG) 以 ADC 为输入源。使用 ADC 单次转换模式驱动从 RNG 生成随机数时,随机性会减弱。 :SOC_ADC_DMA_SUPPORTED: - 一个 ADC 单元每次只能在一种操作模式下运行,可以是连续模式或单次模式。:cpp:func:`adc_oneshot_start` 提供了保护措施。 :esp32 or esp32s2 or esp32s3: - Wi-Fi 也使用 ADC2,:cpp:func:`adc_oneshot_read` 提供了 Wi-Fi 驱动与 ADC 单次转换模式驱动间的保护。 :esp32c3: - 由于硬件限制,现已不再支持使用 ADC2 DMA 功能获取 ADC 转换结果。使用 ADC2 单次转换的结果可能不稳定,具体可参考 `ESP32-C3 系列芯片勘误表 `__。出于兼容性考虑,可以启用 :ref:`CONFIG_ADC_ONESHOT_FORCE_USE_ADC2_ON_C3`,强制使用 ADC2。