/*
 * eeprom16.c
 *
 * code to interface with 16-bit addressed I2C EEPROMs
 * tested up to max size of 512Kbit (64Kbyte)
 *
 */

#include "eeprom16.h"
#include "comm/i2c.h"


uint8_t eep16_read_byte(uint16_t addr)
{
    return 0;
}

void eep16_read(uint16_t addr, uint8_t *dat, uint32_t len)
{
    if (len > EEPROM_SIZE) return;

    i2c_addr(EEPROM_I2C_ADDR, 0);
    i2c_wr(addr >> 8);
    i2c_wr(addr & 0xff);
    i2c_restart();
    i2c_wr(EEPROM_I2C_ADDR | 1);

    while (len--) *dat++ = i2c_rd(len > 0);

    i2c_stop();
}

void eep16_write(uint16_t addr, uint8_t *dat, uint16_t len)
{
    uint8_t nack;

    // this function can only write one page at a time
    if (len > EEPROM_PAGE_SIZE) return;

    // this function does not detect page boundaries.
    // it is your responsibility to correctly address for
    // page boundaries. most eeprom roll over page boundaries.

    i2c_addr(EEPROM_I2C_ADDR, 0);
    i2c_wr(addr >> 8);
    i2c_wr(addr & 0xff);

    while (len--) {
        nack = i2c_wr(*dat++);
        if (nack) break;
    }

    i2c_stop();
}

uint8_t eep16_is_ready()
{
    uint8_t ack;

    i2c_start();
    ack = i2c_wr(EEPROM_I2C_ADDR | 0);
    i2c_stop();

    if (ack) return 0;
    return 1;
}


