<?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>Processor Expert SoftwareのトピックIMXRT1060 USB Device RNDIS + lwIP -- lwIP stuck at netif_add</title>
    <link>https://community.nxp.com/t5/Processor-Expert-Software/IMXRT1060-USB-Device-RNDIS-lwIP-lwIP-stuck-at-netif-add/m-p/1678727#M5659</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I try to develop USB Device RNDIS and lwIP together. My code is based on &lt;EM&gt;usb_vnic_bm&lt;/EM&gt;,&amp;nbsp;&lt;EM&gt;lwip_ping_bm &lt;/EM&gt;and&amp;nbsp;&lt;EM&gt;lwip_dhcp_usb_bm&lt;/EM&gt;&lt;EM&gt;&amp;nbsp;&lt;/EM&gt;examples.&lt;/P&gt;&lt;P&gt;My flow is as follows:&lt;/P&gt;&lt;P&gt;- in main function I initialize USB Device, then lwIP, then in while loop i set up a task that Transmits and Receives buffers by USB (similar to&amp;nbsp;&lt;EM&gt;usb_vnic_bm&lt;/EM&gt;),&lt;/P&gt;&lt;P&gt;- virtual_enet_adapter.c/.h files were remade to not use ENET functions,&amp;nbsp; but to just use queue feature&lt;/P&gt;&lt;P&gt;- netif-&amp;gt;linkoutput function puts packets incoming from IP stack to host and later they are send in USB_DeviceVnicTransmit function&amp;nbsp;(it checks if there are any new packet on queue, if so it transmits them on USB)&lt;/P&gt;&lt;P&gt;- packets incoming from host to IP stack are captured into queue by USB_DeviceVnicReceive function&amp;nbsp; that happens in main loop which&amp;nbsp;issues neitf-&amp;gt;input function&lt;/P&gt;&lt;P&gt;By this point, please tell me if by workflow is correct. I call lwIP only from mainloop callstack (at least i think i do so &lt;LI-EMOJI id="lia_slightly-smiling-face" title=":slightly_smiling_face:"&gt;&lt;/LI-EMOJI&gt; )&lt;/P&gt;&lt;P&gt;My problem begins in initialization. Actually, main function never gets to while loop. It stops in EthernetifRndisInit function. Code is listed below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;void main(void)
{
    BOARD_ConfigMPU();

    BOARD_InitBootPins();
    BOARD_InitBootClocks();
    BOARD_InitDebugConsole();

    IOMUXC_EnableMode(IOMUXC_GPR, kIOMUXC_GPR_ENET1TxClkOutputDir, true);

    USB_DeviceApplicationInit();
    lwIPInit();

    while (1)
    {
#if USB_DEVICE_CONFIG_USE_TASK
        USB_DeviceTaskFn(g_cdcVnic.deviceHandle);
#endif
        if ((1 == g_cdcVnic.attach))
        {
          /* User Code */
          USB_DeviceVnicTransmit();
          USB_DeviceVnicReceive();
        }

        sys_check_timeouts(); /* Handle all system timeouts for all core protocols */
    }
}&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;LI-CODE lang="c"&gt;err_t EthernetifRndisInit(struct netif *netif)
{
	LWIP_ASSERT("netif != NULL", (netif != NULL));

	netif-&amp;gt;mtu = 1500U;
	netif-&amp;gt;state = NULL;
	netif-&amp;gt;name[0] = IFNAME0;
	netif-&amp;gt;name[1] = IFNAME1;
	netif-&amp;gt;output = etharp_output;
	netif-&amp;gt;linkoutput = EthernetifOutput;
	/* set MAC hardware address length */
	netif-&amp;gt;hwaddr_len = ETH_HWADDR_LEN;

	memcpy(netif-&amp;gt;hwaddr,&amp;amp;g_hwaddr[0], ENET_MAC_ADDR_SIZE);

	/*USB enet card is ready*/
	netif-&amp;gt;flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;

        usb_echo("lwIP started\r\n");

	return ERR_OK;
}

