新建bsp_trng.c 和 bsp_trng.h 具体代码如下: #include "bsp_trng.h"
//产生一个随机数
unsigned int trng_module_get_data()
{
unsigned int ret;
trng_init_t init;
md_trng_struct_init(&init);
/* Initialize trng */
md_trng_init(&init);
/* Start calculating random */
md_trng_enable_trngen(TRNG);
/* Waiting flag */
while (!(md_trng_is_active_flag_davld(TRNG)));
/* Get the result */
ret = md_trng_get_data(TRNG);
UNUSED(ret);
return ret;
}
#ifndef _BSP_TRNG_H
#define _BSP_TRNG_H
#include "md_trng.h"
unsigned int trng_module_get_data(void);
#endif
主函数中新建一个线程任务用来产生随机数,具体代码如下: #include "board.h"
#include "rtthread.h"
struct rt_thread Task1_thread;
struct rt_thread Task2_thread;
rt_uint8_t rt_task1_thread_stack[1024];
rt_uint8_t rt_task2_thread_stack[1024];
void task1(void *parameter);
void task2(void *parameter);
int main()
{
rt_kprintf("The Project is ES32F0654 RT-Thread Demo.........\r\n");
rt_thread_init(&Task1_thread,"task1",
task1,RT_NULL,&rt_task1_thread_stack[0],
sizeof(rt_task1_thread_stack),3,20);
rt_thread_init(&Task2_thread,"task2",
task2,RT_NULL,&rt_task2_thread_stack[0],
sizeof(rt_task2_thread_stack),3,20);
rt_thread_startup(&Task1_thread);
rt_thread_startup(&Task2_thread);
}
//任务1:状态指示
void task1(void *parameter)
{
while(1)
{
LED1_TOGGLE;
rt_thread_delay(500);
rt_kprintf("Thread1 is running.........\r\n");
}
}
unsigned int num;
//任务2:产生一个随机数
void task2(void *parameter)
{
while(1)
{
num = trng_module_get_data();
rt_thread_delay(500);
rt_kprintf("Thread2 is running Generate random number:%d \r\n",num);
}
}
|