Skip to content

Commit 2be25cb

Browse files
committed
control spped
1 parent 8a4625a commit 2be25cb

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

bsp/gd32/arm/gd32e230-lckfb/board/Kconfig

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,31 @@ menu "On-chip Peripheral Drivers"
150150
bool "Enable I2C0"
151151
default y
152152

153+
config BSP_HW_I2C0_CLOCK_SPEED
154+
int "I2C0 Clock Speed (Hz)"
155+
depends on BSP_USING_HW_I2C0
156+
range 100000 1000000
157+
default 400000
158+
help
159+
Set the I2C0 clock speed in Hz.
160+
Standard mode: 100000 (100KHz)
161+
Fast mode: 400000 (400KHz)
162+
Fast mode plus: 1000000 (1MHz)
163+
153164
config BSP_USING_HW_I2C1
154165
bool "Enable I2C1"
155166
default n
167+
168+
config BSP_HW_I2C1_CLOCK_SPEED
169+
int "I2C1 Clock Speed (Hz)"
170+
depends on BSP_USING_HW_I2C1
171+
range 100000 1000000
172+
default 400000
173+
help
174+
Set the I2C1 clock speed in Hz.
175+
Standard mode: 100000 (100KHz)
176+
Fast mode: 400000 (400KHz)
177+
Fast mode plus: 1000000 (1MHz)
156178
endif
157179
menuconfig BSP_USING_I2C0
158180
bool "Enable I2C0 BUS (software simulation)"

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ static const struct gd32_i2c_config i2c_configs[] =
2525
.scl_clk = RCU_GPIOA,
2626
.scl_port = GPIOA,
2727
.scl_pin = GPIO_PIN_9,
28-
.scl_af = GPIO_AF_1,
28+
.scl_af = GPIO_AF_4,
2929
.sda_clk = RCU_GPIOA,
3030
.sda_port = GPIOA,
3131
.sda_pin = GPIO_PIN_10,
32-
.sda_af = GPIO_AF_1,
32+
.sda_af = GPIO_AF_4,
3333

34+
.i2c_clock_hz = BSP_HW_I2C0_CLOCK_SPEED,
3435
.device_name = "i2c0",
3536
},
3637
#endif
@@ -47,11 +48,11 @@ static const struct gd32_i2c_config i2c_configs[] =
4748
.sda_port = GPIOB,
4849
.sda_pin = GPIO_PIN_11,
4950
.sda_af = GPIO_AF_1,
51+
.i2c_clock_hz = BSP_HW_I2C1_CLOCK_SPEED,
5052
.device_name = "i2c1",
5153
},
5254
#endif
5355
};
54-
};
5556

5657
static struct gd32_i2c i2c_objs[sizeof(i2c_configs) / sizeof(i2c_configs[0])];
5758

@@ -65,19 +66,17 @@ static rt_ssize_t gd32_i2c_master_xfer(struct rt_i2c_bus_device *bus,
6566
rt_uint32_t i;
6667
rt_err_t ret = RT_EOK;
6768

68-
/* 等待总线空闲 */
6969
while (i2c_flag_get(i2c_periph, I2C_FLAG_I2CBSY));
7070

7171
for (i = 0; i < num; i++)
7272
{
7373
struct rt_i2c_msg *msg = &msgs[i];
7474
rt_uint16_t slave_addr = msg->addr;
7575

76-
/* 发送起始信号 */
7776
i2c_start_on_bus(i2c_periph);
7877
while (!i2c_flag_get(i2c_periph, I2C_FLAG_SBSEND));
7978

80-
if (msg->flags & RT_I2C_RD) /* 读操作 */
79+
if (msg->flags & RT_I2C_RD)
8180
{
8281
i2c_master_addressing(i2c_periph, slave_addr << 1, I2C_RECEIVER);
8382
}
@@ -89,7 +88,7 @@ static rt_ssize_t gd32_i2c_master_xfer(struct rt_i2c_bus_device *bus,
8988
while (!i2c_flag_get(i2c_periph, I2C_FLAG_ADDSEND));
9089
i2c_flag_clear(i2c_periph, I2C_FLAG_ADDSEND);
9190

92-
if (msg->flags & RT_I2C_RD) /* 读数据 */
91+
if (msg->flags & RT_I2C_RD)
9392
{
9493
if (msg->len == 1)
9594
{
@@ -150,11 +149,7 @@ int rt_hw_i2c_init(void)
150149

151150
i2c_obj->config = config;
152151

153-
/*
154-
* TODO: 从 board.h 获取时钟频率, 此处为示例
155-
* 您可以在板级配置中定义 I2C0_CLOCK_HZ, I2C1_CLOCK_HZ
156-
*/
157-
i2c_obj->i2c_clock_hz = 100000; // 默认为 100KHz
152+
i2c_obj->i2c_clock_hz = config->i2c_clock_hz;
158153

159154
rcu_periph_clock_enable(config->periph_clk);
160155
rcu_periph_clock_enable(config->scl_clk);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ struct gd32_i2c_config
2525
rt_uint32_t sda_pin;
2626
rt_uint32_t sda_af;
2727

28+
rt_uint32_t i2c_clock_hz; /* I2C clock frequency in Hz */
2829
const char *device_name;
2930
};
3031

31-
/* GD32 硬件 I2C 总线设备结构体 */
3232
struct gd32_i2c
3333
{
3434
struct rt_i2c_bus_device parent;

0 commit comments

Comments
 (0)