[En-Nut-Discussion] Read and Write EEPROM

Peter Sodermanns peter.sodermanns at aixcon.de
Wed Oct 5 14:50:36 CEST 2005


Hi Jari,

after a short glance I assume that the misbehaviour of your program is 
caused by the line
	server.server_IP="x";
since the variable is declared as char it should read
	server.server_IP='x';

And, if you just want to save network configuration, the OS provides a 
function for that:
	NutNetSaveConfig();


Well, here are some excerpts of my program files, just in case you can 
use it.

Hope, I could help.

Regards
                 Peter



file "defs.h":

	// global parameters
	typedef struct {
		char			AnzWr;
		char			Name[41];
		char			dhcp_enabled;
		...
	} TGlobalPar;

	extern TGlobalPar	*gp;


file "config.c":

//----------------------------------------------------------------------------------------
// Load configuration from EEPROM.
// return 0 on success, -1 if no configuration data had been found.
//----------------------------------------------------------------------------------------
int ConfigLoad(void)
{
int rc;
int addr;
char *buf;
int oldaddr;

	rc = -1;
	addr = CONFAPP_EE_OFFSET;
	buf = NutHeapAlloc(MAXLEN_URL + 1);
	if (NULL != buf) {
		addr += ConfigLoadString(addr, buf, sizeof(CONFAPP_EE_NAME));
#ifdef DEBUG_CONFIG
		printf_P(PSTR("  application = %s\n"), buf);
#endif
		if (strcmp(buf, CONFAPP_EE_NAME) == 0) {
			rc = 0;
			oldaddr = addr;
			addr += ConfigLoadBinary(addr, gp, sizeof(TGlobalPar));
			printf_P(PSTR("  %d config bytes read from EEPROM\n"), addr-oldaddr);
		}
		free(buf);
	}
#ifdef DEBUG_CONFIG
	printf_P(PSTR("  %d WR\n"), gp->AnzWr);
	printf_P(PSTR("	group %s\n"), gp->Name);
#endif
	return rc;
}

//----------------------------------------------------------------------------------------
// Save configuration into EEPROM.
//----------------------------------------------------------------------------------------
void ConfigSave(void)
{
int addr = CONFAPP_EE_OFFSET;
int oldaddr;

	NutNetSaveConfig();
	/* Save our name. */
	oldaddr = addr;
	addr += ConfigSaveString(addr, CONFAPP_EE_NAME);
	/* Save parameters */
	oldaddr = addr;
	addr += ConfigSaveBinary(addr, gp, sizeof(TGlobalPar));
	printf_P(PSTR("  application %s: parameters (%d bytes) saved\n"), 
CONFAPP_EE_NAME, addr-oldaddr);	
}


file "main.c":

#ifdef DEBUG_MAIN
	printf_P(PSTR("init step %2d - read configuration...\n"), cfg_step++);
#endif
	ConfigLoad();
//	gp->version_2_1 = 0xFF;
	// Version number already existing?
	if (gp->version_2_1) {
		setFactoryDefaults();
		printf_P(PSTR("init step %d - default configuration set\n\n"), 
cfg_step++);
		ConfigSave();
	}




More information about the En-Nut-Discussion mailing list