Skip to content

Commit 6c28e8a

Browse files
committed
add rng driver
1 parent 59501c9 commit 6c28e8a

2 files changed

Lines changed: 56 additions & 1 deletion

File tree

src/fw/drivers/sf32lb/rng.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include "drivers/rng.h"
2+
3+
#include "drivers/periph_config.h"
4+
#include "system/passert.h"
5+
#include "kernel/util/sleep.h"
6+
#include "board/board_sf32lb.h"
7+
8+
#include <mcu.h>
9+
10+
bool flag_inited = false;
11+
RNG_HandleTypeDef RngHandle;
12+
13+
bool rng_rand(uint32_t *rand_out) {
14+
15+
PBL_ASSERTN(rand_out);
16+
HAL_StatusTypeDef status;
17+
uint32_t seed;
18+
19+
/*##-1- Initialize RNG peripheral #######################################*/
20+
if(!flag_inited)
21+
{
22+
RngHandle.Instance = hwp_trng;
23+
status = HAL_RNG_Init(&RngHandle);
24+
if ( status != HAL_OK)
25+
{
26+
/* Initialization Error */
27+
//PBL_LOG(LOG_LEVEL_ERROR,"rng_rand init fail!");
28+
return false;
29+
}
30+
//PBL_LOG(LOG_LEVEL_INFO,"rng_rand first init ok!");
31+
flag_inited = true;
32+
}
33+
34+
35+
/*##-2- Generate random ###################################*/
36+
seed = 0;
37+
status = HAL_RNG_Generate(&RngHandle, &seed, 1);
38+
//PBL_LOG(LOG_LEVEL_INFO,"Generate Random value=%x, status=%d", (unsigned int)seed, (int)status);
39+
PBL_ASSERTN(seed != 0);
40+
*rand_out = seed;
41+
42+
return true;
43+
}
44+
void example_rng(void)
45+
{
46+
uint8_t i = 0;
47+
uint32_t value;
48+
for(i = 0; i < 8; i++)
49+
{
50+
rng_rand(&value);
51+
PBL_LOG(LOG_LEVEL_INFO,"get random value 0x%x;", (unsigned int)value);
52+
}
53+
54+
}

src/fw/drivers/wscript_build

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1662,7 +1662,8 @@ if mcu_family == 'SF32LB':
16621662
name='driver_sf32lb',
16631663
source=[
16641664
'sf32lb/stubs/rtc.c',
1665-
'sf32lb/stubs/rng.c',
1665+
#'sf32lb/stubs/rng.c',
1666+
'sf32lb/rng.c',
16661667
'flash/flash_api.c',
16671668
'flash/flash_crc.c',
16681669
'flash/flash_erase.c',

0 commit comments

Comments
 (0)