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)
143158 #define UART_RX_PIN 9U
144159 #define UART_AF 7U
145160#elif defined(BOARD_STM32439I_EVAL )
161+ /* UART4 PC10(TX)/PC11(RX) AF8 -- STM32437I-EVAL VCP route, matches the
162+ * wolfssl-examples-stm32 BARE F437 reference firmware. */
146163 #define UART_BASE 0x40004C00UL /* UART4 */
147164 #define UART_CLK_BIT RCC_APB1ENR_UART4EN
148165 #define UART_PORT GPIOC_BASE
@@ -505,24 +522,38 @@ static void eth_clk_init(void)
505522}
506523
507524/* ===================================================================== */
508- /* wolfIP random number source */
525+ /* wolfIP random number source - STM32F4 hardware RNG */
509526/* ===================================================================== */
510527
528+ static void rng_init (void )
529+ {
530+ RCC_AHB2ENR |= RCC_AHB2ENR_RNGEN ;
531+ (void )RCC_AHB2ENR ;
532+ RNG_CR = RNG_CR_RNGEN ;
533+ }
534+
511535uint32_t wolfIP_getrandom (void )
512536{
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 ;
537+ uint32_t sr ;
538+ uint32_t retries = 100U ;
539+
540+ while (retries -- > 0U ) {
541+ sr = RNG_SR ;
542+ if (sr & (RNG_SR_CEIS | RNG_SR_SEIS )) {
543+ /* Clear error flags and restart the RNG. CEIS indicates
544+ * PLL48CLK trouble; SEIS indicates a seed-quality failure.
545+ * Both are recoverable per RM0090. */
546+ RNG_SR = sr & ~(RNG_SR_CEIS | RNG_SR_SEIS );
547+ RNG_CR = 0U ;
548+ RNG_CR = RNG_CR_RNGEN ;
549+ continue ;
550+ }
551+ if (sr & RNG_SR_DRDY )
552+ return RNG_DR ;
521553 }
522- lfsr ^= lfsr << 13 ;
523- lfsr ^= lfsr >> 17 ;
524- lfsr ^= lfsr << 5 ;
525- return lfsr ;
554+ /* RNG never produced data - fall back to a fixed value rather than
555+ * hanging the stack. This should not happen on a healthy board. */
556+ return 0xDEADBEEFU ;
526557}
527558
528559/* ===================================================================== */
@@ -596,6 +627,7 @@ int main(void)
596627
597628 systick_init ();
598629 uart_init ();
630+ rng_init ();
599631
600632 printf ("\n\n=== wolfIP STM32F437/F439 (" BOARD_NAME ") ===\n" );
601633 printf ("Build: " __DATE__ " " __TIME__ "\n" );
@@ -620,7 +652,7 @@ int main(void)
620652
621653#ifdef DHCP
622654 printf ("Starting DHCP...\n" );
623- (void )wolfIP_poll (IPStack , HAL_time_ms );
655+ (void )wolfIP_poll (IPStack , stm32f4_hal_time_ms () );
624656 (void )dhcp_client_init (IPStack );
625657#else
626658 {
@@ -642,10 +674,22 @@ int main(void)
642674
643675 printf ("TCP echo server on port %d\n" , ECHO_PORT );
644676 listen_fd = wolfIP_sock_socket (IPStack , AF_INET , IPSTACK_SOCK_STREAM , 0 );
677+ if (listen_fd < 0 ) {
678+ printf (" ERROR: wolfIP_sock_socket failed (%d)\n" , listen_fd );
679+ for (;;) { __asm volatile ("wfi" ); }
680+ }
645681 wolfIP_register_callback (IPStack , listen_fd , echo_cb , IPStack );
646- ( void ) wolfIP_sock_bind (IPStack , listen_fd ,
682+ ret = wolfIP_sock_bind (IPStack , listen_fd ,
647683 (struct wolfIP_sockaddr * )& addr , sizeof (addr ));
648- (void )wolfIP_sock_listen (IPStack , listen_fd , 1 );
684+ if (ret < 0 ) {
685+ printf (" ERROR: wolfIP_sock_bind failed (%d)\n" , ret );
686+ for (;;) { __asm volatile ("wfi" ); }
687+ }
688+ ret = wolfIP_sock_listen (IPStack , listen_fd , 1 );
689+ if (ret < 0 ) {
690+ printf (" ERROR: wolfIP_sock_listen failed (%d)\n" , ret );
691+ for (;;) { __asm volatile ("wfi" ); }
692+ }
649693
650694 printf ("Ready! Test with:\n" );
651695 printf (" ping <ip>\n" );
@@ -654,13 +698,13 @@ int main(void)
654698 {
655699 uint64_t last_diag_ms = 0 ;
656700#ifdef DHCP
657- uint64_t dhcp_start_ms = HAL_time_ms ;
658- uint64_t dhcp_reinit_ms = HAL_time_ms ;
701+ uint64_t dhcp_start_ms = stm32f4_hal_time_ms () ;
702+ uint64_t dhcp_reinit_ms = dhcp_start_ms ;
659703 int dhcp_done = 0 ;
660704#endif
661705
662706 for (;;) {
663- uint64_t now = HAL_time_ms ;
707+ uint64_t now = stm32f4_hal_time_ms () ;
664708 (void )wolfIP_poll (IPStack , now );
665709
666710#ifdef DHCP
0 commit comments