mirror of
https://github.com/RT-Thread/rt-thread.git
synced 2026-09-27 23:38:32 +08:00
[bsp/gd32] update gd32 i2c driver #10641
ToolsCI / Tools (push) Has been cancelled
AutoTestCI / components/cpp11 (push) Has been cancelled
AutoTestCI / kernel/atomic (push) Has been cancelled
AutoTestCI / kernel/atomic/riscv64 (push) Has been cancelled
AutoTestCI / kernel/atomic_c11 (push) Has been cancelled
AutoTestCI / kernel/atomic_c11/riscv64 (push) Has been cancelled
AutoTestCI / kernel/device (push) Has been cancelled
AutoTestCI / kernel/ipc (push) Has been cancelled
AutoTestCI / kernel/irq (push) Has been cancelled
AutoTestCI / kernel/mem (push) Has been cancelled
AutoTestCI / kernel/mem/riscv64 (push) Has been cancelled
AutoTestCI / kernel/thread (push) Has been cancelled
AutoTestCI / kernel/timer (push) Has been cancelled
AutoTestCI / rtsmart/aarch64 (push) Has been cancelled
AutoTestCI / rtsmart/arm (push) Has been cancelled
AutoTestCI / rtsmart/riscv64 (push) Has been cancelled
AutoTestCI / components/utest (push) Has been cancelled
RT-Thread BSP Static Build Check / 🔍 Summary of Git Diff Changes (push) Has been cancelled
RT-Thread BSP Static Build Check / ${{ matrix.legs.RTT_BSP }} (push) Has been cancelled
RT-Thread BSP Static Build Check / collect-artifacts (push) Has been cancelled
pkgs_test / change (push) Has been cancelled
utest_auto_run / AARCH64-rtsmart :default.cfg (push) Has been cancelled
utest_auto_run / A9-rtsmart :default.cfg (push) Has been cancelled
utest_auto_run / RISCV-rtsmart :default.cfg (push) Has been cancelled
utest_auto_run / XUANTIE-rtsmart :default.cfg (push) Has been cancelled
utest_auto_run / AARCH64 :default.cfg (push) Has been cancelled
utest_auto_run / A9 :default.cfg (push) Has been cancelled
utest_auto_run / A9-smp :default.cfg (push) Has been cancelled
utest_auto_run / RISCV :default.cfg (push) Has been cancelled
ToolsCI / Tools (push) Has been cancelled
AutoTestCI / components/cpp11 (push) Has been cancelled
AutoTestCI / kernel/atomic (push) Has been cancelled
AutoTestCI / kernel/atomic/riscv64 (push) Has been cancelled
AutoTestCI / kernel/atomic_c11 (push) Has been cancelled
AutoTestCI / kernel/atomic_c11/riscv64 (push) Has been cancelled
AutoTestCI / kernel/device (push) Has been cancelled
AutoTestCI / kernel/ipc (push) Has been cancelled
AutoTestCI / kernel/irq (push) Has been cancelled
AutoTestCI / kernel/mem (push) Has been cancelled
AutoTestCI / kernel/mem/riscv64 (push) Has been cancelled
AutoTestCI / kernel/thread (push) Has been cancelled
AutoTestCI / kernel/timer (push) Has been cancelled
AutoTestCI / rtsmart/aarch64 (push) Has been cancelled
AutoTestCI / rtsmart/arm (push) Has been cancelled
AutoTestCI / rtsmart/riscv64 (push) Has been cancelled
AutoTestCI / components/utest (push) Has been cancelled
RT-Thread BSP Static Build Check / 🔍 Summary of Git Diff Changes (push) Has been cancelled
RT-Thread BSP Static Build Check / ${{ matrix.legs.RTT_BSP }} (push) Has been cancelled
RT-Thread BSP Static Build Check / collect-artifacts (push) Has been cancelled
pkgs_test / change (push) Has been cancelled
utest_auto_run / AARCH64-rtsmart :default.cfg (push) Has been cancelled
utest_auto_run / A9-rtsmart :default.cfg (push) Has been cancelled
utest_auto_run / RISCV-rtsmart :default.cfg (push) Has been cancelled
utest_auto_run / XUANTIE-rtsmart :default.cfg (push) Has been cancelled
utest_auto_run / AARCH64 :default.cfg (push) Has been cancelled
utest_auto_run / A9 :default.cfg (push) Has been cancelled
utest_auto_run / A9-smp :default.cfg (push) Has been cancelled
utest_auto_run / RISCV :default.cfg (push) Has been cancelled
This commit is contained in:
@@ -104,11 +104,9 @@
|
||||
#
|
||||
# CONFIG_RT_KLIBC_USING_USER_STRNLEN is not set
|
||||
# end of rt_strnlen options
|
||||
|
||||
# CONFIG_RT_UTEST_TC_USING_KLIBC is not set
|
||||
# end of klibc options
|
||||
|
||||
CONFIG_RT_NAME_MAX=8
|
||||
CONFIG_RT_NAME_MAX=16
|
||||
# CONFIG_RT_USING_ARCH_DATA_TYPE is not set
|
||||
# CONFIG_RT_USING_NANO is not set
|
||||
# CONFIG_RT_USING_SMART is not set
|
||||
@@ -1443,6 +1441,7 @@ CONFIG_BSP_USING_UART0=y
|
||||
# CONFIG_BSP_USING_UART1 is not set
|
||||
# CONFIG_BSP_USING_UART2 is not set
|
||||
# CONFIG_BSP_USING_UART3 is not set
|
||||
# CONFIG_BSP_USING_I2C is not set
|
||||
# end of On-chip Peripheral Drivers
|
||||
|
||||
#
|
||||
|
||||
@@ -69,8 +69,37 @@ menu "On-chip Peripheral Drivers"
|
||||
|
||||
endif
|
||||
|
||||
source "$(BSP_DIR)/../libraries/gd32_drivers/Kconfig"
|
||||
menuconfig BSP_USING_I2C
|
||||
bool "Enable I2C"
|
||||
default n
|
||||
if BSP_USING_I2C
|
||||
menuconfig BSP_USING_HARD_I2C
|
||||
bool "Enable Hardware I2C"
|
||||
default n
|
||||
select RT_USING_I2C
|
||||
if BSP_USING_HARD_I2C
|
||||
config BSP_USING_HARD_I2C0
|
||||
bool "Enable I2C0 Hardware BUS"
|
||||
default n
|
||||
config BSP_USING_HARD_I2C1
|
||||
bool "Enable I2C1 Hardware BUS"
|
||||
default n
|
||||
config BSP_USING_HARD_I2C2
|
||||
bool "Enable I2C2 Hardware BUS"
|
||||
default n
|
||||
config BSP_USING_HARD_I2C3
|
||||
bool "Enable I2C3 Hardware BUS"
|
||||
default n
|
||||
config BSP_USING_HARD_I2C4
|
||||
bool "Enable I2C4 Hardware BUS"
|
||||
default n
|
||||
config BSP_USING_HARD_I2C5
|
||||
bool "Enable I2C5 Hardware BUS"
|
||||
default n
|
||||
endif
|
||||
endif
|
||||
|
||||
source "$(BSP_DIR)/../libraries/gd32_drivers/Kconfig"
|
||||
endmenu
|
||||
|
||||
menu "Board extended module Drivers"
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -138,7 +138,7 @@
|
||||
</Flash1>
|
||||
<bUseTDR>1</bUseTDR>
|
||||
<Flash2>BIN\UL2CM3.DLL</Flash2>
|
||||
<Flash3>"" ()</Flash3>
|
||||
<Flash3></Flash3>
|
||||
<Flash4></Flash4>
|
||||
<pFcarmOut></pFcarmOut>
|
||||
<pFcarmGrp></pFcarmGrp>
|
||||
@@ -338,9 +338,9 @@
|
||||
<v6Rtti>0</v6Rtti>
|
||||
<VariousControls>
|
||||
<MiscControls></MiscControls>
|
||||
<Define>GD32H7XX, RT_USING_ARMLIBC, USE_STDPERIPH_DRIVER, __CLK_TCK=RT_TICK_PER_SECOND, RT_USING_LIBC, __STDC_LIMIT_MACROS, __RTTHREAD__</Define>
|
||||
<Define>GD32H7XX, RT_USING_ARMLIBC, __CLK_TCK=RT_TICK_PER_SECOND, __RTTHREAD__, __STDC_LIMIT_MACROS, RT_USING_LIBC, USE_STDPERIPH_DRIVER</Define>
|
||||
<Undefine></Undefine>
|
||||
<IncludePath>packages\gd32-arm-cmsis-latest\GD32H7xx\GD\GD32H7xx\Include;..\..\..\..\libcpu\arm\cortex-m7;..\..\..\..\libcpu\arm\common;..\..\..\..\components\drivers\include;..\..\..\..\components\libc\compilers\common\extension;.;..\..\..\..\components\libc\compilers\common\include;..\..\..\..\components\drivers\smp_call;packages\gd32-arm-cmsis-latest\GD32H7xx;..\..\..\..\components\drivers\include;..\..\..\..\components\finsh;..\..\..\..\components\dfs\dfs_v1\include;..\..\..\..\components\drivers\include;..\..\..\..\components\drivers\phy;..\..\..\..\include;..\..\..\..\components\dfs\dfs_v1\filesystems\devfs;board;..\libraries\gd32_drivers;..\..\..\..\components\libc\posix\io\eventfd;..\..\..\..\components\libc\posix\ipc;..\..\..\..\components\drivers\include;..\..\..\..\components\drivers\include;applications;..\..\..\..\components\libc\posix\io\poll;packages\gd32-arm-series-latest\GD32H7xx\GD32H7xx_standard_peripheral\Include;..\..\..\..\components\libc\compilers\common\extension\fcntl\octal;..\..\..\..\components\libc\posix\io\epoll;..\..\..\..\components\drivers\include</IncludePath>
|
||||
<IncludePath>..\..\..\..\components\drivers\include;..\..\..\..\libcpu\arm\cortex-m7;packages\gd32-arm-cmsis-latest\GD32H7xx;packages\gd32-arm-cmsis-latest\GD32H7xx\GD\GD32H7xx\Include;..\..\..\..\components\drivers\include;..\..\..\..\components\libc\compilers\common\extension;..\..\..\..\components\finsh;..\..\..\..\components\drivers\smp_call;..\..\..\..\components\libc\posix\io\poll;..\..\..\..\components\libc\posix\io\epoll;..\..\..\..\components\libc\compilers\common\include;..\..\..\..\libcpu\arm\common;..\..\..\..\components\libc\posix\ipc;applications;..\..\..\..\components\libc\posix\io\eventfd;..\..\..\..\components\drivers\include;packages\at24cxx-latest;..\libraries\gd32_drivers;.;..\..\..\..\components\drivers\phy;..\..\..\..\components\drivers\include;board;..\..\..\..\components\drivers\include;..\..\..\..\components\libc\compilers\common\extension\fcntl\octal;..\..\..\..\components\drivers\include;..\..\..\..\components\dfs\dfs_v1\include;..\..\..\..\components\dfs\dfs_v1\filesystems\devfs;..\..\..\..\include;packages\gd32-arm-series-latest\GD32H7xx\GD32H7xx_standard_peripheral\Include</IncludePath>
|
||||
</VariousControls>
|
||||
</Cads>
|
||||
<Aads>
|
||||
@@ -391,6 +391,16 @@
|
||||
</File>
|
||||
</Files>
|
||||
</Group>
|
||||
<Group>
|
||||
<GroupName>at24cxx</GroupName>
|
||||
<Files>
|
||||
<File>
|
||||
<FileName>at24cxx.c</FileName>
|
||||
<FileType>1</FileType>
|
||||
<FilePath>packages\at24cxx-latest\at24cxx.c</FilePath>
|
||||
</File>
|
||||
</Files>
|
||||
</Group>
|
||||
<Group>
|
||||
<GroupName>Compiler</GroupName>
|
||||
<Files>
|
||||
@@ -663,6 +673,62 @@
|
||||
</FileArmAds>
|
||||
</FileOption>
|
||||
</File>
|
||||
<File>
|
||||
<FileName>dev_soft_i2c.c</FileName>
|
||||
<FileType>1</FileType>
|
||||
<FilePath>..\..\..\..\components\drivers\i2c\dev_soft_i2c.c</FilePath>
|
||||
<FileOption>
|
||||
<CommonProperty>
|
||||
<UseCPPCompiler>2</UseCPPCompiler>
|
||||
<RVCTCodeConst>0</RVCTCodeConst>
|
||||
<RVCTZI>0</RVCTZI>
|
||||
<RVCTOtherData>0</RVCTOtherData>
|
||||
<ModuleSelection>0</ModuleSelection>
|
||||
<IncludeInBuild>1</IncludeInBuild>
|
||||
<AlwaysBuild>0</AlwaysBuild>
|
||||
<GenerateAssemblyFile>0</GenerateAssemblyFile>
|
||||
<AssembleAssemblyFile>0</AssembleAssemblyFile>
|
||||
<PublicsOnly>0</PublicsOnly>
|
||||
<StopOnExitCode>3</StopOnExitCode>
|
||||
<CustomArgument></CustomArgument>
|
||||
<IncludeLibraryModules></IncludeLibraryModules>
|
||||
<ComprImg>1</ComprImg>
|
||||
</CommonProperty>
|
||||
<FileArmAds>
|
||||
<Cads>
|
||||
<interw>2</interw>
|
||||
<Optim>0</Optim>
|
||||
<oTime>2</oTime>
|
||||
<SplitLS>2</SplitLS>
|
||||
<OneElfS>2</OneElfS>
|
||||
<Strict>2</Strict>
|
||||
<EnumInt>2</EnumInt>
|
||||
<PlainCh>2</PlainCh>
|
||||
<Ropi>2</Ropi>
|
||||
<Rwpi>2</Rwpi>
|
||||
<wLevel>0</wLevel>
|
||||
<uThumb>2</uThumb>
|
||||
<uSurpInc>2</uSurpInc>
|
||||
<uC99>2</uC99>
|
||||
<uGnu>2</uGnu>
|
||||
<useXO>2</useXO>
|
||||
<v6Lang>0</v6Lang>
|
||||
<v6LangP>0</v6LangP>
|
||||
<vShortEn>2</vShortEn>
|
||||
<vShortWch>2</vShortWch>
|
||||
<v6Lto>2</v6Lto>
|
||||
<v6WtE>2</v6WtE>
|
||||
<v6Rtti>2</v6Rtti>
|
||||
<VariousControls>
|
||||
<MiscControls> </MiscControls>
|
||||
<Define>__RT_IPC_SOURCE__</Define>
|
||||
<Undefine> </Undefine>
|
||||
<IncludePath></IncludePath>
|
||||
</VariousControls>
|
||||
</Cads>
|
||||
</FileArmAds>
|
||||
</FileOption>
|
||||
</File>
|
||||
<File>
|
||||
<FileName>completion_comm.c</FileName>
|
||||
<FileType>1</FileType>
|
||||
@@ -1589,6 +1655,16 @@
|
||||
<Group>
|
||||
<GroupName>Finsh</GroupName>
|
||||
<Files>
|
||||
<File>
|
||||
<FileName>msh_file.c</FileName>
|
||||
<FileType>1</FileType>
|
||||
<FilePath>..\..\..\..\components\finsh\msh_file.c</FilePath>
|
||||
</File>
|
||||
<File>
|
||||
<FileName>shell.c</FileName>
|
||||
<FileType>1</FileType>
|
||||
<FilePath>..\..\..\..\components\finsh\shell.c</FilePath>
|
||||
</File>
|
||||
<File>
|
||||
<FileName>cmd.c</FileName>
|
||||
<FileType>1</FileType>
|
||||
@@ -1599,16 +1675,6 @@
|
||||
<FileType>1</FileType>
|
||||
<FilePath>..\..\..\..\components\finsh\msh_parse.c</FilePath>
|
||||
</File>
|
||||
<File>
|
||||
<FileName>shell.c</FileName>
|
||||
<FileType>1</FileType>
|
||||
<FilePath>..\..\..\..\components\finsh\shell.c</FilePath>
|
||||
</File>
|
||||
<File>
|
||||
<FileName>msh_file.c</FileName>
|
||||
<FileType>1</FileType>
|
||||
<FilePath>..\..\..\..\components\finsh\msh_file.c</FilePath>
|
||||
</File>
|
||||
<File>
|
||||
<FileName>msh.c</FileName>
|
||||
<FileType>1</FileType>
|
||||
@@ -2464,21 +2530,6 @@
|
||||
<Group>
|
||||
<GroupName>klibc</GroupName>
|
||||
<Files>
|
||||
<File>
|
||||
<FileName>kstdio.c</FileName>
|
||||
<FileType>1</FileType>
|
||||
<FilePath>..\..\..\..\src\klibc\kstdio.c</FilePath>
|
||||
</File>
|
||||
<File>
|
||||
<FileName>rt_vsscanf.c</FileName>
|
||||
<FileType>1</FileType>
|
||||
<FilePath>..\..\..\..\src\klibc\rt_vsscanf.c</FilePath>
|
||||
</File>
|
||||
<File>
|
||||
<FileName>rt_vsnprintf_tiny.c</FileName>
|
||||
<FileType>1</FileType>
|
||||
<FilePath>..\..\..\..\src\klibc\rt_vsnprintf_tiny.c</FilePath>
|
||||
</File>
|
||||
<File>
|
||||
<FileName>kerrno.c</FileName>
|
||||
<FileType>1</FileType>
|
||||
@@ -2489,6 +2540,21 @@
|
||||
<FileType>1</FileType>
|
||||
<FilePath>..\..\..\..\src\klibc\kstring.c</FilePath>
|
||||
</File>
|
||||
<File>
|
||||
<FileName>kstdio.c</FileName>
|
||||
<FileType>1</FileType>
|
||||
<FilePath>..\..\..\..\src\klibc\kstdio.c</FilePath>
|
||||
</File>
|
||||
<File>
|
||||
<FileName>rt_vsnprintf_tiny.c</FileName>
|
||||
<FileType>1</FileType>
|
||||
<FilePath>..\..\..\..\src\klibc\rt_vsnprintf_tiny.c</FilePath>
|
||||
</File>
|
||||
<File>
|
||||
<FileName>rt_vsscanf.c</FileName>
|
||||
<FileType>1</FileType>
|
||||
<FilePath>..\..\..\..\src\klibc\rt_vsscanf.c</FilePath>
|
||||
</File>
|
||||
</Files>
|
||||
</Group>
|
||||
<Group>
|
||||
@@ -2539,6 +2605,11 @@
|
||||
<FileType>1</FileType>
|
||||
<FilePath>packages\gd32-arm-cmsis-latest\GD32H7xx\GD\GD32H7xx\Source\system_gd32h7xx.c</FilePath>
|
||||
</File>
|
||||
<File>
|
||||
<FileName>gd32h7xx_dma.c</FileName>
|
||||
<FileType>1</FileType>
|
||||
<FilePath>packages\gd32-arm-series-latest\GD32H7xx\GD32H7xx_standard_peripheral\Source\gd32h7xx_dma.c</FilePath>
|
||||
</File>
|
||||
<File>
|
||||
<FileName>gd32h7xx_exti.c</FileName>
|
||||
<FileType>1</FileType>
|
||||
|
||||
@@ -61,7 +61,7 @@
|
||||
|
||||
/* end of rt_strnlen options */
|
||||
/* end of klibc options */
|
||||
#define RT_NAME_MAX 8
|
||||
#define RT_NAME_MAX 16
|
||||
#define RT_CPUS_NR 1
|
||||
#define RT_ALIGN_SIZE 8
|
||||
#define RT_THREAD_PRIORITY_32
|
||||
|
||||
@@ -26,8 +26,8 @@ if GetDepend(['RT_USING_I2C', 'RT_USING_I2C_BITOPS']):
|
||||
|
||||
# add i2c hard drivers.
|
||||
if GetDepend(['RT_USING_I2C']):
|
||||
if GetDepend('BSP_USING_HW_I2C0') or GetDepend('BSP_USING_HW_I2C1'):
|
||||
src += ['drv_hw_i2c.c']
|
||||
if GetDepend('BSP_USING_HARD_I2C0') or GetDepend('BSP_USING_HARD_I2C1') or GetDepend('BSP_USING_HARD_I2C2') or GetDepend('BSP_USING_HARD_I2C3') or GetDepend('BSP_USING_HARD_I2C4') or GetDepend('BSP_USING_HARD_I2C5'):
|
||||
src += ['drv_hard_i2c.c']
|
||||
|
||||
# add spi drivers.
|
||||
if GetDepend('RT_USING_SPI'):
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2025, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2021-12-20 BruceOu the first version
|
||||
*/
|
||||
|
||||
#ifndef __DRV_I2C__
|
||||
#define __DRV_I2C__
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rthw.h>
|
||||
#include <rtdevice.h>
|
||||
#include <board.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* GD32 i2c driver */
|
||||
struct gd32_i2c_bus
|
||||
{
|
||||
uint32_t i2c_periph; /* Todo: 3bits */
|
||||
|
||||
rcu_periph_enum per_clk; /* Todo: 5bits */
|
||||
rcu_periph_enum scl_gpio_clk; /* Todo: 5bits */
|
||||
rcu_periph_enum sda_gpio_clk; /* Todo: 5bits */
|
||||
|
||||
uint32_t scl_port; /* Todo: 4bits */
|
||||
uint16_t scl_af; /* Todo: 4bits */
|
||||
uint16_t scl_pin; /* Todo: 4bits */
|
||||
|
||||
uint32_t sda_port; /* Todo: 4bits */
|
||||
uint16_t sda_af; /* Todo: 4bits */
|
||||
uint16_t sda_pin; /* Todo: 4bits */
|
||||
|
||||
struct rt_i2c_bus_device *i2c_bus;
|
||||
char *device_name;
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __DRV_I2C__ */
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,68 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2025, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __DRV_HW_I2C_H__
|
||||
#define __DRV_HW_I2C_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rthw.h>
|
||||
#include <rtdevice.h>
|
||||
#include <board.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* I2C hardware configuration */
|
||||
struct gd32_i2c_config
|
||||
{
|
||||
rt_uint32_t i2c_periph; /* I2C peripheral base address */
|
||||
rcu_periph_enum periph_clk; /* I2C peripheral clock */
|
||||
|
||||
rcu_periph_enum scl_clk; /* SCL pin clock */
|
||||
rt_uint32_t scl_port; /* SCL pin port */
|
||||
rt_uint32_t scl_pin; /* SCL pin */
|
||||
rt_uint32_t scl_af; /* SCL pin alternate function */
|
||||
|
||||
rcu_periph_enum sda_clk; /* SDA pin clock */
|
||||
rt_uint32_t sda_port; /* SDA pin port */
|
||||
rt_uint32_t sda_pin; /* SDA pin */
|
||||
rt_uint32_t sda_af; /* SDA pin alternate function */
|
||||
|
||||
IRQn_Type ev_irq_type; /* Event IRQn */
|
||||
IRQn_Type er_irq_type; /* Error IRQn */
|
||||
|
||||
rt_uint32_t i2c_clock_hz; /* I2C clock speed in Hz, e.g., 100000 for 100kHz */
|
||||
|
||||
const char *device_name; /* Device name */
|
||||
};
|
||||
|
||||
/* I2C runtime context */
|
||||
struct gd32_i2c
|
||||
{
|
||||
struct rt_i2c_bus_device parent;
|
||||
const struct gd32_i2c_config *config;
|
||||
|
||||
struct rt_mutex bus_mutex;
|
||||
struct rt_completion sync_sem;
|
||||
uint32_t dev_config;
|
||||
uint16_t addr1;
|
||||
uint16_t addr2;
|
||||
uint32_t xfer_len;
|
||||
struct rt_i2c_msg *current;
|
||||
uint8_t errs;
|
||||
rt_bool_t is_restart;
|
||||
};
|
||||
|
||||
int rt_hw_i2c_init(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __DRV_HW_I2C_H__ */
|
||||
|
||||
@@ -1,212 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2022, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2021-12-20 BruceOu the first version
|
||||
*/
|
||||
#include "drv_soft_i2c.h"
|
||||
|
||||
#ifdef RT_USING_I2C
|
||||
|
||||
#define LOG_TAG "drv.i2c"
|
||||
#include <rtdbg.h>
|
||||
|
||||
#if !defined(BSP_USING_I2C0) && !defined(BSP_USING_I2C1) && !defined(BSP_USING_I2C2) && !defined(BSP_USING_I2C3)
|
||||
#error "Please define at least one BSP_USING_I2Cx"
|
||||
/* this driver can be disabled at menuconfig → RT-Thread Components → Device Drivers */
|
||||
#endif
|
||||
|
||||
static const struct gd32_soft_i2c_config soft_i2c_config[] =
|
||||
{
|
||||
#ifdef BSP_USING_I2C0
|
||||
I2C0_BUS_CONFIG,
|
||||
#endif
|
||||
#ifdef BSP_USING_I2C1
|
||||
I2C1_BUS_CONFIG,
|
||||
#endif
|
||||
#ifdef BSP_USING_I2C2
|
||||
I2C2_BUS_CONFIG,
|
||||
#endif
|
||||
#ifdef BSP_USING_I2C3
|
||||
I2C3_BUS_CONFIG,
|
||||
#endif
|
||||
};
|
||||
|
||||
static struct gd32_i2c i2c_obj[sizeof(soft_i2c_config) / sizeof(soft_i2c_config[0])];
|
||||
|
||||
/**
|
||||
* @brief This function initializes the i2c pin.
|
||||
* @param i2c
|
||||
* @retval None
|
||||
*/
|
||||
static void gd32_i2c_gpio_init(struct gd32_i2c *i2c)
|
||||
{
|
||||
struct gd32_soft_i2c_config* cfg = (struct gd32_soft_i2c_config*)i2c->ops.data;
|
||||
|
||||
rt_pin_mode(cfg->scl, PIN_MODE_OUTPUT_OD);
|
||||
rt_pin_mode(cfg->sda, PIN_MODE_OUTPUT_OD);
|
||||
|
||||
rt_pin_write(cfg->scl, PIN_HIGH);
|
||||
rt_pin_write(cfg->sda, PIN_HIGH);
|
||||
}
|
||||
|
||||
static void gd32_i2c_pin_init(void)
|
||||
{
|
||||
rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct gd32_i2c);
|
||||
|
||||
for(rt_size_t i = 0; i < obj_num; i++)
|
||||
{
|
||||
gd32_i2c_gpio_init(&i2c_obj[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function sets the sda pin.
|
||||
* @param data, state
|
||||
* @retval None
|
||||
*/
|
||||
static void gd32_set_sda(void *data, rt_int32_t state)
|
||||
{
|
||||
struct gd32_soft_i2c_config* cfg = (struct gd32_soft_i2c_config*)data;
|
||||
if (state)
|
||||
{
|
||||
rt_pin_write(cfg->sda, PIN_HIGH);
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_pin_write(cfg->sda, PIN_LOW);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function sets the scl pin.
|
||||
* @param data, state
|
||||
* @retval None
|
||||
*/
|
||||
static void gd32_set_scl(void *data, rt_int32_t state)
|
||||
{
|
||||
struct gd32_soft_i2c_config* cfg = (struct gd32_soft_i2c_config*)data;
|
||||
if (state)
|
||||
{
|
||||
rt_pin_write(cfg->scl, PIN_HIGH);
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_pin_write(cfg->scl, PIN_LOW);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function gets the sda pin state.
|
||||
* @param data
|
||||
* @retval None
|
||||
*/
|
||||
static rt_int32_t gd32_get_sda(void *data)
|
||||
{
|
||||
struct gd32_soft_i2c_config* cfg = (struct gd32_soft_i2c_config*)data;
|
||||
return rt_pin_read(cfg->sda);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief This function gets the scl pin state.
|
||||
* @param data
|
||||
* @retval None
|
||||
*/
|
||||
static rt_int32_t gd32_get_scl(void *data)
|
||||
{
|
||||
struct gd32_soft_i2c_config* cfg = (struct gd32_soft_i2c_config*)data;
|
||||
return rt_pin_read(cfg->scl);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief The time delay function.
|
||||
* @param us
|
||||
* @retval None
|
||||
*/
|
||||
static void gd32_udelay(rt_uint32_t us)
|
||||
{
|
||||
int i = ( rcu_clock_freq_get(CK_SYS) / 4000000 * us);
|
||||
while(i)
|
||||
{
|
||||
i--;
|
||||
}
|
||||
}
|
||||
|
||||
static const struct rt_i2c_bit_ops gd32_bit_ops_default =
|
||||
{
|
||||
.data = RT_NULL,
|
||||
.pin_init = gd32_i2c_pin_init,
|
||||
.set_sda = gd32_set_sda,
|
||||
.set_scl = gd32_set_scl,
|
||||
.get_sda = gd32_get_sda,
|
||||
.get_scl = gd32_get_scl,
|
||||
.udelay = gd32_udelay,
|
||||
.delay_us = 1,
|
||||
.timeout = 100,
|
||||
.i2c_pin_init_flag = RT_FALSE
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief if i2c is locked, this function will unlock it
|
||||
* @param cfg
|
||||
* @retval RT_EOK indicates successful unlock.
|
||||
*/
|
||||
static rt_err_t gd32_i2c_bus_unlock(const struct gd32_soft_i2c_config *cfg)
|
||||
{
|
||||
rt_int32_t i = 0;
|
||||
|
||||
if (PIN_LOW == rt_pin_read(cfg->sda))
|
||||
{
|
||||
while (i++ < 9)
|
||||
{
|
||||
rt_pin_write(cfg->scl, PIN_HIGH);
|
||||
gd32_udelay(100);
|
||||
rt_pin_write(cfg->scl, PIN_LOW);
|
||||
gd32_udelay(100);
|
||||
}
|
||||
}
|
||||
if (PIN_LOW == rt_pin_read(cfg->sda))
|
||||
{
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief I2C initialization function
|
||||
* @param None
|
||||
* @retval RT_EOK indicates successful initialization.
|
||||
*/
|
||||
int rt_hw_i2c_init(void)
|
||||
{
|
||||
rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct gd32_i2c);
|
||||
rt_err_t result;
|
||||
|
||||
for (rt_size_t i = 0; i < obj_num; i++)
|
||||
{
|
||||
i2c_obj[i].ops = gd32_bit_ops_default;
|
||||
i2c_obj[i].ops.data = (void*)&soft_i2c_config[i];
|
||||
i2c_obj[i].i2c_bus.priv = &i2c_obj[i].ops;
|
||||
|
||||
result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c_bus, soft_i2c_config[i].bus_name);
|
||||
|
||||
RT_ASSERT(result == RT_EOK);
|
||||
|
||||
gd32_i2c_bus_unlock(&soft_i2c_config[i]);
|
||||
|
||||
LOG_D("software simulation %s init done, pin scl: %d, pin sda %d",
|
||||
soft_i2c_config[i].bus_name,
|
||||
soft_i2c_config[i].scl,
|
||||
soft_i2c_config[i].sda);
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
INIT_BOARD_EXPORT(rt_hw_i2c_init);
|
||||
|
||||
#endif /* RT_USING_I2C */
|
||||
@@ -1,78 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2022, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2021-12-20 BruceOu the first version
|
||||
*/
|
||||
|
||||
#ifndef __DRV_I2C__
|
||||
#define __DRV_I2C__
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rthw.h>
|
||||
#include <rtdevice.h>
|
||||
#include <board.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* gd32 config class */
|
||||
struct gd32_soft_i2c_config
|
||||
{
|
||||
rt_uint8_t scl;
|
||||
rt_uint8_t sda;
|
||||
const char *bus_name;
|
||||
};
|
||||
|
||||
/* gd32 i2c dirver class */
|
||||
struct gd32_i2c
|
||||
{
|
||||
struct rt_i2c_bit_ops ops;
|
||||
struct rt_i2c_bus_device i2c_bus;
|
||||
};
|
||||
|
||||
#ifdef BSP_USING_I2C0
|
||||
#define I2C0_BUS_CONFIG \
|
||||
{ \
|
||||
.scl = BSP_I2C0_SCL_PIN, \
|
||||
.sda = BSP_I2C0_SDA_PIN, \
|
||||
.bus_name = "i2c0", \
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef BSP_USING_I2C1
|
||||
#define I2C1_BUS_CONFIG \
|
||||
{ \
|
||||
.scl = BSP_I2C1_SCL_PIN, \
|
||||
.sda = BSP_I2C1_SDA_PIN, \
|
||||
.bus_name = "i2c1", \
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef BSP_USING_I2C2
|
||||
#define I2C2_BUS_CONFIG \
|
||||
{ \
|
||||
.scl = BSP_I2C2_SCL_PIN, \
|
||||
.sda = BSP_I2C2_SDA_PIN, \
|
||||
.bus_name = "i2c2", \
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef BSP_USING_I2C3
|
||||
#define I2C3_BUS_CONFIG \
|
||||
{ \
|
||||
.scl = BSP_I2C3_SCL_PIN, \
|
||||
.sda = BSP_I2C3_SDA_PIN, \
|
||||
.bus_name = "i2c3", \
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __DRV_I2C__ */
|
||||
Reference in New Issue
Block a user