Ewout van Bekkum | d1669a8 | 2020-12-04 16:00:47 -0800 | [diff] [blame] | 1 | .. _module-pw_i2c: |
| 2 | |
| 3 | ------ |
| 4 | pw_i2c |
| 5 | ------ |
| 6 | |
| 7 | .. warning:: |
| 8 | This module is under construction, not ready for use, and the documentation |
| 9 | is incomplete. |
| 10 | |
| 11 | pw_i2c contains interfaces and utility functions for using I2C. |
| 12 | |
| 13 | Features |
| 14 | ======== |
| 15 | |
| 16 | pw::i2c::Initiator |
| 17 | ------------------ |
Rob Mohr | 8b2ebc4 | 2021-05-22 10:34:56 -0700 | [diff] [blame] | 18 | .. inclusive-language: disable |
| 19 | |
Ewout van Bekkum | d1669a8 | 2020-12-04 16:00:47 -0800 | [diff] [blame] | 20 | The common interface for initiating transactions with devices on an I2C bus. |
| 21 | Other documentation sources may call this style of interface an I2C "master", |
| 22 | "central" or "controller". |
Kevin Zeng | 6e15a13 | 2021-04-16 00:34:17 -0700 | [diff] [blame] | 23 | |
Rob Mohr | 8b2ebc4 | 2021-05-22 10:34:56 -0700 | [diff] [blame] | 24 | .. inclusive-language: enable |
| 25 | |
Kevin Zeng | 6e15a13 | 2021-04-16 00:34:17 -0700 | [diff] [blame] | 26 | pw::i2c::Device |
| 27 | --------------- |
| 28 | The common interface for interfacing with generic I2C devices. This object |
| 29 | contains ``pw::i2c::Address`` and wraps the ``pw::i2c::Initiator`` API. |
| 30 | Common use case includes streaming arbitrary data (Read/Write). Only works |
| 31 | with devices with a single device address. |
Kevin Zeng | 58b43d3 | 2021-04-16 00:36:16 -0700 | [diff] [blame] | 32 | |
| 33 | pw::i2c::RegisterDevice |
| 34 | ----------------------- |
| 35 | The common interface for interfacing with register devices. Contains methods |
| 36 | to help read and write registers from and to the device. Users should have a |
| 37 | understanding of the capabilities of their device such as register address |
| 38 | sizes, register data sizes, byte addressability, bulk transactions, etc in |
| 39 | order to effectively use this interface. |
Nathaniel Brough | 7c773ff | 2021-05-05 08:24:31 +0800 | [diff] [blame] | 40 | |
| 41 | pw::i2c::MockInitiator |
| 42 | ---------------------- |
Nathaniel Brough | 7c773ff | 2021-05-05 08:24:31 +0800 | [diff] [blame] | 43 | A generic mocked backend for for pw::i2c::Initiator. This is specifically |
| 44 | intended for use when developing drivers for i2c devices. This is structured |
| 45 | around a set of 'transactions' where each transaction contains a write, read and |
| 46 | a timeout. A transaction list can then be passed to the MockInitiator, where |
| 47 | each consecutive call to read/write will iterate to the next transaction in the |
| 48 | list. 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 Mohr | 8b2ebc4 | 2021-05-22 10:34:56 -0700 | [diff] [blame] | 80 | EXPECT_EQ(mocked_i2c.Finalize(), OkStatus()); |
Kevin Zeng | 6a5bf0b | 2021-06-08 11:01:38 -0700 | [diff] [blame] | 81 | |
| 82 | pw::i2c::GmockInitiator |
| 83 | ----------------------- |
| 84 | gMock of Initiator used for testing and mocking out the Initiator. |