im trying to write driver for EEPROM (24LC64):
- page size: 32 byte
- capacity: 8KB (64Kbit)
definition code:
#define E24LC64_WRITE_PAGE_SIZE 32
#define E24LC64_TOTAL_SIZE 8000
#define E24LC64_WRITE_DELAY_MS 5
class public function:
uint32_t E24LC64::eeprom_write(uint16_t address, const uint8_t* buff,int32_t length){
// 19 this is test. trimmed? 22
uint8_t page_head_addr ;
int32_t sent_length;
uint32_t err_val = TWI_SUCCESS;
//do we need to write next page:
while ( (length>0) && (TWI_SUCCESS == err_val) ){
err_val = _PageWrite(address, buff, length);
page_head_addr = (address % E24LC64_WRITE_PAGE_SIZE);
sent_length = E24LC64_WRITE_PAGE_SIZE - page_head_addr;
length -= sent_length;
address += sent_length;
buff += sent_length;
}
return err_val;
}
class private function to write pages
/*
this function write pages
it trims the right bytes if they exceed the page boundary.
max length = EEPROM page size.
*/
uint32_t E24LC64::_PageWrite(uint16_t ROMADDR, const uint8_t *buff, uint8_t length){
uint8_t send_length;
uint8_t page_head_addr = (ROMADDR % E24LC64_WRITE_PAGE_SIZE);
if ( (page_head_addr + length)>E24LC64_WRITE_PAGE_SIZE){
send_length = E24LC64_WRITE_PAGE_SIZE - page_head_addr; //trim the extra
}else{
send_length = length;
}
twi_packet_t packet;
packet.buffer = (void*) buff;
packet.length = send_length;
packet.chip = _I2Caddr;
packet.addr[0] = uint8_t(ROMADDR & 0xff00);
packet.addr[1] = uint8_t((ROMADDR >> 8) & 0x00ff);
packet.addr_length = 2;
uint32_t err_val = twi_master_write(_pTWI, &packet);
if (err_val == TWI_SUCCESS)
delay_ms(E24LC64_WRITE_DELAY_MS);
return err_val;
}
user call:
E24LC64 ext_eeprom(TWI0, 0x50);
char mystr[] = "this is test. trimmed?";
ext_eeprom.eeprom_write(19,(uint8_t*)mystr,strlen(mystr))
the error: the function eeprom seems to write back to address 19!
so it looks like this code didnt execute?!
address += sent_length;
_PageWrite
function. \$\endgroup\$