【问题标题】:How to Determine if Ethernet Cable Has Been Unplugged During Runtime如何确定运行时以太网电缆是否已拔出
【发布时间】:2019-08-05 15:03:13
【问题描述】:

*更新:Cube 有状态和链接回调。还没有尝试过,但我认为这是最好的解决方案

我正在使用我的STM32F429ZI 上的以太网外围设备和

CubeMX 生成的 LWIP 中间件

This question 类似,但适用于 PC。

我发现很难理解产品规格中的以太网部分。我可以查看哪些寄存器来检查被关闭的链接并不明显。

我还研究了 LWIP 代码,以防有明显的非注册观察方法。在主循环中,它使用MX_LWIP_Process 轮询网络接口。在读取时,它通过以下方式知道是否有数据包:

void ethernetif_input(struct netif *netif)
{
  err_t err;
  struct pbuf *p;

  /* move received packet into a new pbuf */
  p = low_level_input(netif);

  /* no packet could be read, silently ignore this */
  if (p == NULL) return;

在发送时,tcp_output() 函数非常麻烦。它有无效的 netif、无效的 local_ip 和一个我可以看到的一般错误,但没有任何东西直接告诉我链接已关闭。

我最后的想法是以太网接头的 LED 指示灯在拔下电缆时会关闭。我将查看 STM32F4 DK 的硬件图,看看是否可以观看。

【问题讨论】:

    标签: stm32 ethernet


    【解决方案1】:

    是的,您可以使用回调。

    你需要做的就是:

    1-) 在 lwipopts.h 文件中启用 LWIP_NETIF_LINK_CALLBACK 定义。 默认可以为0,勾选即可。

    2-) 在 void ethernetif_input(struct netif *netif) 函数中初始化“netif_set_link_callback(netif, ethernetif_update_config);”。

    3-) 读取 PHY 寄存器并为所欲为。

    参见我的示例,如果以太网电缆断开并重新连接,系统将自行重置。

    void ethernetif_input(struct netif *netif)
    {
     err_t err;
     struct pbuf *p;
    
     /* move received packet into a new pbuf */
     p = low_level_input(netif);
     uint32_t regvalue = 0;
     netif_set_link_callback(netif, ethernetif_update_config); //added by Volkan
     // Read PHY link status
    
     if (HAL_ETH_ReadPHYRegister(&EthHandle, PHY_BSR, &regvalue) == HAL_OK) 
    
    {
        if((regvalue & PHY_LINKED_STATUS)== (uint16_t)RESET) 
        {
          // Link status = disconnected
           if (netif_is_link_up(netif))
    
           {
    
              netif_set_down(netif);
              printf("unplugged\r\n");
              netif_set_link_down(netif);
    
          }
      }
    
    else
    
    {
    
    // Link status = connected
    
        if (!netif_is_link_up(netif))
        {
    
            printf("plugged\r\n");
            NVIC_SystemReset();
    
        }
     }
    }
    
    /* no packet could be read, silently ignore this */
    if (p == NULL) return;
    
    /* entry point to the LwIP stack */
    err = netif->input(p, netif);
    
    if (err != ERR_OK)
    {
    LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
    pbuf_free(p);
    p = NULL;
    }
    }
    

    就是这样。

    【讨论】:

      猜你喜欢
      • 2019-08-19
      • 2011-02-24
      • 2019-02-16
      • 1970-01-01
      • 2015-12-29
      • 1970-01-01
      • 1970-01-01
      • 2015-09-27
      • 1970-01-01
      相关资源
      最近更新 更多