Skip to content

Commit dab11d7

Browse files
committed
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 <[email protected]>
1 parent 197cf51 commit dab11d7

File tree

6 files changed

+111
-2
lines changed

6 files changed

+111
-2
lines changed

cpu/native/Makefile.dep

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@ ifeq ($(OS),Linux)
44
USEMODULE += periph_gpio_linux
55
endif
66
endif
7+
ifneq (,$(filter periph_i2c,$(USEMODULE)))
8+
USEMODULE += periph_i2c_mock
9+
endif
710
ifneq (,$(filter periph_spi,$(USEMODULE)))
811
USEMODULE += periph_spidev_linux
912
endif
1013
else
1114
ifneq (,$(filter periph_gpio,$(USEMODULE)))
1215
USEMODULE += periph_gpio_mock
1316
endif
17+
ifneq (,$(filter periph_i2c,$(USEMODULE)))
18+
USEMODULE += periph_i2c_mock
19+
endif
1420
endif
1521

1622
ifneq (,$(filter stdio_default,$(USEMODULE)))

cpu/native/Makefile.features

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ FEATURES_PROVIDED += periph_eeprom
1414
FEATURES_PROVIDED += periph_flashpage
1515
FEATURES_PROVIDED += periph_flashpage_pagewise
1616
FEATURES_PROVIDED += periph_hwrng
17+
FEATURES_PROVIDED += periph_i2c
1718
FEATURES_PROVIDED += periph_pm
1819
FEATURES_PROVIDED += periph_pwm
1920
FEATURES_PROVIDED += periph_timer_periodic

cpu/native/include/periph_conf.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,21 @@ extern "C" {
7171
# define QDEC_NUMOF (8U)
7272
#endif
7373

74-
/* MARK: - SPI configuration (Linux host only) */
74+
/* MARK: - I2C configuration (mock implementation) */
75+
/**
76+
* @name I2C configuration (mock implementation)
77+
* @{
78+
*/
79+
#if !defined(I2C_NUMOF) || defined(DOXYGEN)
80+
/**
81+
* @brief Amount of I2C devices
82+
*
83+
* Can be overridden during compile time with `CFLAGS+=-DI2C_NUMOF=n`.
84+
*/
85+
# define I2C_NUMOF 1
86+
#endif
87+
/** @} */
88+
7589
/**
7690
* @name SPI configuration (Linux host only)
7791
* @{

cpu/native/include/periph_cpu.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,27 @@ typedef gpio_mock_t* gpio_t;
178178
#define PROVIDES_PM_SET_LOWEST
179179
/** @} */
180180

181+
/**
182+
* @name I2C Configuration
183+
*
184+
* The common I2C implementation is requested to provide the default implementations of the
185+
* `i2c_{read,write}_{reg,regs}` functions.
186+
*/
187+
188+
#define PERIPH_I2C_NEED_READ_REG
189+
#define PERIPH_I2C_NEED_READ_REGS
190+
#define PERIPH_I2C_NEED_WRITE_REG
191+
#define PERIPH_I2C_NEED_WRITE_REGS
192+
193+
#if defined(MODULE_PERIPH_I2C_MOCK) || defined(DOXYGEN)
194+
/**
195+
* @brief I2C configuration structure type
196+
*/
197+
typedef struct {
198+
void *dummy; /**< dummy attribute */
199+
} i2c_conf_t;
200+
#endif
201+
181202
/* Configuration for the wrapper around the Linux SPI API (periph_spidev_linux)
182203
*
183204
* Needs to go here, otherwise the SPI_NEEDS_ are defined after inclusion of

cpu/native/periph/i2c_mock.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2024 COGIP Robotics Association
3+
* SPDX-License-Identifier: LGPL-2.1-only
4+
*/
5+
6+
/**
7+
* @ingroup cpu_native
8+
* @ingroup drivers_periph_i2c
9+
* @{
10+
*
11+
* @file
12+
* @brief Pluggable no-op I2C implementation
13+
*
14+
* By default, operations on this bus will always succeed (without actually performing any
15+
* action). Reads will produce zeros.
16+
*
17+
* To provide mocks of a particular connected device, application authors can provide any
18+
* of the functions themselves, because all the I2C functions in this implementation are
19+
* weak symbols. There is per-bus or per-address dispatch. That means that if multiple
20+
* buses or devices are to be mocked with non-zero implementations, the application needs
21+
* to provide a single implementation that fans out to the respective mocked device.
22+
*
23+
* @author Gilles DOFFE <[email protected]>
24+
*/
25+
26+
#include <string.h>
27+
28+
#include "periph/i2c.h"
29+
30+
__attribute__((weak)) void i2c_init(i2c_t dev)
31+
{
32+
(void)dev;
33+
}
34+
35+
__attribute__((weak)) void i2c_acquire(i2c_t dev)
36+
{
37+
(void)dev;
38+
}
39+
40+
__attribute__((weak)) void i2c_release(i2c_t dev)
41+
{
42+
(void)dev;
43+
}
44+
45+
__attribute__((weak)) int i2c_read_bytes(i2c_t dev, uint16_t addr,
46+
void *data, size_t len, uint8_t flags)
47+
{
48+
(void)dev;
49+
(void)addr;
50+
memset(data, 0, len);
51+
(void)len;
52+
(void)flags;
53+
54+
return 0;
55+
}
56+
57+
__attribute__((weak)) int i2c_write_bytes(i2c_t dev, uint16_t addr, const void *data,
58+
size_t len, uint8_t flags)
59+
{
60+
(void)dev;
61+
(void)addr;
62+
(void)data;
63+
(void)len;
64+
(void)flags;
65+
66+
return 0;
67+
}

makefiles/features_modules.inc.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ USEMODULE += $(filter arduino_pwm, $(FEATURES_USED))
126126

127127
# always register a peripheral driver as a required feature when the corresponding
128128
# module is requested
129-
PERIPH_IGNORE_MODULES += periph_usbdev_clk periph_gpio_mock periph_gpio_linux periph_spidev_linux
129+
PERIPH_IGNORE_MODULES += periph_usbdev_clk periph_gpio_mock periph_gpio_linux periph_i2c_mock periph_spidev_linux
130130

131131
ifneq (,$(filter periph_%,$(DEFAULT_MODULE)))
132132
FEATURES_REQUIRED += $(filter-out $(PERIPH_IGNORE_MODULES),$(filter periph_%,$(USEMODULE)))

0 commit comments

Comments
 (0)