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

asif khan asif.iitm at gmail.com
Wed May 20 11:06:00 CEST 2009


hi everyone ,

am trying to program eeprom (BR24L64W), am using AT91SAM7X256-EK board ,
ETHERNUT version 4.8 .Am not getting any errors while writing data to EEPROM
,but when reading data from EEPROM am getting timeout error .

CODE:

#include <cfg/os.h>
#include <cfg/clock.h>
#include <dev/board.h>
#include <dev/irqreg.h>
#include <dev/twif.h>
#include <stdlib.h>
#include <string.h>
#include <sys/timer.h>
#include <stdio.h>
#include <cfg/crt.h>    /* Floating point configuration. */
#include <io.h>
#include <cfg/arch.h>

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


/*
 *reg  2byte: 0-2^12
 *data 1byte: 0-63
 *slave addrs pins 0-7
 */

int main(void)
{
    u_long baud =115200;
    uint8_t SLAVE;
    unsigned int reg;
    static char data[2048];
    unsigned int slave_addrs_pins;
    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;
    slave_addrs_pins=0;
    SLAVE |=slave_addrs_pins;
    reg=0;

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

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

    write_page(SLAVE,reg,data, sizeof(data));
    printf("after write data\n");
    read_block(SLAVE,reg,data,sizeof(data));
    printf("finished reading\n");

    return 0;
}




/*
 * write page
 */
int write_page(uint8_t SLAVE,unsigned int reg,char *data,int len)
{
        u_char slave_addrs[3];
        int i,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)%256;
            slave_addrs[0] = (reg+i)>>8;



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 -1;
            }
        }

        return 0;
}

int read_block(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);
    for( i=0; i<len; i++ )
    {
        slave_addrs[1]=(reg+i)%256;
        slave_addrs[0]=(reg+i)>>8;

    error=TwMasterTransact(SLAVE,&slave_addrs,2,(data+i),1,500);

    if( error )
    {
        printf("ERROR read from EEPROM failed at offset %d addr
%u\n",i,reg+i);
    }
    }

    /*
    slave_addrs[1]=(reg+i)%256;
    slave_addrs[0]=(reg+i)>>8;

    error=TwMasterTransact(SLAVE,&slave_addrs,2,data,sizeof(data),500);

    */

    return 0;
}

i would be thankful if somebody could point out where am doing a mistake ,

thanking you ,
asif



More information about the En-Nut-Discussion mailing list