Files
ldc1612_cmake_vscode/Inc/i2c.h
T
hulk c7433a8df7 release(golden): 托底线整体替换为生产实装版原样复制(v26.1.0.3)并重编 B 槽固件
- 生产仓库(GitLab ceramixnano_eddy_cmake_vscode dev@151828f,已在机器上实装)原样复制 8 文件:Src/command.c、i2c.c、ldc1612.c、tmp112.c;Inc/command.h、i2c.h、ldc1612.h;CommunicationProtocol.md(copy 后 fc 逐字节核验一致)
- 保留 B 槽专属构建(LD 0x0800B000/0x4BF8、VTOR 0xB000、gap-fill 0xFF、BUILD_VARIANT=golden),仅 VERSION_PATCH 2→3 对齐生产
- 与 dev 线从此的功能差异属预期(勿"修复"):LDC1612 驱动电流 GEN(8)(生产 2026-03-23 调参,dev 线仍 GEN(18))、命令集 M1~M4+M999/M9999(无 M5/M6)、M4 无符号温度算法、旧 8 位寄存器地址 i2c 驱动(配套 tmp112/ldc1612 整组生效);IIC 新驱动留待后续 A 线版本
- 保底判定值 v26.1.0.2 → v26.1.0.3;两个版本号均永不再用于 OTA 升级包;dev 线后续 A 版本必须 > V1.0.3(当前 dev 同为 V1.0.3,正式发布前须升版)
- 重编译:text+data = 12,036B;向量表校验通过(复位向量 0x0800CC2C 落 B 槽)
- FACTORY 工厂镜像(BL EDDY V0.1.0 + B 满槽 + flag,A 擦除态)已打包:Build\golden\CeramixNano-EDDY_FACTORY_B-V1.0.3_2026-10-25.bin,12 项字节校验全过
2026-10-25 13:13:41 +08:00

190 lines
6.2 KiB
C

//
// Created by dell on 24-12-20.
//
#ifndef I2C_H
#define I2C_H
#include "gd32e23x_it.h"
#include "gd32e23x.h"
#include "systick.h"
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "board_config.h"
/******************************************************************************/
#define I2C_SPEED 100000U /* 100kHz */
#define I2C_TIME_OUT 5000U /* 5000 loops timeout */
#define I2C_MAX_RETRY 3U /* Maximum retry attempts */
#define I2C_DELAY_10US 10U /* Delay in microseconds for bus reset */
#define I2C_RECOVERY_CLOCKS 9U /* Clock pulses for bus recovery */
#define I2C_MASTER_ADDRESS 0x00U /* Master address (not used) */
/* Legacy compatibility */
#define I2C_OK 1
#define I2C_FAIL 0
#define I2C_END 1
/******************************************************************************/
/* I2C result enumeration */
typedef enum {
I2C_RESULT_SUCCESS = 0, /* Operation successful */
I2C_RESULT_TIMEOUT, /* Timeout occurred */
I2C_RESULT_NACK, /* No acknowledge received */
I2C_RESULT_BUS_BUSY, /* Bus is busy */
I2C_RESULT_ERROR, /* General error */
I2C_RESULT_INVALID_PARAM, /* Invalid parameter */
I2C_RECOVERY_OK,
I2C_RECOVERY_SDA_STUCK_LOW,
I2C_RECOVERY_SCL_STUCK_LOW
} i2c_result_t;
/* I2C state machine enumeration */
typedef enum {
I2C_STATE_IDLE = 0, /* Idle state */
I2C_STATE_START, /* Generate start condition */
I2C_STATE_SEND_ADDRESS, /* Send slave address */
I2C_STATE_CLEAR_ADDRESS, /* Clear address flag */
I2C_STATE_TRANSMIT_REG, /* Transmit register address */
I2C_STATE_TRANSMIT_DATA, /* Transmit data */
I2C_STATE_RESTART, /* Generate restart condition */
I2C_STATE_RECEIVE_DATA, /* Receive data */
I2C_STATE_STOP, /* Generate stop condition */
I2C_STATE_ERROR, /* Error state */
I2C_STATE_END
} i2c_state_t;
/******************************************************************************/
/* Function declarations */
/*!
\brief configure the I2C interface
\param[in] none
\param[out] none
\retval i2c_result_t
*/
i2c_result_t i2c_config(void);
/*!
\brief reset I2C bus with proper recovery
\param[in] none
\param[out] none
\retval i2c_result_t
*/
i2c_result_t i2c_bus_reset(void);
/*!
\brief scan I2C bus for devices
\param[in] none
\param[out] none
\retval none
*/
void i2c_scan(void);
/*!
\brief write 16-bit data to I2C device
\param[in] slave_addr: 7-bit slave address
\param[in] reg_addr: register address
\param[in] data: pointer to 2-byte data array
\param[out] none
\retval i2c_result_t
*/
i2c_result_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[2]);
/*!
\brief read 16-bit data from I2C device
\param[in] slave_addr: 7-bit slave address
\param[in] reg_addr: register address
\param[out] data: pointer to 2-byte data buffer
\retval i2c_result_t
*/
i2c_result_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data);
/* Generic read/write functions with configurable length */
/*!
\brief write data to I2C device with configurable length
\param[in] slave_addr: slave device address (7-bit)
\param[in] reg_addr: register address
\param[in] data: pointer to data buffer
\param[in] length: number of bytes to write (1-255)
\param[out] none
\retval i2c_result_t: operation result
*/
i2c_result_t i2c_write(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8_t length);
/*!
\brief read data from I2C device with configurable length
\param[in] slave_addr: slave device address (7-bit)
\param[in] reg_addr: register address
\param[out] data: pointer to data buffer
\param[in] length: number of bytes to read (1-255)
\retval i2c_result_t: operation result
*/
i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8_t length);
/* Convenience functions for common operations */
/*!
\brief write single byte to I2C device
\param[in] slave_addr: slave device address (7-bit)
\param[in] reg_addr: register address
\param[in] data: data byte to write
\retval i2c_result_t: operation result
*/
i2c_result_t i2c_write_8bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data);
/*!
\brief read single byte from I2C device
\param[in] slave_addr: slave device address (7-bit)
\param[in] reg_addr: register address
\param[out] data: pointer to data byte
\retval i2c_result_t: operation result
*/
i2c_result_t i2c_read_8bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data);
/*!
\brief write 32-bit data to I2C device
\param[in] slave_addr: slave device address (7-bit)
\param[in] reg_addr: register address
\param[in] data: pointer to 4-byte data array
\retval i2c_result_t: operation result
*/
i2c_result_t i2c_write_32bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[4]);
/*!
\brief read 32-bit data from I2C device
\param[in] slave_addr: slave device address (7-bit)
\param[in] reg_addr: register address
\param[out] data: pointer to 4-byte data buffer
\retval i2c_result_t: operation result
*/
i2c_result_t i2c_read_32bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data);
/*!
\brief read display panel parameters (multi-byte)
\param[in] slave_addr: slave device address (7-bit)
\param[in] reg_addr: register address
\param[out] data: pointer to data buffer
\param[in] length: number of bytes to read (1-13)
\retval i2c_result_t: operation result
*/
i2c_result_t i2c_read_display_params(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8_t length);
#ifdef DEBUG_VERBOSE
/*!
\brief get status string for debugging
\param[in] status: i2c_result_t value
\param[out] none
\retval const char* status string
*/
const char* i2c_get_status_string(i2c_result_t status);
#endif
#endif //I2C_H