Skip to content

Commit bff52cc

Browse files
committed
Peer review fixes (thanks Copilot)
1 parent 0fbf34a commit bff52cc

3 files changed

Lines changed: 87 additions & 26 deletions

File tree

src/port/stm32f4/stm32f4_eth.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@
4040
#define ETH_DEBUG(...) do { } while (0)
4141
#endif
4242

43-
/* Millisecond tick provided by the board port (SysTick handler). */
44-
extern volatile uint64_t HAL_time_ms;
43+
/* HAL_time_ms (declared in stm32f4_eth.h) is the SysTick-driven millisecond
44+
* tick maintained by the board port. Use stm32f4_hal_time_ms() to read it
45+
* tear-free. */
4546

4647
/* HCLK frequency provided by the board port (used to pick MDIO divider). */
4748
extern uint32_t stm32f4_eth_hclk_hz(void);
@@ -405,10 +406,10 @@ static int eth_phy_init(void)
405406

406407
/* Soft reset and wait. */
407408
(void)mdio_write(STM32F4_ETH_PHY_ADDR, PHY_BCR, BCR_RESET);
408-
deadline = HAL_time_ms + 500U;
409+
deadline = stm32f4_hal_time_ms() + 500U;
409410
do {
410411
bmcr = mdio_read(STM32F4_ETH_PHY_ADDR, PHY_BCR);
411-
} while ((bmcr & BCR_RESET) && (HAL_time_ms < deadline));
412+
} while ((bmcr & BCR_RESET) && (stm32f4_hal_time_ms() < deadline));
412413
if (bmcr & BCR_RESET) {
413414
printf(" PHY: reset did not clear\n");
414415
return -1;
@@ -424,12 +425,13 @@ static int eth_phy_init(void)
424425
BCR_AUTONEG | BCR_RESTART_AN);
425426

426427
/* Wait up to 5s for link up + AN complete. */
427-
deadline = HAL_time_ms + 5000U;
428+
deadline = stm32f4_hal_time_ms() + 5000U;
428429
do {
429430
bsr = mdio_read(STM32F4_ETH_PHY_ADDR, PHY_BSR);
430431
bsr = mdio_read(STM32F4_ETH_PHY_ADDR, PHY_BSR); /* latch-clear */
431432
} while (((bsr & (BSR_LINK | BSR_AN_COMPLETE)) !=
432-
(BSR_LINK | BSR_AN_COMPLETE)) && (HAL_time_ms < deadline));
433+
(BSR_LINK | BSR_AN_COMPLETE)) &&
434+
(stm32f4_hal_time_ms() < deadline));
433435

434436
printf(" PHY link: %s, AN: %s\n",
435437
(bsr & BSR_LINK) ? "UP" : "DOWN",
@@ -620,7 +622,7 @@ int stm32f4_eth_init(struct wolfIP_ll_dev *ll, const uint8_t *mac)
620622
eth_stop();
621623

622624
if (eth_hw_reset() != 0)
623-
return -2;
625+
return -1;
624626

625627
eth_config_dma();
626628
eth_init_desc();

src/port/stm32f4/stm32f4_eth.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,23 @@
2727
#include <stdint.h>
2828
#include "wolfip.h"
2929

30+
/* Millisecond tick counter, maintained by SysTick_Handler in the port's
31+
* main.c. Reads on Cortex-M4 are non-atomic (LDRD is interruptible), so
32+
* always read it through stm32f4_hal_time_ms() below to avoid tearing. */
33+
extern volatile uint64_t HAL_time_ms;
34+
35+
/* Tear-free read of HAL_time_ms. Lamport double-read: in the worst case a
36+
* SysTick fires between the two halves and a/b disagree, so re-read. */
37+
static inline uint64_t stm32f4_hal_time_ms(void)
38+
{
39+
uint64_t a, b;
40+
do {
41+
a = HAL_time_ms;
42+
b = HAL_time_ms;
43+
} while (a != b);
44+
return b;
45+
}
46+
3047
/* Initialize the STM32F4 Ethernet MAC + DMA + PHY and hook the driver into
3148
* the wolfIP link-layer device. PHY auto-negotiation is run synchronously
3249
* with a 5-second timeout. Returns 0 on success, -2 if the PHY is reachable

src/port/stm32f439/main.c

Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,25 @@
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+
511533
uint32_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

Comments
 (0)