/**
 *******************************************************************************
 * @file    eeprom.c
 *
 * @brief   eeprom program body.
 *
 * @author  DPM
 *
 * @version V1.0.0
 *
 * @date    2024-5-6
 *
 *******************************************************************************/

/*
    Copyright (c) 2024, DP Microelectronics Inc.

    Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this
       list of conditions and the following disclaimer.
    2. 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.
    3. Neither the name of the copyright holder 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 HOLDER 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.
*/

#include "eeprom.h"
#include "dpm32m0xx.h"
#include <stdio.h>
#include "i2c.h"
#include "systick.h"


#define EEPROM_ADDRESS           0xA0
#define BUFFER_SIZE              16
#define I2C_PAGE_SIZE            8
#define I2C_TIME_OUT            (uint16_t)(5000)

uint8_t EEPROM_TransmitTimeOut(uint8_t SlaveAddress, uint8_t WriteAddr, uint8_t *Buf, uint16_t Len, uint32_t Timeout);
uint8_t EEPROM_ReceiveTimeOut(uint8_t SlaveAddress, uint8_t WriteAddr, uint8_t *Buf, uint16_t Len, uint32_t Timeout);

/**
 *******************************************************************************
 * @brief   eeprom read and write functions, eeprom test example.
 * @param   None.
 * @retval  uint8_t I2C_OK or I2C_FAIL.
 ******************************************************************************/
uint8_t EEPROM_AT24C02Test(void)
{
  uint16_t i;
  uint16_t addr = 0;
  uint8_t i2c_buffer_write[BUFFER_SIZE];
  uint8_t i2c_buffer_read[BUFFER_SIZE];

  printf("\r\nAT24C02 writing...\r\n");

  /* initialize i2c_buffer_write */
  for(i = 0; i < BUFFER_SIZE; i++)
  {
    i2c_buffer_write[i] = i;
    printf("0x%02X ", i2c_buffer_write[i]);

    if(15 == i % 16)
    {
      printf("\r\n");
    }
  }

  if(EEPROM_TransmitTimeOut(EEPROM_ADDRESS, addr, i2c_buffer_write, BUFFER_SIZE, I2C_TIME_OUT) != 0)
  {
    printf("\nAT24C02 write err.\n\r");
    return 1;
  }

  printf("\nAT24C02 reading...\r\n");

  DelayMs(500);

  /* EEPROM data read */
  if(EEPROM_ReceiveTimeOut(EEPROM_ADDRESS, addr, i2c_buffer_read, BUFFER_SIZE, I2C_TIME_OUT) != 0)
  {
    printf("\nAT24C02 read err.\n\r");
    return 1;
  }

  /* initialize i2c_buffer_read */
  for(i = 0; i < BUFFER_SIZE; i++)
  {
    printf("0x%02X ", i2c_buffer_read[i]);

    if(15 == i % 16)
    {
      printf("\r\n");
    }
  }

  printf("***************check**************\n");

  /* compare the read buffer and write buffer */
  for(i = 0; i < BUFFER_SIZE; i++)
  {
    if(i2c_buffer_read[i] != i2c_buffer_write[i])
    {
      printf("0x%02X ", i2c_buffer_read[i]);
      printf("\nErr:data read and write aren't matching.\n\r");
      return 1;
    }

    printf("0x%02X ", i2c_buffer_read[i]);

    if(15 == i % 16)
    {
      printf("\r\n");
    }
  }

  printf("\nI2C-AT24C02 test passed!\n\r");

  return 0;
}

/**
 *******************************************************************************
 * @brief   EEPROM transmit a buffer with timeout.
 * @param   [in]  SlaveAddress: Slave I2C address.
 * @param   [in]  WriteAddr: Data write address.
 * @param   [in]  Buf: Data buffer.
 * @param   [in]  Len: Data len.
 * @param   [in]  Timeout: Timeout time.
 * @retval  uint8_t OK retrun 0, Fail return 1.
 ******************************************************************************/
uint8_t EEPROM_TransmitTimeOut(uint8_t SlaveAddress, uint8_t WriteAddr, uint8_t *Buf, uint16_t Len, uint32_t Timeout)
{
  uint8_t number_of_page = 0, number_of_single = 0, address = 0, count = 0;

  address = WriteAddr % I2C_PAGE_SIZE;
  count = I2C_PAGE_SIZE - address;
  number_of_page = Len / I2C_PAGE_SIZE;
  number_of_single = Len % I2C_PAGE_SIZE;


  /* if write_address is I2C_PAGE_SIZE aligned */
  if(0 == address)
  {
    while(number_of_page--)
    {
      if(I2C_BufferTransmitTimeOut(SlaveAddress, WriteAddr, Buf, I2C_PAGE_SIZE, Timeout) != 0)
      {
        return 1;
      }

      DelayMs(5);
      WriteAddr +=  I2C_PAGE_SIZE;
      Buf += I2C_PAGE_SIZE;
    }

    if(0 != number_of_single)
    {
      if(I2C_BufferTransmitTimeOut(SlaveAddress, WriteAddr, Buf, I2C_PAGE_SIZE, Timeout) != 0)
      {
        return 1;
      }

      DelayMs(5);
    }
  }
  else
  {
    /* if write_address is not I2C_PAGE_SIZE aligned */
    if(Len < count)
    {
      if(I2C_BufferTransmitTimeOut(SlaveAddress, WriteAddr, Buf, Len, Timeout) != 0)
      {
        return 1;
      }
    }
    else
    {
      Len -= count;
      number_of_page =  Len / I2C_PAGE_SIZE;
      number_of_single = Len % I2C_PAGE_SIZE;

      if(0 != count)
      {
        if(I2C_BufferTransmitTimeOut(SlaveAddress, WriteAddr, Buf, count, Timeout) != 0)
        {
          return 1;
        }

        DelayMs(5);
        WriteAddr += count;
        Buf += count;
      }

      /* write page */
      while(number_of_page--)
      {
        if(I2C_BufferTransmitTimeOut(SlaveAddress, WriteAddr, Buf, I2C_PAGE_SIZE, Timeout) != 0)
        {
          return 1;
        }

        DelayMs(5);
        WriteAddr +=  I2C_PAGE_SIZE;
        Buf += I2C_PAGE_SIZE;
      }

      /* write single */
      if(0 != number_of_single)
      {
        if(I2C_BufferTransmitTimeOut(SlaveAddress, WriteAddr, Buf, number_of_single, Timeout) != 0)
        {
          return 1;
        }
      }
    }
  }

  return 0;
}

/**
 *******************************************************************************
 * @brief   EEPROM receive a buffer with timeout.
 * @param   [in]  SlaveAddress: Slave I2C address.
 * @param   [in]  WriteAddr: Data write address.
 * @param   [in]  Buf: Data buffer.
 * @param   [in]  Len: Data len.
 * @param   [in]  Timeout: Timeout time.
 * @retval  uint8_t OK retrun 0, Fail return 1.
 ******************************************************************************/
uint8_t EEPROM_ReceiveTimeOut(uint8_t SlaveAddress, uint8_t WriteAddr, uint8_t *Buf, uint16_t Len, uint32_t Timeout)
{
  return I2C_BufferReceiveTimeOut(SlaveAddress, WriteAddr, Buf, Len, Timeout);
}
