/**
 * @file eeprom.c
 * @author Chen Jihang (embedded@eseaoptics.com)
 * @brief EEPROM驱动
 * @version 1.0
 * @date 2023-01-04
 *
 * @copyright ESEA (c) 2020
 *
 */

// osEventFlagsId_t eepromEventHandle;
// const osEventFlagsAttr_t eepromEvent_attributes = {
//     .name = "eepromEvent"
// };


#include "eeprom.h"
#define pEepromI2C &hi2c1
EEPROM_TypeDef *eeprom;


My_EEPROM_TypeDef my_eeprom=
{
    Eeprom_Init,
    Eeprom_DeInit,
    Eeprom_Port,
    AT24C02,
    EEPROM_MEM_ADDR_TYPE_Bit8,
    // EEPROM_MEM_ADDR_BIT_10,
    0xA0,   // 7+1 bit or 10 + 1 bit, EEPROM 8bit , write A0 , read  A1
    0,
};

void Eeprom_Init()
{
    eeprom  = (EEPROM_TypeDef *)malloc(sizeof(EEPROM_TypeDef));
    if ( eeprom == NULL )
    {
        return NULL;
    }
    eeprom->hi2c = pEepromI2C;
    // eeprom->i2chelper = I2CHelper_Init( );
    // I2CHelper_Set_Hi2c( eeprom->i2chelper, pEepromI2C );
    Eeprom_SET_Device_Model( eeprom, my_eeprom.model );
    Eeprom_SET_Device_Addr( eeprom, my_eeprom.dev_addr );
    Eeprom_SET_Mem_Addr_Type( eeprom, my_eeprom.mem_addr_type );

    eeprom->writeCplt       = 0;
    eeprom->readCplt        = 0;
    eeprom->size            = 0;
    eeprom->is_busy         = 0;
    eeprom->time_base         = 0;
    eeprom->write_delay_ms  = 5;
    eeprom->mem_addr        = 0;
    return 0;
}

void Eeprom_DeInit(  )
{
    free(eeprom);
}

void Eeprom_Port(  )
{
    uint32_t  i;
    int st;
    for (;;)
    {
        /* 判断size, size 大于零才开始工作*/
        if (eeprom->size == 0) 
        {
            eeprom->send_buf = NULL;
            eeprom->is_busy == 0;
            // log_i("eeprom port func...");
            osDelay(100);
        };

        /* size> 0, 写入一个数据 */
        if ( eeprom->size > 0 && eeprom->writeCplt == 0 && eeprom->is_busy == 0 )
        {
            eeprom->time_base   = osKernelGetTickCount( );
            eeprom->is_busy     = 1;
            // log_i("eeporm  write ... "); 
            st = HAL_I2C_Mem_Write(eeprom->hi2c, eeprom->dev_addr, AT24C02, eeprom->mem_addr_type, eeprom->send_buf, 1, eeprom->write_delay_ms);
            if (st != HAL_OK) {
                log_e(" Eeprom Write error");
                eeprom->size        = 0;
                eeprom->send_buf    = NULL;
                eeprom->is_busy     = 0;        
                break;        
            }
            // I2CHelper_Write( eeprom->i2chelper, eeprom->send_buf, 1 );
            continue;
        };

        /*  等待 eeprom->write_delay_ms ，写入数据*/
        if ( eeprom->size > 0 && eeprom->writeCplt == 0 && eeprom->is_busy == 1 )
        {
            if( (osKernelGetTickCount( ) - eeprom->time_base)  > eeprom->write_delay_ms)
            {
                // log_i("eeporm  write ... 5ms "); 
                eeprom->writeCplt = 1 ;
            }
            continue;
        };

        /* 写完成，处理数据等待进入下一组 */
        if ( eeprom->size > 0 && eeprom->writeCplt == 1 )
        {
            eeprom->send_buf++;
            eeprom->mem_addr++;
            eeprom->size--;
            eeprom->is_busy = 0; 
            eeprom->writeCplt = 0;
            continue;
        };
        osDelay( 1);
    }
}

// int Eeprom_SET_I2CHelper( EEPROM_TypeDef *eeprom, I2CHelper_TypeDef *i2chelper )
// {
//     eeprom->i2chelper = i2chelper;
//     return 0;
// }

