#ifndef EEPROM_H_
#define EEPROM_H_
#include <stdint.h>
#include <stdlib.h>
#include <stddef.h>
#include "main.h"
#include "stm32f407xx.h"

#include "FreeRTOSConfig.h"
#include "cmsis_os2.h"
// #include "i2c.h"
// #include "time_clock.h"
// enum I2C_WORKMODE {

 
// enum  MEM_ADDR_TYPE{
//     BIT8_MEM,
//     BIT16_MEM
// };

// enum  EEPROM_MODEL{ 
//     AT24C01=127,
//     AT24C02=255,
//     AT24C04=511,
//     AT24C08=1023,
//     AT24C16=2047,
//     AT24C32=4095,
//     AT24C64=8191,
//     AT24C128=16383,
//     AT24C256=32767, 
// };

 
// typedef void (*read_callback)();
// typedef void (*write_callback)();
typedef struct _eeprom
{
    I2C_HandleTypeDef *i2c;
    uint16_t  model;       //  AT24C01  AT24C02  AT24C256
    uint8_t mem_addr_type;          //  1: 8bit  2: 16bit I2C_MEMADD_SIZE_8BIT  I2C_MEMADD_SIZE_16BIT
    uint8_t dev_addr;
    uint32_t time_base;             // 从系统获得一个systick的毫秒数
    uint8_t page_addr;
 
    uint8_t *send_buf;      // 将写的buf放到这里，然后让IIC自己工作
    uint16_t size;          // 多少个字节
    uint16_t mem_addr;      //？？
    uint8_t write_delay_ms; // 5-8ms
    uint8_t WORKMODE;      // polling 0 , it 1,  dma 2
    uint8_t writeCplt;
    uint8_t readCplt;

    HAL_I2C_StateTypeDef stat;
}eeprom_t;

extern eeprom_t *eeprom;

eeprom_t *Eeprom_init(  );
uint8_t Eeprom_SET_I2C(eeprom_t *eeprom, I2C_HandleTypeDef *i2c);
uint8_t Eeprom_SET_Device_Model(eeprom_t *eeprom, uint16_t model);
uint8_t Eeprom_SET_Device_Addr(eeprom_t *eeprom, uint8_t dev_addr);
uint8_t Eeprom_SET_Mem_Addr_Type(eeprom_t *eeprom, uint8_t mem_addr_type);

// eeprom_t *eeprom_init( eeprom_t *eeprom,struct i2c_buff *i2c,uint8_t dev_addr,uint8_t mem_addr_size,uint8_t write_delay_ms);
// uint8_t eeprom_write( eeprom_t *eeprom, uint8_t page_addr, uint16_t mem_addr, uint8_t *buf, uint32_t size );
// uint8_t eeprom_read( eeprom_t *eeprom, uint8_t page_addr, uint16_t mem_addr, uint8_t *buf, uint32_t size );
// uint8_t eeprom_send_callback( eeprom_t *eeprom);
// uint8_t eeprom_get_state( eeprom_t *eeprom);

uint8_t Eeprom_Read_One_Byte(eeprom_t *eeprom, uint16_t mem_addr, uint8_t *buf);

uint8_t Eeprom_Write_One_Byte(eeprom_t *eeprom, uint16_t mem_addr, uint8_t *buf);
uint8_t Eeprom_Read_One_Byte_IT(eeprom_t *eeprom, uint16_t mem_addr, uint8_t *buf);

uint8_t Eeprom_Write_One_Byte_IT(eeprom_t *eeprom, uint16_t mem_addr, uint8_t *buf);

uint8_t Eeprom_Read_Multi_Byte(eeprom_t *eeprom, uint16_t mem_addr, uint8_t *buf, uint16_t size, uint32_t timeout  );
uint8_t Eeprom_Write_Multi_Byte(eeprom_t *eeprom, uint16_t mem_addr, uint8_t *buf, uint16_t size , uint32_t timeout );
uint8_t Eeprom_Read_Multi_Byte_IT(eeprom_t *eeprom, uint16_t mem_addr, uint8_t *buf, uint16_t size  );
uint8_t Eeprom_Write_Multi_Byte_IT(eeprom_t *eeprom, uint16_t mem_addr, uint8_t *buf, uint16_t size  );
void Eeprom_send_callback(eeprom_t *eeprom);
uint8_t Eeprom_send(eeprom_t *eeprom);
#endif