Skip to content

cpu/native: introduce periph_i2c_mock#20430

Merged
crasbe merged 13 commits intoRIOT-OS:masterfrom
cogip:native_i2c_mock
Nov 3, 2025
Merged

cpu/native: introduce periph_i2c_mock#20430
crasbe merged 13 commits intoRIOT-OS:masterfrom
cogip:native_i2c_mock

Conversation

@gdoffe
Copy link
Copy Markdown
Contributor

@gdoffe gdoffe commented Feb 24, 2024

Contribution description

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

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

Testing procedure

$ make -C tests/periph/i2c/ BOARD=native
make : on entre dans le répertoire « /home/gdo/Developpement/Informatique/Personnel/cogip/RIOT/tests/periph/i2c »
Building application "tests_i2c" for "native" with MCU "native".

"make" -C /home/gdo/Developpement/Informatique/Personnel/cogip/RIOT/boards/common/init
"make" -C /home/gdo/Developpement/Informatique/Personnel/cogip/RIOT/boards/native
"make" -C /home/gdo/Developpement/Informatique/Personnel/cogip/RIOT/boards/native/drivers
"make" -C /home/gdo/Developpement/Informatique/Personnel/cogip/RIOT/core
"make" -C /home/gdo/Developpement/Informatique/Personnel/cogip/RIOT/core/lib
"make" -C /home/gdo/Developpement/Informatique/Personnel/cogip/RIOT/cpu/native
"make" -C /home/gdo/Developpement/Informatique/Personnel/cogip/RIOT/cpu/native/periph
"make" -C /home/gdo/Developpement/Informatique/Personnel/cogip/RIOT/cpu/native/stdio_native
"make" -C /home/gdo/Developpement/Informatique/Personnel/cogip/RIOT/drivers
"make" -C /home/gdo/Developpement/Informatique/Personnel/cogip/RIOT/drivers/periph_common
"make" -C /home/gdo/Developpement/Informatique/Personnel/cogip/RIOT/sys
"make" -C /home/gdo/Developpement/Informatique/Personnel/cogip/RIOT/sys/auto_init
"make" -C /home/gdo/Developpement/Informatique/Personnel/cogip/RIOT/sys/div
"make" -C /home/gdo/Developpement/Informatique/Personnel/cogip/RIOT/sys/frac
"make" -C /home/gdo/Developpement/Informatique/Personnel/cogip/RIOT/sys/libc
"make" -C /home/gdo/Developpement/Informatique/Personnel/cogip/RIOT/sys/preprocessor
"make" -C /home/gdo/Developpement/Informatique/Personnel/cogip/RIOT/sys/shell
"make" -C /home/gdo/Developpement/Informatique/Personnel/cogip/RIOT/sys/shell/cmds
"make" -C /home/gdo/Developpement/Informatique/Personnel/cogip/RIOT/sys/ztimer
   text	   data	    bss	    dec	    hex	filename
  45915	    844	  47996	  94755	  17223	/home/gdo/Developpement/Informatique/Personnel/cogip/RIOT/tests/periph/i2c/bin/native/tests_i2c.elf
make : on quitte le répertoire « /home/gdo/Developpement/Informatique/Personnel/cogip/RIOT/tests/periph/i2c »

@github-actions github-actions bot added Platform: native Platform: This PR/issue effects the native platform Area: build system Area: Build system Area: cpu Area: CPU/MCU ports labels Feb 24, 2024
@chrysn chrysn added Reviewed: 1-fundamentals The fundamentals of the PR were reviewed according to the maintainer guidelines Reviewed: 2-code-design The code design of the PR was reviewed according to the maintainer guidelines Reviewed: 4-code-style The adherence to coding conventions by the PR were reviewed according to the maintainer guidelines CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Feb 25, 2024
@riot-ci
Copy link
Copy Markdown

riot-ci commented Feb 25, 2024

Murdock results

✔️ PASSED

4de38f2 drivers/at24mac: add missing stdbool.h include

Success Failures Total Runtime
10915 0 10916 10m:47s

Artifacts

@github-actions github-actions bot added the Area: drivers Area: Device drivers label Feb 26, 2024
@gdoffe
Copy link
Copy Markdown
Contributor Author

gdoffe commented Feb 26, 2024

Murdock results

FAILED

e5e81d1 drivers/bme680: fix empty initializer

Success Failures Total Runtime
97 0 9367 01m:27s

Artifacts

@chrysn thanks for your suggestions, applied them.
I added a commit to try to fix murdock test.

@chrysn
Copy link
Copy Markdown
Member

