[En-Nut-Discussion] Read and Write EEPROM

Jari Seppälä jari.seppala at postikaista.net
Tue Oct 4 20:50:51 CEST 2005


> Message: 7
> Date: Tue, 04 Oct 2005 08:52:23 +0200
> From: Peter Sodermanns <peter.sodermanns at aixcon.de>
> Subject: Re: [En-Nut-Discussion] Read and Write EEPROM
> To: "Ethernut User Chat (English)" <en-nut-discussion at egnite.de>
> Message-ID: <434226A7.2090809 at aixcon.de>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> Hi Jari,
>
> I found a solution for saving parameters in the nutpiper application
> example, file config.c.
>
>
> Regards,
>
>                Peter
>
>
> ------------------------------
>
Hi Peter,
I test nutpiper and it is useful to this.
But, i need simple way to save and read eg. server ip from eeprom.
Here you have code i try to make work but noup...It does not work.
Why?

/////////////////////////////////////////////////
//Config.h
#ifndef _CONFIG_H_
#define _CONFIG_H_
#define CONFAPP_EE_OFFSET   512
#define CONFAPP_EE_NAME     "EEPROMTEST"
#define MAXLEN_URL          32
typedef struct {
    char server_IP;
} SERVERCONTROL;
extern SERVERCONTROL server;
extern size_t ConfigSize(void);
extern int ConfigLoad(void);
extern void ConfigSave(void);
extern void ConfigSaveControl(void);
#endif
/////////////////////////////////////////////////
//config.c
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <net/if_var.h>
#include "config.h"
#include <stdio.h>

SERVERCONTROL server;

/*
 * Save a binary into the EEPROM.
 */
static int ConfigSaveBinary(int addr, void *val, size_t len)
{
    size_t i;
    u_char *cp = val;

    for (i = 0; i < len; cp++, i++)
        if (eeprom_read_byte((void *) (addr + i)) != *cp)
            eeprom_write_byte((void *) (addr + i), *cp);

    return len;
}

/*
 * Save a string into the EEPROM.
 */
static int ConfigSaveString(int addr, u_char * str)
{
    int rc = 0;

    do {
        if (eeprom_read_byte((void *) (addr + rc)) != *str)
            eeprom_write_byte((void *) (addr + rc), *str);
        rc++;
    } while (*str++);

    return rc;
}

/*
 * Read a string from EEPROM.
 */
static size_t ConfigLoadString(int addr, u_char * str, size_t size)
{
    size_t rc = 0;

    while (rc < size) {
        *str = eeprom_read_byte((void *) (addr + rc));
        rc++;
        if (*str++ == 0)
            break;
    }
    return rc;
}

/*
 * Read a binary value from EEPROM.
 */
static int ConfigLoadBinary(int addr, void *val, size_t len)
{
    size_t i;
    u_char *cp = val;

    for (i = 0; i < len; cp++, i++)
        *cp = eeprom_read_byte((void *) (addr + i));

    return len;
}

int ConfigLoad(void)
{
    int rc = 0;
    int addr = CONFAPP_EE_OFFSET;
    char *buf;

    buf = malloc(MAXLEN_URL + 1);
    addr += ConfigLoadString(addr, buf, sizeof(CONFAPP_EE_NAME));
    if (strcmp(buf, CONFAPP_EE_NAME) == 0) {
        /*
         * Read settings from EEPROM.
         */
        addr += ConfigLoadBinary(addr, &server.server_IP,
sizeof(server.server_IP));

    }
    free(buf);

    return rc;
}

/*
 * Save configuration in EEPROM.
 */

void ConfigSave(void)
{
    int addr = CONFAPP_EE_OFFSET;

    NutNetSaveConfig();

    /* Save our name. */
    addr += ConfigSaveString(addr, CONFAPP_EE_NAME);

    /* Save server control. */
    addr += ConfigSaveBinary(addr, &server.server_IP,
sizeof(server.server_IP));

}
/////////////////////////////////////////////////
//nutpiper.c
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <io.h>
#include <dev/debug.h>
#include <dev/term.h>
#include <dev/hd44780.h>
#include <dev/vs1001k.h>
#ifdef ETHERNUT2
#include <dev/lanc111.h>
#else
#include <dev/nicrtl.h>
#endif

#include <sys/heap.h>
#include <sys/thread.h>
#include <sys/timer.h>

#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <net/route.h>

#include <pro/dhcp.h>

#include <sys/bankmem.h>
#include <dev/irsony.h>

#include "config.h"
//#include "display.h"
//#include "scanner.h"
//#include "player.h"

int main(void)
{
    /* Unique MAC address of the Ethernut Board. */
    u_char mac[6] = { 0x00, 0x06, 0x98, 0x21, 0x07, 0x34 };
    /* Unique IP address of the Ethernut Board. Ignored if DHCP is used. */
    u_long ip_addr = inet_addr("192.168.0.100");
    /* IP network mask of the Ethernut Board. Ignored if DHCP is used. */
    u_long ip_mask = inet_addr("255.255.255.0");
    /* Gateway IP address for the Ethernut Board. Ignored if DHCP is used.
*/
    u_long ip_gate = inet_addr("192.168.0.1");
    /* Baudrate for debug output. */
    u_long baud = 115200;

    /*
     * Assign stdout to the debug device.
     */
    NutRegisterDevice(&devDebug0, 0, 0);
    freopen("uart0", "w", stdout);
    _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
	ConfigLoad();

    /*
     * LAN configuration using EEPROM values or DHCP/ARP method.
     * If it fails, use fixed values.
     */
    if (NutRegisterDevice(&DEV_ETHER, 0x8300, 5))
        puts("Registering device failed");
    if (NutDhcpIfConfig("eth0", 0, 60000)) {
        puts("EEPROM/DHCP/ARP config failed");
        NutNetIfConfig("eth0", mac, ip_addr, ip_mask);
        NutIpRouteAdd(0, 0, ip_gate, &DEV_ETHER);
    }

	//Set ip to struct
	server.server_IP="x";

	ConfigSave();
	printf("ip = %s\n", server.server_IP); //Why server ip does not appear to
hyperterminal window???
    puts("ready\n");

    for (;;)
        NutSleep(1000);

    return 0;
}

Thanks very much for your time.
-jari




More information about the En-Nut-Discussion mailing list