void lwIPInit(void)
{
	ip4_addr_t netifIPaddr, netifNetmask, netifGw;
	time_init();

	IP4_ADDR(&amp;amp;netifIPaddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3);
	IP4_ADDR(&amp;amp;netifNetmask, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3);
	IP4_ADDR(&amp;amp;netifGw, configGW_ADDR0, configGW_ADDR1, configGW_ADDR2, configGW_ADDR3);

	lwip_init();

	netif_add(&amp;amp;rndisNetif, &amp;amp;netifIPaddr, &amp;amp;netifNetmask, &amp;amp;netifGw, NULL, 
                   EthernetifRndisInit, ethernet_input);
	netif_set_default(&amp;amp;rndisNetif);
	netif_set_up(&amp;amp;rndisNetif);
	//netif_set_link_up(&amp;amp;rndisNetif);
	ping_init(&amp;amp;netifGw);
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My function stops exactly at&amp;nbsp;usb_echo("lwIP started\r\n"); print. It prints only "lwIP start", not more, and does nothing more. I can see that device didn't die, because all the callbacks from USB happen, but I think that I got into some kind of infinite loop, but I can find where it is, and why.&lt;/P&gt;&lt;P&gt;I tried putting whole lwIPInit function in critical section, which helped, because whole initialization worked, but right after getting out of it, same thing happened and main loop didn't go further.&lt;/P&gt;&lt;P&gt;Can anyone provide me some tips on what it can be? Or maybe example on how USB Device RNDIS + lwIP should work together?&lt;/P&gt;&lt;P&gt;Thank you, and have a nice day.&lt;/P&gt;</description>
    <pubDate>Thu, 29 Jun 2023 12:47:33 GMT</pubDate>
    <dc:creator>mbora</dc:creator>
    <dc:date>2023-06-29T12:47:33Z</dc:date>
    <item>
      <title>IMXRT1060 USB Device RNDIS + lwIP -- lwIP stuck at netif_add</title>
      <link>https://community.nxp.com/t5/Processor-Expert-Software/IMXRT1060-USB-Device-RNDIS-lwIP-lwIP-stuck-at-netif-add/m-p/1678727#M5659</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I try to develop USB Device RNDIS and lwIP together. My code is based on &lt;EM&gt;usb_vnic_bm&lt;/EM&gt;,&amp;nbsp;&lt;EM&gt;lwip_ping_bm &lt;/EM&gt;and&amp;nbsp;&lt;EM&gt;lwip_dhcp_usb_bm&lt;/EM&gt;&lt;EM&gt;&amp;nbsp;&lt;/EM&gt;examples.&lt;/P&gt;&lt;P&gt;My flow is as follows:&lt;/P&gt;&lt;P&gt;- in main function I initialize USB Device, then lwIP, then in while loop i set up a task that Transmits and Receives buffers by USB (similar to&amp;nbsp;&lt;EM&gt;usb_vnic_bm&lt;/EM&gt;),&lt;/P&gt;&lt;P&gt;- virtual_enet_adapter.c/.h files were remade to not use ENET functions,&amp;nbsp; but to just use queue feature&lt;/P&gt;&lt;P&gt;- netif-&amp;gt;linkoutput function puts packets incoming from IP stack to host and later they are send in USB_DeviceVnicTransmit function&amp;nbsp;(it checks if there are any new packet on queue, if so it transmits them on USB)&lt;/P&gt;&lt;P&gt;- packets incoming from host to IP stack are captured into queue by USB_DeviceVnicReceive function&amp;nbsp; that happens in main loop which&amp;nbsp;issues neitf-&amp;gt;input function&lt;/P&gt;&lt;P&gt;By this point, please tell me if by workflow is correct. I call lwIP only from mainloop callstack (at least i think i do so &lt;LI-EMOJI id="lia_slightly-smiling-face" title=":slightly_smiling_face:"&gt;&lt;/LI-EMOJI&gt; )&lt;/P&gt;&lt;P&gt;My problem begins in initialization. Actually, main function never gets to while loop. It stops in EthernetifRndisInit function. Code is listed below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;void main(void)
{
    BOARD_ConfigMPU();

    BOARD_InitBootPins();
    BOARD_InitBootClocks();
    BOARD_InitDebugConsole();

    IOMUXC_EnableMode(IOMUXC_GPR, kIOMUXC_GPR_ENET1TxClkOutputDir, true);

    USB_DeviceApplicationInit();
    lwIPInit();

    while (1)
    {
#if USB_DEVICE_CONFIG_USE_TASK
        USB_DeviceTaskFn(g_cdcVnic.deviceHandle);
#endif
        if ((1 == g_cdcVnic.attach))
        {
          /* User Code */
          USB_DeviceVnicTransmit();
          USB_DeviceVnicReceive();
        }

        sys_check_timeouts(); /* Handle all system timeouts for all core protocols */
    }
}&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;LI-CODE lang="c"&gt;err_t EthernetifRndisInit(struct netif *netif)
{
	LWIP_ASSERT("netif != NULL", (netif != NULL));

	netif-&amp;gt;mtu = 1500U;
	netif-&amp;gt;state = NULL;
	netif-&amp;gt;name[0] = IFNAME0;
	netif-&amp;gt;name[1] = IFNAME1;
	netif-&amp;gt;output = etharp_output;
	netif-&amp;gt;linkoutput = EthernetifOutput;
	/* set MAC hardware address length */
	netif-&amp;gt;hwaddr_len = ETH_HWADDR_LEN;

	memcpy(netif-&amp;gt;hwaddr,&amp;amp;g_hwaddr[0], ENET_MAC_ADDR_SIZE);

	/*USB enet card is ready*/
	netif-&amp;gt;flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;

        usb_echo("lwIP started\r\n");

	return ERR_OK;
}

void lwIPInit(void)
{
	ip4_addr_t netifIPaddr, netifNetmask, netifGw;
	time_init();

	IP4_ADDR(&amp;amp;netifIPaddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3);
	IP4_ADDR(&amp;amp;netifNetmask, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3);
	IP4_ADDR(&amp;amp;netifGw, configGW_ADDR0, configGW_ADDR1, configGW_ADDR2, configGW_ADDR3);

	lwip_init();

	netif_add(&amp;amp;rndisNetif, &amp;amp;netifIPaddr, &amp;amp;netifNetmask, &amp;amp;netifGw, NULL, 
                   EthernetifRndisInit, ethernet_input);
	netif_set_default(&amp;amp;rndisNetif);
	netif_set_up(&amp;amp;rndisNetif);
	//netif_set_link_up(&amp;amp;rndisNetif);
	ping_init(&amp;amp;netifGw);
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My function stops exactly at&amp;nbsp;usb_echo("lwIP started\r\n"); print. It prints only "lwIP start", not more, and does nothing more. I can see that device didn't die, because all the callbacks from USB happen, but I think that I got into some kind of infinite loop, but I can find where it is, and why.&lt;/P&gt;&lt;P&gt;I tried putting whole lwIPInit function in critical section, which helped, because whole initialization worked, but right after getting out of it, same thing happened and main loop didn't go further.&lt;/P&gt;&lt;P&gt;Can anyone provide me some tips on what it can be? Or maybe example on how USB Device RNDIS + lwIP should work together?&lt;/P&gt;&lt;P&gt;Thank you, and have a nice day.&lt;/P&gt;</description>
      <pubDate>Thu, 29 Jun 2023 12:47:33 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Processor-Expert-Software/IMXRT1060-USB-Device-RNDIS-lwIP-lwIP-stuck-at-netif-add/m-p/1678727#M5659</guid>
      <dc:creator>mbora</dc:creator>
      <dc:date>2023-06-29T12:47:33Z</dc:date>
    </item>
  </channel>
</rss>

