surenyi
6 months ago
9 changed files with 155 additions and 13 deletions
@ -0,0 +1,95 @@ |
|||
#include "stm32f4xx.h" |
|||
#include "rnglib.h" |
|||
#include "config.h" |
|||
#if defined(__RTTHREAD__) |
|||
#include "rtthread.h" |
|||
#endif |
|||
|
|||
#ifndef BOARD_RNG_NUM |
|||
#define BOARD_RNG_NUM (8) |
|||
#endif |
|||
|
|||
struct rngbuf { |
|||
uint32_t rngbuf[BOARD_RNG_NUM]; |
|||
uint16_t head; |
|||
uint16_t tail; |
|||
}; |
|||
|
|||
static struct rngbuf _G[1]; |
|||
|
|||
static void rngbuf_put(uint32_t _new) |
|||
{ |
|||
_G->rngbuf[_G->tail % BOARD_RNG_NUM] = _new; |
|||
++_G->tail; |
|||
} |
|||
|
|||
static size_t rngbuf_space() |
|||
{ |
|||
return BOARD_RNG_NUM - (uint16_t)(_G->tail - _G->head); |
|||
} |
|||
|
|||
static uint32_t rngbuf_get() |
|||
{ |
|||
uint32_t data = _G->rngbuf[_G->head % BOARD_RNG_NUM]; |
|||
++_G->head; |
|||
return data; |
|||
} |
|||
|
|||
unsigned int rng_rand() |
|||
{ |
|||
if (_G->head == _G->tail) { |
|||
if (RNG_GetFlagStatus(RNG_FLAG_DRDY) != RESET) { |
|||
return RNG_GetRandomNumber(); |
|||
} else { |
|||
RNG_ITConfig(ENABLE); |
|||
while (_G->head == _G->tail) { |
|||
rt_thread_mdelay(1); |
|||
} |
|||
} |
|||
} |
|||
return rngbuf_get(); |
|||
} |
|||
|
|||
unsigned int rand(void) __attribute__((weak, alias("rng_rand"))); |
|||
|
|||
int rng_init() |
|||
{ |
|||
NVIC_InitTypeDef irq_conf = {0}; |
|||
|
|||
RNG_DeInit(); |
|||
|
|||
RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_RNG, ENABLE); |
|||
|
|||
irq_conf.NVIC_IRQChannel = HASH_RNG_IRQn; |
|||
irq_conf.NVIC_IRQChannelCmd = ENABLE; |
|||
|
|||
NVIC_Init(&irq_conf); |
|||
|
|||
RNG_Cmd(ENABLE); |
|||
|
|||
RNG_ITConfig(ENABLE); |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
void HASH_RNG_IRQHandler() |
|||
{ |
|||
#if defined(__RTTHREAD__) |
|||
rt_interrupt_enter(); |
|||
#endif |
|||
if (RNG_GetITStatus((RNG_IT_CEI | RNG_IT_SEI)) != RESET) { |
|||
RNG_ClearITPendingBit(RNG_IT_CEI | RNG_IT_SEI); |
|||
} |
|||
|
|||
if (RNG_GetFlagStatus(RNG_FLAG_DRDY) != RESET) { |
|||
if (rngbuf_space() > 0) { |
|||
rngbuf_put(RNG_GetRandomNumber()); |
|||
} else { |
|||
RNG_ITConfig(DISABLE); |
|||
} |
|||
} |
|||
|
|||
#if defined(__RTTHREAD__) |
|||
rt_interrupt_leave(); |
|||
#endif |
|||
} |
@ -0,0 +1,15 @@ |
|||
#ifndef __rng_lib__h__ |
|||
#define __rng_lib__h__ |
|||
#ifdef __cplusplus |
|||
extern "C" { |
|||
#endif |
|||
|
|||
int rng_init(); |
|||
|
|||
unsigned int rng_rand(); |
|||
|
|||
#ifdef __cplusplus |
|||
} |
|||
#endif |
|||
|
|||
#endif |
@ -1,10 +0,0 @@ |
|||
from building import * |
|||
|
|||
cwd = GetCurrentDir() |
|||
path = [cwd] |
|||
|
|||
src = Glob('*.c') |
|||
|
|||
group = DefineGroup('lwIP', src, depend = ['RT_USING_LWIP'], CPPPATH = path) |
|||
|
|||
Return('group') |
Loading…
Reference in new issue