6666#define RCC_PLLCFGR (*(volatile uint32_t *)(RCC_BASE + 0x04U))
6767#define RCC_CFGR (*(volatile uint32_t *)(RCC_BASE + 0x08U))
6868#define RCC_AHB1ENR (*(volatile uint32_t *)(RCC_BASE + 0x30U))
69+ #define RCC_AHB2ENR (*(volatile uint32_t *)(RCC_BASE + 0x34U))
6970#define RCC_APB1ENR (*(volatile uint32_t *)(RCC_BASE + 0x40U))
7071#define RCC_APB2ENR (*(volatile uint32_t *)(RCC_BASE + 0x44U))
7172#define RCC_AHB1RSTR (*(volatile uint32_t *)(RCC_BASE + 0x10U))
7273
74+ #define RCC_AHB2ENR_RNGEN (1U << 6)
75+
76+ /* Hardware RNG (RM0090 sec 24). Clocked from PLL48CLK = 48 MHz (PLLQ=7). */
77+ #define RNG_BASE 0x50060800UL
78+ #define RNG_CR (*(volatile uint32_t *)(RNG_BASE + 0x00U))
79+ #define RNG_SR (*(volatile uint32_t *)(RNG_BASE + 0x04U))
80+ #define RNG_DR (*(volatile uint32_t *)(RNG_BASE + 0x08U))
81+ #define RNG_CR_RNGEN (1U << 2)
82+ #define RNG_SR_DRDY (1U << 0)
83+ #define RNG_SR_CECS (1U << 1)
84+ #define RNG_SR_SECS (1U << 2)
85+ #define RNG_SR_CEIS (1U << 5)
86+ #define RNG_SR_SEIS (1U << 6)
87+
7388#define RCC_CR_HSEON (1U << 16)
7489#define RCC_CR_HSERDY (1U << 17)
7590#define RCC_CR_HSEBYP (1U << 18)
@@ -505,24 +520,38 @@ static void eth_clk_init(void)
505520}
506521
507522/* ===================================================================== */
508- /* wolfIP random number source */
523+ /* wolfIP random number source - STM32F4 hardware RNG */
509524/* ===================================================================== */
510525
526+ static void rng_init (void )
527+ {
528+ RCC_AHB2ENR |= RCC_AHB2ENR_RNGEN ;
529+ (void )RCC_AHB2ENR ;
530+ RNG_CR = RNG_CR_RNGEN ;
531+ }
532+
511533uint32_t wolfIP_getrandom (void )
512534{
513- static uint32_t lfsr ;
514- static int seeded ;
515-
516- if (!seeded ) {
517- lfsr = (uint32_t )HAL_time_ms ;
518- if (lfsr == 0U )
519- lfsr = 0xC0FFEE01U ;
520- seeded = 1 ;
535+ uint32_t sr ;
536+ uint32_t retries = 100U ;
537+
538+ while (retries -- > 0U ) {
539+ sr = RNG_SR ;
540+ if (sr & (RNG_SR_CEIS | RNG_SR_SEIS )) {
541+ /* Clear error flags and restart the RNG. CEIS indicates
542+ * PLL48CLK trouble; SEIS indicates a seed-quality failure.
543+ * Both are recoverable per RM0090. */
544+ RNG_SR = sr & ~(RNG_SR_CEIS | RNG_SR_SEIS );
545+ RNG_CR = 0U ;
546+ RNG_CR = RNG_CR_RNGEN ;
547+ continue ;
548+ }
549+ if (sr & RNG_SR_DRDY )
550+ return RNG_DR ;
521551 }
522- lfsr ^= lfsr << 13 ;
523- lfsr ^= lfsr >> 17 ;
524- lfsr ^= lfsr << 5 ;
525- return lfsr ;
552+ /* RNG never produced data - fall back to a fixed value rather than
553+ * hanging the stack. This should not happen on a healthy board. */
554+ return 0xDEADBEEFU ;
526555}
527556
528557/* ===================================================================== */
@@ -596,6 +625,7 @@ int main(void)
596625
597626 systick_init ();
598627 uart_init ();
628+ rng_init ();
599629
600630 printf ("\n\n=== wolfIP STM32F437/F439 (" BOARD_NAME ") ===\n" );
601631 printf ("Build: " __DATE__ " " __TIME__ "\n" );
@@ -620,7 +650,7 @@ int main(void)
620650
621651#ifdef DHCP
622652 printf ("Starting DHCP...\n" );
623- (void )wolfIP_poll (IPStack , HAL_time_ms );
653+ (void )wolfIP_poll (IPStack , stm32f4_hal_time_ms () );
624654 (void )dhcp_client_init (IPStack );
625655#else
626656 {
@@ -642,10 +672,22 @@ int main(void)
642672
643673 printf ("TCP echo server on port %d\n" , ECHO_PORT );
644674 listen_fd = wolfIP_sock_socket (IPStack , AF_INET , IPSTACK_SOCK_STREAM , 0 );
675+ if (listen_fd < 0 ) {
676+ printf (" ERROR: wolfIP_sock_socket failed (%d)\n" , listen_fd );
677+ for (;;) { __asm volatile ("wfi" ); }
678+ }
645679 wolfIP_register_callback (IPStack , listen_fd , echo_cb , IPStack );
646- ( void ) wolfIP_sock_bind (IPStack , listen_fd ,
680+ ret = wolfIP_sock_bind (IPStack , listen_fd ,
647681 (struct wolfIP_sockaddr * )& addr , sizeof (addr ));
648- (void )wolfIP_sock_listen (IPStack , listen_fd , 1 );
682+ if (ret < 0 ) {
683+ printf (" ERROR: wolfIP_sock_bind failed (%d)\n" , ret );
684+ for (;;) { __asm volatile ("wfi" ); }
685+ }
686+ ret = wolfIP_sock_listen (IPStack , listen_fd , 1 );
687+ if (ret < 0 ) {
688+ printf (" ERROR: wolfIP_sock_listen failed (%d)\n" , ret );
689+ for (;;) { __asm volatile ("wfi" ); }
690+ }
649691
650692 printf ("Ready! Test with:\n" );
651693 printf (" ping <ip>\n" );
@@ -654,13 +696,13 @@ int main(void)
654696 {
655697 uint64_t last_diag_ms = 0 ;
656698#ifdef DHCP
657- uint64_t dhcp_start_ms = HAL_time_ms ;
658- uint64_t dhcp_reinit_ms = HAL_time_ms ;
699+ uint64_t dhcp_start_ms = stm32f4_hal_time_ms () ;
700+ uint64_t dhcp_reinit_ms = dhcp_start_ms ;
659701 int dhcp_done = 0 ;
660702#endif
661703
662704 for (;;) {
663- uint64_t now = HAL_time_ms ;
705+ uint64_t now = stm32f4_hal_time_ms () ;
664706 (void )wolfIP_poll (IPStack , now );
665707
666708#ifdef DHCP
0 commit comments