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

This commit is contained in:
gaoxu
2026-05-11 09:08:25 +08:00
parent fab36071f8
commit 3d7a643ff7
8 changed files with 116 additions and 34 deletions
@@ -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();
}
@@ -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
@@ -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
@@ -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");