/***********************************************
 * I2C メモリドライバインターフェース:
 *   利用可能デバイス:
 *     24FC64, 24LC64, 24FC256, 24LC256, 24FC512, 24LC512
 ************************************************/
#include <xc.h>
#include <stdint.h>
#include "i2c.h"
#include "i2c-24fc64.h"

/* 書込ページサイズ */
#define I2C_24FC64_PAGESIZE_BITS    (32-1)  /*  32 Byte / Page */
#define I2C_24FC256_PAGESIZE_BITS   (64-1)  /*  64 Byte / Page */
#define I2C_24FC512_PAGESIZE_BITS  (128-1)  /* 128 Byte / Page */

/* Read/Write */
enum i2c_24fc64_rw_e {
  I2C_24FC64_WR = 0,
  I2C_24FC64_RD = 1,
};

static uint8_t  i2c_24fc64_caddr  = 0;
static uint32_t i2c_24fc64_maddr  = 0;
static uint32_t i2c_24fc64_nbytes = 0;

/*--------------------------------------------*/

/* 制御バイト送出 */
static enum i2c_ack_type_e
i2c_24fc64_send_ctl_byte(enum i2c_24fc64_rw_e r_w,
                         uint32_t maddr)
{
  return
    I2C_Write(/* Control Byte */
              I2C_24FC64_CTLBYTE
              /* A0,A1,A2: Chip Select Bits */
              | ((unsigned)i2c_24fc64_caddr << 1)
              /* R/nW */
              | ((r_w == I2C_24FC64_RD) ? 0x01 : 0x00)); 
}

/* メモリアドレスを設定 */
static void i2c_24fc64_set_addr(uint32_t maddr)
{
 polling_ack_retry:
  I2C_Start();
  if (i2c_24fc64_send_ctl_byte(I2C_24FC64_WR, maddr)
      != I2C_ACKTYPE_ACK) {
    goto polling_ack_retry;
  }
  I2C_Write((unsigned)(maddr >> 8));   /* Addr High Byte */
  I2C_Write((unsigned)(maddr) & 0xFF); /* Addr Low Byte  */
}

/* 読出し開始 */
void i2c_24fc64_read_begin(uint8_t caddr,
                           uint32_t maddr,
                           uint32_t nbytes)
{
  i2c_24fc64_caddr  = (unsigned)caddr & 0x07;
  i2c_24fc64_maddr  = maddr;
  i2c_24fc64_nbytes = nbytes;
  i2c_24fc64_set_addr(i2c_24fc64_maddr);
  I2C_Repstart();
  i2c_24fc64_send_ctl_byte(I2C_24FC64_RD, i2c_24fc64_maddr);
}

/* 1バイト読み出し */
uint8_t i2c_24fc64_read_byte(void)
{
  uint8_t d = 0;
  if (i2c_24fc64_nbytes == 0) { /* 設定バイト数を読出し済み */
    return 0;
  }
  if (i2c_24fc64_nbytes == 1) { /* 最後の１バイト */
    d = I2C_Read(I2C_ACKTYPE_NOACK);
  } else {
    d = I2C_Read(I2C_ACKTYPE_ACK);
  }
  i2c_24fc64_maddr++;
  i2c_24fc64_nbytes--;
  return d;
}

/* 読出し途中終了 */
void i2c_24fc64_read_done(void)
{
  if (i2c_24fc64_nbytes != 0) {
    I2C_Read(I2C_ACKTYPE_NOACK);
    i2c_24fc64_nbytes = 0;
  }
}

/* 書込み開始 */
void i2c_24fc64_write_begin(uint8_t caddr,
                            uint32_t maddr,
                            uint32_t nbytes)
{
  i2c_24fc64_caddr  = (unsigned)caddr & 0x07;
  i2c_24fc64_maddr  = maddr;
  i2c_24fc64_nbytes = nbytes;
  i2c_24fc64_set_addr(i2c_24fc64_maddr);
}

/* 1バイト書込み */
void i2c_24fc64_write_byte(uint8_t d)
{
  if (i2c_24fc64_nbytes == 0) { /* 設定バイト数を書込み済み */
    return;
  }
  I2C_Write(d);
  i2c_24fc64_maddr++;
  i2c_24fc64_nbytes--;
  if (i2c_24fc64_nbytes == 0) { /* 最後の1バイト */ 
    I2C_Stop();
  } else if ((i2c_24fc64_maddr & I2C_24FC64_PAGESIZE_BITS)
             == 0x0000) { 
    /* ページ境界 */
    I2C_Stop();
    i2c_24fc64_set_addr(i2c_24fc64_maddr);
  }
  /* 注意: プログラム簡略化のため, どのデバイスでも
   *       連続書込は32バイトで行っている */
}

/* 書込み途中終了 */
void i2c_24fc64_write_done(void)
{
  if (i2c_24fc64_nbytes != 0) {
    I2C_Stop();
    i2c_24fc64_nbytes = 0;
  }
}
