cancel
Showing results for 
Search instead for 
Did you mean: 

LwIP & CubeMX: Changing a static IP address during code initialization

micbem
Associate

I develop an app with ETH interface and static IP address based on the STM32F207 MCU. The IP address shall be derived from status of input pins, like DIP switch.

CubeMX allows to set the static IP address however does not accept user constants. This IP address is then set to LwIP internal variables in the lwip.c MX_LWIP_Init() routine. I would need to change the IP address in the routine before the LwIP stack gets initialized. I would expect some /* USER CODE BEGIN x */ section there, like in the ethernetif.c low_level_init() routine for easy changing the MAC address (section /* USER CODE BEGIN MACADDRESS */).

Any ideas how to do it without my code being overwritten each time CubeMX re-generates the code? Maybe I miss something. I tried a few hacks (like #undef the #define ip_init()) without success. Would be nice to be able to define own USER CODE section.

CubeMX version: 4.27.0

1 REPLY 1
SStor
Senior

You can insert your code for IP address in the user code section at the end of MX_LWIP_Init() and reinit the address with function netif_set_addr(). But you are right, a specific user section would be helpful.

<Code>

/**

 * LwIP initialization function

 */

void MX_LWIP_Init(void)

{

 /* IP addresses initialization */

 IP_ADDRESS[0] = 0;

 IP_ADDRESS[1] = 0;

 IP_ADDRESS[2] = 0;

 IP_ADDRESS[3] = 0;

 NETMASK_ADDRESS[0] = 0;

 NETMASK_ADDRESS[1] = 0;

 NETMASK_ADDRESS[2] = 0;

 NETMASK_ADDRESS[3] = 0;

 GATEWAY_ADDRESS[0] = 0;

 GATEWAY_ADDRESS[1] = 0;

 GATEWAY_ADDRESS[2] = 0;

 GATEWAY_ADDRESS[3] = 0;

 /* Initilialize the LwIP stack with RTOS */

 tcpip_init( NULL, NULL );

 /* IP addresses initialization without DHCP (IPv4) */

 IP4_ADDR(&ipaddr, IP_ADDRESS[0], IP_ADDRESS[1], IP_ADDRESS[2], IP_ADDRESS[3]);

 IP4_ADDR(&netmask, NETMASK_ADDRESS[0], NETMASK_ADDRESS[1] , NETMASK_ADDRESS[2], NETMASK_ADDRESS[3]);

 IP4_ADDR(&gw, GATEWAY_ADDRESS[0], GATEWAY_ADDRESS[1], GATEWAY_ADDRESS[2], GATEWAY_ADDRESS[3]);

 /* add the network interface (IPv4/IPv6) with RTOS */

 netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);

 /* Registers the default network interface */

 netif_set_default(&gnetif);

 if (netif_is_link_up(&gnetif))

 {

   /* When the netif is fully configured this function must be called */

   netif_set_up(&gnetif);

 }

 else

 {

   /* When the netif link is down this function must be called */

   netif_set_down(&gnetif);

 }

 /* Set the link callback function, this function is called on change of link status*/

 netif_set_link_callback(&gnetif, ethernetif_update_config);

 /* create a binary semaphore used for informing ethernetif of frame reception */

 osSemaphoreDef(Netif_SEM);

 Netif_LinkSemaphore = osSemaphoreCreate(osSemaphore(Netif_SEM) , 1 );

 link_arg.netif = &gnetif;

 link_arg.semaphore = Netif_LinkSemaphore;

 /* Create the Ethernet link handler thread */

 osThreadDef(LinkThr, ethernetif_set_link, osPriorityNormal, 0, configMINIMAL_STACK_SIZE * 2);

 osThreadCreate (osThread(LinkThr), &link_arg);

/* USER CODE BEGIN 3 */

 IP_ADDRESS[0] = SAVEINFOSTRUCT.TCPIP_IPADRESS[0];

 IP_ADDRESS[1] = SAVEINFOSTRUCT.TCPIP_IPADRESS[1];

 IP_ADDRESS[2] = SAVEINFOSTRUCT.TCPIP_IPADRESS[2];

 IP_ADDRESS[3] = SAVEINFOSTRUCT.TCPIP_IPADRESS[3];

 NETMASK_ADDRESS[0] = SAVEINFOSTRUCT.TCPIP_NETMASK[0];

 NETMASK_ADDRESS[1] = SAVEINFOSTRUCT.TCPIP_NETMASK[1];

 NETMASK_ADDRESS[2] = SAVEINFOSTRUCT.TCPIP_NETMASK[2];

 NETMASK_ADDRESS[3] = SAVEINFOSTRUCT.TCPIP_NETMASK[3];

 GATEWAY_ADDRESS[0] = SAVEINFOSTRUCT.TCPIP_GATEWAY[0];

 GATEWAY_ADDRESS[1] = SAVEINFOSTRUCT.TCPIP_GATEWAY[1];

 GATEWAY_ADDRESS[2] = SAVEINFOSTRUCT.TCPIP_GATEWAY[2];

 GATEWAY_ADDRESS[3] = SAVEINFOSTRUCT.TCPIP_GATEWAY[3];

 IP4_ADDR(&ipaddr, IP_ADDRESS[0], IP_ADDRESS[1], IP_ADDRESS[2], IP_ADDRESS[3]);

 IP4_ADDR(&netmask, NETMASK_ADDRESS[0], NETMASK_ADDRESS[1] , NETMASK_ADDRESS[2], NETMASK_ADDRESS[3]);

 IP4_ADDR(&gw, GATEWAY_ADDRESS[0], GATEWAY_ADDRESS[1], GATEWAY_ADDRESS[2], GATEWAY_ADDRESS[3]);

 netif_set_addr(&gnetif, &ipaddr, &netmask, &gw);

 ethernetif_update_config(&gnetif);   // call update change on init

/* USER CODE END 3 */

}

</Code>