int Eeprom_SET_Device_Model(EEPROM_TypeDef *eeprom, EEPROM_MODEL_TypeDef model)
{
    eeprom->model = model;
    if (eeprom->model < AT24C04 )
    {
        eeprom->mem_addr_type = I2C_MEMADD_SIZE_8BIT;
    }else{
        eeprom->mem_addr_type = I2C_MEMADD_SIZE_16BIT;
    }
    return 0;
}

int Eeprom_SET_Device_Addr(EEPROM_TypeDef *eeprom, uint8_t dev_addr)
{
    eeprom->dev_addr = dev_addr;
    // TODO: 需要对I2C 进行设置
    return 0;
}

int Eeprom_SET_Mem_Addr ( EEPROM_TypeDef *eeprom, uint16_t mem_addr  )
{
    eeprom->mem_addr = mem_addr;
    return 0;
}

int Eeprom_SET_Mem_Addr_Type(EEPROM_TypeDef *eeprom, EEPROM_MEM_ADDR_TypeDef mem_addr_type)
{
    eeprom->mem_addr_type = mem_addr_type;
    return 0;
}

int Eeprom_SET_Mem_Addr_Bit(EEPROM_TypeDef *eeprom, EEPROM_MEM_ADDR_BIT_TypeDef mem_addr_bit)
{
    // eeprom->mem_addr_bit = mem_addr_bit;
    return 0;
}

// int Eeprom_SET_WorkMode( EEPROM_TypeDef *eeprom, I2C_HELPER_MODE_TypeDef mode )
// {
//     eeprom->mode = mode;
//     I2CHelper_Set_Mode( eeprom->i2chelper, mode );
//     return 0;
// }

int Eeprom_SET_Write_Delay_ms( EEPROM_TypeDef *eeprom, uint8_t write_delay_ms)
{
    eeprom->write_delay_ms = write_delay_ms;
    return 0;
}

// int Eeprom_Transmit(EEPROM_TypeDef *eeprom,  uint8_t * buf, uint16_t size)
// {
//     // HAL_I2C_Master_Transmit_DMA ( eeprom->i2chelper->hi2c, eeprom->dev_addr, buf ,size );
// }

// int Eeprom_Begin_Rcv(EEPROM_TypeDef *eeprom,  uint8_t * buf, uint16_t size)
// {
//     // HAL_I2C_Master_Receive_IT( eeprom->i2chelper->hi2c, eeprom->dev_addr, buf ,size );
//     // I2CHelper_Begin_Rcv(eeprom->i2chelper,   buf, size);

// //  HAL_I2C_Mem_Read(eeprom->i2c, eeprom->dev_addr, mem_addr, eeprom->mem_addr_type, buf, 1,eeprom->write_delay_ms);
// //  HAL_I2C_Mem_Write(eeprom->i2c, eeprom->dev_addr, mem_addr, eeprom->mem_addr_type, buf, 1,eeprom->write_delay_ms);

// }

int Eeprom_Read(EEPROM_TypeDef *eeprom,  uint8_t * buf, uint16_t size)
{
    // osEventFlagsGet(eepromEventHandle, EEPROM_EVT_READ);
    eeprom->readCplt = 0;
    int st;
    st = HAL_I2C_Mem_Read( eeprom->hi2c, eeprom->dev_addr, eeprom->mem_addr, eeprom->mem_addr_type, buf, size, 0xFFFF);
    if ( st != HAL_OK ) {
        return -1;
    }
    return 0;
    // HAL_I2C_Master_Transmit_IT (eeprom->hi2c, eeprom->dev_addr, buf ,size);
}

// int Eeprom_Write(EEPROM_TypeDef *eeprom,  uint8_t * buf, uint16_t size)
// {
//     // osEventFlagsGet(eepromEventHandle, EEPROM_EVT_WRITE);
//     eeprom->writeCplt = 0;
//     // HAL_I2C_Master_Transmit_IT (eeprom->i2chelper->hi2c, eeprom->dev_addr, buf ,size);
// }

void Eeprom_Set_Sendbuf_Size(EEPROM_TypeDef *eeprom,  uint8_t * buf, uint16_t size)
{
    eeprom->send_buf    = buf;
    eeprom->size        = size;
    return ;
}

