/**
 * @file EepromMiddle.c
 * @brief 中间层封装的EEPROM通用代码
 * @author Landworld0321 (Landworld@landworld.com)
 * @version V1.0
 * @date 2025-04-23
 * @copyright Copyright (c) 2025 by Landworld.Co.Ltd. All rights reserved
 * 版本号    修改日期        修改人        修改内容
 * ---------------------------------------------------------------------------
 * v1.0    2025-04-23     WuHongHui     整理初版程序
 */

/*INCLUDE HEAD*********************************************************/
#include "EepromMiddle.h"
#include "Common_Define.h"
/*FUNCTION*********************************************************/

uint8_t count = 0;
void i2c_Callback(i2c_master_event_t event, void *userData)
{
	if (event == I2C_MASTER_EVENT_END_TRANSFER)
	{
		IO_EEPROM_STBOFF;
	}
}

/**
 * @brief 初始化EEPROM相关的I2C外设
 * 
 */
void EepromMiddle_Init(void)
{
	I2C_MasterInit(&I2C_Config_instance, &I2C_Config_MasterConfig0);
}

void delay_ms(UINT32 nms)
{
	volatile UINT32 MsDef = 11420;
	volatile UINT32 i = nms*MsDef;
	while(i--)
	{
	}
}

/**
 * @brief 写数据(16位寻址)，该函数每次向EEPROM的指定地址写入1个数据
 * 
 * @param Addr EEPROM的16位地址
 * @param Data 待写入的1字节的数据
 * @return UINT8 返回操作结果.SUCCESS:写入成功  FAIL:写入失败
 */
UINT8 App_EERPOM_Wt1Byte(UINT16 Addr, UINT8 Data)
{
	UINT32 bytesRem = 0;
	UINT8 TxBuffer[3] = {0};
	UINT8 status = FAIL;
	volatile UINT32 timeout = 0;

	TxBuffer[0] = (Addr >> 8) & 0xFF;
	TxBuffer[1] = (Addr     ) & 0xFF;
	TxBuffer[2] = Data;

	IO_EEPROM_STBON;

	while(STATUS_SUCCESS != I2C_MasterSendDataBlocking(&I2C_Config_instance,&TxBuffer[0],3,true,10));

}

UINT8 EepMiddle_Read(UINT16 Addr,UINT8* Data)
{
    status_t status = STATUS_SUCCESS;
    UINT8 t_u8RdArr[2] = {0};
    UINT8 t_u8RdData = 0;
    static UINT32 s_u32RdTick5ms = 0;
    UINT32 bytesRem = 0;

    t_u8RdArr[0] = (Addr >> 8) & 0xFF;
	t_u8RdArr[1] = (Addr >> 0) & 0xFF;

	IO_EEPROM_STBOFF;
    I2C_MasterSendDataBlocking(&I2C_Config_instance,&t_u8RdArr[0],2,true,10);
    status = I2C_MasterReceiveDataBlocking(&I2C_Config_instance,&t_u8RdData,1,true,10);

    *Data = t_u8RdData;
}

/**
 * @brief 读数据(16位寻址)，该函数每次向EEPROM的指定地址读取1个数据
 * 
 * @param Addr 指定EEPROM地址读取1个数据
 * @param Data 待读取的数据
 * @return UINT8 返回操作结果.SUCCESS:写入成功  FAIL:写入失败
 */
UINT8 App_EERPOM_Rd1Byte(UINT16 Addr, UINT8 *Data)
{
	UINT32 bytesRem = 0;
	volatile UINT32 timeout = FLAG_TIMEOUT;
	UINT8 RxBuffer = 0;
	UINT8 TxBuffer[2] = {0};
	UINT8 status = FAIL;

	TxBuffer[0] = (Addr >> 8) & 0xFF;
	TxBuffer[1] = (Addr     ) & 0xFF;

	IO_EEPROM_STBON;
	timeout = FLAG_TIMEOUT;
	if(0 == timeout)
	{
		IO_EEPROM_STBOFF;
		*Data = 0x00;
		return status;
	}
	I2C_MasterSendData(&I2C_Config_instance,TxBuffer,2,false);
	timeout = FLAG_TIMEOUT;
	while((I2C_MasterGetTransferStatus(&I2C_Config_instance , &bytesRem) != STATUS_SUCCESS)&&(timeout--));
	if(0 == timeout)
	{
		IO_EEPROM_STBOFF;
		*Data = 0x00;
		return status;
	}

	if(0 == timeout)
	{
		IO_EEPROM_STBOFF;
		*Data = 0x00;
		return status;
	}
	I2C_MasterReceiveData(&I2C_Config_instance,&RxBuffer,1,true);
	timeout = FLAG_TIMEOUT;
	while((I2C_MasterGetTransferStatus(&I2C_Config_instance , &bytesRem) != STATUS_SUCCESS)&&(timeout--));
	if(0 == timeout)
	{
		IO_EEPROM_STBOFF;	*Data = 0x00;
		return status;
	}
	else
	{
		*Data = RxBuffer;
		status = SUCCESS;
	}
	IO_EEPROM_STBOFF;
	delay_ms(1);
	return status;
}
