/***
 * Basic functions for the Microchip 2465 I2C eeprom
 * Copyright (c) 2003, Inventronik
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this 
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice, 
 * this list of conditions and the following disclaimer in the documentation 
 * and/or other materials provided with the distribution.
 *
 * Neither the name of the Inventronik nor the names of its contributors may be 
 * used to endorse or promote products derived from this software without specific 
 * prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 
 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * $Id: eep2465.c,v 1.1 2005/01/27 18:14:09 jensi Exp $ *
 */
#ifndef SDA_LINE
#define SDA_LINE PIN_B1
#define SCL_LINE PIN_B0
#endif

#define hi(x) (*(&x+1))
#use i2c(master, sda=SDA_LINE, scl=SCL_LINE)

#define EEP_ADDRESS long int
#define EEP_SIZE 8192

/***
 * Initialize used i2c lines
 */
void init_ext_eeprom() 
{
    output_float(SCL_LINE);
    output_float(SDA_LINE);
}

/***
 * Write a single byte into the eeprom
 */
void write_byte_ext_eeprom(long int addr, byte data) 
{
    i2c_start();
    i2c_write(0xa0);
    i2c_write(hi(addr));
    i2c_write(addr);
    i2c_write(data);
    i2c_stop();
    delay_ms(8);
}

/***
 * Read a single byte from the eeprom
 */
byte read_byte_ext_eeprom(long int addr) 
{
    byte data;

    i2c_start();
    i2c_write(0xa0);
    i2c_write(hi(addr));
    i2c_write(addr);
    i2c_start();
    i2c_write(0xa1);
    data=i2c_read(0);
    i2c_stop();

    return(data);
}

/***
 * Read n bytes from eeprom
 */
void read_ext_eeprom(long int addr, byte *buff, long int n)
{
    long int i;

    for(i=0; i<n; i++) {
        buff[i] = read_byte_ext_eeprom(addr+i);
    }
}

/***
 * Write n bytes into eeprom
 */
void write_ext_eeprom(long int addr, byte *buff, long int n)
{
    long int i;

    for(i=0; i<n; i++) {
        write_byte_ext_eeprom(addr+i, buff[i]);
    }
}

/***
 * Delete all eeprom content
 */
void delete_ext_eeprom()
{
    long int i;

    for(i=0; i<EEP_SIZE; i++) {
        write_byte_ext_eeprom(i, 0xFF);
    }
}

/***
 * read a long int from external eeprom
 */
long int read_longint_ext_eeprom(long int addr) 
{
    return ((long int) read_byte_ext_eeprom(addr<<1)<<8 
        | read_byte_ext_eeprom((addr<<1)+1));
}

/***
 * write a long int to the external eeprom
 */
void write_longint_ext_eeprom(long int addr, long int value) 
{
    write_byte_ext_eeprom(addr<<1, (byte) hi(value));
    write_byte_ext_eeprom((addr<<1)+1, (byte) value);
}

