[En-Nut-Discussion] twi -problem with EEPROM programming

asif khan asif.iitm at gmail.com
Wed May 20 16:53:45 CEST 2009


hello everyone ,

let me be more specific about the problem .The code i sent before needs some
changes sorry about that . When i use EEPROM code which is not based on
Ehernut am able to read and write fine . But when am using Ethernut i was
not able to write properly , it was able to write 67 of 200 , rest of them
am getting data not acknowledged msg. And the adrresses where am able to
write are not changing they are fixed , am not able to read completely i
have given a timeout of 500 secs for read as you can see . i have given TWI
SET SPEED as 400000 in TwInit .






CODE:

#include <cfg/os.h>
#include <dev/board.h>
#include <dev/twif.h>
#include <stdlib.h>
#include <string.h>
#include <sys/timer.h>
#include <stdio.h>
#include <io.h>


int write_data(uint8_t SLAVE,unsigned int reg,char *data,int len);
int read_data(uint8_t SLAVE,unsigned int reg,char *data,int len);



int main(void)
{
    u_long baud =115200;
    uint8_t SLAVE;
    static char data[200];
    int i;

    NutRegisterDevice(&DEV_DEBUG, 0, 0);
    freopen(DEV_DEBUG_NAME, "w", stdout);
    _ioctl(_fileno(stdout), UART_SETSPEED, &baud);

    printf("start of program\n");
    SLAVE = 0X50;

    if(TwInit(0))
        printf("problem wit twinit\n");

    printf("Testing EEPROM\n");
    for( i=0; i<sizeof(data); i++ )
    {
        data[i] = 'I';
    }

    write_data(SLAVE,0,data,sizeof(data));
    printf("after write data\n");
    NutSleep(500);
    memset( data, 0, sizeof(data) );
    read_data(SLAVE,0,data,sizeof(data));
    printf("finished reading\n");

    return 0;
}


int write_data(uint8_t SLAVE,unsigned int reg,char *data,int len)
{
        u_char slave_addrs[3];
        int i=0,error;

        printf("EEPROM write to addr %u len %d\n",reg, len);
        for( i=0; i<len; i++ )
        {
            slave_addrs[2] = *(data+i);
            slave_addrs[1] = (reg+i)&(0xFF);
            slave_addrs[0] = (reg+i)>>8;
            printf("0=%0x
....1=%0x...2=%0x...\n",slave_addrs[0],slave_addrs[1],slave_addrs[2]);

 error=TwMasterTransact(SLAVE,&slave_addrs,3,0,0,NUT_WAIT_INFINITE);
            if( error )
            {
                printf("ERROR: write to EEPROM failed at offset %d addr
%d\n",i,reg+i);
            }
        }
        return 0;
}

int read_data(uint8_t SLAVE,unsigned int reg,char *data,int len)
{
    u_char slave_addrs[2];
    int i=0, error;

    printf("EEPROM read from addr %u len %d\n",reg,len);
    slave_addrs[1]=(reg+i)%256;
    slave_addrs[0]=(reg+i)>>8;
    error=TwMasterTransact(SLAVE,&slave_addrs,2,0,0,NUT_WAIT_INFINITE);

    while(len>0)
    {
    error=TwMasterTransact(SLAVE,0,0,data,len,500);
    if( error )
        printf("ERROR read from EEPROM failed at offset %d addr %u
\n",i,(reg+i));
    len--;
    i++;
    }

    return 0;
}



More information about the En-Nut-Discussion mailing list