Skip to content

Commit 22b81d0

Browse files
committed
update
1 parent 548f0f1 commit 22b81d0

File tree

2 files changed

+52
-109
lines changed

2 files changed

+52
-109
lines changed

bsp/gd32/arm/libraries/gd32_drivers/drv_hw_i2c.c

Lines changed: 3 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -5,62 +5,7 @@
55
*
66
*/
77

8-
#include <rtthread.h>
9-
#include <rtdevice.h>
10-
#include "board.h"
11-
#include <rthw.h>
12-
#include <board.h>
13-
14-
#ifdef __cplusplus
15-
extern "C" {
16-
#endif
17-
18-
/* I2C hardware configuration */
19-
struct gd32_i2c_config
20-
{
21-
rt_uint32_t i2c_periph; /* I2C peripheral base address */
22-
rcu_periph_enum periph_clk; /* I2C peripheral clock */
23-
24-
rcu_periph_enum scl_clk; /* SCL pin clock */
25-
rt_uint32_t scl_port; /* SCL pin port */
26-
rt_uint32_t scl_pin; /* SCL pin */
27-
rt_uint32_t scl_af; /* SCL pin alternate function */
28-
29-
rcu_periph_enum sda_clk; /* SDA pin clock */
30-
rt_uint32_t sda_port; /* SDA pin port */
31-
rt_uint32_t sda_pin; /* SDA pin */
32-
rt_uint32_t sda_af; /* SDA pin alternate function */
33-
34-
IRQn_Type ev_irq_type; /* Event IRQn */
35-
IRQn_Type er_irq_type; /* Error IRQn */
36-
37-
rt_uint32_t i2c_clock_hz; /* I2C clock speed in Hz, e.g., 100000 for 100kHz */
38-
39-
const char *device_name; /* Device name */
40-
};
41-
42-
/* I2C runtime context */
43-
struct gd32_i2c
44-
{
45-
struct rt_i2c_bus_device parent;
46-
const struct gd32_i2c_config *config;
47-
48-
struct rt_mutex bus_mutex;
49-
struct rt_completion sync_sem;
50-
uint32_t dev_config;
51-
uint16_t addr1;
52-
uint16_t addr2;
53-
uint32_t xfer_len;
54-
struct rt_i2c_msg *current;
55-
uint8_t errs;
56-
rt_bool_t is_restart;
57-
};
58-
59-
int rt_hw_i2c_init(void);
60-
61-
#ifdef __cplusplus
62-
}
63-
#endif
8+
#include "drv_hw_i2c.h"
649

6510

6611
#ifdef RT_USING_I2C
@@ -122,24 +67,19 @@ static inline void gd32_i2c_xfer_read(struct gd32_i2c *i2c_obj)
12267
rt_uint8_t read_byte;
12368

12469
i2c_obj->current->len--;
125-
read_byte = I2C_DATA(cfg->i2c_periph); // 先读取数据
126-
*i2c_obj->current->buf = read_byte; // 存入缓冲区
70+
read_byte = I2C_DATA(cfg->i2c_periph);
71+
*i2c_obj->current->buf = read_byte;
12772

128-
/* 新增日志:打印读到的字节 */
12973
LOG_D("[%s] < Read byte: 0x%02X", cfg->device_name, read_byte);
13074

13175
i2c_obj->current->buf++;
13276

133-
/* 如果当前 msg 读完,但总传输长度 > 0,则切换到下一个 msg */
13477
if ((i2c_obj->xfer_len > 0U) && (i2c_obj->current->len == 0U))
13578
{
13679
i2c_obj->current++;
13780
}
13881
}
13982

140-
/**
141-
* @brief 将当前消息缓冲区的一个字节写入 I2C_DATA 寄存器
142-
*/
14383
static inline void gd32_i2c_xfer_write(struct gd32_i2c *i2c_obj)
14484
{
14585
const struct gd32_i2c_config *cfg = i2c_obj->config;
@@ -157,9 +97,6 @@ static inline void gd32_i2c_xfer_write(struct gd32_i2c *i2c_obj)
15797
}
15898
}
15999

160-
/**
161-
* @brief 处理 TBE (发送缓冲区为空) 事件
162-
*/
163100
static void gd32_i2c_handle_tbe(struct gd32_i2c *i2c_obj)
164101
{
165102
const struct gd32_i2c_config *cfg = i2c_obj->config;
@@ -183,9 +120,6 @@ static void gd32_i2c_handle_tbe(struct gd32_i2c *i2c_obj)
183120
}
184121
}
185122

186-
/**
187-
* @brief 处理 RBNE (接收缓冲区非空) 事件
188-
*/
189123
static void gd32_i2c_handle_rbne(struct gd32_i2c *i2c_obj)
190124
{
191125
const struct gd32_i2c_config *cfg = i2c_obj->config;
@@ -215,9 +149,6 @@ static void gd32_i2c_handle_rbne(struct gd32_i2c *i2c_obj)
215149
}
216150
}
217151

218-
/**
219-
* @brief 处理 BTC (字节传输完成) 事件
220-
*/
221152
static void gd32_i2c_handle_btc(struct gd32_i2c *i2c_obj)
222153
{
223154
const struct gd32_i2c_config *cfg = i2c_obj->config;
@@ -254,9 +185,6 @@ static void gd32_i2c_handle_btc(struct gd32_i2c *i2c_obj)
254185
}
255186
}
256187