chrysn commented Feb 26, 2024

There's still a build error left, and I'd appreciated if the mocking part was somehow shown / tested.

I've done both, but can't push directly to this branch, but you can pick the commits from cogip#1.

@chrysn chrysn added Reviewed: 3-testing The PR was tested according to the maintainer guidelines Reviewed: 5-documentation The documentation details of the PR were reviewed according to the maintainer guidelines labels Feb 26, 2024
@github-actions github-actions bot added the Area: tests Area: tests and testing framework label Feb 28, 2024
@gdoffe gdoffe force-pushed the native_i2c_mock branch 2 times, most recently from fc45e56 to 333dc02 Compare February 28, 2024 15:23
@gdoffe
Copy link
Copy Markdown
Contributor Author

gdoffe commented Feb 28, 2024

There's still a build error left, and I'd appreciated if the mocking part was somehow shown / tested.

I've done both, but can't push directly to this branch, but you can pick the commits from cogip#1.

Thx for patches @chrysn . Applied and tested:

> i2c_write_bytes 0 10 0 0 1 0 1 2
i2c_write_bytes 0 10 0 0 1 0 1 2
Command: i2c_write_bytes(0, 0x0a, 0x00, [0x00, 0x01, 0x00, 0x01, 0x02])
Mock write intercepted; this write of 5 byte(s) is still ignored.
Success: i2c_0 wrote 5 bytes

Copy link
Copy Markdown
Member

@chrysn chrysn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, let's go with this.

The failing test appears to expose an issue with the si1133 driver (missing includes), can you fix that in passing?

@gdoffe
Copy link
Copy Markdown
Contributor Author

gdoffe commented Feb 28, 2024

Great, let's go with this.

The failing test appears to expose an issue with the si1133 driver (missing includes), can you fix that in passing?

done 🤞

@chrysn
Copy link
Copy Markdown
Member

chrysn commented Feb 28, 2024

Awesome, thanks!

@crasbe crasbe added the Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation label Oct 20, 2025
Copy link
Copy Markdown
Contributor

@crasbe crasbe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some small style related comments.

@crasbe
Copy link
Copy Markdown
Contributor

crasbe commented Oct 20, 2025

Thank you for getting back to this :)

@gdoffe gdoffe force-pushed the native_i2c_mock branch 2 times, most recently from fef5ed6 to f899055 Compare October 22, 2025 09:47
@gdoffe gdoffe requested a review from crasbe October 22, 2025 12:52
@crasbe
Copy link
Copy Markdown
Contributor

crasbe commented Oct 22, 2025

I'm surprised by all the churn.

I second this. The changes in the drivers seem so unrelated to this PR 🤔

@gdoffe
Copy link
Copy Markdown
Contributor Author

gdoffe commented Oct 23, 2025

I'm surprised by all the churn.

I second this. The changes in the drivers seem so unrelated to this PR 🤔

Activating periph_i2c on the native architecture triggers all tests requiring that feature to run on native boards. As a consequence, I need to fix those tests to pass CI.

Since this is just a mock implementation, there is no real benefit to building all those tests, as they are not relevant without real hardware. The alternative would be to blacklist native boards for those tests, but I am not a fan of this solution because:

  1. I already have an incoming real periph_i2c native implementation to test
  2. These 7-8 commits simply bring consistency across architectures without changing any behavior;

I kept commits atomic for easier review, but I'm happy to squash related ones (e.g., all printf fixes) if you prefer. Let me know! 😉

Copy link
Copy Markdown
Contributor

@crasbe crasbe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just two small changes to get rid of two static erros.

gdoffe and others added 11 commits October 30, 2025 23:51
Fix duplicate GPIO mode values on native platform when using Linux
kernel 5.5 or later. The kernel renamed GPIOHANDLE_REQUEST_PULL_* flags
to GPIOHANDLE_REQUEST_BIAS_PULL_* in version 5.5.

Without this fix, both GPIO_IN_PU and GPIO_OD_PU would have the same
fallback value (0xFF), causing compilation errors in switch statements.

Error: "duplicate case value"

Signed-off-by: Gilles DOFFE <[email protected]>
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]>
- Add missing modules.h include for IS_USED macro in atca.h
- Fix printf format specifiers in psa_atca_se_driver.c: use %zu for size_t
  parameters instead of %d to prevent warnings on native platform

