Skip to content

Commit df58de4

Browse files
authored
Merge pull request #20430 from cogip/native_i2c_mock
cpu/native: introduce periph_i2c_mock
2 parents 775d436 + 4de38f2 commit df58de4

File tree

23 files changed

+210
-55
lines changed

23 files changed

+210
-55
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: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,21 @@ extern "C" {
6868
* @brief Pull-down
6969
*/
7070
# if !defined(GPIOHANDLE_REQUEST_PULL_DOWN) || defined(DOXYGEN)
71-
# define GPIOHANDLE_REQUEST_PULL_DOWN (0xFF)
71+
# if defined(GPIOHANDLE_REQUEST_BIAS_PULL_DOWN)
72+
# define GPIOHANDLE_REQUEST_PULL_DOWN GPIOHANDLE_REQUEST_BIAS_PULL_DOWN
73+
# else
74+
# define GPIOHANDLE_REQUEST_PULL_DOWN (0xFF)
75+
# endif
7276
# endif
7377
/**
7478
* @brief Pull-up
7579
*/
7680
# if !defined(GPIOHANDLE_REQUEST_PULL_UP) || defined(DOXYGEN)
77-
# define GPIOHANDLE_REQUEST_PULL_UP (0xFF)
81+
# if defined(GPIOHANDLE_REQUEST_BIAS_PULL_UP)
82+
# define GPIOHANDLE_REQUEST_PULL_UP GPIOHANDLE_REQUEST_BIAS_PULL_UP
83+
# else
84+
# define GPIOHANDLE_REQUEST_PULL_UP (0xFF)
85+
# endif
7886
# endif
7987

8088
/**
@@ -170,6 +178,27 @@ typedef gpio_mock_t* gpio_t;
170178
#define PROVIDES_PM_SET_LOWEST
171179
/** @} */
172180

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+
173202
/* Configuration for the wrapper around the Linux SPI API (periph_spidev_linux)
174203
*
175204
* 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+
}

drivers/at24mac/at24mac.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
*/
2020

2121
#include <errno.h>
22+
#include <stdbool.h>
23+
2224
#include "kernel_defines.h"
2325

2426
#include "at24mac.h"

drivers/bmx280/include/bmx280_params.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include "board.h"
2626
#include "bmx280.h"
27+
#include "kernel_defines.h"
2728
#include "saul_reg.h"
2829

2930
#ifdef __cplusplus

drivers/include/hm330x.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* @author Francisco Molina <[email protected]>
3333
*/
3434

35+
#include "modules.h"
3536
#include "periph/i2c.h"
3637
#include "periph/gpio.h"
3738

drivers/include/mcp23x17.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@
346346
extern "C" {
347347
#endif
348348

349+
#include "modules.h"
349350
#include "periph/gpio.h"
350351
#include "periph/i2c.h"
351352
#include "periph/spi.h"

drivers/include/vl6180x.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ extern "C"
400400
#include <stdbool.h>
401401
#include <stdint.h>
402402

403+
#include "modules.h"
403404
#include "periph/gpio.h"
404405
#include "periph/i2c.h"
405406

0 commit comments

Comments
 (0)