blob: 168a7841286ad24e5dda6f3e670b3f46c741e761 [file] [log] [blame]
Ewout van Bekkumd1669a82020-12-04 16:00:47 -08001.. _module-pw_i2c:
2
3------
4pw_i2c
5------
6
7.. warning::
8 This module is under construction, not ready for use, and the documentation
9 is incomplete.
10
11pw_i2c contains interfaces and utility functions for using I2C.
12
13Features
14========
15
16pw::i2c::Initiator
17------------------
Rob Mohr8b2ebc42021-05-22 10:34:56 -070018.. inclusive-language: disable
19
Ewout van Bekkumd1669a82020-12-04 16:00:47 -080020The common interface for initiating transactions with devices on an I2C bus.
21Other documentation sources may call this style of interface an I2C "master",
22"central" or "controller".
Kevin Zeng6e15a132021-04-16 00:34:17 -070023
Rob Mohr8b2ebc42021-05-22 10:34:56 -070024.. inclusive-language: enable
25
Kevin Zeng6e15a132021-04-16 00:34:17 -070026pw::i2c::Device
27---------------
28The common interface for interfacing with generic I2C devices. This object
29contains ``pw::i2c::Address`` and wraps the ``pw::i2c::Initiator`` API.
30Common use case includes streaming arbitrary data (Read/Write). Only works
31with devices with a single device address.
Kevin Zeng58b43d32021-04-16 00:36:16 -070032
33pw::i2c::RegisterDevice
34-----------------------
35The common interface for interfacing with register devices. Contains methods
36to help read and write registers from and to the device. Users should have a
37understanding of the capabilities of their device such as register address
38sizes, register data sizes, byte addressability, bulk transactions, etc in
39order to effectively use this interface.
Nathaniel Brough7c773ff2021-05-05 08:24:31 +080040
41pw::i2c::MockInitiator
42----------------------
Nathaniel Brough7c773ff2021-05-05 08:24:31 +080043A generic mocked backend for for pw::i2c::Initiator. This is specifically
44intended for use when developing drivers for i2c devices. This is structured
45around a set of 'transactions' where each transaction contains a write, read and
46a timeout. A transaction list can then be passed to the MockInitiator, where
47each consecutive call to read/write will iterate to the next transaction in the
48list. An example of this is shown below:
49
50.. code-block:: cpp
51
52 using pw::i2c::Address;
53 using pw::i2c::MakeExpectedTransactionlist;
54 using pw::i2c::MockInitiator;
55 using pw::i2c::WriteTransaction;
56 using std::literals::chrono_literals::ms;
57
58 constexpr Address kAddress1 = Address::SevenBit<0x01>();
59 constexpr auto kExpectWrite1 = pw::bytes::Array<1, 2, 3, 4, 5>();
60 constexpr auto kExpectWrite2 = pw::bytes::Array<3, 4, 5>();
61 auto expected_transactions = MakeExpectedTransactionArray(
62 {WriteTransaction(pw::OkStatus(), kAddress1, kExpectWrite1, 1ms),
63 WriteTransaction(pw::OkStatus(), kAddress2, kExpectWrite2, 1ms)});
64 MockInitiator i2c_mock(expected_transactions);
65
66 // Begin driver code
67 ConstByteSpan write1 = kExpectWrite1;
68 // write1 is ok as i2c_mock expects {1, 2, 3, 4, 5} == {1, 2, 3, 4, 5}
69 Status status = i2c_mock.WriteFor(kAddress1, write1, 2ms);
70
71 // Takes the first two bytes from the expected array to build a mismatching
72 // span to write.
73 ConstByteSpan write2 = std::span(kExpectWrite2).first(2);
74 // write2 fails as i2c_mock expects {3, 4, 5} != {3, 4}
75 status = i2c_mock.WriteFor(kAddress2, write2, 2ms);
76 // End driver code
77
78 // Optionally check if the mocked transaction list has been exhausted.
79 // Alternatively this is also called from MockInitiator::~MockInitiator().
Rob Mohr8b2ebc42021-05-22 10:34:56 -070080 EXPECT_EQ(mocked_i2c.Finalize(), OkStatus());
Kevin Zeng6a5bf0b2021-06-08 11:01:38 -070081
82pw::i2c::GmockInitiator
83-----------------------
84gMock of Initiator used for testing and mocking out the Initiator.