generated from hulk/gd32e23x_template_cmake_vscode
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 项字节校验全过
This commit is contained in:
+10
-92
@@ -74,15 +74,6 @@
|
||||
#define RESP_TYPE_LEN_ERR 0xF4 /**< 长度错误 */
|
||||
/** @} */
|
||||
|
||||
/* 温度补偿参数定义
|
||||
* TEMP_COMP_REF_TEMP_X10000: 参考温度,单位 0.0001°C
|
||||
* TEMP_COMP_COEFF_NUMERATOR / TEMP_COMP_COEFF_DENOMINATOR: 温漂系数,默认表示 counts/°C
|
||||
* 补偿公式: compensated = raw - (temp - ref) * coeff
|
||||
*/
|
||||
#define TEMP_COMP_REF_TEMP_X10000 (250000L)
|
||||
#define TEMP_COMP_COEFF_NUMERATOR (0L)
|
||||
#define TEMP_COMP_COEFF_DENOMINATOR (10000L)
|
||||
|
||||
/* ============================================================================
|
||||
* 模块内部变量
|
||||
* ============================================================================ */
|
||||
@@ -104,9 +95,6 @@ static const uint8_t s_report_status_ok[] = { 'o', 'k' }; /**< 成功响应
|
||||
static const uint8_t s_report_status_err[] = { 'e','r','r' }; /**< 错误响应数据 */
|
||||
/** @} */
|
||||
|
||||
static int32_t tmp112_get_temperature_x10000(void);
|
||||
static int64_t calculate_temperature_compensation_offset(int32_t temperature_x10000);
|
||||
|
||||
/* ============================================================================
|
||||
* 公共接口函数
|
||||
* ============================================================================ */
|
||||
@@ -344,14 +332,6 @@ void handle_command(const uint8_t *frame, uint8_t len) {
|
||||
temperature_raw_value_report();
|
||||
return;
|
||||
|
||||
case 5u:
|
||||
eddy_current_compensated_report();
|
||||
return;
|
||||
|
||||
case 6u:
|
||||
calibration_data_report();
|
||||
return;
|
||||
|
||||
// case 201u: // M201命令
|
||||
// send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok));
|
||||
// return;
|
||||
@@ -533,82 +513,20 @@ void eddy_current_report(void) {
|
||||
send_response(RESP_TYPE_OK, sensor_data, sizeof(sensor_data));
|
||||
}
|
||||
|
||||
static int32_t tmp112_get_temperature_x10000(void)
|
||||
{
|
||||
uint8_t value[2] = {0};
|
||||
int16_t temp_raw;
|
||||
|
||||
tmp112a_get_raw_temperature_value(value);
|
||||
|
||||
temp_raw = (int16_t)(((uint16_t)value[0] << 8) | value[1]);
|
||||
temp_raw >>= 4;
|
||||
|
||||
return (int32_t)temp_raw * 625;
|
||||
}
|
||||
|
||||
static int64_t calculate_temperature_compensation_offset(int32_t temperature_x10000)
|
||||
{
|
||||
int32_t delta_temp_x10000 = temperature_x10000 - TEMP_COMP_REF_TEMP_X10000;
|
||||
|
||||
return ((int64_t)delta_temp_x10000 * TEMP_COMP_COEFF_NUMERATOR) /
|
||||
TEMP_COMP_COEFF_DENOMINATOR;
|
||||
}
|
||||
|
||||
void temperature_raw_value_report(void) {
|
||||
// if (!g_temperature_sensor_report_enabled) return;
|
||||
uint8_t raw_result[4];
|
||||
int32_t raw_value = tmp112_get_temperature_x10000();
|
||||
uint8_t value[2] = {0};
|
||||
uint32_t raw_value = 0;
|
||||
|
||||
raw_result[0] = (uint8_t)((uint32_t)raw_value >> 24);
|
||||
raw_result[1] = (uint8_t)((uint32_t)raw_value >> 16);
|
||||
raw_result[2] = (uint8_t)((uint32_t)raw_value >> 8);
|
||||
raw_result[3] = (uint8_t)((uint32_t)raw_value & 0xFF);
|
||||
// i2c_read_16bits(TMP112A_ADDR, TMP112A_TEMP_REG, value);
|
||||
tmp112a_get_raw_temperature_value(value);
|
||||
raw_value = (uint32_t)((uint16_t) (value[0] << 4) | (value[1]>>4)) * 625;
|
||||
raw_result[0] = (uint8_t)(raw_value >> 24);
|
||||
raw_result[1] = (uint8_t)(raw_value >> 16);
|
||||
raw_result[2] = (uint8_t)(raw_value >> 8);
|
||||
raw_result[3] = (uint8_t)(raw_value & 0xFF);
|
||||
|
||||
send_response(RESP_TYPE_OK, raw_result, sizeof(raw_result));
|
||||
}
|
||||
|
||||
void eddy_current_compensated_report(void)
|
||||
{
|
||||
uint8_t sensor_data[4];
|
||||
uint32_t raw_result = ldc1612_get_raw_channel_result(CHANNEL_0);
|
||||
|
||||
if ((raw_result & 0xF0000000u) == 0u) {
|
||||
int32_t temperature_x10000 = tmp112_get_temperature_x10000();
|
||||
int64_t compensated_value = (int64_t)raw_result -
|
||||
calculate_temperature_compensation_offset(temperature_x10000);
|
||||
|
||||
if (compensated_value < 0) {
|
||||
raw_result = 0;
|
||||
} else if (compensated_value > 0x0FFFFFFFLL) {
|
||||
raw_result = 0x0FFFFFFFu;
|
||||
} else {
|
||||
raw_result = (uint32_t)compensated_value;
|
||||
}
|
||||
}
|
||||
|
||||
sensor_data[0] = (uint8_t)(raw_result >> 24);
|
||||
sensor_data[1] = (uint8_t)(raw_result >> 16);
|
||||
sensor_data[2] = (uint8_t)(raw_result >> 8);
|
||||
sensor_data[3] = (uint8_t)(raw_result & 0xFF);
|
||||
|
||||
send_response(RESP_TYPE_OK, sensor_data, sizeof(sensor_data));
|
||||
}
|
||||
|
||||
void calibration_data_report(void)
|
||||
{
|
||||
uint8_t payload[9];
|
||||
uint32_t eddy_raw_value = ldc1612_get_raw_channel_result(CHANNEL_0);
|
||||
int32_t temperature_raw_value = tmp112_get_temperature_x10000();
|
||||
|
||||
payload[0] = (uint8_t)(eddy_raw_value >> 24);
|
||||
payload[1] = (uint8_t)(eddy_raw_value >> 16);
|
||||
payload[2] = (uint8_t)(eddy_raw_value >> 8);
|
||||
payload[3] = (uint8_t)(eddy_raw_value & 0xFF);
|
||||
payload[4] = 0xFF;
|
||||
payload[5] = (uint8_t)((uint32_t)temperature_raw_value >> 24);
|
||||
payload[6] = (uint8_t)((uint32_t)temperature_raw_value >> 16);
|
||||
payload[7] = (uint8_t)((uint32_t)temperature_raw_value >> 8);
|
||||
payload[8] = (uint8_t)((uint32_t)temperature_raw_value & 0xFF);
|
||||
|
||||
send_response(RESP_TYPE_OK, payload, sizeof(payload));
|
||||
}
|
||||
|
||||
@@ -34,7 +34,6 @@ void i2c_gpio_config(void) {
|
||||
i2c_result_t i2c_config(void) {
|
||||
/* configure I2C GPIO */
|
||||
i2c_gpio_config();
|
||||
|
||||
/* enable I2C clock */
|
||||
rcu_periph_clock_enable(RCU_I2C);
|
||||
/* configure I2C clock */
|
||||
@@ -150,7 +149,7 @@ i2c_result_t i2c_bus_reset(void) {
|
||||
}
|
||||
|
||||
#ifdef DEBUG_VERBOSE
|
||||
printf("I2C bus reset: bus recovered\r\n");
|
||||
printf("I2C bus reset: bus recovered\r\n");
|
||||
#endif
|
||||
/* 8. Reconfigure & enable peripheral */
|
||||
i2c_config();
|
||||
@@ -198,12 +197,10 @@ void i2c_scan(void) {
|
||||
i2c_master_addressing(I2C0, (address << 1), I2C_TRANSMITTER);
|
||||
timeout = 0;
|
||||
|
||||
// 等待地址发送完成或错误
|
||||
while (!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND) && !i2c_flag_get(I2C0, I2C_FLAG_AERR) && (timeout < I2C_TIME_OUT))
|
||||
// 等待地址发送完成
|
||||
while (!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND) && (timeout < I2C_TIME_OUT))
|
||||
timeout++;
|
||||
|
||||
if (timeout < I2C_TIME_OUT && i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) {
|
||||
// 设备响应 - 清除地址标志
|
||||
if (timeout < I2C_TIME_OUT) {
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||
// printf("Found device at 0x%02X\r\n", address);
|
||||
const char* msg2_prefix = "Found device at 0x";
|
||||
@@ -224,9 +221,6 @@ void i2c_scan(void) {
|
||||
}
|
||||
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TC) == RESET) {}
|
||||
found_devices++;
|
||||
} else if (i2c_flag_get(I2C0, I2C_FLAG_AERR)) {
|
||||
// 设备无响应 - 清除错误标志
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
||||
}
|
||||
|
||||
// 生成停止条件
|
||||
@@ -269,53 +263,6 @@ void i2c_scan(void) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 内部辅助函数:等待指定的I2C标志位被设置,带有超时。
|
||||
* @param[in] flag: 要等待的I2C事件标志。
|
||||
* @retval i2c_result_t: I2C_RESULT_SUCCESS 或 I2C_RESULT_TIMEOUT。
|
||||
*/
|
||||
static i2c_result_t _i2c_wait_flag_timeout(uint32_t flag)
|
||||
{
|
||||
uint16_t timeout = 0;
|
||||
while(!i2c_flag_get(I2C0, flag)){
|
||||
if(timeout++ > I2C_TIME_OUT){
|
||||
#ifdef DEBUG_VERBOSE
|
||||
const char* fname = "UNKNOWN";
|
||||
switch (flag) {
|
||||
case I2C_FLAG_SBSEND: fname = "SBSEND"; break;
|
||||
case I2C_FLAG_ADDSEND: fname = "ADDSEND"; break;
|
||||
case I2C_FLAG_TBE: fname = "TBE"; break;
|
||||
case I2C_FLAG_RBNE: fname = "RBNE"; break;
|
||||
case I2C_FLAG_BTC: fname = "BTC"; break;
|
||||
case I2C_FLAG_AERR: fname = "AERR"; break;
|
||||
case I2C_FLAG_BERR: fname = "BERR"; break;
|
||||
case I2C_FLAG_LOSTARB: fname = "LOSTARB"; break;
|
||||
case I2C_FLAG_I2CBSY: fname = "I2CBSY"; break;
|
||||
}
|
||||
printf("I2C wait flag timeout: flag=0x%08X (%s)\r\n", (unsigned int)flag, fname);
|
||||
#endif
|
||||
return I2C_RESULT_TIMEOUT;
|
||||
}
|
||||
}
|
||||
return I2C_RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 内部辅助函数:等待指定的I2C标志位被清除,带有超时。
|
||||
* @param[in] flag: 要等待的I2C事件标志。
|
||||
* @retval i2c_result_t: I2C_RESULT_SUCCESS 或 I2C_RESULT_TIMEOUT。
|
||||
*/
|
||||
static i2c_result_t _i2c_wait_flag_clear_timeout(uint32_t flag)
|
||||
{
|
||||
uint16_t timeout = 0;
|
||||
while(i2c_flag_get(I2C0, flag)){
|
||||
if(timeout++ > I2C_TIME_OUT){
|
||||
return I2C_RESULT_TIMEOUT;
|
||||
}
|
||||
}
|
||||
return I2C_RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
i2c_result_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[2]) {
|
||||
i2c_state_t state = I2C_STATE_START;
|
||||
uint16_t timeout = 0;
|
||||
@@ -378,7 +325,7 @@ i2c_result_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data
|
||||
} else {
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
||||
timeout =0;
|
||||
#ifdef DEBUG_VERBOSE
|
||||
#ifdef DEBUG_VERBOES
|
||||
printf("IIC write failed for Error Slave Address. \n");
|
||||
#endif
|
||||
return I2C_RESULT_NACK;
|
||||
@@ -386,28 +333,39 @@ i2c_result_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data
|
||||
|
||||
case I2C_STATE_TRANSMIT_REG:
|
||||
/* wait until the transmit data buffer is empty */
|
||||
if(_i2c_wait_flag_timeout(I2C_FLAG_TBE) != I2C_RESULT_SUCCESS) {
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
/* send register address */
|
||||
i2c_data_transmit(I2C0, reg_addr);
|
||||
timeout = 0;
|
||||
state = I2C_STATE_TRANSMIT_DATA;
|
||||
break;
|
||||
|
||||
case I2C_STATE_TRANSMIT_DATA:
|
||||
/* wait until the transmit data buffer is empty */
|
||||
if(_i2c_wait_flag_timeout(I2C_FLAG_TBE) != I2C_RESULT_SUCCESS) {
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
/* send register MSB value */
|
||||
i2c_data_transmit(I2C0, data[0]);
|
||||
|
||||
timeout = 0;
|
||||
|
||||
/* wait until the transmit data buffer is empty */
|
||||
if(_i2c_wait_flag_timeout(I2C_FLAG_TBE) != I2C_RESULT_SUCCESS) {
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
@@ -423,9 +381,13 @@ i2c_result_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data
|
||||
|
||||
/* send register LSB value */
|
||||
i2c_data_transmit(I2C0, data[1]);
|
||||
|
||||
timeout = 0;
|
||||
|
||||
/* wait until BTC bit is set */
|
||||
if(_i2c_wait_flag_timeout(I2C_FLAG_BTC) != I2C_RESULT_SUCCESS) {
|
||||
while (!i2c_flag_get(I2C0, I2C_FLAG_BTC) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
@@ -451,13 +413,25 @@ i2c_result_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data
|
||||
return I2C_RESULT_SUCCESS;
|
||||
|
||||
case I2C_STATE_ERROR:
|
||||
/* I2C bus error, try to reset the bus and retry */
|
||||
i2c_bus_reset();
|
||||
/* send a stop condition to I2C bus */
|
||||
i2c_stop_on_bus(I2C0);
|
||||
|
||||
timeout = 0;
|
||||
while ((I2C_CTL0(I2C0) & I2C_CTL0_STOP) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
return I2C_RESULT_ERROR;
|
||||
}
|
||||
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_BERR);
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_LOSTARB);
|
||||
|
||||
retry_count ++;
|
||||
if (retry_count >= I2C_MAX_RETRY)
|
||||
{
|
||||
#ifdef DEBUG_VERBOSE
|
||||
#ifdef DEBUG_VERBOES
|
||||
printf("IIC write failed after %d retries\n", I2C_MAX_RETRY);
|
||||
#endif
|
||||
return I2C_RESULT_ERROR;
|
||||
@@ -470,7 +444,7 @@ i2c_result_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data
|
||||
/* small delay before retry */
|
||||
delay_10us(10);
|
||||
break;
|
||||
|
||||
|
||||
default:
|
||||
state = I2C_STATE_START;
|
||||
break;
|
||||
@@ -498,8 +472,11 @@ i2c_result_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data
|
||||
case I2C_STATE_START:
|
||||
timeout = 0;
|
||||
|
||||
/* wait for bus to be idle */
|
||||
if(_i2c_wait_flag_clear_timeout(I2C_FLAG_I2CBSY) != I2C_RESULT_SUCCESS) {
|
||||
// wait for bus to be idle
|
||||
while (i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
@@ -507,11 +484,15 @@ i2c_result_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data
|
||||
// send start condition
|
||||
i2c_start_on_bus(I2C0);
|
||||
state = I2C_STATE_SEND_ADDRESS;
|
||||
timeout = 0;
|
||||
break;
|
||||
|
||||
case I2C_STATE_SEND_ADDRESS:
|
||||
/* wait for start condition to be sent */
|
||||
if(_i2c_wait_flag_timeout(I2C_FLAG_SBSEND) != I2C_RESULT_SUCCESS) {
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
@@ -559,7 +540,10 @@ i2c_result_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data
|
||||
|
||||
case I2C_STATE_TRANSMIT_DATA:
|
||||
/* wait for transmit buffer to be empty */
|
||||
if(_i2c_wait_flag_timeout(I2C_FLAG_TBE) != I2C_RESULT_SUCCESS) {
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
@@ -567,20 +551,28 @@ i2c_result_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data
|
||||
/* send register address */
|
||||
i2c_data_transmit(I2C0, reg_addr);
|
||||
state = I2C_STATE_RESTART;
|
||||
timeout = 0;
|
||||
break;
|
||||
|
||||
case I2C_STATE_RESTART:
|
||||
/* wait for byte transfer complete BTC: Bit Transfer Complete */
|
||||
if(_i2c_wait_flag_timeout(I2C_FLAG_BTC) != I2C_RESULT_SUCCESS) {
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
/* generate repeated start condition */
|
||||
i2c_start_on_bus(I2C0);
|
||||
|
||||
|
||||
/* wait for repeated start condition to be sent */
|
||||
if(_i2c_wait_flag_timeout(I2C_FLAG_SBSEND) != I2C_RESULT_SUCCESS) {
|
||||
timeout = 0;
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
@@ -596,7 +588,10 @@ i2c_result_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data
|
||||
|
||||
case I2C_STATE_RECEIVE_DATA:
|
||||
/* Wait for BTC (both bytes received) */
|
||||
if(_i2c_wait_flag_timeout(I2C_FLAG_BTC) != I2C_RESULT_SUCCESS) {
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
@@ -626,13 +621,13 @@ i2c_result_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data
|
||||
return I2C_RESULT_SUCCESS;
|
||||
|
||||
case I2C_STATE_ERROR:
|
||||
/* I2C bus error, try to reset the bus and retry */
|
||||
i2c_bus_reset();
|
||||
/* send stop condition to release bus */
|
||||
i2c_stop_on_bus(I2C0);
|
||||
|
||||
retry_count++;
|
||||
if (retry_count >= I2C_MAX_RETRY) {
|
||||
#ifdef DEBUG_VERBOSE
|
||||
printf("IIC read failed after %d retries\n", I2C_MAX_RETRY);
|
||||
#ifdef DEBUG_VERBOES
|
||||
printf("IIC read failed after %d retries\n", I2C_RETRY_MAX);
|
||||
#endif
|
||||
return I2C_RESULT_ERROR;
|
||||
}
|
||||
@@ -657,13 +652,13 @@ i2c_result_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data
|
||||
/*!
|
||||
\brief write data to I2C device with configurable length
|
||||
\param[in] slave_addr: slave device address (7-bit)
|
||||
\param[in] reg_addr: register address (16-bit, high byte first)
|
||||
\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, uint16_t reg_addr, uint8_t *data, uint8_t length) {
|
||||
i2c_result_t i2c_write(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8_t length) {
|
||||
i2c_state_t state = I2C_STATE_START;
|
||||
uint16_t timeout = 0;
|
||||
uint8_t retry_count = 0;
|
||||
@@ -671,9 +666,6 @@ i2c_result_t i2c_write(uint8_t slave_addr, uint16_t reg_addr, uint8_t *data, uin
|
||||
|
||||
/* parameter validation */
|
||||
if (data == NULL || slave_addr > 0x7F || length == 0) {
|
||||
#ifdef DEBUG_VERBOSE
|
||||
printf("I2C write invalid param: slave=0x%02X, len=%u, data=%p\r\n", slave_addr, length, data);
|
||||
#endif
|
||||
return I2C_RESULT_INVALID_PARAM;
|
||||
}
|
||||
|
||||
@@ -727,71 +719,67 @@ i2c_result_t i2c_write(uint8_t slave_addr, uint16_t reg_addr, uint8_t *data, uin
|
||||
state = I2C_STATE_TRANSMIT_REG;
|
||||
break;
|
||||
} else {
|
||||
/* NACK received - address not acknowledged */
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
||||
i2c_stop_on_bus(I2C0);
|
||||
timeout = 0;
|
||||
#ifdef DEBUG_VERBOSE
|
||||
printf("I2C write failed for Error Slave Address. \n");
|
||||
printf("IIC write failed for Error Slave Address. \n");
|
||||
#endif
|
||||
return I2C_RESULT_NACK;
|
||||
}
|
||||
|
||||
case I2C_STATE_TRANSMIT_REG:
|
||||
/* wait until the transmit data buffer is empty */
|
||||
if(_i2c_wait_flag_timeout(I2C_FLAG_TBE) != I2C_RESULT_SUCCESS) {
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
/* send register address, high byte first */
|
||||
if(_i2c_wait_flag_timeout(I2C_FLAG_TBE) != I2C_RESULT_SUCCESS) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
i2c_data_transmit(I2C0, (uint8_t)(reg_addr >> 8));
|
||||
|
||||
if(_i2c_wait_flag_timeout(I2C_FLAG_TBE) != I2C_RESULT_SUCCESS) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
i2c_data_transmit(I2C0, (uint8_t)(reg_addr & 0xFF));
|
||||
/* send register address */
|
||||
i2c_data_transmit(I2C0, reg_addr);
|
||||
timeout = 0;
|
||||
state = I2C_STATE_TRANSMIT_DATA;
|
||||
break;
|
||||
|
||||
case I2C_STATE_TRANSMIT_DATA:
|
||||
/* wait until the transmit data buffer is empty */
|
||||
while (data_index < length) {
|
||||
if(_i2c_wait_flag_timeout(I2C_FLAG_TBE) != I2C_RESULT_SUCCESS) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
/* send data byte */
|
||||
i2c_data_transmit(I2C0, data[data_index]);
|
||||
data_index++;
|
||||
|
||||
/* check for errors */
|
||||
if (i2c_flag_get(I2C0, I2C_FLAG_AERR)) {
|
||||
i2c_stop_on_bus(I2C0);
|
||||
return I2C_RESULT_NACK;
|
||||
} else if (i2c_flag_get(I2C0, I2C_FLAG_BERR) || i2c_flag_get(I2C0, I2C_FLAG_LOSTARB)) {
|
||||
i2c_stop_on_bus(I2C0);
|
||||
return I2C_RESULT_ERROR;
|
||||
}
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
if(state == I2C_STATE_ERROR) break;
|
||||
|
||||
/* send data byte */
|
||||
i2c_data_transmit(I2C0, data[data_index]);
|
||||
data_index++;
|
||||
|
||||
/* check for errors */
|
||||
if (i2c_flag_get(I2C0, I2C_FLAG_AERR)) {
|
||||
i2c_stop_on_bus(I2C0);
|
||||
return I2C_RESULT_NACK;
|
||||
} else if (i2c_flag_get(I2C0, I2C_FLAG_BERR) || i2c_flag_get(I2C0, I2C_FLAG_LOSTARB)) {
|
||||
i2c_stop_on_bus(I2C0);
|
||||
return I2C_RESULT_ERROR;
|
||||
}
|
||||
|
||||
/* check if all data has been sent */
|
||||
if (data_index >= length) {
|
||||
/* wait until BTC bit is set for last byte */
|
||||
if(_i2c_wait_flag_timeout(I2C_FLAG_BTC) != I2C_RESULT_SUCCESS) {
|
||||
timeout = 0;
|
||||
while (!i2c_flag_get(I2C0, I2C_FLAG_BTC) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
state = I2C_STATE_STOP;
|
||||
}
|
||||
timeout = 0;
|
||||
break;
|
||||
|
||||
case I2C_STATE_STOP:
|
||||
@@ -811,8 +799,17 @@ i2c_result_t i2c_write(uint8_t slave_addr, uint16_t reg_addr, uint8_t *data, uin
|
||||
return I2C_RESULT_SUCCESS;
|
||||
|
||||
case I2C_STATE_ERROR:
|
||||
/* I2C bus error, try to reset the bus and retry */
|
||||
i2c_bus_reset();
|
||||
/* send a stop condition to I2C bus */
|
||||
i2c_stop_on_bus(I2C0);
|
||||
|
||||
timeout = 0;
|
||||
while ((I2C_CTL0(I2C0) & I2C_CTL0_STOP) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_BERR);
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_LOSTARB);
|
||||
|
||||
retry_count++;
|
||||
if (retry_count >= I2C_MAX_RETRY) {
|
||||
@@ -829,7 +826,7 @@ i2c_result_t i2c_write(uint8_t slave_addr, uint16_t reg_addr, uint8_t *data, uin
|
||||
/* small delay before retry */
|
||||
delay_10us(10);
|
||||
break;
|
||||
|
||||
|
||||
default:
|
||||
state = I2C_STATE_START;
|
||||
break;
|
||||
@@ -841,12 +838,12 @@ i2c_result_t i2c_write(uint8_t slave_addr, uint16_t reg_addr, uint8_t *data, uin
|
||||
/*!
|
||||
\brief read data from I2C device with configurable length
|
||||
\param[in] slave_addr: slave device address (7-bit)
|
||||
\param[in] reg_addr: register address (16-bit, high byte first)
|
||||
\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, uint16_t reg_addr, uint8_t *data, uint8_t length) {
|
||||
i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8_t length) {
|
||||
i2c_state_t state = I2C_STATE_START;
|
||||
uint16_t timeout = 0;
|
||||
uint8_t retry_count = 0;
|
||||
@@ -861,10 +858,6 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint16_t reg_addr, uint8_t *data, uint
|
||||
/* enable acknowledge */
|
||||
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
||||
|
||||
#ifdef DEBUG_VERBOSE
|
||||
printf("I2C read start: slave=0x%02X reg=0x%02X len=%u\r\n", slave_addr, reg_addr, length);
|
||||
#endif
|
||||
|
||||
while (retry_count < (uint8_t)I2C_MAX_RETRY) {
|
||||
switch (state) {
|
||||
case I2C_STATE_START:
|
||||
@@ -872,7 +865,10 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint16_t reg_addr, uint8_t *data, uint
|
||||
data_index = 0;
|
||||
|
||||
/* wait for bus to be idle */
|
||||
if(_i2c_wait_flag_clear_timeout(I2C_FLAG_I2CBSY) != I2C_RESULT_SUCCESS) {
|
||||
while (i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
@@ -880,11 +876,15 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint16_t reg_addr, uint8_t *data, uint
|
||||
/* send start condition */
|
||||
i2c_start_on_bus(I2C0);
|
||||
state = I2C_STATE_SEND_ADDRESS;
|
||||
timeout = 0;
|
||||
break;
|
||||
|
||||
case I2C_STATE_SEND_ADDRESS:
|
||||
/* wait for start condition to be sent */
|
||||
if(_i2c_wait_flag_timeout(I2C_FLAG_SBSEND) != I2C_RESULT_SUCCESS) {
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
@@ -912,92 +912,67 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint16_t reg_addr, uint8_t *data, uint
|
||||
break;
|
||||
}
|
||||
|
||||
/* debug: show address ack and error flags */
|
||||
#ifdef DEBUG_VERBOSE
|
||||
printf("I2C CLEAR_ADDRESS: write_phase=%d ADDSEND=%d AERR=%d\r\n",
|
||||
(int)write_phase,
|
||||
(int)i2c_flag_get(I2C0, I2C_FLAG_ADDSEND),
|
||||
(int)i2c_flag_get(I2C0, I2C_FLAG_AERR));
|
||||
#endif
|
||||
|
||||
if (write_phase) {
|
||||
/* clear address flag (write phase) */
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||
state = I2C_STATE_TRANSMIT_DATA;
|
||||
} else {
|
||||
/* READ phase: handle ACK/ACKPOS based on requested length
|
||||
- length == 1: disable ACK before clearing ADDR
|
||||
- length == 2: set ACKPOS_NEXT and disable ACK before clearing ADDR
|
||||
- length > 2: keep ACK enabled and clear ADDR; we'll handle N-2/BTF sequence later */
|
||||
/* read phase setup based on length */
|
||||
if (length == 1) {
|
||||
/* single byte read: disable ACK before clearing ADDR */
|
||||
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
||||
#ifdef DEBUG_VERBOSE
|
||||
printf("I2C READ phase: length=1, disabling ACK before clearing ADDSEND\r\n");
|
||||
#endif
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||
/* send STOP immediately after clearing ADDR for single byte */
|
||||
i2c_stop_on_bus(I2C0);
|
||||
} else if (length == 2) {
|
||||
/* two bytes read: set POS=NEXT and disable ACK before clearing ADDR */
|
||||
i2c_ackpos_config(I2C0, I2C_ACKPOS_NEXT);
|
||||
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
||||
#ifdef DEBUG_VERBOSE
|
||||
printf("I2C READ phase: length=2, set ACKPOS_NEXT and disabling ACK before clearing ADDSEND\r\n");
|
||||
#endif
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||
} else {
|
||||
/* length > 2: keep ACK enabled so slave will clock out data */
|
||||
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
||||
#ifdef DEBUG_VERBOSE
|
||||
printf("I2C READ phase: length=%u (>2), keeping ACK enabled\r\n", length);
|
||||
#endif
|
||||
/* multi-byte read: clear ADDR with ACK enabled */
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||
}
|
||||
|
||||
/* now clear address flag to release SCL and enter data phase */
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||
|
||||
state = I2C_STATE_RECEIVE_DATA;
|
||||
}
|
||||
|
||||
timeout = 0;
|
||||
break;
|
||||
|
||||
case I2C_STATE_TRANSMIT_DATA:
|
||||
/* wait for transmit buffer to be empty */
|
||||
if(_i2c_wait_flag_timeout(I2C_FLAG_TBE) != I2C_RESULT_SUCCESS) {
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
/* send register address, high byte first */
|
||||
if(_i2c_wait_flag_timeout(I2C_FLAG_TBE) != I2C_RESULT_SUCCESS) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
i2c_data_transmit(I2C0, (uint8_t)(reg_addr >> 8));
|
||||
|
||||
if(_i2c_wait_flag_timeout(I2C_FLAG_TBE) != I2C_RESULT_SUCCESS) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
i2c_data_transmit(I2C0, (uint8_t)(reg_addr & 0xFF));
|
||||
/* send register address */
|
||||
i2c_data_transmit(I2C0, reg_addr);
|
||||
state = I2C_STATE_RESTART;
|
||||
timeout = 0;
|
||||
break;
|
||||
|
||||
case I2C_STATE_RESTART:
|
||||
/* wait for byte transfer complete */
|
||||
if(_i2c_wait_flag_timeout(I2C_FLAG_BTC) != I2C_RESULT_SUCCESS) {
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
#ifdef DEBUG_VERBOSE
|
||||
printf("I2C RESTART: after BTC, delay 5ms and issuing repeated START\r\n");
|
||||
#endif
|
||||
/* small delay to allow slave device to prepare multi-byte data before repeated start */
|
||||
/* increased to 5ms to improve reliability for DLPC multi-byte reads */
|
||||
delay_ms(5);
|
||||
|
||||
/* generate repeated start condition */
|
||||
i2c_start_on_bus(I2C0);
|
||||
|
||||
|
||||
/* wait for repeated start condition to be sent */
|
||||
if(_i2c_wait_flag_timeout(I2C_FLAG_SBSEND) != I2C_RESULT_SUCCESS) {
|
||||
timeout = 0;
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
@@ -1009,18 +984,15 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint16_t reg_addr, uint8_t *data, uint
|
||||
write_phase = false;
|
||||
state = I2C_STATE_CLEAR_ADDRESS;
|
||||
timeout = 0;
|
||||
#ifdef DEBUG_VERBOSE
|
||||
printf("I2C addressing sent for read (slave=0x%02X)\r\n", slave_addr);
|
||||
#endif
|
||||
break;
|
||||
|
||||
case I2C_STATE_RECEIVE_DATA:
|
||||
#ifdef DEBUG_VERBOSE
|
||||
printf("I2C RECEIVE_DATA: expecting %u bytes\r\n", length);
|
||||
#endif
|
||||
if (length == 1) {
|
||||
/* single byte read */
|
||||
if(_i2c_wait_flag_timeout(I2C_FLAG_RBNE) != I2C_RESULT_SUCCESS) {
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_RBNE)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
@@ -1028,7 +1000,10 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint16_t reg_addr, uint8_t *data, uint
|
||||
state = I2C_STATE_STOP;
|
||||
} else if (length == 2) {
|
||||
/* two bytes read */
|
||||
if(_i2c_wait_flag_timeout(I2C_FLAG_BTC) != I2C_RESULT_SUCCESS) {
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
@@ -1041,39 +1016,40 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint16_t reg_addr, uint8_t *data, uint
|
||||
} else {
|
||||
/* multi-byte read (length > 2) */
|
||||
while (data_index < length) {
|
||||
if (data_index < length - 2) {
|
||||
/* normal bytes: wait for RBNE and read with ACK */
|
||||
if(_i2c_wait_flag_timeout(I2C_FLAG_RBNE) != I2C_RESULT_SUCCESS) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
data[data_index] = i2c_data_receive(I2C0);
|
||||
data_index++;
|
||||
} else if (data_index == length - 2) {
|
||||
/* second to last byte: wait for BTF, then disable ACK and read */
|
||||
if(_i2c_wait_flag_timeout(I2C_FLAG_BTC) != I2C_RESULT_SUCCESS) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
/* disable ACK before reading N-1 byte */
|
||||
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
||||
/* send STOP before reading N-1 byte */
|
||||
i2c_stop_on_bus(I2C0);
|
||||
/* read N-1 byte */
|
||||
data[data_index] = i2c_data_receive(I2C0);
|
||||
data_index++;
|
||||
} else {
|
||||
/* last byte: wait for RBNE and read */
|
||||
if(_i2c_wait_flag_timeout(I2C_FLAG_RBNE) != I2C_RESULT_SUCCESS) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
/* read last byte */
|
||||
data[data_index] = i2c_data_receive(I2C0);
|
||||
data_index++;
|
||||
/* wait for RBNE (receive buffer not empty) */
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_RBNE)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
/* special handling for last 3 bytes */
|
||||
if (data_index == length - 3) {
|
||||
/* wait for BTF (byte transfer finished) before reading N-2 */
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
/* disable ACK for last 2 bytes */
|
||||
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
||||
}
|
||||
|
||||
/* read data byte */
|
||||
data[data_index] = i2c_data_receive(I2C0);
|
||||
data_index++;
|
||||
|
||||
/* send STOP after reading N-1 byte */
|
||||
if (data_index == length - 1) {
|
||||
i2c_stop_on_bus(I2C0);
|
||||
}
|
||||
|
||||
timeout = 0;
|
||||
}
|
||||
if(state == I2C_STATE_ERROR) break;
|
||||
state = I2C_STATE_STOP;
|
||||
}
|
||||
break;
|
||||
@@ -1096,11 +1072,8 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint16_t reg_addr, uint8_t *data, uint
|
||||
return I2C_RESULT_SUCCESS;
|
||||
|
||||
case I2C_STATE_ERROR:
|
||||
/* I2C bus error, try to reset the bus and retry */
|
||||
#ifdef DEBUG_VERBOSE
|
||||
printf("I2C_STATE_ERROR: resetting bus and retrying (retry_count=%u)\r\n", retry_count);
|
||||
#endif
|
||||
i2c_bus_reset();
|
||||
/* send stop condition to release bus */
|
||||
i2c_stop_on_bus(I2C0);
|
||||
|
||||
/* reset ACK configuration */
|
||||
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
||||
@@ -1109,13 +1082,7 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint16_t reg_addr, uint8_t *data, uint
|
||||
retry_count++;
|
||||
if (retry_count >= I2C_MAX_RETRY) {
|
||||
#ifdef DEBUG_VERBOSE
|
||||
/* Print diagnostic flags to help root-cause analysis */
|
||||
printf("I2C read final failure after %d retries, last_state=%d\r\n", I2C_MAX_RETRY, state);
|
||||
printf(" FLAGS: AERR=%d BERR=%d LOSTARB=%d I2CBSY=%d\r\n",
|
||||
(int)i2c_flag_get(I2C0, I2C_FLAG_AERR),
|
||||
(int)i2c_flag_get(I2C0, I2C_FLAG_BERR),
|
||||
(int)i2c_flag_get(I2C0, I2C_FLAG_LOSTARB),
|
||||
(int)i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));
|
||||
printf("I2C read failed after %d retries\n", I2C_MAX_RETRY);
|
||||
#endif
|
||||
return I2C_RESULT_ERROR;
|
||||
}
|
||||
@@ -1134,179 +1101,39 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint16_t reg_addr, uint8_t *data, uint
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* timeout path: provide debug info */
|
||||
#ifdef DEBUG_VERBOSE
|
||||
printf("I2C read timeout (end state). slave=0x%02X, len=%u\r\n", slave_addr, length);
|
||||
printf(" FLAGS: AERR=%d BERR=%d LOSTARB=%d I2CBSY=%d\r\n",
|
||||
(int)i2c_flag_get(I2C0, I2C_FLAG_AERR),
|
||||
(int)i2c_flag_get(I2C0, I2C_FLAG_BERR),
|
||||
(int)i2c_flag_get(I2C0, I2C_FLAG_LOSTARB),
|
||||
(int)i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));
|
||||
#endif
|
||||
return I2C_RESULT_TIMEOUT;
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief read data from I2C device without register/sub-address phase
|
||||
\param[in] slave_addr: slave device address (7-bit)
|
||||
\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_raw(uint8_t slave_addr, uint8_t *data, uint8_t length)
|
||||
{
|
||||
i2c_state_t state = I2C_STATE_START;
|
||||
uint16_t timeout = 0;
|
||||
uint8_t retry_count = 0;
|
||||
uint8_t data_index = 0;
|
||||
/* convenience functions for common operations */
|
||||
i2c_result_t i2c_write_8bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data) {
|
||||
return i2c_write(slave_addr, reg_addr, &data, 1);
|
||||
}
|
||||
|
||||
if (data == NULL || slave_addr > 0x7F || length == 0) {
|
||||
i2c_result_t i2c_read_8bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data) {
|
||||
return i2c_read(slave_addr, reg_addr, data, 1);
|
||||
}
|
||||
|
||||
i2c_result_t i2c_write_32bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[4]) {
|
||||
return i2c_write(slave_addr, reg_addr, data, 4);
|
||||
}
|
||||
|
||||
i2c_result_t i2c_read_32bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data) {
|
||||
return i2c_read(slave_addr, reg_addr, data, 4);
|
||||
}
|
||||
|
||||
/* 显示面板专用函数 - 支持读取显示界面参数 */
|
||||
i2c_result_t i2c_read_display_params(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8_t length) {
|
||||
/* 针对显示面板的多字节读取,支持13字节的完整参数读取 */
|
||||
if (length > 13) {
|
||||
return I2C_RESULT_INVALID_PARAM;
|
||||
}
|
||||
|
||||
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
||||
|
||||
while (retry_count < (uint8_t)I2C_MAX_RETRY) {
|
||||
switch (state) {
|
||||
case I2C_STATE_START:
|
||||
timeout = 0;
|
||||
data_index = 0;
|
||||
|
||||
if (_i2c_wait_flag_clear_timeout(I2C_FLAG_I2CBSY) != I2C_RESULT_SUCCESS) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
i2c_start_on_bus(I2C0);
|
||||
state = I2C_STATE_SEND_ADDRESS;
|
||||
break;
|
||||
|
||||
case I2C_STATE_SEND_ADDRESS:
|
||||
if (_i2c_wait_flag_timeout(I2C_FLAG_SBSEND) != I2C_RESULT_SUCCESS) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
i2c_master_addressing(I2C0, (slave_addr << 1), I2C_RECEIVER);
|
||||
state = I2C_STATE_CLEAR_ADDRESS;
|
||||
timeout = 0;
|
||||
break;
|
||||
|
||||
case I2C_STATE_CLEAR_ADDRESS:
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) && (!i2c_flag_get(I2C0, I2C_FLAG_AERR)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
if (i2c_flag_get(I2C0, I2C_FLAG_AERR)) {
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
||||
i2c_stop_on_bus(I2C0);
|
||||
return I2C_RESULT_NACK;
|
||||
}
|
||||
|
||||
if (length == 1) {
|
||||
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
||||
} else if (length == 2) {
|
||||
i2c_ackpos_config(I2C0, I2C_ACKPOS_NEXT);
|
||||
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
||||
} else {
|
||||
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
||||
}
|
||||
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||
state = I2C_STATE_RECEIVE_DATA;
|
||||
timeout = 0;
|
||||
break;
|
||||
|
||||
case I2C_STATE_RECEIVE_DATA:
|
||||
if (length == 1) {
|
||||
if (_i2c_wait_flag_timeout(I2C_FLAG_RBNE) != I2C_RESULT_SUCCESS) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
data[0] = i2c_data_receive(I2C0);
|
||||
state = I2C_STATE_STOP;
|
||||
} else if (length == 2) {
|
||||
if (_i2c_wait_flag_timeout(I2C_FLAG_BTC) != I2C_RESULT_SUCCESS) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
i2c_stop_on_bus(I2C0);
|
||||
data[0] = i2c_data_receive(I2C0);
|
||||
data[1] = i2c_data_receive(I2C0);
|
||||
state = I2C_STATE_STOP;
|
||||
} else {
|
||||
while (data_index < length) {
|
||||
if (data_index < length - 2) {
|
||||
if (_i2c_wait_flag_timeout(I2C_FLAG_RBNE) != I2C_RESULT_SUCCESS) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
data[data_index++] = i2c_data_receive(I2C0);
|
||||
} else if (data_index == length - 2) {
|
||||
if (_i2c_wait_flag_timeout(I2C_FLAG_BTC) != I2C_RESULT_SUCCESS) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
||||
i2c_stop_on_bus(I2C0);
|
||||
data[data_index++] = i2c_data_receive(I2C0);
|
||||
} else {
|
||||
if (_i2c_wait_flag_timeout(I2C_FLAG_RBNE) != I2C_RESULT_SUCCESS) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
data[data_index++] = i2c_data_receive(I2C0);
|
||||
}
|
||||
}
|
||||
if (state == I2C_STATE_ERROR) break;
|
||||
state = I2C_STATE_STOP;
|
||||
}
|
||||
break;
|
||||
|
||||
case I2C_STATE_STOP:
|
||||
while ((I2C_CTL0(I2C0) & I2C_CTL0_STOP) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
||||
i2c_ackpos_config(I2C0, I2C_ACKPOS_CURRENT);
|
||||
return I2C_RESULT_SUCCESS;
|
||||
|
||||
case I2C_STATE_ERROR:
|
||||
i2c_bus_reset();
|
||||
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
||||
i2c_ackpos_config(I2C0, I2C_ACKPOS_CURRENT);
|
||||
|
||||
retry_count++;
|
||||
if (retry_count >= I2C_MAX_RETRY) {
|
||||
return I2C_RESULT_ERROR;
|
||||
}
|
||||
|
||||
state = I2C_STATE_START;
|
||||
timeout = 0;
|
||||
delay_10us(10);
|
||||
break;
|
||||
|
||||
default:
|
||||
state = I2C_STATE_START;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return I2C_RESULT_TIMEOUT;
|
||||
return i2c_read(slave_addr, reg_addr, data, length);
|
||||
}
|
||||
|
||||
#ifdef DEBUG_VERBOSE
|
||||
/*!
|
||||
\brief get status string for debugging
|
||||
\param[in] status: i2c_result_t value
|
||||
\param[in] status: i2c_status_t value
|
||||
\param[out] none
|
||||
\retval const char* status string
|
||||
*/
|
||||
|
||||
+9
-21
@@ -148,17 +148,11 @@ ldc1612_status_t ldc1612_init(void) {
|
||||
*/
|
||||
ldc1612_status_t ldc1612_config_single_channel(uint8_t channel) {
|
||||
i2c_result_t status;
|
||||
uint16_t freq_divider;
|
||||
|
||||
if (channel > 1) {
|
||||
return LDC1612_STATUS_INVALID_PARAM;
|
||||
}
|
||||
|
||||
freq_divider = ldc1612_calculate_freq_divider(channel);
|
||||
if (freq_divider == 0) {
|
||||
return LDC1612_STATUS_INVALID_PARAM;
|
||||
}
|
||||
|
||||
/* 配置顺序严格按照TI官方文档要求 */
|
||||
|
||||
/* Step 1: 确保传感器处于睡眠模式 - 配置前必须 */
|
||||
@@ -167,34 +161,28 @@ ldc1612_status_t ldc1612_config_single_channel(uint8_t channel) {
|
||||
delay_ms(10);
|
||||
|
||||
/* Step 2: 配置频率分频 - 必须在其他配置之前 */
|
||||
status = ldc1612_write_register(SET_FREQ_REG_START + channel, freq_divider);
|
||||
if (status != I2C_RESULT_SUCCESS) return LDC1612_STATUS_ERROR;
|
||||
uint16_t freq_divider = ldc1612_calculate_freq_divider(channel);
|
||||
ldc1612_write_register(SET_FREQ_REG_START + channel, freq_divider);
|
||||
delay_ms(5);
|
||||
|
||||
/* Step 3: 配置LC稳定时间 - 影响测量精度 */
|
||||
status = ldc1612_write_register(SET_SETTLECOUNT_REG_START + channel, LDC1612_SETTLECOUNT_CH0);
|
||||
if (status != I2C_RESULT_SUCCESS) return LDC1612_STATUS_ERROR;
|
||||
ldc1612_write_register(SET_SETTLECOUNT_REG_START + channel, LDC1612_SETTLECOUNT_CH0);
|
||||
|
||||
/* Step 4: 配置转换时间 - 影响测量速度和精度 */
|
||||
status = ldc1612_write_register(SET_CONVERSION_TIME_REG_START + channel, LDC1612_RCOUNT_TIME_CH0);
|
||||
if (status != I2C_RESULT_SUCCESS) return LDC1612_STATUS_ERROR;
|
||||
ldc1612_write_register(SET_CONVERSION_TIME_REG_START + channel, LDC1612_RCOUNT_TIME_CH0);
|
||||
|
||||
/* Step 5: 配置转换偏移 */
|
||||
status = ldc1612_write_register(SET_CONVERSION_OFFSET_REG_START + channel, SET_CONVERSION_OFFSET_CH0);
|
||||
if (status != I2C_RESULT_SUCCESS) return LDC1612_STATUS_ERROR;
|
||||
ldc1612_write_register(SET_CONVERSION_OFFSET_REG_START + channel, SET_CONVERSION_OFFSET_CH0);
|
||||
|
||||
/* Step 6: 配置驱动电流 - 影响传感器灵敏度 */
|
||||
status = ldc1612_write_register(SET_DRIVER_CURRENT_REG + channel, LDC1612_DRIVE_CURRENT);
|
||||
if (status != I2C_RESULT_SUCCESS) return LDC1612_STATUS_ERROR;
|
||||
ldc1612_write_register(SET_DRIVER_CURRENT_REG + channel, LDC1612_DRIVE_CURRENT);
|
||||
|
||||
/* Step 7: 配置多路复用器 - 设置通道选择和滤波 */
|
||||
// ldc1612_configure_mux_register(LDC1612_MUX_AUTOSCAN_DISABLE, LDC1612_MUX_RR_SEQUENCE_0, LDC1612_MUX_FILTER_ALL_LOW, LDC1612_MUX_FILTER_NONE);
|
||||
status = ldc1612_write_register(MUX_CONFIG_REG, LDC1612_MUX_CONFIG);
|
||||
if (status != I2C_RESULT_SUCCESS) return LDC1612_STATUS_ERROR;
|
||||
ldc1612_write_register(MUX_CONFIG_REG, LDC1612_MUX_CONFIG);
|
||||
|
||||
/* Step 8: 配置错误输出 */
|
||||
status = ldc1612_write_register(ERROR_CONFIG_REG, LDC1612_ERROR_CONFIG_DEFAULT);
|
||||
if (status != I2C_RESULT_SUCCESS) return LDC1612_STATUS_ERROR;
|
||||
ldc1612_write_register(ERROR_CONFIG_REG, LDC1612_ERROR_CONFIG_DEFAULT);
|
||||
|
||||
/* Step 9: 最后启动传感器 - 必须最后一步 */
|
||||
status = ldc1612_write_register(SENSOR_CONFIG_REG, LDC1612_SENSOR_CONFIG_CH0);
|
||||
@@ -344,4 +332,4 @@ uint16_t ldc1612_check_status_and_log_errors(void) {
|
||||
|
||||
// 读取STATUS寄存器会自动清除错误标志,但不会清除DRDY和UNREADCONV标志
|
||||
return status;
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -86,7 +86,8 @@ tmp112a_status_t tmp112a_read_temperature(tmp112a_result_t *result) {
|
||||
}
|
||||
|
||||
void tmp112a_get_raw_temperature_value(uint8_t *value) {
|
||||
i2c_read_16bits(TMP112A_ADDR, TMP112A_TEMP_REG, value);
|
||||
// i2c_read_16bits(TMP112A_ADDR, TMP112A_TEMP_REG, value);
|
||||
i2c_read(TMP112A_ADDR, TMP112A_TEMP_REG, value, 2);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user