@@ -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
5657static 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 );
0 commit comments