2020-06-17 16:30:11 +08:00
|
|
|
/**************************************************************************//**
|
|
|
|
*
|
|
|
|
* @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2020-3-4 CHChen First version
|
2022-06-03 10:01:15 +08:00
|
|
|
* 2022-3-15 Wayne Remove SW rand function
|
2020-06-17 16:30:11 +08:00
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include <rtconfig.h>
|
|
|
|
|
2020-06-24 00:32:10 +08:00
|
|
|
#if (defined(BSP_USING_TRNG) && defined(RT_HWCRYPTO_USING_RNG))
|
2020-06-17 16:30:11 +08:00
|
|
|
|
|
|
|
#include <rtdevice.h>
|
|
|
|
#include "NuMicro.h"
|
|
|
|
|
|
|
|
#define NU_CRYPTO_TRNG_NAME "nu_TRNG"
|
|
|
|
|
2022-06-03 10:01:15 +08:00
|
|
|
#define LOG_TAG "TRNG"
|
|
|
|
#define DBG_ENABLE
|
|
|
|
#define DBG_SECTION_NAME "TRNG"
|
|
|
|
#define DBG_LEVEL DBG_INFO
|
|
|
|
#define DBG_COLOR
|
|
|
|
#include <rtdbg.h>
|
2020-06-17 16:30:11 +08:00
|
|
|
|
2022-06-03 10:01:15 +08:00
|
|
|
/* Private variables ------------------------------------------------------------*/
|
2020-06-17 16:30:11 +08:00
|
|
|
rt_err_t nu_trng_init(void)
|
|
|
|
{
|
|
|
|
if ((SYS->CSERVER & SYS_CSERVER_VERSION_Msk) == 0x0)
|
|
|
|
{
|
2022-06-03 10:01:15 +08:00
|
|
|
LOG_E("This chip does not support TRNG!");
|
2020-06-17 16:30:11 +08:00
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
|
|
|
|
2022-06-03 10:01:15 +08:00
|
|
|
CLK_EnableModuleClock(TRNG_MODULE);
|
2020-06-17 16:30:11 +08:00
|
|
|
SYS_ResetModule(TRNG_RST);
|
|
|
|
|
2022-06-03 10:01:15 +08:00
|
|
|
TRNG_Open();
|
|
|
|
|
|
|
|
return RT_EOK;
|
2020-06-17 16:30:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
rt_uint32_t nu_trng_rand(struct hwcrypto_rng *ctx)
|
|
|
|
{
|
2022-06-03 10:01:15 +08:00
|
|
|
uint32_t u32RNGValue;
|
|
|
|
|
|
|
|
TRNG_GenWord(&u32RNGValue);
|
2020-06-17 16:30:11 +08:00
|
|
|
|
2022-06-03 10:01:15 +08:00
|
|
|
return u32RNGValue;
|
2020-06-17 16:30:11 +08:00
|
|
|
}
|
|
|
|
|
2020-06-24 00:32:10 +08:00
|
|
|
#endif //#if (defined(BSP_USING_TRNG) && defined(RT_HWCRYPTO_USING_RNG))
|