int Eeprom_Test(  )
{
    uint8_t test_send[1] = { 0xFF  };  // 不同model,地址不同  方法也不同
    uint8_t test_rcv  = 0;
    int st;

    // st  = Eeprom_Write( eeprom,  test_send, 1 );
    st = HAL_I2C_Mem_Write(eeprom->hi2c, eeprom->dev_addr, AT24C02, eeprom->mem_addr_type, test_send, 1, eeprom->write_delay_ms);
    osDelay( 10 );

    // st = Eeprom_Read( eeprom, &test_rcv, 1);
    st = HAL_I2C_Mem_Read(eeprom->hi2c, eeprom->dev_addr, AT24C02, eeprom->mem_addr_type, &test_rcv, 1, eeprom->write_delay_ms);
    osDelay( 5 );

    if ( test_rcv == 255 ){
        // log_i( " Test Result  :  PASS " );
        return 0;
    }
    // log_i( " Test Result :  Error  st %d  val %d  " , st, test_rcv );
    return -1;
}

// //主机模式发送回调函数
// void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *I2cHandle)
// {
//     if (I2cHandle == pEepromI2C)
//     {
//         eeprom->writeCplt =1;
//         log_i("send cplt");
//     }
// }
// //主机模式接收回调函数
// void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *I2cHandle)
// {
//     if (I2cHandle == pEepromI2C)
//     {
//         eeprom->readCplt =1;
//         log_i("rcv cplt");
//     }
// }


// https://blog.csdn.net/qq_35496059/article/details/101279408
// void I2C3_EV_IRQHandler(void)
// {
//   /* USER CODE BEGIN I2C3_EV_IRQn 0 */

//   /* USER CODE END I2C3_EV_IRQn 0 */
// 	loop1:
//   	HAL_I2C_EV_IRQHandler(&hi2c3);
//   /* USER CODE BEGIN I2C3_EV_IRQn 1 */
// 	if((((I2C3->ISR)&0x08)>>3)==1)//判断是addr触发的中断
// 	{
// 		I2C3->ISR|=0x01;//TXE=1
// 		I2C3->ICR|=0x08;//清除addr
// 	}

// //读写处理
// 		{
			
// 			if((((I2C3->ISR)&0x10000)>>16)==1)//dir
// 			{
// 				int i=0;
// 				int j=0;
// 				while(((((I2C3->ISR)&0x32)>>5)==0))//detect stop
// 				{
// 					if(((((I2C3->ISR)&0x10000)>>16)==0)||((((I2C3->ISR)&0x08)>>3)==1))//addr or read
// 				goto loop1;			
// 				if(((((I2C3->ISR)&0x02)>>1)==1))TXIS
// 				{
// 					if(I2C3->RXDR==0x00)
// 					I2C3->TXDR=ppp1[i];
// 					if(I2C3->RXDR==0x12)
// 					{
// 						I2C3->TXDR=ppp2[i];
						
// 					}
					
// 					//[i]=I2C3->RXDR;
// 					i++;
			
// 				}
// 						if(i==21)
// 					{
// 					//21个字节 退出	
// 						ppp2[0]++;
// 						return;
// 					}
			
// 				}
				
// 				//x=HAL_I2C_Slave_Receive(&hi2c3,ppp,2,5);
// 				I2C3->ICR=(I2C3->ICR)|0x32;//clear stop

// 			else//read
// 			{
// 				int i=0;
// 				while(((((I2C3->ISR)&0x32)>>5)==0))//detect stop
// 				{
// 					if(((((I2C3->ISR)&0x10000)>>16)==1)||((((I2C3->ISR)&0x08)>>3)==1))
// 						goto loop1;					
// 					if(((((I2C3->ISR)&0x04)>>2)==1))//RXNE
// 					{
// 						ppp[i]=I2C3->RXDR;
// 						i++;
// 					}
// 					}
// 				//x=HAL_I2C_Slave_Receive(&hi2c3,ppp,2,5);
// 				I2C3->ICR=(I2C3->ICR)|0x32;
// 			}
	
// 		}

// //	I2C3->CR1|=0x08;

// //  /* USER CODE END I2C3_EV_IRQn 1 */
// }

