Merge branch 'feat/basic_rng_support_h4' into 'master'

RNG support and refactor on ESP32-H4

Closes IDF-12265

See merge request espressif/esp-idf!45361
This commit is contained in:
Gao Xu
2026-05-27 11:14:38 +08:00
38 changed files with 561 additions and 316 deletions
+114 -3
View File
@@ -6,14 +6,15 @@
#pragma once
#include <stdbool.h>
#include <stdint.h>
#include "hal/config.h"
#include "soc/soc.h"
#include "soc/lp_system_reg.h"
#if HAL_CONFIG(CHIP_SUPPORT_MIN_REV) >= 300
#include "soc/trng_reg.h"
#include "soc/trng_reg.h"
#include "soc/trng_struct.h"
#else
#include "soc/lp_system_reg.h"
#include "soc/lp_system_reg.h"
#endif
#ifdef __cplusplus
@@ -29,6 +30,116 @@ static inline uint32_t rng_ll_read_data(void)
#endif
}
/**
* @brief Enable or disable RNG clock
*
* @param enable true to enable, false to disable
*/
static inline void rng_ll_enable_clock(bool enable)
{
#if HAL_CONFIG(CHIP_SUPPORT_MIN_REV) >= 300
LP_TRNG.date.clk_en = enable;
#else
(void) enable;
#endif
}
/**
* @brief Reset RNG module
*/
static inline void rng_ll_reset(void)
{
#if HAL_CONFIG(CHIP_SUPPORT_MIN_REV) >= 300
LP_TRNG.rstn.rstn = 0;
LP_TRNG.rstn.rstn = 1;
#endif
}
/**
* @brief Enable or disable RNG sampling
*
* @param enable true to enable, false to disable
*/
static inline void rng_ll_enable_sample(bool enable)
{
#if HAL_CONFIG(CHIP_SUPPORT_MIN_REV) >= 300
LP_TRNG.cfg.sample_enable = enable;
#else
(void) enable;
#endif
}
/**
* @brief Set RNG timer prescaler
*
* @param prescaler Timer prescaler value (0-255)
*/
static inline void rng_ll_set_timer_prescaler(uint8_t prescaler)
{
#if HAL_CONFIG(CHIP_SUPPORT_MIN_REV) >= 300
LP_TRNG.cfg.timer_pscale = prescaler;
#else
(void) prescaler;
#endif
}
/**
* @brief Enable or disable RNG timer XOR
*
* @param enable true to enable, false to disable
*/
static inline void rng_ll_enable_timer(bool enable)
{
#if HAL_CONFIG(CHIP_SUPPORT_MIN_REV) >= 300
LP_TRNG.cfg.timer_en = enable;
#else
(void) enable;
#endif
}
/**
* @brief Get RNG sample count
*
* @return Current sample count (0-255)
*/
static inline uint8_t rng_ll_get_sample_count(void)
{
#if HAL_CONFIG(CHIP_SUPPORT_MIN_REV) >= 300
return (uint8_t)LP_TRNG.cfg.sample_cnt;
#else
return 0;
#endif
}
/**
* @brief Enable RNG module
*
* TODO: unify in rng_hal.c
*/
static inline void rng_ll_enable(void)
{
#if HAL_CONFIG(CHIP_SUPPORT_MIN_REV) >= 300
rng_ll_enable_clock(true);
rng_ll_reset();
rng_ll_enable_timer(true);
rng_ll_enable_sample(true);
#endif
}
/**
* @brief Disable RNG module
*
* TODO: unify in rng_hal.c
*/
static inline void rng_ll_disable(void)
{
#if HAL_CONFIG(CHIP_SUPPORT_MIN_REV) >= 300
rng_ll_enable_sample(false);
rng_ll_enable_timer(false);
rng_ll_enable_clock(false);
#endif
}
#ifdef __cplusplus
}
#endif
@@ -1,116 +0,0 @@
/*
* SPDX-FileCopyrightText: 2025 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 "hal/config.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Enable or disable TRNG 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 Reset TRNG module
*/
static inline void trng_ll_reset(void)
{
LP_TRNG.rstn.rstn = 0;
LP_TRNG.rstn.rstn = 1;
}
/**
* @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.cfg.sample_enable = enable;
}
/**
* @brief Set TRNG timer prescaler
*
* @param prescaler Timer prescaler value (0-255)
*/
static inline void trng_ll_set_timer_prescaler(uint8_t prescaler)
{
LP_TRNG.cfg.timer_pscale = prescaler;
}
/**
* @brief Enable or disable TRNG timer XOR
*
* @param enable true to enable, false to disable
*/
static inline void trng_ll_enable_timer(bool enable)
{
LP_TRNG.cfg.timer_en = enable;
}
/**
* @brief Get TRNG sample count
*
* @return Current sample count (0-255)
*/
static inline uint8_t trng_ll_get_sample_count(void)
{
return (uint8_t)LP_TRNG.cfg.sample_cnt;
}
/**
* @brief Read random data from TRNG
*
* @return 32-bit random data
*/
static inline uint32_t trng_ll_read_data(void)
{
return LP_TRNG.data.data;
}
/**
* @brief Enable TRNG module
*
* TODO: unify in trng_hal.c
*/
static inline void trng_ll_enable(void)
{
trng_ll_enable_clock(true);
trng_ll_reset();
trng_ll_enable_timer(true);
trng_ll_enable_sample(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_timer(false);
trng_ll_enable_clock(false);
}
#ifdef __cplusplus
}
#endif