diff --git a/cpu/native/Makefile.dep b/cpu/native/Makefile.dep index 2921b4c70b..217f00ada2 100644 --- a/cpu/native/Makefile.dep +++ b/cpu/native/Makefile.dep @@ -4,6 +4,9 @@ ifeq ($(OS),Linux) USEMODULE += periph_gpio_linux endif endif + ifneq (,$(filter periph_i2c,$(USEMODULE))) + USEMODULE += periph_i2c_mock + endif ifneq (,$(filter periph_spi,$(USEMODULE))) USEMODULE += periph_spidev_linux endif @@ -11,6 +14,9 @@ else ifneq (,$(filter periph_gpio,$(USEMODULE))) USEMODULE += periph_gpio_mock endif + ifneq (,$(filter periph_i2c,$(USEMODULE))) + USEMODULE += periph_i2c_mock + endif endif ifneq (,$(filter stdio_default,$(USEMODULE))) diff --git a/cpu/native/Makefile.features b/cpu/native/Makefile.features index 25a8cc10fd..a83970969f 100644 --- a/cpu/native/Makefile.features +++ b/cpu/native/Makefile.features @@ -14,6 +14,7 @@ FEATURES_PROVIDED += periph_eeprom FEATURES_PROVIDED += periph_flashpage FEATURES_PROVIDED += periph_flashpage_pagewise FEATURES_PROVIDED += periph_hwrng +FEATURES_PROVIDED += periph_i2c FEATURES_PROVIDED += periph_pm FEATURES_PROVIDED += periph_pwm FEATURES_PROVIDED += periph_timer_periodic diff --git a/cpu/native/include/periph_conf.h b/cpu/native/include/periph_conf.h index d9928d4333..4a857c16ae 100644 --- a/cpu/native/include/periph_conf.h +++ b/cpu/native/include/periph_conf.h @@ -71,7 +71,21 @@ extern "C" { # define QDEC_NUMOF (8U) #endif -/* MARK: - SPI configuration (Linux host only) */ +/* MARK: - I2C configuration (mock implementation) */ +/** + * @name I2C configuration (mock implementation) + * @{ + */ +#if !defined(I2C_NUMOF) || defined(DOXYGEN) +/** + * @brief Amount of I2C devices + * + * Can be overridden during compile time with `CFLAGS+=-DI2C_NUMOF=n`. + */ +# define I2C_NUMOF 1 +#endif +/** @} */ + /** * @name SPI configuration (Linux host only) * @{ diff --git a/cpu/native/include/periph_cpu.h b/cpu/native/include/periph_cpu.h index 0210be298a..737800d0cb 100644 --- a/cpu/native/include/periph_cpu.h +++ b/cpu/native/include/periph_cpu.h @@ -178,6 +178,27 @@ typedef gpio_mock_t* gpio_t; #define PROVIDES_PM_SET_LOWEST /** @} */ +/** + * @name I2C Configuration + * + * The common I2C implementation is requested to provide the default implementations of the + * `i2c_{read,write}_{reg,regs}` functions. + */ + +#define PERIPH_I2C_NEED_READ_REG +#define PERIPH_I2C_NEED_READ_REGS +#define PERIPH_I2C_NEED_WRITE_REG +#define PERIPH_I2C_NEED_WRITE_REGS + +#if defined(MODULE_PERIPH_I2C_MOCK) || defined(DOXYGEN) +/** + * @brief I2C configuration structure type + */ +typedef struct { + void *dummy; /**< dummy attribute */ +} i2c_conf_t; +#endif + /* Configuration for the wrapper around the Linux SPI API (periph_spidev_linux) * * Needs to go here, otherwise the SPI_NEEDS_ are defined after inclusion of diff --git a/cpu/native/periph/i2c_mock.c b/cpu/native/periph/i2c_mock.c new file mode 100644 index 0000000000..79a813cda8 --- /dev/null +++ b/cpu/native/periph/i2c_mock.c @@ -0,0 +1,67 @@ +/* + * SPDX-FileCopyrightText: 2024 COGIP Robotics Association + * SPDX-License-Identifier: LGPL-2.1-only + */ + +/** + * @ingroup cpu_native + * @ingroup drivers_periph_i2c + * @{ + * + * @file + * @brief Pluggable no-op I2C implementation + * + * By default, operations on this bus will always succeed (without actually performing any + * action). Reads will produce zeros. + * + * To provide mocks of a particular connected device, application authors can provide any + * of the functions themselves, because all the I2C functions in this implementation are + * weak symbols. There is per-bus or per-address dispatch. That means that if multiple + * buses or devices are to be mocked with non-zero implementations, the application needs + * to provide a single implementation that fans out to the respective mocked device. + * + * @author Gilles DOFFE + */ + +#include + +#include "periph/i2c.h" + +__attribute__((weak)) void i2c_init(i2c_t dev) +{ + (void)dev; +} + +__attribute__((weak)) void i2c_acquire(i2c_t dev) +{ + (void)dev; +} + +__attribute__((weak)) void i2c_release(i2c_t dev) +{ + (void)dev; +} + +__attribute__((weak)) int i2c_read_bytes(i2c_t dev, uint16_t addr, + void *data, size_t len, uint8_t flags) +{ + (void)dev; + (void)addr; + memset(data, 0, len); + (void)len; + (void)flags; + + return 0; +} + +__attribute__((weak)) int i2c_write_bytes(i2c_t dev, uint16_t addr, const void *data, + size_t len, uint8_t flags) +{ + (void)dev; + (void)addr; + (void)data; + (void)len; + (void)flags; + + return 0; +} diff --git a/makefiles/features_modules.inc.mk b/makefiles/features_modules.inc.mk index 975cf003c0..e0c7cf89e4 100644 --- a/makefiles/features_modules.inc.mk +++ b/makefiles/features_modules.inc.mk @@ -126,7 +126,7 @@ USEMODULE += $(filter arduino_pwm, $(FEATURES_USED)) # always register a peripheral driver as a required feature when the corresponding # module is requested -PERIPH_IGNORE_MODULES += periph_usbdev_clk periph_gpio_mock periph_gpio_linux periph_spidev_linux +PERIPH_IGNORE_MODULES += periph_usbdev_clk periph_gpio_mock periph_gpio_linux periph_i2c_mock periph_spidev_linux ifneq (,$(filter periph_%,$(DEFAULT_MODULE))) FEATURES_REQUIRED += $(filter-out $(PERIPH_IGNORE_MODULES),$(filter periph_%,$(USEMODULE)))