Signed-off-by: Gilles DOFFE <[email protected]>
Native boards fail to link due to an upstream bug in cryptoauthlib's
test infrastructure. The file build/pkg/cryptoauthlib/test/cmd-processor.c
has incorrect conditional compilation logic:

    #if defined(_WIN32) || defined(__linux__) || defined(__APPLE__)
        int main(int argc, char* argv[]) { ... }
    #elif defined(RIOT_APPLICATION)
        int atca_run_cmd(char *command, size_t len) { ... }
    #endif

On native boards, __linux__ is defined, so the code compiles a main()
function instead of atca_run_cmd(). This conflicts with RIOT's main()
and causes linker errors:

    - multiple definition of 'main'
    - undefined reference to 'atca_run_cmd'

On ARM boards, none of these platform macros are defined, so the code
correctly falls through to the RIOT_APPLICATION branch and compiles
atca_run_cmd() instead.

The fix would require patching cryptoauthlib to check RIOT_APPLICATION
before checking platform macros, or reporting this upstream to Microchip.

Signed-off-by: Gilles DOFFE <[email protected]>
Fix compilation errors on native boards caused by missing modules.h
include in driver headers that use IS_USED() in preprocessor directives.

The IS_USED macro is defined in modules.h and must be included in any
header file that uses it in #if directives.

Error: "missing binary operator before token '('"

Signed-off-by: Gilles DOFFE <[email protected]>
Fix compilation errors on native boards caused by missing ARRAY_SIZE
macro definition. The macro is defined in container.h which is included
by kernel_defines.h.

On ARM/Cortex-M platforms, kernel_defines.h is indirectly included via
cpu/cortexm_common/include/irq_arch.h, making ARRAY_SIZE available.
On native platform, no CPU header includes kernel_defines.h, requiring
explicit inclusion.

Error: "implicit declaration of function 'ARRAY_SIZE'"

Signed-off-by: Gilles DOFFE <[email protected]>
Security warnings (telnet, stdio_udp, psa_crypto) were displayed even
for info-* targets, polluting the output and breaking tools that parse
make output (e.g., compile_and_test_for_board.py).

These warnings are only relevant when actually building/running code,
not when querying build system information.

Signed-off-by: Gilles DOFFE <[email protected]>
@crasbe crasbe added the CI: no fast fail don't abort PR build after first error label Oct 30, 2025
Comment on lines 21 to 22
#include <stdio.h>
#include <inttypes.h>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#include <stdio.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdbool.h>
#include <inttypes.h>

I'm not sure why this error is triggered by adding kernel_defines.h tbh 🤔

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because stdbool.h is included in various low-level headers according to the platform.

For example in irq_arch.h for all cortexm:

$ grep "stdbool.h" -r cpu/cortexm_common/
cpu/cortexm_common/include/irq_arch.h:#include <stdbool.h>
cpu/cortexm_common/include/mpu.h:#include <stdbool.h>

So everything in "upper" code benefits from that includes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be more than expected, I try to find them with:

./dist/tools/compile_and_test_for_board/compile_and_test_for_board.py . native64 --no-test -j10

I will repush a bit later

Fix compilation error on native platform where bool type is used but
stdbool.h is not explicitly included.

On many platforms (ARM, AVR, etc.), stdbool.h is indirectly included
via CPU headers, but on native this is not the case.

Signed-off-by: Gilles DOFFE <[email protected]>
Fix compilation error on native platform where bool type is used but
stdbool.h is not explicitly included.

On many platforms, stdbool.h is indirectly included via CPU headers,
but on native this is not the case.

Signed-off-by: Gilles DOFFE <[email protected]>
@crasbe crasbe added this pull request to the merge queue Nov 3, 2025
Merged via the queue into RIOT-OS:master with commit df58de4 Nov 3, 2025
25 checks passed
@gdoffe gdoffe deleted the native_i2c_mock branch November 3, 2025 20:00
@leandrolanzieri leandrolanzieri added this to the Release 2026.01 milestone Jan 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: build system Area: Build system Area: cpu Area: CPU/MCU ports Area: drivers Area: Device drivers Area: pkg Area: External package ports Area: SAUL Area: Sensor/Actuator Uber Layer Area: sys Area: System Area: tests Area: tests and testing framework CI: no fast fail don't abort PR build after first error CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Platform: native Platform: This PR/issue effects the native platform Reviewed: 1-fundamentals The fundamentals of the PR were reviewed according to the maintainer guidelines Reviewed: 2-code-design The code design of the PR was reviewed according to the maintainer guidelines Reviewed: 3-testing The PR was tested according to the maintainer guidelines Reviewed: 4-code-style The adherence to coding conventions by the PR were reviewed according to the maintainer guidelines Reviewed: 5-documentation The documentation details of the PR were reviewed according to the maintainer guidelines Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants