/*
 * eeprom_m24.h
 *
 *  Created on: Oct 24, 2024
 *      Author: Invcuser_102
 */

#ifndef MEMORY_DRIVER_EEPROM_M24C16_EEPROM_M24_H_
#define MEMORY_DRIVER_EEPROM_M24C16_EEPROM_M24_H_

#include <stdint.h>

#define M24C16_FAIL 1
#define M24C16_SCS 0

#define M24C16_TOTAL_SIZE 2048
#define M24C16_EEPROM_I2C_ADDRESS 0xA0  /**< EEPROM I2C address */
#define M24C16_EEPROM_PAGE_SIZE   16    /**< EEPROM page size in bytes */
#define M24C16_WRITE_ENABLE       0     /**< Write enable state */
#define M24C16_WRITE_DISABLE      1     /**< Write disable state */

/**
 * @brief Function pointer for the read API.
 */
typedef uint8_t (*m24c16_read_fptr_t)(void* intf_hdl, uint16_t slv_addr, uint8_t *data, uint16_t len);

/**
 * @brief Function pointer for the write API.
 */
typedef uint8_t (*m24c16_write_fptr_t)(void* intf_hdl, uint16_t slv_addr, uint8_t *data, uint16_t len);

/**
 * @brief Function pointer for the write control pin API.
 */
typedef uint8_t (*m24c16_wc_pin_ctrl_fptr_t)(uint8_t state);

/**
 * @brief Struct representing the M24C16 EEPROM handler.
 */
typedef struct {
    void* intf_hdl; /**< Interface handle */
    m24c16_read_fptr_t read; /**< Read function pointer */
    m24c16_write_fptr_t write; /**< Write function pointer */
} m24c16_t;

/**
 * @brief Write a buffer of data to the EEPROM.
 *
 * @param m24c16_hdl The EEPROM handler
 * @param address The EEPROM starting address to write to
 * @param data Pointer to the buffer of data to write
 * @param length Number of bytes to write
 * @return uint8_t Status of the write operation (true if successful, false otherwise)
 */
uint8_t m24c16_eeprom_write_buffer(m24c16_t* m24c16_hdl, uint16_t address, uint8_t *data, uint16_t length);

/**
 * @brief Read a buffer of data from the EEPROM.
 *
 * @param m24c16_hdl The EEPROM handler
 * @param address The EEPROM starting address to read from
 * @param data Pointer to the buffer to store read data
 * @param length Number of bytes to read
 * @return uint8_t Status of the read operation (true if successful, false otherwise)
 */
uint8_t m24c16_eeprom_read_buffer(m24c16_t* m24c16_hdl, uint16_t address, uint8_t *data, uint16_t length);

uint8_t m24c16_eeprom_erase_full_chip(m24c16_t* m24c16_hdl);

void m24c16_delay_ms(uint16_t ms);

/*!
 *  @brief this function init eeprom
 */
void eeprom_m24c16_init(uint8_t ahw_id);

/*!
 *  @brief this function used to i2c read in m24c16
 */
uint8_t m24c16_i2c_read(void* intf_hdl, uint16_t slv_addr, uint8_t *data, uint16_t len);

/*!
 *  @brief this function used to i2c write in m24c16
 */
uint8_t m24c16_i2c_write(void* intf_hdl, uint16_t slv_addr, uint8_t *data, uint16_t len);

#endif /* MEMORY_DRIVER_EEPROM_M24C16_EEPROM_M24_H_ */
