/**
 * @file he24c64soh.h
 * @brief HE24C64SOH EEPROM驱动库头文件（简化版）
 * @description 只包含读写字节功能，不包含初始化
 * @note 假设I2C和WP已在board.c中初始化
 */

#ifndef HE24C64SOH_H
#define HE24C64SOH_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>
#include <stdbool.h>
#include "driverlib.h"

// ============================================================================
// 宏定义
// ============================================================================

// //*****************************************************************************
// //
// // Macro to call SysCtl_delay() to achieve a delay in microseconds. The macro
// // will convert the desired delay in microseconds to the count value expected
// // by the function. \b x is the number of microseconds to delay.
// //
// //*****************************************************************************
// #define DEVICE_DELAY_US(x) SysCtl_delay(((((long double)(x)) / (1000000.0L /  \
//                                 (long double)DEVICE_SYSCLK_FREQ)) - 9.0L) / 5.0L)




// 设备地址定义
#define HE24C64SOH_WRITE_ADDR    0xA0    // 写地址
#define HE24C64SOH_READ_ADDR     0xA1    // 读地址

// 容量定义
#define HE24C64SOH_TOTAL_SIZE    8192    // 8KB (8192字节)
#define HE24C64SOH_PAGE_SIZE 32         // 一页32字节

// 状态码定义
#define HE24C64SOH_SUCCESS       0x00    // 操作成功
#define HE24C64SOH_ERROR         0x01    // 操作失败
#define HE24C64SOH_TIMEOUT       0x02    // 操作超时
#define HE24C64SOH_BUS_BUSY      0x03    // I2C总线忙
#define HE24C64SOH_WRITE_FAIL    0x04    // 写入失败
#define HE24C64SOH_READ_FAIL     0x05    // 读取失败
#define HE24C64SOH_ADDR_ERROR    0x06    // 地址错误
#define HE24C64SOH_NACK_ERROR    0x07    // NACK错误
#define HE24C64SOH_PARAM_ERROR   0x08    // 数据字节数错误
#define HE24C64SOH_PAGE_ERROR    0x09    // 地址跨页错误

// 时序参数
#define HE24C64SOH_WRITE_CYCLE_MS    5   // 最大写周期时间5ms

// I2C模块定义
#define HE24C64SOH_I2C_BASE      I2CA_BASE  // 使用I2CA模块

// ============================================================================
// 函数声明
// ============================================================================

/**
 * @brief 任意字节写入函数（支持跨页）
 * @param address: 要写入的起始地址（16位）
 * @param data: 要写入的数据指针
 * @param data_len: 要写入的数据字节数
 * @return 写入状态
 */
uint8_t HE24C64SOH_WriteData(uint16_t address, const uint8_t *data, uint16_t data_len);

/**
 * @brief 页写入函数
 * @param address: 要写入的起始地址（16位）
 * @param data: 要写入的数据指针
 * @param data_len: 要写入的数据字节数（最大32字节）
 * @return 写入状态
 */
uint8_t HE24C64SOH_PageWriteBytes(uint16_t address, const uint8_t *data, uint8_t data_len);

/**
 * @brief 多字节读取函数
 * @param address: 要读取的起始地址（16位）
 * @param data: 存储读取数据的指针
 * @param data_len: 要读取的数据字节数
 * @return 读取状态
 */
uint8_t HE24C64SOH_ReadBytes(uint16_t address, uint8_t *data, uint8_t data_len);

/**
 * @brief I2C总线诊断函数 - 用于定位SDA/SCL被拉低的原因
 * 
 * 使用方法：在Board_init()前后调用此函数
 */
void I2C_BusDiagnosis(void);

#ifdef __cplusplus
}
#endif

#endif /* HE24C64SOH_H */


