Programming the original JP1 remote controls with Arduino

Part 3 (last part):

// ? - Product information
void info() {
SerCRLF();
send_msg("JP1 EEPROM Programming Adapter\r\n");
send_msg("Copyright 2010 Kevin Timmerman\r\n");
send_msg("Ported to Arduino 2012 Tim6502\r\n");
send_msg("Rev 003 Config ");
tx_hex_byte(ee_size);
SerTx(' ');
SerTx( (fTwoByteAddress) ? '2' : '1' );
SerCRLF();
SerCRLF();
send_msg("d Display EEPROM contents\r\n");
send_msg("f Fill with FF\r\n");
send_msg("z Fill with 00\r\n");
send_msg("t Fill with test pattern\r\n");
send_msg("4 24C04\r\n");
send_msg("8 24C08\r\n");
send_msg("6 24C16\r\n");
send_msg("3 24C32");
SerCRLF();
}

// --- Fill EEPROM with ee_fill
void fill() {
send_msg("Fill EEPROM with ");
if (fFillTestPattern) {
send_msg("test pattern");
} else {
tx_hex_byte(ee_fill);
}
SerTx(' ');
SerTx('?');
SerCRLF();

byte b = SerRx();
if (b != (byte)'Y') {
fill_abort();
return;
}

addrl = 0;
addrh = 0;
fShowAckWait = true;

SerCRLF();
tx_hex_byte(addrh);
tx_hex_byte(addrl);

unsigned address=addrl + 256*addrh;

while (address < ee_size*256) {
if (fFillTestPattern) {
eeprom_write_byte(DEVADDR, address, ee_fill);
} else {
eeprom_write_byte(DEVADDR, address, addrl+addrh);
addrl++;
if (addrl==0) addrh++;
}
}
SerCRLF();
}

// - Abort fill
void fill_abort() {
send_msg("Fill Canceled");
SerCRLF();
}

// --- Get address and length from host, update checksum
byte get_addr_len() {
addrh = SerRxChk();
addrl = SerRxChk();
jp1_len = SerRxChk();
}

// - CR LF
void SerCRLF() {
SerTx(13);
SerTx(10);
}

// - Send byte as ASCII hex
void tx_hex_byte(byte b) {
byte nib = b / 16;
SerTx(hex[nib]);
nib = b % 16;
SerTx(hex[nib]);
}

void SerTx(byte b) {
Serial.write(b);
}

byte SerRx() {
int b = -1;
while (b < 0) {
while (Serial.available() < 0) {
// wait here
};
b = Serial.read();
}
return (byte) b;
}

byte SerRxChk() {
byte b = SerRx();
jp1_chk = (b ^ jp1_chk);
return b;
}

void eeprom_write_byte(byte deviceaddress, int eeaddress, byte data)
{
// Three lsb of Device address byte are bits 8-10 of eeaddress
byte devaddr = deviceaddress | ((eeaddress >> 8) & 0x07);
byte addr = eeaddress;
Wire.beginTransmission(devaddr);
Wire.write(int(addr));
Wire.write(int(data));
Wire.endTransmission();
delay(10);
}

int eeprom_read_byte(byte deviceaddress, unsigned eeaddr)
{
byte rdata = -1;

// Three lsb of Device address byte are bits 8-10 of eeaddress
byte devaddr = deviceaddress | ((eeaddr >> 8) & 0x07);
byte addr = eeaddr;

Wire.beginTransmission(devaddr);
Wire.write(int(addr));
Wire.endTransmission();
Wire.requestFrom(int(devaddr), 1);
if (Wire.available()) {
rdata = Wire.read();
}
return rdata;
}

// Pages are blocks of 16 bytes, starting at 0x000.
// That is, pages start at 0x000, 0x010, 0x020, ...
// For a device "page write", the last byte must be
// on the same page as the first byte.
void eeprom_write_pages(byte deviceaddress, unsigned eeaddr, unsigned length, const byte * data)
{
unsigned count=0;
while (count < length) {
// Three lsb of Device address byte are bits 8-10 of eeaddress
byte devaddr = deviceaddress | ((eeaddr >> 8) & 0x07);
byte addr = eeaddr;
byte nibble = addr & 0x07;
byte i = 0;

Wire.beginTransmission(devaddr);
Wire.write(int(addr));
do {
Wire.write(data[count]);
count++; i++;
} while (count < length && (((nibble + i) & 0x07) != 0));
eeaddr += i;
Wire.endTransmission();
delay(10);
}

}

Moderator edit: smiles turned off.