static void
eeprom_write(int addr, cyg_uint8 val)
{
    cyg_uint8 start_byte;
    int i;

    start_byte = 0xA0;  // write

    if (addr & (1 << 8))
	start_byte |= 2;

    for (i = 0; i < 10; i++)
	if (eeprom_start(start_byte))
	    break;

    if (i == 10) {
	diag_printf("eeprom_write: Can't get start ACK\n");
	return;
    }

    if (!eeprom_putb(addr & 0xff)) {
	diag_printf("eeprom_write: Can't get address ACK\n");
	return;
    }

    if (!eeprom_putb(val)) {
	diag_printf("eeprom_write: no data ACK\n");
	return;
    }
    eeprom_stop();
}
uint8_t eeprom_read_byte(const uint16_t address) {

  /* Start data transmission on the bus. */
  eeprom_start();

  /* Send a dummy write to the bus with the requested read address. */
  eeprom_i2c_write(EEPROM_I2C_WRITE_ADDRESS);
  eeprom_i2c_write(address >> 8);
  eeprom_i2c_write(address);

  /* Start data transmission on the bus. */
  eeprom_start();

  /* Send to the bus a read request. */
  eeprom_i2c_write(EEPROM_I2C_READ_ADDRESS);

  /* Read the byte from the EEPROM. */
  uint8_t byte = eeprom_i2c_read();

  /* Return a NACK for acknowledgement. */
  eeprom_i2c_send_ack(EEPROM_I2C_NACK);

  /* Stop data transmission on the bus. */
  eeprom_stop();

  return byte;
}
static int
eeprom_read(int addr, cyg_uint8 *buf, int nbytes)
{
    cyg_uint8 start_byte;
    int i;

    start_byte = 0xA0;  // write

    if (addr & (1 << 8))
	start_byte |= 2;

    
    for (i = 0; i < 10; i++)
	if (eeprom_start(start_byte))
	    break;

    if (i == 10) {
	diag_printf("eeprom_read: Can't get write start ACK\n");
	return 0;
    }

    if (!eeprom_putb(addr & 0xff)) {
	diag_printf("eeprom_read: Can't get address ACK\n");
	return 0;
    }

    start_byte |= 1; // READ command
    if (!eeprom_start(start_byte)) {
	diag_printf("eeprom_read: Can't get read start ACK\n");
	return 0;
    }

    for (i = 0; i < (nbytes - 1); i++)
	*buf++ = eeprom_getb(1);

    *buf++ = eeprom_getb(0);
    hal_delay_us(5);
    eeprom_stop();

    return nbytes;
}
bool eeprom_write_byte(const uint8_t value, const uint16_t address) {

  /* Start data transmission on the bus. */
  eeprom_start();

  /* Send a write request containing the target address to the bus. */
  eeprom_i2c_write(EEPROM_I2C_WRITE_ADDRESS);
  eeprom_i2c_write(address >> 8);
  eeprom_i2c_write(address);

  /* Send the byte to write to the bus. */
  eeprom_i2c_write(value);

  /* Read the acknowledgement result from the operation. */
  bool result = eeprom_i2c_get_ack();

  /* Stop data transmission on the bus. */
  eeprom_stop();

  return result;
}