Skip to content

Commit

Permalink
Wait for link to be established
Browse files Browse the repository at this point in the history
  • Loading branch information
Extrems committed May 6, 2024
1 parent 76a2f80 commit f9b575c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
13 changes: 9 additions & 4 deletions lwip/netif/enc28j60if.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ struct enc28j60if {
s32 chan;
s32 dev;
lwpq_t unlockQueue;
lwpq_t linkupQueue;
sem_t txSemaphore;
struct eth_addr *ethaddr;
};
Expand Down Expand Up @@ -530,9 +531,10 @@ static s32 ExiHandler(s32 chan, s32 dev)
if (eir & ENC28J60_EIR_LINKIF) {
u16 phir;

if (ENC28J60_GetLinkState(chan))
if (ENC28J60_GetLinkState(chan)) {
enc28j60_netif->flags |= NETIF_FLAG_LINK_UP;
else
LWP_ThreadBroadcast(enc28j60if->linkupQueue);
} else
enc28j60_netif->flags &= ~NETIF_FLAG_LINK_UP;

ENC28J60_ReadPHYReg(chan, ENC28J60_PHIR, &phir);
Expand Down Expand Up @@ -665,12 +667,13 @@ static err_t enc28j60_output(struct netif *netif, struct pbuf *p)

u32 level = IRQ_Disable();

if (!(netif->flags & NETIF_FLAG_LINK_UP)) {
if (!(netif->flags & NETIF_FLAG_LINK_UP) &&
LWP_ThreadTimedSleep(enc28j60if->linkupQueue, &(struct timespec){2, 500000000})) {
IRQ_Restore(level);
return ERR_CONN;
}

if (LWP_SemTimedWait(enc28j60if->txSemaphore, &(struct timespec){2, 0})) {
if (LWP_SemTimedWait(enc28j60if->txSemaphore, &(struct timespec){2, 500000000})) {
IRQ_Restore(level);
return ERR_IF;
}
Expand Down Expand Up @@ -717,6 +720,7 @@ err_t enc28j60if_init(struct netif *netif)
netif->flags = NETIF_FLAG_BROADCAST;

LWP_InitQueue(&enc28j60if->unlockQueue);
LWP_InitQueue(&enc28j60if->linkupQueue);
LWP_SemInit(&enc28j60if->txSemaphore, 1, 1);

enc28j60if->ethaddr = (struct eth_addr *)netif->hwaddr;
Expand All @@ -743,6 +747,7 @@ err_t enc28j60if_init(struct netif *netif)
}

LWP_SemDestroy(enc28j60if->txSemaphore);
LWP_CloseQueue(enc28j60if->linkupQueue);
LWP_CloseQueue(enc28j60if->unlockQueue);

mem_free(enc28j60if);
Expand Down
4 changes: 3 additions & 1 deletion lwip/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -1550,8 +1550,10 @@ s32 if_configex(struct in_addr *local_ip,struct in_addr *netmask,struct in_addr

//last and least start the tcpip layer
ret = net_init();
if(ret<0) return ret;

if ( ret == 0 && g_hNetIF.dhcp != NULL ) {
if ( (g_hNetIF.flags & NETIF_FLAG_LINK_UP) == 0 ) return -2;
if ( g_hNetIF.dhcp != NULL ) {
// wait for dhcp to bind
while ( g_hNetIF.ip_addr.addr == 0 && g_hNetIF.dhcp->tries <= 4 )
LWP_YieldThread();
Expand Down

0 comments on commit f9b575c

Please sign in to comment.