From a5e991d75cffbe1f62a5c330400103c256f04705 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9F=B3=E9=B8=BF=E8=B6=85?= <13010364+shi-hongchao@user.noreply.gitee.com> Date: Sun, 11 Jan 2026 19:18:36 +0800 Subject: [PATCH] =?UTF-8?q?fix(bsp/gd32/arm):=20=E4=BF=AE=E5=A4=8Dgd32?= =?UTF-8?q?=E7=A1=AC=E4=BB=B6i2c=E9=A9=B1=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 硬件i2c主机接收驱动采用方案B,但不支持接收小于3字节,现修复该问题 - 增加方案A。 --- bsp/gd32/arm/gd32405rg/.config | 20 +- bsp/gd32/arm/gd32405rg/board/Kconfig | 29 +++ bsp/gd32/arm/gd32405rg/project.uvoptx | 224 ++++++++---------- bsp/gd32/arm/gd32405rg/project.uvprojx | 42 ++-- bsp/gd32/arm/gd32405rg/rtconfig.h | 16 +- .../arm/libraries/gd32_drivers/drv_hard_i2c.c | 86 ++++++- 6 files changed, 231 insertions(+), 186 deletions(-) diff --git a/bsp/gd32/arm/gd32405rg/.config b/bsp/gd32/arm/gd32405rg/.config index d9defb3a43..8dcdda7301 100644 --- a/bsp/gd32/arm/gd32405rg/.config +++ b/bsp/gd32/arm/gd32405rg/.config @@ -1440,22 +1440,16 @@ CONFIG_BSP_USING_UART0=y # CONFIG_BSP_USING_UART3 is not set # CONFIG_BSP_USING_UART4 is not set # CONFIG_BSP_USING_UART5 is not set +CONFIG_BSP_USING_HARD_I2C=y +CONFIG_BSP_USING_RECEIVING_A=y +# CONFIG_BSP_USING_RECEIVING_B is not set +CONFIG_BSP_USING_HARD_I2C0=y +# CONFIG_BSP_USING_HARD_I2C1 is not set +# CONFIG_BSP_USING_HARD_I2C2 is not set # CONFIG_BSP_USING_SPI is not set # CONFIG_BSP_USING_ADC is not set # CONFIG_BSP_USING_HWTIMER is not set -CONFIG_BSP_USING_PWM=y -CONFIG_BSP_USING_PWM0=y -CONFIG_BSP_USING_PWM1=y -CONFIG_BSP_USING_PWM2=y -CONFIG_BSP_USING_PWM3=y -CONFIG_BSP_USING_PWM4=y -CONFIG_BSP_USING_PWM7=y -CONFIG_BSP_USING_PWM8=y -CONFIG_BSP_USING_PWM9=y -CONFIG_BSP_USING_PWM10=y -CONFIG_BSP_USING_PWM11=y -CONFIG_BSP_USING_PWM12=y -CONFIG_BSP_USING_PWM13=y +# CONFIG_BSP_USING_PWM is not set # CONFIG_BSP_USING_ONCHIP_RTC is not set # CONFIG_BSP_USING_WDT is not set # CONFIG_BSP_USING_SDIO is not set diff --git a/bsp/gd32/arm/gd32405rg/board/Kconfig b/bsp/gd32/arm/gd32405rg/board/Kconfig index ea08346a41..36805b18fb 100644 --- a/bsp/gd32/arm/gd32405rg/board/Kconfig +++ b/bsp/gd32/arm/gd32405rg/board/Kconfig @@ -232,6 +232,35 @@ menu "On-chip Peripheral Drivers" default 32 endif + menuconfig BSP_USING_HARD_I2C + bool "Enable I2C" + select RT_USING_I2C + default n + if BSP_USING_HARD_I2C + choice + prompt "Select I2C Receiving Scheme" + default BSP_USING_RECEIVING_A + + config BSP_USING_RECEIVING_A + bool "master receiving secheme A --- requires that the software be capable of responding quickly to the 12C event." + + config BSP_USING_RECEIVING_B + bool "master receiving secheme B --- don't requires that the software be capable of responding quickly to the 12C event." + endchoice + + config BSP_USING_HARD_I2C0 + bool "enable hard I2C0" + default n + + config BSP_USING_HARD_I2C1 + bool "enable hard I2C1" + default n + + config BSP_USING_HARD_I2C2 + bool "enable hard I2C2" + default n + endif + menuconfig BSP_USING_SPI bool "Enable SPI BUS" default n diff --git a/bsp/gd32/arm/gd32405rg/project.uvoptx b/bsp/gd32/arm/gd32405rg/project.uvoptx index e8e6ea1538..7fd705460a 100644 --- a/bsp/gd32/arm/gd32405rg/project.uvoptx +++ b/bsp/gd32/arm/gd32405rg/project.uvoptx @@ -117,26 +117,6 @@ BIN\CMSIS_AGDI.dll - - 0 - ARMRTXEVENTFLAGS - -L70 -Z18 -C0 -M0 -T1 - - - 0 - DLGTARM - (1010=-1,-1,-1,-1,0)(1007=-1,-1,-1,-1,0)(1008=-1,-1,-1,-1,0)(1009=-1,-1,-1,-1,0)(1012=-1,-1,-1,-1,0) - - - 0 - ARMDBGFLAGS - - - - 0 - DLGUARM - - 0 CMSIS_AGDI @@ -155,12 +135,12 @@ 0 0 - 1 + 0 0 0 0 0 - 1 + 0 0 0 0 @@ -191,16 +171,6 @@ - - - System Viewer\TIMER1 - 35905 - - - System Viewer\TIMER2 - 35904 - - @@ -577,6 +547,18 @@ 0 0 0 + ..\libraries\gd32_drivers\drv_hard_i2c.c + drv_hard_i2c.c + 0 + 0 + + + 4 + 30 + 1 + 0 + 0 + 0 ..\libraries\gd32_drivers\drv_hwtimer.c drv_hwtimer.c 0 @@ -584,7 +566,7 @@ 4 - 30 + 31 1 0 0 @@ -596,7 +578,7 @@ 4 - 31 + 32 1 0 0 @@ -614,18 +596,6 @@ 0 0 0 - - 5 - 32 - 1 - 0 - 0 - 0 - ..\..\..\..\components\finsh\msh_parse.c - msh_parse.c - 0 - 0 - 5 33 @@ -633,18 +603,6 @@ 0 0 0 - ..\..\..\..\components\finsh\shell.c - shell.c - 0 - 0 - - - 5 - 34 - 1 - 0 - 0 - 0 ..\..\..\..\components\finsh\msh.c msh.c 0 @@ -652,7 +610,7 @@ 5 - 35 + 34 1 0 0 @@ -662,6 +620,30 @@ 0 0 + + 5 + 35 + 1 + 0 + 0 + 0 + ..\..\..\..\components\finsh\shell.c + shell.c + 0 + 0 + + + 5 + 36 + 1 + 0 + 0 + 0 + ..\..\..\..\components\finsh\msh_parse.c + msh_parse.c + 0 + 0 + @@ -672,7 +654,7 @@ 0 6 - 36 + 37 1 0 0 @@ -684,7 +666,7 @@ 6 - 37 + 38 1 0 0 @@ -696,7 +678,7 @@ 6 - 38 + 39 1 0 0 @@ -708,7 +690,7 @@ 6 - 39 + 40 1 0 0 @@ -720,7 +702,7 @@ 6 - 40 + 41 1 0 0 @@ -732,7 +714,7 @@ 6 - 41 + 42 1 0 0 @@ -744,7 +726,7 @@ 6 - 42 + 43 1 0 0 @@ -756,7 +738,7 @@ 6 - 43 + 44 1 0 0 @@ -768,7 +750,7 @@ 6 - 44 + 45 1 0 0 @@ -780,7 +762,7 @@ 6 - 45 + 46 1 0 0 @@ -792,7 +774,7 @@ 6 - 46 + 47 1 0 0 @@ -804,7 +786,7 @@ 6 - 47 + 48 1 0 0 @@ -816,7 +798,7 @@ 6 - 48 + 49 1 0 0 @@ -828,7 +810,7 @@ 6 - 49 + 50 1 0 0 @@ -840,7 +822,7 @@ 6 - 50 + 51 1 0 0 @@ -858,18 +840,6 @@ 0 0 0 - - 7 - 51 - 1 - 0 - 0 - 0 - ..\..\..\..\src\klibc\kstring.c - kstring.c - 0 - 0 - 7 52 @@ -877,23 +847,23 @@ 0 0 0 - ..\..\..\..\src\klibc\rt_vsscanf.c - rt_vsscanf.c - 0 - 0 - - - 7 - 53 - 1 - 0 - 0 - 0 ..\..\..\..\src\klibc\kerrno.c kerrno.c 0 0 + + 7 + 53 + 1 + 0 + 0 + 0 + ..\..\..\..\src\klibc\rt_vsscanf.c + rt_vsscanf.c + 0 + 0 + 7 54 @@ -901,6 +871,18 @@ 0 0 0 + ..\..\..\..\src\klibc\kstring.c + kstring.c + 0 + 0 + + + 7 + 55 + 1 + 0 + 0 + 0 ..\..\..\..\src\klibc\kstdio.c kstdio.c 0 @@ -908,7 +890,7 @@ 7 - 55 + 56 1 0 0 @@ -928,7 +910,7 @@ 0 8 - 56 + 57 1 0 0 @@ -940,7 +922,7 @@ 8 - 57 + 58 1 0 0 @@ -952,7 +934,7 @@ 8 - 58 + 59 1 0 0 @@ -964,7 +946,7 @@ 8 - 59 + 60 2 0 0 @@ -976,7 +958,7 @@ 8 - 60 + 61 1 0 0 @@ -990,13 +972,13 @@ Libraries - 1 + 0 0 0 0 9 - 61 + 62 2 0 0 @@ -1008,7 +990,7 @@ 9 - 62 + 63 1 0 0 @@ -1020,7 +1002,7 @@ 9 - 63 + 64 1 0 0 @@ -1032,7 +1014,7 @@ 9 - 64 + 65 1 0 0 @@ -1044,7 +1026,7 @@ 9 - 65 + 66 1 0 0 @@ -1056,7 +1038,7 @@ 9 - 66 + 67 1 0 0 @@ -1068,7 +1050,7 @@ 9 - 67 + 68 1 0 0 @@ -1080,7 +1062,7 @@ 9 - 68 + 69 1 0 0 @@ -1092,7 +1074,7 @@ 9 - 69 + 70 1 0 0 @@ -1104,7 +1086,7 @@ 9 - 70 + 71 1 0 0 @@ -1114,18 +1096,6 @@ 0 0 - - 9 - 71 - 1 - 0 - 0 - 0 - .\packages\gd32-arm-series-latest\GD32F4xx\GD32F4xx_standard_peripheral\Source\gd32f4xx_timer.c - gd32f4xx_timer.c - 0 - 0 - diff --git a/bsp/gd32/arm/gd32405rg/project.uvprojx b/bsp/gd32/arm/gd32405rg/project.uvprojx index 9ab03ac12e..69f501cf6d 100644 --- a/bsp/gd32/arm/gd32405rg/project.uvprojx +++ b/bsp/gd32/arm/gd32405rg/project.uvprojx @@ -338,9 +338,9 @@ 0 - RT_USING_LIBC, HXTAL_VALUE=8000000U, RT_USING_ARMLIBC, USE_STDPERIPH_DRIVER, __RTTHREAD__, __CLK_TCK=RT_TICK_PER_SECOND, GD32F405, __STDC_LIMIT_MACROS + RT_USING_LIBC, GD32F405, __RTTHREAD__, USE_STDPERIPH_DRIVER, __CLK_TCK=RT_TICK_PER_SECOND, RT_USING_ARMLIBC, __STDC_LIMIT_MACROS, HXTAL_VALUE=8000000U - packages\gd32-arm-cmsis-latest\GD32F4xx\GD\GD32F4xx\Include;packages\gd32-arm-cmsis-latest\GD32F4xx;.;..\..\..\..\components\drivers\include;..\..\..\..\components\libc\compilers\common\extension\fcntl\octal;..\..\..\..\components\drivers\smp_call;..\..\..\..\components\libc\posix\io\poll;..\..\..\..\components\net\utest;applications;..\..\..\..\components\libc\compilers\common\extension;..\..\..\..\components\libc\posix\io\epoll;..\..\..\..\components\libc\compilers\common\include;..\..\..\..\components\libc\posix\ipc;..\..\..\..\components\drivers\include;packages\gd32-arm-series-latest\GD32F4xx\GD32F4xx_standard_peripheral\Include;..\..\..\..\components\drivers\phy;..\..\..\..\components\drivers\include;..\..\..\..\components\drivers\include;board;..\..\..\..\libcpu\arm\cortex-m4;..\..\..\..\libcpu\arm\common;..\..\..\..\include;..\..\..\..\components\drivers\include;..\..\..\..\components\drivers\include;..\..\..\..\components\finsh;..\..\..\..\components\drivers\include;..\libraries\gd32_drivers;..\..\..\..\components\drivers\include;..\..\..\..\components\libc\posix\io\eventfd + applications;..\..\..\..\components\drivers\include;..\..\..\..\components\libc\compilers\common\extension\fcntl\octal;..\..\..\..\components\drivers\include;..\..\..\..\components\libc\posix\ipc;..\..\..\..\components\libc\posix\io\poll;..\..\..\..\components\libc\posix\io\eventfd;board;..\..\..\..\components\drivers\include;..\..\..\..\components\drivers\smp_call;..\..\..\..\libcpu\arm\common;..\..\..\..\components\drivers\include;..\..\..\..\libcpu\arm\cortex-m4;packages\gd32-arm-cmsis-latest\GD32F4xx\GD\GD32F4xx\Include;..\..\..\..\components\libc\posix\io\epoll;..\libraries\gd32_drivers;..\..\..\..\components\libc\compilers\common\extension;..\..\..\..\components\net\utest;..\..\..\..\components\drivers\phy;..\..\..\..\include;packages\gd32-arm-cmsis-latest\GD32F4xx;.;..\..\..\..\components\drivers\include;packages\gd32-arm-series-latest\GD32F4xx\GD32F4xx_standard_peripheral\Include;..\..\..\..\components\drivers\include;..\..\..\..\components\drivers\include;..\..\..\..\components\libc\compilers\common\include;..\..\..\..\components\finsh;..\..\..\..\components\drivers\include @@ -1406,6 +1406,11 @@ 1 ..\libraries\gd32_drivers\drv_gpio.c + + drv_hard_i2c.c + 1 + ..\libraries\gd32_drivers\drv_hard_i2c.c + drv_hwtimer.c 1 @@ -1426,16 +1431,6 @@ Finsh - - msh_parse.c - 1 - ..\..\..\..\components\finsh\msh_parse.c - - - shell.c - 1 - ..\..\..\..\components\finsh\shell.c - msh.c 1 @@ -1446,6 +1441,16 @@ 1 ..\..\..\..\components\finsh\cmd.c + + shell.c + 1 + ..\..\..\..\components\finsh\shell.c + + + msh_parse.c + 1 + ..\..\..\..\components\finsh\msh_parse.c + @@ -2297,9 +2302,9 @@ klibc - kstring.c + kerrno.c 1 - ..\..\..\..\src\klibc\kstring.c + ..\..\..\..\src\klibc\kerrno.c rt_vsscanf.c @@ -2307,9 +2312,9 @@ ..\..\..\..\src\klibc\rt_vsscanf.c - kerrno.c + kstring.c 1 - ..\..\..\..\src\klibc\kerrno.c + ..\..\..\..\src\klibc\kstring.c kstdio.c @@ -2406,11 +2411,6 @@ 1 packages\gd32-arm-series-latest\GD32F4xx\GD32F4xx_standard_peripheral\Source\gd32f4xx_usart.c - - gd32f4xx_timer.c - 1 - .\packages\gd32-arm-series-latest\GD32F4xx\GD32F4xx_standard_peripheral\Source\gd32f4xx_timer.c - diff --git a/bsp/gd32/arm/gd32405rg/rtconfig.h b/bsp/gd32/arm/gd32405rg/rtconfig.h index 598194241a..ad67872218 100644 --- a/bsp/gd32/arm/gd32405rg/rtconfig.h +++ b/bsp/gd32/arm/gd32405rg/rtconfig.h @@ -422,19 +422,9 @@ #define BSP_USING_GPIO #define BSP_USING_UART #define BSP_USING_UART0 -#define BSP_USING_PWM -#define BSP_USING_PWM0 -#define BSP_USING_PWM1 -#define BSP_USING_PWM2 -#define BSP_USING_PWM3 -#define BSP_USING_PWM4 -#define BSP_USING_PWM7 -#define BSP_USING_PWM8 -#define BSP_USING_PWM9 -#define BSP_USING_PWM10 -#define BSP_USING_PWM11 -#define BSP_USING_PWM12 -#define BSP_USING_PWM13 +#define BSP_USING_HARD_I2C +#define BSP_USING_RECEIVING_A +#define BSP_USING_HARD_I2C0 #define BSP_USING_GD_DBG /* end of On-chip Peripheral Drivers */ diff --git a/bsp/gd32/arm/libraries/gd32_drivers/drv_hard_i2c.c b/bsp/gd32/arm/libraries/gd32_drivers/drv_hard_i2c.c index 725b306fde..48dd5154e4 100644 --- a/bsp/gd32/arm/libraries/gd32_drivers/drv_hard_i2c.c +++ b/bsp/gd32/arm/libraries/gd32_drivers/drv_hard_i2c.c @@ -6,6 +6,8 @@ * Change Logs: * Date Author Notes * 2021-12-20 BruceOu the first version + * 2026-01-11 ShiHongchao Fix the I2C master receive mode B software + * flow and add support for mode A */ #include "drv_hard_i2c.h" @@ -112,8 +114,8 @@ static const struct gd32_i2c_bus gd_i2c_config[] = { RCU_I2C0, RCU_GPIOB, RCU_GPIOB, /* periph clock, scl gpio clock, sda gpio clock */ - GPIOB, GPIO_AF_4, GPIO_PIN_6, /* scl port, scl alternate, scl pin */ - GPIOB, GPIO_AF_4, GPIO_PIN_7, /* sda port, sda alternate, sda pin */ + GPIOB, GPIO_AF_4, GPIO_PIN_8, /* scl port, scl alternate, scl pin */ + GPIOB, GPIO_AF_4, GPIO_PIN_9, /* sda port, sda alternate, sda pin */ &i2c0, "hwi2c0", @@ -229,13 +231,52 @@ static void gd32_i2c_gpio_init(const struct gd32_i2c_bus *i2c) static uint8_t gd32_i2c_read(rt_uint32_t i2c_periph, rt_uint8_t *p_buffer, rt_uint16_t data_byte) { if (data_byte == 0) return 1; - /* while there is data to be read */ +#ifdef BSP_USING_RECEIVING_A + /* + In single-byte reception, disable ACK because the master needs to send + NACK after receiving the first byte,indicating no more data will be + received, then immediately send the stop condition + */ + if(data_byte == 1) + { + /* disable acknowledge */ + i2c_ack_config(i2c_periph, I2C_ACK_DISABLE); + /* send a stop condition to I2C bus */ + i2c_stop_on_bus(i2c_periph); + } +#endif + + /* while there is data to be read */ while(data_byte) { #if defined (SOC_SERIES_GD32F5xx) || defined (SOC_SERIES_GD32F4xx) if(IS_I2C_LEGACY(i2c_periph)) { +#ifdef BSP_USING_RECEIVING_A + /* + After receiving the second-to-last byte, ACK should be disabled + and STOP should be set, to ensure that NACK is sent after receiving + the last byte and the stop condition is transmitted + */ + if(2 == data_byte) + { + /* wait until BTC bit is set */ + while(!i2c_flag_get(i2c_periph, I2C_FLAG_RBNE)); + /* disable acknowledge */ + i2c_ack_config(i2c_periph, I2C_ACK_DISABLE); + /* send a stop condition to I2C bus */ + i2c_stop_on_bus(i2c_periph); + } +#elif defined(BSP_USING_RECEIVING_B) + /* + For 3-byte reception: Wait for byte transfer completion, then + disable ACK so NACK is automatically sent after receiving the + last byte + For 2-byte reception: Wait for byte transfer completion, then + send stop condition to ensure direct stop after receiving the + last byte instead of sending ACK + */ if(3 == data_byte) { /* wait until BTC bit is set */ @@ -243,14 +284,16 @@ static uint8_t gd32_i2c_read(rt_uint32_t i2c_periph, rt_uint8_t *p_buffer, rt_ui /* disable acknowledge */ i2c_ack_config(i2c_periph, I2C_ACK_DISABLE); } - - if(2 == data_byte) + else if(2 == data_byte) { /* wait until BTC bit is set */ while(!i2c_flag_get(i2c_periph, I2C_FLAG_BTC)); /* send a stop condition to I2C bus */ i2c_stop_on_bus(i2c_periph); } +#else +#error "Please select the receiving secheme." +#endif /* wait until RBNE bit is set */ if(i2c_flag_get(i2c_periph, I2C_FLAG_RBNE)) { @@ -379,11 +422,18 @@ static rt_ssize_t gd32_i2c_master_xfer(struct rt_i2c_bus_device *bus, struct rt_ { i2c_stop_on_bus(gd32_i2c->i2c_periph); } - /* enable acknowledge */ + /* enable acknowledge */ i2c_ack_config(gd32_i2c->i2c_periph, I2C_ACK_ENABLE); - /* i2c master sends start signal only when the bus is idle */ + /* i2c master sends start signal only when the bus is idle */ while(i2c_flag_get(gd32_i2c->i2c_periph, I2C_FLAG_I2CBSY)); - /* send the start signal */ +#ifdef BSP_USING_RECEIVING_B + /* */ + if(msg->len == 2) + { + i2c_ackpos_config(gd32_i2c->i2c_periph, I2C_ACKPOS_NEXT); + } +#endif + /* send the start signal */ i2c_start_on_bus(gd32_i2c->i2c_periph); /* i2c master sends START signal successfully */ while(!i2c_flag_get(gd32_i2c->i2c_periph, I2C_FLAG_SBSEND)); @@ -391,14 +441,26 @@ static rt_ssize_t gd32_i2c_master_xfer(struct rt_i2c_bus_device *bus, struct rt_ i2c_master_addressing(gd32_i2c->i2c_periph, msg->addr, I2C_RECEIVER); while(!i2c_flag_get(gd32_i2c->i2c_periph, I2C_FLAG_ADDSEND)); - /* address flag set means i2c slave sends ACK */ +#ifdef BSP_USING_RECEIVING_B + if(msg->len <= 2) + { + i2c_ack_config(gd32_i2c->i2c_periph, I2C_ACK_DISABLE); + } +#endif + /* address flag set means i2c slave sends ACK */ i2c_flag_clear(gd32_i2c->i2c_periph, I2C_FLAG_ADDSEND); +#ifdef BSP_USING_RECEIVING_B + if(msg->len == 1) + { + i2c_stop_on_bus(gd32_i2c->i2c_periph); + } +#endif }else { - /* configure slave address */ + /* configure slave address */ while(i2c_flag_get(gd32_i2c->i2c_periph, I2C_FLAG_I2CBSY)); - //i2c_transfer_byte_number_config(gd32_i2c->i2c_periph, w_total_byte); - /* send a start condition to I2C bus */ + //i2c_transfer_byte_number_config(gd32_i2c->i2c_periph, w_total_byte); + /* send a start condition to I2C bus */ i2c_start_on_bus(gd32_i2c->i2c_periph); while(!i2c_flag_get(gd32_i2c->i2c_periph, I2C_FLAG_SBSEND));