I'm using the MCU SDK with networking on a LM3S9B92.
I have configured it wit a static IP address, and it works well.
How do I change the IP Address from my code? (I would like to read it from EEPROM on startup)
This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
I'm using the MCU SDK with networking on a LM3S9B92.
I have configured it wit a static IP address, and it works well.
How do I change the IP Address from my code? (I would like to read it from EEPROM on startup)
Hi George Cerff,
The quick answer is you need to use one of the available NDK hook functions to change the IP address, more specifically you can do this with the "stack thread begin hook".
The IP address configuration happens at run time in some C code that is auto generated, based on the settings chosen in the BIOS configuration file (*.cfg file).
When you choose a static IP address from the BIOS configuration, some global values similar to the following will be generated:
/* Static IP Address settings */
char *LocalIPAddr = "192.168.1.2";
char *LocalIPMask = "255.255.254.0";
char *GatewayIP = "0.0.0.0";
char *DomainName = "demo.net";
These values will be used when the NDK is initializing. So you can write a hook function that overrides these globals (or just the LocalIPAddr value if you want) *before* this initialization happens.
You could have a function similar to this:
extern char *LocalIPAddr;
void readIPAddr()
{
*LocalIPAddr = /* read IP address value from ROM and assign it here */
}
Then you can set it to the value for "stack thread begin hook" in your configuration. This will result in your function readIPAddr being called in the beginning of the NDK stack thread:
Steve
Hi Steve
Thanks for the response!
I tried it the way you suggest, but the IP address remains the same.
On closer inspection of the generated code it seems like it does not use the global variable "LocalIPAddr" :-
Void ti_ndk_config_ip_init(HANDLE hCfg)
{
/* Add our global hostname to hCfg (to be claimed in all connected domains) */
CfgAddEntry(hCfg, CFGTAG_SYSINFO, CFGITEM_DHCP_HOSTNAME, 0,
strlen(ti_ndk_config_Global_HostName),
(UINT8 *)ti_ndk_config_Global_HostName, 0);
/* Configure IP address manually on interface 1 */
{
CI_IPNET NA;
CI_ROUTE RT;
/* Setup manual IP address */
bzero(&NA, sizeof(NA));
NA.IPAddr = inet_addr("192.168.0.199");
NA.IPMask = inet_addr("255.255.255.0");
strcpy(NA.Domain, "demo.net");
NA.NetType = 0;
CfgAddEntry(hCfg, CFGTAG_IPNET, 1, 0,
sizeof(CI_IPNET), (UINT8 *)&NA, 0);
/*
* Add the default gateway. Since it is the default, the
* destination address and mask are both zero (we go ahead
* and show the assignment for clarity).
*/
bzero(&RT, sizeof(RT));
RT.IPDestAddr = 0;
RT.IPDestMask = 0;
RT.IPGateAddr = inet_addr("0.0.0.0");
CfgAddEntry(hCfg, CFGTAG_ROUTE, 0, 0,
sizeof(CI_ROUTE), (UINT8 *)&RT, 0);
}
}
Any idea how to get around it?
Regards
George
George,
I actually realized the same thing shortly after making the post. As you discovered, there is a bug in that the LocalIPAddr variable is not being used to configure the static IP ... it's setting it, but it's still using the value from the BIOS configuration later in the code (where it counts).
I'm trying to find a work around for you now and will get back to you on it...
I've also filed a bug to ensure this problem gets fixed:
SDOCM00096773 static IP address assignment should use global variable LocalIPAddr not Ip.address value
Steve
George,
Ok, I have a workaround for you. Due to the above mentioned bug, there's no way to hijack the IP address value that's set in the BIOS configuration, you'll have to let it first initialize with that IP, then remove it and add your IP address from ROM.
What you'll need to do is:
Here's the updated code I used to do this (covers steps 2 + 3 above). You can change it to read and use the IP address in ROM:
void readIPAddr(){
CI_IPNET NA;
HANDLE hCfgIpAddr;
/* Setup manual IP address */
bzero(&NA, sizeof(NA));
NA.IPAddr = inet_addr("146.252.161.45");
NA.IPMask = inet_addr("255.255.254.0");
strcpy(NA.Domain, "demo.net");
NA.NetType = 0;
/* get the current static IP entry */
CfgGetEntry(0, CFGTAG_IPNET, 1, 1, &hCfgIpAddr);
/* remove the current static IP entry */
CfgRemoveEntry(0, hCfgIpAddr);
/* add a new static IP entry */
CfgAddEntry(0, CFGTAG_IPNET, 1, 0,
sizeof(CI_IPNET), (UINT8 *)&NA, 0);
}
Steve