/////////////////////////////////////////////////////////////////////////////////////////
////   Library for a 24LC1025 serial EEPROM                                           ////
////                                                                                 ////
////   init_ext_eeprom();    Call before the other functions are used                ////
////                                                                                 ////
////   write_ext_eeprom(c, a, d);  Write the byte d to the address a, of chip c      ////
////                                                                                 ////
////   d = read_ext_eeprom(c, a);   Read the byte d from the address a, of chip c    ////
////                                                                                 ////
////   The main program may define eeprom_sda                                        ////
////   and eeprom_scl to override the defaults below.                                ////
////                                                                                 ////
/////////////////////////////////////////////////////////////////////////////////////////

/*
#ifndef EEPROM_SDA

#define EEPROM_SDA  PIN_B1
#define EEPROM_SCL  PIN_B0

#endif

#use i2c(master, sda=EEPROM_SDA, scl=EEPROM_SCL)
*/

#define EEPROM_ADDRESS long int
#define EEPROM_BLOCKSIZE   65536
#define EEPROM_SIZE   131072

void init_ext_eeprom()
{
   output_float(EEPROM_SCL);
   output_float(EEPROM_SDA);

}


void write_ext_eeprom(int8 deviceID, int32 address, int8 data)
{
   short int status;
   i2c_start();
   i2c_write(0b10100000 | (deviceID<<1) | (bit_test(address,16)<<3));
   i2c_write(address>>8);
   i2c_write(address);
   i2c_write(data);
   i2c_stop();
   i2c_start();
   status = i2c_write(0b10100000 | (deviceID<<1) | (bit_test(address,16)<<3));
   while(status==1)
   {
   i2c_start();
   status=i2c_write(0b10100000 | (deviceID<<1) | (bit_test(address,16)<<3));
   }
}


BYTE read_ext_eeprom(int8 deviceID, int32 address) {
   BYTE data;
   i2c_start();
   i2c_write(0b10100000 | (deviceID<<1) | (bit_test(address,16)<<3));
   i2c_write(address>>8);
   i2c_write(address);
   i2c_start();
   i2c_write(0b10100001 | (deviceID<<1) | (bit_test(address,16)<<3));
   data = i2c_read(0);
   i2c_stop();
   return(data);
}

