<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic SDK init ethernet phy trouble in MCUXpresso IDE</title>
    <link>https://community.nxp.com/t5/MCUXpresso-IDE/SDK-init-ethernet-phy-trouble/m-p/1439608#M7988</link>
    <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I use RT1064 with LAN 8720A phy, MCUXpresso 11.5.0 and SDK 2.11.0.&lt;/P&gt;&lt;P&gt;Below there is the&amp;nbsp;ethernetif_phy_init() function:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;void ethernetif_phy_init(struct ethernetif *ethernetif,
                         const ethernetif_config_t *ethernetifConfig,
                         phy_speed_t *speed,
                         phy_duplex_t *duplex)
{
    status_t status;
    bool link              = false;
    bool autonego          = false;
    uint32_t initWaitCount = 0;
    uint32_t autoWaitCount = 0;
    phy_config_t phyConfig = {
        .phyAddr = ethernetifConfig-&amp;gt;phyHandle-&amp;gt;phyAddr,
        .autoNeg = true,
    };

    ethernetifConfig-&amp;gt;phyHandle-&amp;gt;mdioHandle-&amp;gt;resource.base = *ethernetif_enet_ptr(ethernetif);

    LWIP_PLATFORM_DIAG(("Initializing PHY..."));

    while ((initWaitCount &amp;lt; ENET_ATONEGOTIATION_TIMEOUT) &amp;amp;&amp;amp; (!(link &amp;amp;&amp;amp; autonego)))
    {
        status = PHY_Init(ethernetifConfig-&amp;gt;phyHandle, &amp;amp;phyConfig);

        if (kStatus_Success != status)
        {
            LWIP_ASSERT("\r\nCannot initialize PHY.\r\n", 0);
        }

        /* Wait for auto-negotiation success and link up */
        autoWaitCount = ENET_ATONEGOTIATION_TIMEOUT;
        do
        {
            PHY_GetAutoNegotiationStatus(ethernetifConfig-&amp;gt;phyHandle, &amp;amp;autonego);
            PHY_GetLinkStatus(ethernetifConfig-&amp;gt;phyHandle, &amp;amp;link);
            if (autonego &amp;amp;&amp;amp; link)
            {
                break;
            }
        } while (--autoWaitCount);
        if (!autonego)
        {
            PRINTF("PHY Auto-negotiation failed. Please check the cable connection and link partner setting.\r\n");
        }

        initWaitCount++;
    }

    if (autonego &amp;amp;&amp;amp; link)
    {
        /* Get the actual PHY link speed. */
        PHY_GetLinkSpeedDuplex(ethernetifConfig-&amp;gt;phyHandle, speed, duplex);
    }
#if 0 /* Disable assert. If initial auto-negation is timeout, \ \
         the ENET is set to default (100Mbs and full-duplex). */
    else
    {
        LWIP_ASSERT("\r\nGiving up PHY initialization. Please check the ENET cable connection and link partner setting and reset the board.\r\n", 0);
    }
#endif
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think there is a potential problem.&lt;/P&gt;&lt;P&gt;After the&amp;nbsp;PHY_Init() function the phy is checked for the autonego completion with&amp;nbsp;PHY_GetAutoNegotiationStatus() and&amp;nbsp;PHY_GetLinkStatus() functions. In my case the exit from the inner do-while loop occurs after 6000 to 8000 cycles, so I assigned:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;#define ENET_ATONEGOTIATION_TIMEOUT (0x3FFFU)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;in the lwiopts.h file. This way when the cable is plugged the main while loop is executed only once and the ethernetif_phy_init() function exits immediately.&lt;/P&gt;&lt;P&gt;But if the cable is not plugged, the main while loop is executed&amp;nbsp;ENET_ATONEGOTIATION_TIMEOUT times, and every time&amp;nbsp;the inner do-while loop&amp;nbsp;is executed&amp;nbsp;ENET_ATONEGOTIATION_TIMEOUT times: in this case the&amp;nbsp;the ethernetif_phy_init() function exits in several hours.&lt;/P&gt;&lt;P&gt;This happens because&amp;nbsp;ENET_ATONEGOTIATION_TIMEOUT is used for both the main while loop and the inner do-while loop and it should be high enough to wait for the autonego completion with the cable plugged, but low enough not to wait too long with the cable unplugged.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I suggest the use of different #define constants for every loop as in older SDK version (up to 2.82 there was ENET_ATONEGOTIATION_TIMEOUT and PHY_TIMEOUT_COUNT constants).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks&lt;/P&gt;&lt;P&gt;Biafra&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 06 Apr 2022 09:55:29 GMT</pubDate>
    <dc:creator>biafra</dc:creator>
    <dc:date>2022-04-06T09:55:29Z</dc:date>
    <item>
      <title>SDK init ethernet phy trouble</title>
      <link>https://community.nxp.com/t5/MCUXpresso-IDE/SDK-init-ethernet-phy-trouble/m-p/1439608#M7988</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I use RT1064 with LAN 8720A phy, MCUXpresso 11.5.0 and SDK 2.11.0.&lt;/P&gt;&lt;P&gt;Below there is the&amp;nbsp;ethernetif_phy_init() function:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;void ethernetif_phy_init(struct ethernetif *ethernetif,
                         const ethernetif_config_t *ethernetifConfig,
                         phy_speed_t *speed,
                         phy_duplex_t *duplex)
{
    status_t status;
    bool link              = false;
    bool autonego          = false;
    uint32_t initWaitCount = 0;
    uint32_t autoWaitCount = 0;
    phy_config_t phyConfig = {
        .phyAddr = ethernetifConfig-&amp;gt;phyHandle-&amp;gt;phyAddr,
        .autoNeg = true,
    };

    ethernetifConfig-&amp;gt;phyHandle-&amp;gt;mdioHandle-&amp;gt;resource.base = *ethernetif_enet_ptr(ethernetif);

    LWIP_PLATFORM_DIAG(("Initializing PHY..."));

    while ((initWaitCount &amp;lt; ENET_ATONEGOTIATION_TIMEOUT) &amp;amp;&amp;amp; (!(link &amp;amp;&amp;amp; autonego)))
    {
        status = PHY_Init(ethernetifConfig-&amp;gt;phyHandle, &amp;amp;phyConfig);

        if (kStatus_Success != status)
        {
            LWIP_ASSERT("\r\nCannot initialize PHY.\r\n", 0);
        }

        /* Wait for auto-negotiation success and link up */
        autoWaitCount = ENET_ATONEGOTIATION_TIMEOUT;
        do
        {
            PHY_GetAutoNegotiationStatus(ethernetifConfig-&amp;gt;phyHandle, &amp;amp;autonego);
            PHY_GetLinkStatus(ethernetifConfig-&amp;gt;phyHandle, &amp;amp;link);
            if (autonego &amp;amp;&amp;amp; link)
            {
                break;
            }
        } while (--autoWaitCount);
        if (!autonego)
        {
            PRINTF("PHY Auto-negotiation failed. Please check the cable connection and link partner setting.\r\n");
        }

        initWaitCount++;
    }

    if (autonego &amp;amp;&amp;amp; link)
    {
        /* Get the actual PHY link speed. */
        PHY_GetLinkSpeedDuplex(ethernetifConfig-&amp;gt;phyHandle, speed, duplex);
    }
#if 0 /* Disable assert. If initial auto-negation is timeout, \ \
         the ENET is set to default (100Mbs and full-duplex). */
    else
    {
        LWIP_ASSERT("\r\nGiving up PHY initialization. Please check the ENET cable connection and link partner setting and reset the board.\r\n", 0);
    }
#endif
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think there is a potential problem.&lt;/P&gt;&lt;P&gt;After the&amp;nbsp;PHY_Init() function the phy is checked for the autonego completion with&amp;nbsp;PHY_GetAutoNegotiationStatus() and&amp;nbsp;PHY_GetLinkStatus() functions. In my case the exit from the inner do-while loop occurs after 6000 to 8000 cycles, so I assigned:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;#define ENET_ATONEGOTIATION_TIMEOUT (0x3FFFU)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;in the lwiopts.h file. This way when the cable is plugged the main while loop is executed only once and the ethernetif_phy_init() function exits immediately.&lt;/P&gt;&lt;P&gt;But if the cable is not plugged, the main while loop is executed&amp;nbsp;ENET_ATONEGOTIATION_TIMEOUT times, and every time&amp;nbsp;the inner do-while loop&amp;nbsp;is executed&amp;nbsp;ENET_ATONEGOTIATION_TIMEOUT times: in this case the&amp;nbsp;the ethernetif_phy_init() function exits in several hours.&lt;/P&gt;&lt;P&gt;This happens because&amp;nbsp;ENET_ATONEGOTIATION_TIMEOUT is used for both the main while loop and the inner do-while loop and it should be high enough to wait for the autonego completion with the cable plugged, but low enough not to wait too long with the cable unplugged.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I suggest the use of different #define constants for every loop as in older SDK version (up to 2.82 there was ENET_ATONEGOTIATION_TIMEOUT and PHY_TIMEOUT_COUNT constants).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks&lt;/P&gt;&lt;P&gt;Biafra&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Apr 2022 09:55:29 GMT</pubDate>
      <guid>https://community.nxp.com/t5/MCUXpresso-IDE/SDK-init-ethernet-phy-trouble/m-p/1439608#M7988</guid>
      <dc:creator>biafra</dc:creator>
      <dc:date>2022-04-06T09:55:29Z</dc:date>
    </item>
    <item>
      <title>Re: SDK init ethernet phy trouble</title>
      <link>https://community.nxp.com/t5/MCUXpresso-IDE/SDK-init-ethernet-phy-trouble/m-p/1443790#M7998</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.nxp.com/t5/user/viewprofilepage/user-id/65478"&gt;@biafra&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Many thanks for your the valuable report/suggestion, I will deliver it to the respective team.&lt;/P&gt;
&lt;P&gt;All the best,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Diego.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Apr 2022 04:50:53 GMT</pubDate>
      <guid>https://community.nxp.com/t5/MCUXpresso-IDE/SDK-init-ethernet-phy-trouble/m-p/1443790#M7998</guid>
      <dc:creator>diego_charles</dc:creator>
      <dc:date>2022-04-14T04:50:53Z</dc:date>
    </item>
  </channel>
</rss>

