/***********************************************
 * I2C メモリドライバインターフェース:
 *   24FC1025/24LC1025/24AA1025 
 ************************************************/
#include <xc.h>
#include <stdint.h>
#include "i2c.h"
#include "i2c-24fc1025.h"

/*--------------------------------------------*/
#define I2C_24FC1025_PAGESIZE_BITS   (128-1)

enum i2c_24fc1025_rw_e {
  I2C_24FC1025_WR = 0,
  I2C_24FC1025_RD = 1,
};

static uint8_t  i2c_24fc1025_caddr  = 0;
static uint32_t i2c_24fc1025_maddr  = 0;
static uint32_t i2c_24fc1025_nbytes = 0;

/* 制御バイト送出 */
static enum i2c_ack_type_e
i2c_24fc1025_send_ctl_byte(enum i2c_24fc1025_rw_e r_w,
                           uint32_t maddr)
{
  return
    I2C_Write(/* Control Byte */
              I2C_24FC1025_CTLBYTE
              /* B0:    Block Select Bit */
              | ((maddr >> 13) & 0x08) 
              /* A0,A1: Chip Select Bits */
              | ((unsigned)i2c_24fc1025_caddr << 1)
              /* R/nW */
              | ((r_w == I2C_24FC1025_RD) ? 0x01 : 0x00)); 
}

/* メモリアドレスを設定 */
static void i2c_24fc1025_set_addr(uint32_t maddr)
{
 polling_ack_retry:
  I2C_Start();
  if (i2c_24fc1025_send_ctl_byte(I2C_24FC1025_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_24fc1025_read_begin(uint8_t  caddr,
                             uint32_t maddr,
                             uint32_t nbytes)
{
  i2c_24fc1025_caddr  = (unsigned)caddr & 0x03;
  i2c_24fc1025_maddr  = maddr;
  i2c_24fc1025_nbytes = nbytes;
  i2c_24fc1025_set_addr(i2c_24fc1025_maddr);
  I2C_Repstart();
  i2c_24fc1025_send_ctl_byte(I2C_24FC1025_RD, i2c_24fc1025_maddr);
}

/* １バイト読出し */
uint8_t i2c_24fc1025_read_byte(void)
{
  uint8_t d = 0;
  if (i2c_24fc1025_nbytes == 0) { /* 設定バイト数を読出し済み */
    return 0;
  }
  if (i2c_24fc1025_nbytes == 1) { /* 最後の１バイト */
    d = I2C_Read(I2C_ACKTYPE_NOACK);
  } else if ((i2c_24fc1025_maddr & 0x00FFFF)
             == 0x00FFFF) {       /* ブロック境界 */
    /* ブロック境界を越えた連続読み取り: アドレス再設定 */
    d = I2C_Read(I2C_ACKTYPE_NOACK);
    i2c_24fc1025_set_addr(i2c_24fc1025_maddr + 1); 
    I2C_Repstart();
    i2c_24fc1025_send_ctl_byte(I2C_24FC1025_RD,
                               i2c_24fc1025_maddr + 1);
  } else {                        /* 連続読出し */
    d = I2C_Read(I2C_ACKTYPE_ACK);
  }
  i2c_24fc1025_maddr++;
  i2c_24fc1025_nbytes--;
  return d;
}

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

/* 書込み開始 */
void i2c_24fc1025_write_begin(uint8_t  caddr,
                              uint32_t maddr,
                              uint32_t nbytes)
{
  i2c_24fc1025_caddr  = (unsigned)caddr & 0x03;
  i2c_24fc1025_maddr  = maddr;
  i2c_24fc1025_nbytes = nbytes;
  i2c_24fc1025_set_addr(i2c_24fc1025_maddr);
}

/* 1バイト書込み */
void i2c_24fc1025_write_byte(uint8_t d)
{
  if (i2c_24fc1025_nbytes == 0) { /* 設定バイト数を書込み済み */
    return;
  }
  I2C_Write(d);
  i2c_24fc1025_maddr++;
  i2c_24fc1025_nbytes--;
  if (i2c_24fc1025_nbytes == 0) { /* 最後の1バイト */ 
    I2C_Stop();
  } else if ((i2c_24fc1025_maddr & I2C_24FC1025_PAGESIZE_BITS)
             == 0x0000) {
    /* ページ境界 */
    I2C_Stop();
    i2c_24fc1025_set_addr(i2c_24fc1025_maddr);
  }
}

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