/**
  ******************************************************************************
  * @file     I2C_EEPROM.c
  * @author   SINOMCU-AE
  * @brief    I2C1 EEPROM
  *    
  *          This file provides functions to write and read EEPROM:
  *    I2C_AT24C02_test().
  * 
  ******************************************************************************
  * @attention 
  *                           DISCLAIMER
  * The present firmware which is for guidance only aims at providing customers
  * with coding information regarding their products in order for them to save
  * time. As a result, SinoMCU shall not be held liable for any direct,indirect
  * or consequential damages with respect to any claims arising from the content 
  * of such firmware and/or the use made by customers of the coding information 
  * contained herein in connection with their products.
  *
  * <h2><center>&copy; Copyright (c) 2021 sinomcu All rights reserved.</center></h2>
  *
  ******************************************************************************
  */

#include "stdio.h"
#include "SysTick_Delay.h"
#include "I2C_CFG.h"
#include "I2C_EEPROM.h"

extern ErrorStatus test_result;

/**
  * @brief  I2C_AT24C02_test
  * @param  uint8_t word_address
  * @retval test_result,SUCCESS or ERROR
  */
ErrorStatus I2C_AT24C02_test(uint8_t word_address)
{
  ErrorStatus test_result=SUCCESS;
  
  uint8_t WriteBuffer[AT24C02_BUFF_LEN]={0},ReadBuffer[AT24C02_BUFF_LEN+2]={0};
  uint16_t byte_count=0; 

  byte_count = 0;
  WriteBuffer[byte_count] =word_address;                   // WriteBuffer[0] 1 byte word address

  printf("\r\n--Inf: Write Data: ");
  for(byte_count =1; byte_count < AT24C02_BUFF_LEN; byte_count++)
  {
    WriteBuffer[byte_count]=byte_count;                    // WriteBuffer init 1 2 3 4 5 6 7 8
    printf("%2d",WriteBuffer[byte_count]);
  }

  // Write to EEPROM
  byte_count = 0;
  // MS32F031,write first byte to TXDR,before call MS32_I2C_HandleTransfer()
  while(! MS32_I2C_IsActiveFlag_TXE(I2C1)){ ; }
  MS32_I2C_TransmitData8(I2C1,WriteBuffer[byte_count++]); 
  
  //                          device address                          1B word add +8 B     
  MS32_I2C_HandleTransfer(I2C1, AT24C02_ADD, MS32_I2C_ADDRSLAVE_7BIT, AT24C02_BUFF_LEN, MS32_I2C_MODE_AUTOEND, MS32_I2C_GENERATE_START_WRITE);

  while(!MS32_I2C_IsActiveFlag_STOP(I2C1))                 // wait send finish
  {
    if(MS32_I2C_IsActiveFlag_TXIS(I2C1))                   // TXIS set than wirte TXDR
    {
      MS32_I2C_TransmitData8(I2C1, WriteBuffer[byte_count++]);
    }
  }
  MS32_I2C_ClearFlag_STOP(I2C1);                           // important
  // MS32F031,TXE clear for next send
  MS32_I2C_ClearFlag_TXE(I2C1);

  printf("\r\n--Inf: EEPROM 24C02 Write Finish.");
  
  SysTick_Ms(AT24C02_WR_DELAY_MS);
  
  // Read from EEPROM 
  byte_count = 0;
  ReadBuffer[byte_count] =word_address;                    // word address

  // MS32F031,write first byte to TXDR,before call MS32_I2C_HandleTransfer()
  while(! MS32_I2C_IsActiveFlag_TXE(I2C1)){ ; }
  MS32_I2C_TransmitData8(I2C1,ReadBuffer[byte_count++]);

  // repare for read form word add
  //                          device address                            1B word add  
  MS32_I2C_HandleTransfer(I2C1, AT24C02_ADD, MS32_I2C_ADDRSLAVE_7BIT, AT24C02_WORD_ADD_LEN, MS32_I2C_MODE_AUTOEND, MS32_I2C_GENERATE_START_WRITE);

  while(!MS32_I2C_IsActiveFlag_STOP(I2C1))
  {
    if(MS32_I2C_IsActiveFlag_TXIS(I2C1))
    {
      MS32_I2C_TransmitData8(I2C1,ReadBuffer[byte_count++]);
    }
  }
  MS32_I2C_ClearFlag_STOP(I2C1);                           // important
  //MS32F031,,TXE clear for next send
  MS32_I2C_ClearFlag_TXE(I2C1);
  
  byte_count = AT24C02_WORD_ADD_LEN;  
  //                          device address                            8B(byte) 
  MS32_I2C_HandleTransfer(I2C1, AT24C02_ADD, MS32_I2C_ADDRSLAVE_7BIT, AT24C02_PAGE_SIZE, MS32_I2C_MODE_AUTOEND,MS32_I2C_GENERATE_START_READ);

  while(!MS32_I2C_IsActiveFlag_STOP(I2C1))
  {
    if(MS32_I2C_IsActiveFlag_RXNE(I2C1))                   // RXE set,RXDR not empty            
    {
      ReadBuffer[byte_count++] = MS32_I2C_ReceiveData8(I2C1);
    }
  }
  MS32_I2C_ClearFlag_STOP(I2C1);
  
  printf("\r\n--Inf: EEPROM 24C02 Read Finish.");

  printf("\r\n--Inf: Read Data: ");
  for(byte_count =AT24C02_WORD_ADD_LEN; byte_count < AT24C02_BUFF_LEN;byte_count++) // compare write and read
  {
    if( ReadBuffer[byte_count] != WriteBuffer[byte_count])
    {
      test_result = ERROR;
      //break;
    }
    printf("%2d",ReadBuffer[byte_count]);                                           // print read buff data
  }

  if(test_result == ERROR)
  {
    printf("\r\n**Error: EEPROM 24C02 ERROR! \r\n");
  }
  else
  {
    printf("\r\n--Inf: EEPROM test success. \r\n");
  }
  
  return test_result;
}

/******************************** END OF FILE *********************************/