// 寄存器说明
// ISR:
// |bit23…17|ADDCODE ----|主机地址----------|
// |bit16…|DIR------------------|0读/1写------------|在addr=1时更新
// |bit15…|BUSY---------------| ---------------------|起始信号=1，检测到stop=0
// |bit5…|STOPF---------------|----------------------|检测到stop
// |bit3…|ADDR----------------|----------------------|匹配到地址
// |bit2…|RXNE----------------|----------------------|RXDR不为空
// |bit1…|TXIS------------------|-------------------- -|发送标志
// |bit0…|TXE------------------|-----------------------|TXDR为空
 

















// uint8_t Eeprom_SET_I2C(eeprom_t *eeprom, I2C_HandleTypeDef *i2c)
// {
//     eeprom->i2c = i2c;
//     eeprom->stat = HAL_I2C_STATE_READY;
//     return 0;
// }
// uint8_t Eeprom_SET_Device_Model(eeprom_t *eeprom, uint16_t model)
// {
//     eeprom->model = model;
//     // if (eeprom->model < AT24C16)
//     // {
//     //     eeprom->mem_addr_type = I2C_MEMADD_SIZE_8BIT;
//     // }
//     // else
//     // {
//     //     eeprom->mem_addr_type = I2C_MEMADD_SIZE_16BIT;
//     // }
//     return 0;
// }

// uint8_t Eeprom_SET_Device_Addr(eeprom_t *eeprom, uint8_t dev_addr)
// {
//     eeprom->dev_addr = dev_addr;
//     return 0;
// }

// uint8_t Eeprom_SET_Mem_Addr_Type(eeprom_t *eeprom, uint8_t mem_addr_type)
// {
//     eeprom->mem_addr_type = mem_addr_type;
//     return 0;
// }

// uint8_t Eeprom_SET_Workmode(eeprom_t *eeprom, uint8_t workmode)
// {
//     eeprom->WORKMODE = workmode;
//     return 0;
// }

// uint8_t Eeprom_Read_One_Byte(eeprom_t *eeprom, uint16_t mem_addr, uint8_t *buf)
// {
//     // eeprom->stat = HAL_I2C_Mem_Read_IT(eeprom->i2c, eeprom->dev_addr, mem_addr, eeprom->mem_addr_type, buf, 1);
//     return HAL_I2C_Mem_Read(eeprom->i2c, eeprom->dev_addr, mem_addr, eeprom->mem_addr_type, buf, 1,eeprom->write_delay_ms);
// }

// uint8_t Eeprom_Write_One_Byte(eeprom_t *eeprom, uint16_t mem_addr, uint8_t *buf)
// {
//     return HAL_I2C_Mem_Write(eeprom->i2c, eeprom->dev_addr, mem_addr, eeprom->mem_addr_type, buf, 1,eeprom->write_delay_ms);

// }

// uint8_t Eeprom_Read_One_Byte_IT(eeprom_t *eeprom, uint16_t mem_addr, uint8_t *buf)
// {
//     // eeprom->stat = HAL_I2C_Mem_Read_IT(eeprom->i2c, eeprom->dev_addr, mem_addr, eeprom->mem_addr_type, buf, 1);
//     eeprom->size  = 0;
//     return HAL_I2C_Mem_Read_IT(eeprom->i2c, eeprom->dev_addr, mem_addr, eeprom->mem_addr_type, buf, 1 );
// }

// uint8_t Eeprom_Write_One_Byte_IT(eeprom_t *eeprom, uint16_t mem_addr, uint8_t *buf)
// {
//     eeprom->size  = 0;
//     return HAL_I2C_Mem_Write_IT(eeprom->i2c, eeprom->dev_addr, mem_addr, eeprom->mem_addr_type, buf, 1 );
// }

// uint8_t Eeprom_Read_Multi_Byte(eeprom_t *eeprom, uint16_t mem_addr, uint8_t *buf, uint16_t size, uint32_t timeout )
// {
//     // eeprom->stat = HAL_I2C_Mem_Write_IT(eeprom->i2c, eeprom->dev_addr, mem_addr, eeprom->mem_addr_type, buf, size);
//     return HAL_I2C_Mem_Read(eeprom->i2c, eeprom->dev_addr, mem_addr, eeprom->mem_addr_type, buf, size,timeout);

