Skip to content

Commit 68df79e

Browse files
committed
[bsp/at32] add link detecting thread for ethernet driver
1 parent f3d1e71 commit 68df79e

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

bsp/at32/Libraries/rt_drivers/drv_eth.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,18 @@
3131
#define ETH_RXBUFNB 4
3232
#define ETH_TXBUFNB 2
3333

34+
#define LINK_THREAD_STACK_SIZE 256
35+
#define LINK_THREAD_PREORITY 21
36+
3437
extern ETH_DMADESCTypeDef *DMATxDescToSet;
3538
extern ETH_DMADESCTypeDef *DMARxDescToGet;
3639
extern ETH_DMADESCTypeDef *DMAPTPTxDescToSet;
3740
extern ETH_DMADESCTypeDef *DMAPTPRxDescToGet;
3841

3942
static ETH_DMADESCTypeDef DMARxDscrTab[ETH_RXBUFNB], DMATxDscrTab[ETH_TXBUFNB];
4043
static rt_uint8_t Rx_Buff[ETH_RXBUFNB][ETH_MAX_PACKET_SIZE], Tx_Buff[ETH_TXBUFNB][ETH_MAX_PACKET_SIZE];
44+
static struct rt_thread eth_link_thread;
45+
static rt_uint8_t eth_link_stack[LINK_THREAD_STACK_SIZE];
4146

4247
#define MAX_ADDR_LEN 6
4348
/* Gloable variables ---------------------------------------------------------*/
@@ -634,6 +639,37 @@ struct pbuf *rt_at32_eth_rx(rt_device_t dev)
634639
return p;
635640
}
636641

642+
static void eth_link_thread_entry(void *paramter)
643+
{
644+
uint8_t linked_down = 1;
645+
646+
struct netif *pnetif = at32_eth_device.parent.netif;
647+
648+
while(1){
649+
if((ETH_ReadPHYRegister(PHY_ADDRESS, PHY_BSR) & PHY_Linked_Status) && (linked_down == 1))
650+
{
651+
/* link up */
652+
linked_down = 0;
653+
#ifndef RT_LWIP_DHCP
654+
pnetif->ip_addr = inet_addr(RT_LWIP_IPADDR);
655+
pnetif->gw = inet_addr(RT_LWIP_GWADDR);
656+
pnetif->netmask = inet_addr(RT_LWIP_MSKADDR);
657+
#else
658+
IP4_ADDR(&(pnetif->ip_addr), 0, 0, 0, 0);
659+
IP4_ADDR(&(pnetif->netmask), 0, 0, 0, 0);
660+
IP4_ADDR(&(pnetif->gw), 0, 0, 0, 0);
661+
#endif
662+
eth_device_linkchange(&(at32_eth_device.parent), RT_TRUE);
663+
}else if(!(ETH_ReadPHYRegister(PHY_ADDRESS, PHY_BSR) & PHY_Linked_Status) && (linked_down == 0))
664+
{
665+
/* link down */
666+
linked_down = 1;
667+
eth_device_linkchange(&(at32_eth_device.parent), RT_FALSE);
668+
}
669+
rt_thread_mdelay(500);
670+
}
671+
}
672+
637673
/* interrupt service routine */
638674
void ETH_IRQHandler(void)
639675
{
@@ -707,9 +743,17 @@ static int rt_hw_at32_eth_init(void)
707743

708744
/* register eth device */
709745
state = eth_device_init(&(at32_eth_device.parent), "e0");
746+
710747
if (RT_EOK == state)
711748
{
712749
LOG_D("emac device init success");
750+
751+
state = rt_thread_init(&eth_link_thread, "eth_link_detect", eth_link_thread_entry, RT_NULL,
752+
&eth_link_stack[0], LINK_THREAD_STACK_SIZE, LINK_THREAD_PREORITY, 20);
753+
if (state == RT_EOK)
754+
{
755+
rt_thread_startup(&eth_link_thread);
756+
}
713757
}
714758
else
715759
{

0 commit comments

Comments
 (0)