for debug

This commit is contained in:
2025-08-14 00:13:49 +08:00
parent 93294b0e3c
commit 54bf206ec3
6 changed files with 161 additions and 4 deletions

View File

@@ -4,6 +4,10 @@
"Git Bash": {
"path": "C:\\Program Files\\Git\\bin\\bash.exe",
"icon": "terminal-bash"
},
"Git-Bash": {
"path": "D:\\Git\\bin\\bash.exe",
"icon": "terminal-bash"
}
},
"terminal.integrated.defaultProfile.windows": "Git-Bash",

139
I2C_IMPROVEMENTS.md Normal file
View File

@@ -0,0 +1,139 @@
# I2C驱动改进总结
## 🔧 主要改进内容
### 1. **状态机重构**
- **原问题**: 状态机逻辑混乱使用复杂的read_cycle变量
- **改进方案**:
- 使用清晰的`i2c_state_t`枚举定义状态
- 分离写入和读取的状态流程
- 每个状态职责单一,逻辑清晰
```c
typedef enum {
I2C_STATE_IDLE = 0, /* 空闲状态 */
I2C_STATE_START, /* 生成起始条件 */
I2C_STATE_SEND_ADDRESS, /* 发送从设备地址 */
I2C_STATE_CLEAR_ADDRESS, /* 清除地址标志 */
I2C_STATE_TRANSMIT_REG, /* 发送寄存器地址 */
I2C_STATE_TRANSMIT_DATA, /* 发送数据 */
I2C_STATE_RESTART, /* 生成重启条件 */
I2C_STATE_RECEIVE_DATA, /* 接收数据 */
I2C_STATE_STOP, /* 生成停止条件 */
I2C_STATE_ERROR /* 错误状态 */
} i2c_state_t;
```
### 2. **错误处理改进**
- **原问题**: 函数总是返回成功,无法区分错误类型
- **改进方案**:
- 定义详细的状态码枚举
- 添加参数验证
- 实现重试机制
```c
typedef enum {
I2C_STATUS_SUCCESS = 0, /* 操作成功 */
I2C_STATUS_TIMEOUT, /* 超时 */
I2C_STATUS_NACK, /* 无应答 */
I2C_STATUS_BUS_BUSY, /* 总线忙 */
I2C_STATUS_ERROR, /* 一般错误 */
I2C_STATUS_INVALID_PARAM /* 无效参数 */
} i2c_status_t;
```
### 3. **超时处理优化**
- **原问题**: 超时后无限循环重试
- **改进方案**:
- 限制最大重试次数 (`I2C_MAX_RETRY = 3`)
- 超时后进入错误状态
- 重试前添加延时
### 4. **总线重置完善**
- **原问题**: 总线重置不完整,可能无法恢复卡死状态
- **改进方案**:
- 实现标准的9时钟脉冲恢复
- 生成正确的停止条件
- 重新配置GPIO和I2C外设
```c
/* 生成9个时钟脉冲释放卡死的从设备 */
for (i = 0; i < I2C_RECOVERY_CLOCKS; i++) {
gpio_bit_reset(I2C_SCL_PORT, I2C_SCL_PIN);
delay_us(I2C_DELAY_US);
gpio_bit_set(I2C_SCL_PORT, I2C_SCL_PIN);
delay_us(I2C_DELAY_US);
}
```
### 5. **配置问题修复**
- **原问题**: 硬编码从设备地址0xA0
- **改进方案**: 主机地址设为0x00从设备地址作为参数传入
### 6. **代码结构优化**
- **原问题**: 状态机中有大量重复代码
- **改进方案**:
- 统一的超时检查模式
- 清晰的状态转换逻辑
- 一致的错误处理流程
## 📋 新增功能
### 1. **状态字符串函数**
```c
const char* i2c_get_status_string(i2c_status_t status);
```
用于调试时获取状态描述字符串。
### 2. **参数验证**
```c
if (data == NULL || slave_addr > 0x7F) {
return I2C_STATUS_INVALID_PARAM;
}
```
### 3. **调试信息**
使用`DEBUG_VERBOSE`宏控制调试输出。
## 🔍 状态机流程
### 写入流程:
```
START → SEND_ADDRESS → CLEAR_ADDRESS → TRANSMIT_REG →
TRANSMIT_DATA → STOP → SUCCESS
```
### 读取流程:
```
写阶段: START → SEND_ADDRESS → CLEAR_ADDRESS → TRANSMIT_REG → RESTART
读阶段: START → SEND_ADDRESS → CLEAR_ADDRESS → RECEIVE_DATA → STOP → SUCCESS
```
## 🚀 使用示例
```c
// 写入16位数据
uint8_t write_data[2] = {0x12, 0x34};
i2c_status_t status = i2c_write_16bits(0x48, 0x01, write_data);
if (status != I2C_STATUS_SUCCESS) {
printf("Write failed: %s\r\n", i2c_get_status_string(status));
}
// 读取16位数据
uint8_t read_data[2];
status = i2c_read_16bits(0x48, 0x01, read_data);
if (status == I2C_STATUS_SUCCESS) {
printf("Read data: 0x%02X%02X\r\n", read_data[0], read_data[1]);
} else {
printf("Read failed: %s\r\n", i2c_get_status_string(status));
}
```
## 📝 注意事项
1. **编译选项**: 确保包含`<stdbool.h>`以支持bool类型
2. **调试输出**: 定义`DEBUG_VERBOSE`宏启用调试信息
3. **延时函数**: 确保`delay_us()`函数可用
4. **兼容性**: 保留了原有的函数接口以保持向后兼容
这些改进大大提高了I2C驱动的可靠性、可维护性和调试能力。

View File

@@ -13,8 +13,8 @@
/* >>>>>>>>>>>>>>>>>>>>[DEBUG ASSERTIONS DEFINE]<<<<<<<<<<<<<<<<<<<< */
// #define DEBUG_VERBOSE // Debug Assertions Status : Debug Verbose Information
#undef DEBUG_VERBOSE // Debug Assertions Status : No Debug Verbose Information
#define DEBUG_VERBOSE // Debug Assertions Status : Debug Verbose Information
// #undef DEBUG_VERBOSE // Debug Assertions Status : No Debug Verbose Information
/******************************************************************************/

View File

@@ -8,7 +8,6 @@
#include "gd32e23x_it.h"
#include "gd32e23x.h"
#include "systick.h"
#include "main.h"
#include "board_config.h"

View File

@@ -38,6 +38,8 @@ OF SUCH DAMAGE.
#include "led.h"
#include "command.h"
#include <stdio.h>
#include "i2c.h"
#include "board_config.h"
bool g_status_switch = false;
@@ -56,6 +58,7 @@ int main(void)
led_init();
#ifdef DEBUG_VERBOSE
char hello_world[] = {"Hello World!"};
for (uint8_t i = 0; i < sizeof(hello_world); i++)
@@ -65,6 +68,18 @@ int main(void)
}
while (usart_flag_get(RS485_PHY, USART_FLAG_TC) == RESET) {}
#endif
i2c_config();
// i2c_bus_reset();
#ifdef DEBUG_VERBOSE
i2c_scan();
#endif
while(1){
command_process();

View File

@@ -11,7 +11,7 @@
\retval none
*/
void soft_i2c_delay(void) {
delay_us(20); // Adjust delay as needed
delay_10us(2); // Adjust delay as needed
/* delay to freq
* 15KHz: delay_us(20);
* 65KHz: delay_us(1);