/*
 * @Author: mypx
 * @Date: 2025-06-19 15:36:18
 * @LastEditTime: 2025-09-10 09:33:01
 * @LastEditors: mypx mypx_coder@163.com
 * @Description: 
 */
#ifndef __AT24CX_H__
#define __AT24CX_H__
#include <stdint.h>

// Default I2C address
#define AT24_DEFAULT_ADDR 0x50 // 0xA0 shifted to 7-bit address

// I2C address range macro definitions for different models of AT24C series devices
#define AT24C01_ADDR  0x50
#define AT24C02_ADDR  0x50
#define AT24C04_ADDR  0x50
#define AT24C08_ADDR  0x50
#define AT24C128_ADDR 0xA0 // AT24C128 uses a 16-bit address

// Maximum page size of the device (AT24C128 page size is 64 bytes, other models may be smaller)
// This page size can be modified according to the EEPROM model (unit: bytes)
#ifndef AT24_MAX_PAGE_SIZE
#define AT24_MAX_PAGE_SIZE 64 // Default support for AT24C64 / 128 / 256
#endif

// Define chip models
typedef enum
{
    AT24C01,
    AT24C02,
    AT24C04,
    AT24C08,
    AT24C128
} at24cx_chip_model_t;

// Define chip information structure
typedef struct
{
    uint16_t min_addr;
    uint16_t max_addr;
    uint8_t  addr_size;
} at24cx_chip_info_t;

// AT24 driver structure
typedef struct
{
    // Function pointer to write bytes to the device
    int (*write_bytes)(uint16_t dev_addr, uint16_t reg, uint8_t *data, uint16_t size);
    // Function pointer to read bytes from the device
    int (*read_bytes)(uint16_t dev_addr, uint16_t reg, uint8_t *data, uint16_t size);
    // Function pointer to log messages
    void (*log)(const char *tag, const char *fmt, ...);
    // Device I2C address
    uint8_t dev_addr;
    // Chip model
    at24cx_chip_model_t chip_model;
} at24cx_dev_t;

#ifdef __cplusplus
extern "C"
{
#endif

// Write a single byte to the specified address
int at24cx_write_byte(at24cx_dev_t *device, uint16_t addr, uint8_t data);
// Write multiple bytes to the device
int at24cx_write(at24cx_dev_t *device, uint16_t start_addr, uint8_t *data_buf, uint16_t write_num);
// Read a single byte from the specified register
int at24cx_read_byte(at24cx_dev_t *device, uint16_t reg, uint8_t *data);
// Read multiple bytes from the device
int at24cx_read(at24cx_dev_t *device, uint16_t start_addr, uint8_t *data_buf, uint16_t read_num);

#ifdef __cplusplus
}
#endif
#endif // __AT24CX_H__