257-
/**
258-
* @brief 处理 ADDSEND (地址已发送) 事件
259-
*/
260188
static void gd32_i2c_handle_addsend(struct gd32_i2c *i2c_obj)
261189
{
262190
const struct gd32_i2c_config *cfg = i2c_obj->config;
@@ -388,40 +316,6 @@ static rt_ssize_t gd32_i2c_master_xfer(struct rt_i2c_bus_device *bus, struct rt_
388316

389317
LOG_D("[%s] master_xfer: num_msgs=%d", cfg->device_name, num);
390318

391-
/* ========== 新增的调试打印 Start ========== */
392-
LOG_D("[%s] master_xfer called with %d message(s):", cfg->device_name, num);
393-
for (rt_ssize_t dbg_i = 0; dbg_i < num; dbg_i++)
394-
{
395-
// 为了日志整洁,我们动态构建一个可读的标志字符串
396-
char flags_str[64] = {0};
397-
const char *rw_str = (msgs[dbg_i].flags & RT_I2C_RD) ? "RD" : "WR";
398-
const char *stop_str = (msgs[dbg_i].flags & RT_I2C_NO_STOP) ? ", NO_STOP" : "";
399-
const char *addr_str = (msgs[dbg_i].flags & RT_I2C_ADDR_10BIT) ? ", 10BIT" : "";
400-
rt_snprintf(flags_str, sizeof(flags_str), "%s%s%s", rw_str, stop_str, addr_str);
401-
402-
LOG_D("[%s] - msg[%d]: addr=0x%02X, len=%d, flags=0x%X (%s)",
403-
cfg->device_name, dbg_i, msgs[dbg_i].addr, msgs[dbg_i].len, msgs[dbg_i].flags, flags_str);
404-
405-
// 如果是写消息,并且有数据,则打印数据内容 (最多打印16字节)
406-
if (!(msgs[dbg_i].flags & RT_I2C_RD) && msgs[dbg_i].buf != RT_NULL && msgs[dbg_i].len > 0)
407-
{
408-
#define MAX_PRINT_LEN 16
409-
char data_str[MAX_PRINT_LEN * 3 + 4] = {0};
410-
char *p = data_str;
411-
rt_size_t print_len = (msgs[dbg_i].len > MAX_PRINT_LEN) ? MAX_PRINT_LEN : msgs[dbg_i].len;
412-
for (rt_size_t j = 0; j < print_len; j++)
413-
{
414-
p += rt_snprintf(p, sizeof(data_str) - (p - data_str), "%02X ", msgs[dbg_i].buf[j]);
415-
}
416-
if (msgs[dbg_i].len > MAX_PRINT_LEN)
417-
{
418-
p += rt_snprintf(p, sizeof(data_str) - (p - data_str), "...");
419-
}
420-
LOG_D("[%s] > Data: %s", cfg->device_name, data_str);
421-
}
422-
}
423-
/* ========== 新增的调试打印 End ========== */
424-
425319
for (i = 0; i < num; i++)
426320
{
427321
if ((i < num - 1) && !(msgs[i].flags & RT_I2C_NO_STOP))

bsp/gd32/arm/libraries/gd32_drivers/drv_hw_i2c.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,55 @@
1515
#include <rtdevice.h>
1616
#include <board.h>
1717

18+
#ifdef __cplusplus
19+
extern "C" {
20+
#endif
1821

22+
/* I2C hardware configuration */
23+
struct gd32_i2c_config
24+
{
25+
rt_uint32_t i2c_periph; /* I2C peripheral base address */
26+
rcu_periph_enum periph_clk; /* I2C peripheral clock */
27+
28+
rcu_periph_enum scl_clk; /* SCL pin clock */
29+
rt_uint32_t scl_port; /* SCL pin port */
30+
rt_uint32_t scl_pin; /* SCL pin */
31+
rt_uint32_t scl_af; /* SCL pin alternate function */
32+
33+
rcu_periph_enum sda_clk; /* SDA pin clock */
34+
rt_uint32_t sda_port; /* SDA pin port */
35+
rt_uint32_t sda_pin; /* SDA pin */
36+
rt_uint32_t sda_af; /* SDA pin alternate function */
37+
38+
IRQn_Type ev_irq_type; /* Event IRQn */
39+
IRQn_Type er_irq_type; /* Error IRQn */
40+
41+
rt_uint32_t i2c_clock_hz; /* I2C clock speed in Hz, e.g., 100000 for 100kHz */
42+
43+
const char *device_name; /* Device name */
44+
};
45+
46+
/* I2C runtime context */
47+
struct gd32_i2c
48+
{
49+
struct rt_i2c_bus_device parent;
50+
const struct gd32_i2c_config *config;
51+
52+
struct rt_mutex bus_mutex;
53+
struct rt_completion sync_sem;
54+
uint32_t dev_config;
55+
uint16_t addr1;
56+
uint16_t addr2;
57+
uint32_t xfer_len;
58+
struct rt_i2c_msg *current;
59+
uint8_t errs;
60+
rt_bool_t is_restart;
61+
};
62+
63+
int rt_hw_i2c_init(void);
64+
65+
#ifdef __cplusplus
66+
}
67+
#endif
1968

2069
#endif /* __DRV_HW_I2C_H__ */

0 commit comments

Comments
 (0)