/****************************************************************************
* Company: Microsemi Corporation
*
* File: EEPROM.h
* File history:
*     Revision: Initial version   Date: May 04, 2010
*
* Description:
*             EEPROM(M24512-R from ST) driver.
*             It defines the Initialisation, Write, read and delay functions
*
* Author: Pavan Marisetti
*         pavan.marisetti@microsemi.com
*         Corporate Applications Engineering
*
****************************************************************************/

#ifndef EEPROM_H_
#define EEPROM_H_

#include "../drivers/mss_i2c/mss_i2c.h"

/* I2C master instance, This can be changed if the EEPROM is connected to
I2C0 on the user custom board*/
#define I2C_INSTANCE            &g_mss_i2c1
/* EEPROM Slave address*/
#define EEPROM_SLAVE_ADDRESS    0xA0
/* I2C Clock frequency*/
#define EEPROM_I2C_CLK_FREQ    MSS_I2C_PCLK_DIV_256
/* EEPROM Page size in bytes*/
#define PAGE_SIZE_IN_BYTES      128
/* EEPROM Address lenght in bytes*/
#define ADDR_LEN_IN_BYTES       2
/* EEPROM Address first location*/
#define ADDR_FIRST_LOC          0
/* EEPROM Address second location*/
#define ADDR_SECOND_LOC         1

mss_i2c_status_t I2C_STATUS;
/***************************************************************************//**
 This enumeration is used to specific EEPROM Write status
 */
typedef enum
{
    EEPROM_WRITE_SUCCESS = 0,
    EEPROM_READ_SUCCESS,
    EEPROM_WRITE_UNSUCCESS,
    EEPROM_READ_UNSUCCESS,
    EEPROM_ADDRESS_FAULT
} EEPROM_status_t;

/****************************************************************************
  The EEPROM_initfunction initializes the MSS_I2C to communicate with the
  EEPROM.
 */
void EEPROM_init( void );

/***************************************************************************/
/*
  The EEPROM_write() function is used by MSS I2C masters to transmit data
  organized as a specified number of bytes, which are mentioned in the EEPROM
  spec(M24512-R).
  @param start_address
    The start_address parameter specifies the start address of the EEPROM, where
    data has to be written

  @param size_in_bytes
    The size_in_bytes parameter specifies the number of bytes to be written to
    EEPROM from write_buffer.

  @param write_buffer
    The write_buffer parameter is a pointer to the buffer from where the data
    has to be transmitted

    Note 1:   The summation of start_address and size_in_bytes should be in the
    range of 1 to 65536

    Note 2:   The start_address should be in the range of 0 to 65536

    Note 3:   The size_in_bytes should be in the range of 1 to 65536


  EEPROM write example:
  @code
int main()
{
    uint16_t i, start_address=0;
    uint8_t write_data[300];
    MSS_WD_disable();
    EEPROM_init();

    for(i=0;i<128;i++)
    {
        write_data[i] = 0x44;
    }
    for(i=128;i<256;i++)
    {
        write_data[i] = 0x55;
    }
    for(i=256;i<300;i++)
    {
        write_data[i] = 0x66;
    }

    EEPROM_write(start_address,300,write_data);


    return(0);
}

@endcode */

EEPROM_status_t
EEPROM_write
(
    uint16_t start_address,
    uint16_t size_in_bytes,
    uint8_t  * write_buffer
);

/***************************************************************************/
/*
  The EEPROM_read() function is used by MSS I2C masters to receive the data
  as specified by the number of bytes.

  @param start_address
    The start_address parameter specifies the start address of the EEPROM, from
    where the data has to be received

  @param size_in_bytes
    The size_in_bytes parameter specifies the number of bytes to be read from
    EEPROM to read_buffer.

  @param read_buffer
    The read_buffer parameter is a pointer to the buffer to where the data
    has to be received

    Note 1:   The summation of start_address and size_in_bytes should be in the
    range of 1 to 65536

    Note 2:   The start_address should be in the range of 0 to 65536

    Note 3:   The size_in_bytes should be in the range of 1 to 65536


  EEPROM read example:
  @code
int main()
{
    uint16_t i, start_address=0;
    uint8_t read_data[300];
    MSS_WD_disable();
    EEPROM_init();

    EEPROM_read(start_address,300,read_data);

    return(0);
}

@endcode */

EEPROM_status_t
EEPROM_read
(
    uint16_t start_address,
    uint16_t size_in_bytes,
    uint8_t * read_buffer
);

/***************************************************************************/
/*
  The delay_btw_transfers ( volatile uint32_t n) function is used provide
  delay between two transfers (delay between stop and start conditions)
*/
void delay_btw_transfers ( volatile uint32_t n);

#endif