// }

// uint8_t Eeprom_Write_Multi_Byte(eeprom_t *eeprom, uint16_t mem_addr, uint8_t *buf, uint16_t size, uint32_t timeout )
// {
//     return HAL_I2C_Mem_Write(eeprom->i2c, eeprom->dev_addr, mem_addr, eeprom->mem_addr_type, buf, size,timeout);

// }

// uint8_t Eeprom_Read_Multi_Byte_IT(eeprom_t *eeprom, uint16_t mem_addr, uint8_t *buf, uint16_t size  )
// {
//     // eeprom->stat = HAL_I2C_Mem_Write_IT(eeprom->i2c, eeprom->dev_addr, mem_addr, eeprom->mem_addr_type, buf, size);
//     eeprom->stat =HAL_I2C_Mem_Read_IT(eeprom->i2c, eeprom->dev_addr, mem_addr, eeprom->mem_addr_type, buf, size );
//     // LOG(" readstat %d",eeprom->stat);
//     return eeprom->stat;

// }

// uint8_t Eeprom_Write_Multi_Byte_IT(eeprom_t *eeprom, uint16_t mem_addr, uint8_t *buf, uint16_t size  )
// {
//     // 写入数据前端循环调用  Eeprom_send_callback
//     // return HAL_I2C_Mem_Write_IT(eeprom->i2c, eeprom->dev_addr, mem_addr, eeprom->mem_addr_type, buf, size );
//     eeprom->send_buf = buf;
//     eeprom->size  = size;
//     eeprom->mem_addr = mem_addr;
//     return 0;
 
// }

// /**
//  * @brief EEPROM发送回调,须循环调用单字节写入
//  *
//  * @param eeprom EEPROM描述符
//  * @return int 0
//  */
// uint8_t Eeprom_send(eeprom_t *eeprom)
// {
//     if (eeprom->size == 0)
//     {
//         eeprom->send_buf = NULL;
//     }
//     if (eeprom->send_buf == NULL)
//     {
//         return 0;
//     }
//     if ( HAL_I2C_GetState(eeprom->i2c) == 1 )
//     {
//         return 0;
//     }
//     if ( (osKernelGetTickCount() -eeprom->time_base) < eeprom->write_delay_ms )
//     {
//         return 0;
//     }
//     if ( HAL_I2C_GetState(eeprom->i2c) == 2 )
//     {
//         // __HAL_I2C_CLEAR_FLAG(eeprom->i2c,I2C_FLAG_AF); //  清除非应答状态
//         eeprom->i2c->State = HAL_I2C_STATE_READY;
//         // i2c_clear_state(eeprom->i2c);
//         eeprom->mem_addr--;
//         eeprom->send_buf--;
//         eeprom->size++;
//     }
//     if (eeprom->size == 0)
//     {
//         eeprom->send_buf = NULL;
//         return 0;
//     }

//     // 单个字节写入 ?? 为什么指向的地址内容发送变化了,静态声明
//     // LOG(  " send  buf  : %02x    %d", *eeprom->send_buf, eeprom->send_buf);
//     HAL_I2C_Mem_Write_IT(eeprom->i2c, eeprom->dev_addr, eeprom->mem_addr, eeprom->mem_addr_type, eeprom->send_buf, 1 );
//     // i2c_write_memory(eeprom->i2c,eeprom->dev_addr+eeprom->page_addr,eeprom->mem_addr,eeprom->mem_addr_size,eeprom->send_buf,1);
//     // eeprom->time_base = __get_tick_milliseconds();
//     eeprom->time_base = osKernelGetTickCount();
//     eeprom->mem_addr++;
//     eeprom->send_buf++;
//     eeprom->size--;
//     return 0;
// }

// void Eeprom_send_callback(eeprom_t *eeprom)
// {
//     Eeprom_send(eeprom);
// }

// uint8_t Eeprom_get_state(eeprom_t *eeprom)
// {
//     if (eeprom->send_buf != NULL)
//     {
//         return 1;
//     }
//     if ( HAL_I2C_GetState(eeprom->i2c) == HAL_I2C_STATE_READY )
//     {
//         return 0;
//     };
//     return 2;
// }