Browse Source

add rnglib

Signed-off-by: surenyi <surenyi82@163.com>
master
surenyi 6 months ago
parent
commit
37109c20c5
  1. 2
      CMakeLists.txt
  2. 15
      driver/common.c
  3. 95
      driver/rnglib.c
  4. 15
      driver/rnglib.h
  5. 10
      net/port/SConscript
  6. 19
      net/port/netif/ethernetif.h
  7. 3
      scripts/lwip212.cmake
  8. 7
      src/lowlevel.c
  9. 2
      src/main.c

2
CMakeLists.txt

@ -90,6 +90,7 @@ target_sources(${tgt_name} PRIVATE
driver/vrom.c
driver/sdram.c
driver/flash_if.c
driver/rnglib.c
driver/ymodem.c
driver/common.c
driver/start.c
@ -149,7 +150,6 @@ target_link_options(${tgt_name} PRIVATE
-Wl,--cref
-Wl,--sort-section=alignment
-Wl,-Map,${tgt_name}.map
# -Wl,-pie -Wl,--no-dynamic-linker
-specs=nosys.specs
-specs=nano.specs
$<$<NOT:$<CONFIG:Debug>>:-s>)

15
driver/common.c

@ -6,6 +6,7 @@
#include <rtthread.h>
#include "stm32f4xx.h"
#include "bkp_sram.h"
#include "rnglib.h"
#include "config.h"
/* {{{ reboot */
@ -233,3 +234,17 @@ static int sleep(int argc, char *argv[])
}
MSH_CMD_EXPORT(sleep, Sleep millseconds);
/* }}} */
/* {{{ rand */
static int rng(int argc, char *argv[])
{
unsigned int xrd;
xrd = rng_rand();
rt_kprintf("%x\n", xrd);
return 0;
}
MSH_CMD_EXPORT(rng, Random number);
/* }}} */

95
driver/rnglib.c

@ -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
}

15
driver/rnglib.h

@ -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

10
net/port/SConscript

@ -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')

19
net/port/netif/ethernetif.h

@ -29,6 +29,7 @@ extern "C" {
#define ETHIF_LINK_AUTOUP 0x0000
#define ETHIF_LINK_PHYUP 0x0100
#ifdef RT_USING_DEVICE
struct eth_device
{
/* inherit from rt_device */
@ -48,6 +49,24 @@ struct eth_device
struct pbuf* (*eth_rx)(rt_device_t dev);
rt_err_t (*eth_tx)(rt_device_t dev, struct pbuf* p);
};
#else
struct eth_device
{
/* network interface for lwip */
struct netif *netif;
rt_uint16_t flags;
rt_uint8_t link_changed;
rt_uint8_t link_status;
rt_uint8_t rx_notice;
struct rt_spinlock spinlock;
/* eth device interface */
struct pbuf* (*eth_rx)(struct eth_device dev);
rt_err_t (*eth_tx)(struct eth_device dev, struct pbuf* p);
};
#endif
int eth_system_device_init(void);
void eth_device_deinit(struct eth_device *dev);

3
scripts/lwip212.cmake

@ -43,9 +43,10 @@ set(__lwip_srcs__
${lwip_root}/src/api/tcpip.c
${lwip_root}/src/netif/ethernet.c
${lwip_root}/src/apps/ping/ping.c
# ${lwip_root}/../port/sys_arch.c
)
target_sources(lwip INTERFACE
${__lwip_srcs__})
unset(__lwip_srcs__ CACHE)
unset(__lwip_srcs__ CACHE)

7
src/lowlevel.c

@ -3,6 +3,7 @@
#include "shell.h"
#include "serial.h"
#include "bkp_sram.h"
#include "rnglib.h"
#define CONSOLE_UART (BOARD_CONSOLE_UART)
#define CONSOLE_BAURDRATE (115200)
@ -37,15 +38,19 @@ char rt_hw_console_getchar(void)
return serial_getchar(CONSOLE_UART);
}
void eth_system_device_init_private()
{
}
void _init()
{
serial_setup(CONSOLE_UART, CONSOLE_BAURDRATE, 8, SERIAL_STOPBITS_1);
bkp_sram_init();
rng_init();
}
void bsp_board_post_init()
{
finsh_set_prompt("fish ");
bkp_sram_write(OFF_POS, MAGIC_APP);
finsh_set_prompt("fish ");
}

2
src/main.c

@ -12,8 +12,10 @@ void led_gpio_init()
gpio_clear(PE6);
}
extern void lwip_sys_init();
int main()
{
/* lwip_sys_init(); */
led_gpio_init();
rt_kprintf(" APP version: %s (%s %s)\n", GIT_COMMIT, __DATE__, __TIME__);

Loading…
Cancel
Save