1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-12-24 22:13:52 +01:00

cpu/native: introduce periph_i2c_mock

This allows I2C emulation on native architecture in the same way than
periph_gpio_mock.

All I2C functions from this driver are set as weak to be easily
overridden in each application.

Signed-off-by: Gilles DOFFE <g.doffe@gmail.com>
This commit is contained in:
Gilles DOFFE 2024-01-25 02:10:48 +01:00
parent 197cf51b93
commit dab11d7a9c
6 changed files with 111 additions and 2 deletions

View File

@ -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)))

View File

@ -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

View File

@ -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)
* @{

View File

@ -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

View File

@ -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 <g.doffe@gmail.com>
*/
#include <string.h>
#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;
}

View File

@ -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)))