feat(rng): trng refactor to ll functions on esp32s31

This commit is contained in:
gaoxu
2026-04-15 11:34:37 +08:00
parent fab36071f8
commit 3d7a643ff7
8 changed files with 116 additions and 34 deletions

View File

@@ -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();
}

View File

@@ -0,0 +1,104 @@
/*
* SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stdbool.h>
#include <stdint.h>
#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

View File

@@ -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

View File

@@ -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");

View File

@@ -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.

View File

@@ -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 <https://www.espressif.com/sites/default/files/documentation/esp32-c3_errata_en.pdf>`_. For compatibility, you can enable :ref:`CONFIG_ADC_ONESHOT_FORCE_USE_ADC2_ON_C3` to force use ADC2.

View File

@@ -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 连续转换模式。

View File

@@ -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 系列芯片勘误表 <https://www.espressif.com/sites/default/files/documentation/esp32-c3_errata_cn.pdf>`__。出于兼容性考虑可以启用 :ref:`CONFIG_ADC_ONESHOT_FORCE_USE_ADC2_ON_C3`,强制使用 ADC2。