/*EEPROM.c*/
/*
Device Name: Renesas R1EX24512ASAS0A
device address: 1010 A2,A1,A0,RW
page address a0~a6 128 bytes roll over
MSB bit and byte IIC
*/
#include "Global.h"
#include "I2C.h"
#include "eeprom.h"
#include "delay.h"
static unsigned int PAGE_SIZE=128;
static unsigned char EEPROM_ADDR=0xa0;
/*
unsigned char EEPROM_GetID(unsigned char *ID);
unsigned char EEPROM_WriteByte(unsigned int address,unsigned char cData);
unsigned char EEPROM_ReadByte(unsigned int address,unsigned char *cData);
unsigned char EEPROM_WriteMutiByte();
*/
unsigned char test_eeprom_wr(void)
{
unsigned char j;
unsigned int i,k;
unsigned char ret;
for(j=0;j<0xff;j++)
{
for (i=0;i<256;i++)
{
k=(unsigned int)(j*256+i);
ret=EEPROM_WriteByte(EEPROM_ADDR,k,i);
if (ret!=SUCCESS)
return ret;
//need delay 5mS
_WaitTime5ms(); //EEPROM ask for 5 ms after STOP
}
}
return SUCCESS;
}
unsigned char test_eeprom_rd(void)
{
unsigned char j,tmpbdata;
unsigned int i,k;
unsigned char ret;
//for(j=0;j<0xff;j++)
j=0;
{
for (i=0;i<256;i++)
{
k=(unsigned int)(j*256+i);
ret=EEPROM_ReadByte(EEPROM_ADDR,k,&tmpbdata);
if (ret!=SUCCESS)
return ret;
//need delay 5mS
_WaitTime5ms(); //EEPROM ask for 5 ms after STOP
}
}
return SUCCESS;
}
unsigned char EEPROM_WriteByte(unsigned char slave_addr,unsigned int mem_addr,unsigned char data)
{
unsigned char cData[3];
unsigned char ret;
cData[0]=(mem_addr&0xff00)>>8;
cData[1]=(mem_addr&0xff);
cData[2]=data;
ret=I2C_WriteData(slave_addr,cData,3);
return ret;
}
unsigned char EEPROM_ReadByte(unsigned char slave_addr,unsigned int mem_addr,unsigned char *data)
{
unsigned char cData[3];
unsigned char ret;
cData[0]=(mem_addr&0xff00)>>8;
cData[1]=(mem_addr&0xff);
//cData[2]=data;
ret=I2C_WriteData(slave_addr,cData,2);//Write address only
if (ret!=SUCCESS)
return ret;
_WaitTime5ms(); //EEPROM ask for 5 ms after STOP
ret=I2C_ReadData(slave_addr,data,1);
return ret;
}
unsigned char EEPROM_WriteData(unsigned char slave_addr,unsigned int mem_addr,unsigned char *data,unsigned int size)
{
unsigned char ret=SUCCESS;
unsigned char j;
unsigned int i;
unsigned int end_addr;
end_addr=mem_addr+size;
//check bus
ret=I2C_BusInit();
if (ret!=SUCCESS)
return ERR_I2C_BUS;
//start frame
slave_addr=slave_addr&0xfe; //set as WRITE addr
I2C_SendStart();
//sen slave address
ret=I2C_SendByte(slave_addr); //send address
if (ret==NOACK)
{
I2C_SendStop();
return ERR_I2C_NO_DEVICE;
}
//send memory address
j=(mem_addr&0xff00)>>8;
ret=I2C_SendByte(j); //send address
if (ret==NOACK)
{
I2C_SendStop();
return ERR_I2C_SEND_DATA_FAIL;
}
j=(mem_addr&0x00ff);
ret=I2C_SendByte(j); //send address
if (ret==NOACK)
{
I2C_SendStop();
return ERR_I2C_SEND_DATA_FAIL;
}
for (i=mem_addr;i<end_addr;i++) //send all data
{
ret=I2C_SendByte(*data++); //send current data and move to next byte
if (ret==NOACK)
{
I2C_SendStop();
return ERR_I2C_SEND_DATA_FAIL;
}
//check if next address is 128 byte address
if (((i+1)%PAGE_SIZE)==0)
{
//send address again
I2C_SendStop();
_WaitTime5ms(); //EEPROM ask for 5 ms after STOP
I2C_SendStart();
//sen slave address
ret=I2C_SendByte(slave_addr); //send address
if (ret==NOACK)
{
I2C_SendStop();
return ERR_I2C_NO_DEVICE;
}
//send memory address
j=((i+1)&0xff00)>>8;
ret=I2C_SendByte(j); //send address
if (ret==NOACK)
{
I2C_SendStop();
return ERR_I2C_SEND_DATA_FAIL;
}
j=((i+1)&0x00ff);
ret=I2C_SendByte(j); //send address
if (ret==NOACK)
{
I2C_SendStop();
return ERR_I2C_SEND_DATA_FAIL;
}
}
}
I2C_SendStop();
return SUCCESS;
}
unsigned char EEPROM_ReadData(unsigned char slave_addr,unsigned int mem_addr,unsigned char *data,unsigned int size)
{
unsigned char ret=SUCCESS;
unsigned char j;
unsigned int i;
unsigned int end_addr;
end_addr=mem_addr+size;
//check bus
ret=I2C_BusInit();
if (ret!=SUCCESS)
return ERR_I2C_BUS;
//start frame
slave_addr=slave_addr&0xfe; //set as WRITE addr
I2C_SendStart();
//sen slave address
ret=I2C_SendByte(slave_addr); //send address
if (ret==NOACK)
{
I2C_SendStop();
return ERR_I2C_NO_DEVICE;
}
//send memory address
j=(mem_addr&0xff00)>>8;
ret=I2C_SendByte(j); //send address
if (ret==NOACK)
{
I2C_SendStop();
return ERR_I2C_SEND_DATA_FAIL;
}
j=(mem_addr&0x00ff);
ret=I2C_SendByte(j); //send address
if (ret==NOACK)
{
I2C_SendStop();
return ERR_I2C_SEND_DATA_FAIL;
}
slave_addr=slave_addr|0x01; //set as Read addr
I2C_SendStart();
ret=I2C_SendByte(slave_addr); //send address
if (ret==NOACK)
{
I2C_SendStop();
return ERR_I2C_NO_DEVICE;
}
for (i=mem_addr;i<end_addr;i++) //send all data
{
if (i==end_addr-1)
{
*data++=I2C_GetByte(NOACK);
}
else
{
if (((i+1)%PAGE_SIZE)==0)
{
*data++=I2C_GetByte(NOACK);//next page
}
else
{
*data++=I2C_GetByte(ACK);
}
}
//check if next address is 128 byte address
if (((i+1)%PAGE_SIZE)==0)
{
//send address again
I2C_SendStop();
_WaitTime5ms(); //EEPROM ask for 5 ms after STOP
I2C_SendStart();
//sen slave address
slave_addr=slave_addr&0xfe;
ret=I2C_SendByte(slave_addr); //send address
if (ret==NOACK)
{
I2C_SendStop();
return ERR_I2C_NO_DEVICE;
}
//send memory address
j=((i+1)&0xff00)>>8;
ret=I2C_SendByte(j); //send address
if (ret==NOACK)
{
I2C_SendStop();
return ERR_I2C_SEND_DATA_FAIL;
}
j=((i+1)&0x00ff);
ret=I2C_SendByte(j); //send address
if (ret==NOACK)
{
I2C_SendStop();
return ERR_I2C_SEND_DATA_FAIL;
}
slave_addr=slave_addr|0x01; //set as Read addr
I2C_SendStart();
ret=I2C_SendByte(slave_addr); //send address
if (ret==NOACK)
{
I2C_SendStop();
return ERR_I2C_NO_DEVICE;
}
}
}
I2C_SendStop();
return SUCCESS;
}