pw_i2c#
Cross-platform I2C API with interactive debugging
Stable C++17
#include "hardware/i2c.h"
#include "pw_i2c_rp2040/initiator.h"
constexpr pw::i2c::Rp2040Initiator::Config ki2cConfig{
.clock_frequency_hz = 400'000,
.sda_pin = 8,
.scl_pin = 9,
};
pw::i2c::Rp2040Initiator i2c_bus(ki2cConfig, i2c0);
// Calls these Pico SDK functions:
// * gpio_set_function(8, GPIO_FUNC_I2C)
// * gpio_set_function(9, GPIO_FUNC_I2C)
i2c_bus.Enable();
cc_library(
# ...
deps = [
# ...
"@pigweed//pw_i2c:address",
"@pigweed//pw_i2c:device",
# ...
] + select({
"@platforms//os:freertos": [
"@pigweed//pw_i2c_rp2040:initiator",
],
"//conditions:default": [
# Fake example of a custom implementation.
"//lib/pw_i2c_my_device:initiator",
],
}),
)
pw_i2c
provides C++ libraries and helpers for interacting with I2C
devices.
Responder API#
pw_i2c
provides an API for implementing an I2C responder (target) through
the pw::i2c::Responder
abstract class. This API is defined in
pw_i2c/responder.h
.
Using the Responder#
To use the Responder
API, you need to:
Implement pw::i2c::ResponderEvents: This interface handles I2C events like
OnStartRead
andOnWrite
. A key advantage is that yourResponderEvents
implementation is a part of your application and is thus portable (can be reused across different backends).Select a backend: Like the
Initiator
,pw::i2c::Responder
is an abstract class. You must select a concrete backend implementation for your target hardware in your build configuration. Currently, only the Zephyr backend is implemented.