xref: /aosp_15_r20/external/pigweed/pw_i2c_rp2040/docs.rst (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_i2c_rp2040:
2*61c4878aSAndroid Build Coastguard Worker
3*61c4878aSAndroid Build Coastguard Worker-------------
4*61c4878aSAndroid Build Coastguard Workerpw_i2c_rp2040
5*61c4878aSAndroid Build Coastguard Worker-------------
6*61c4878aSAndroid Build Coastguard Worker.. pigweed-module::
7*61c4878aSAndroid Build Coastguard Worker   :name: pw_i2c_rp2040
8*61c4878aSAndroid Build Coastguard Worker
9*61c4878aSAndroid Build Coastguard Worker.. pw_i2c_rp2040-example-start
10*61c4878aSAndroid Build Coastguard Worker
11*61c4878aSAndroid Build Coastguard Worker.. code-block:: cpp
12*61c4878aSAndroid Build Coastguard Worker
13*61c4878aSAndroid Build Coastguard Worker   #include "pw_i2c_rp2040/initiator.h"
14*61c4878aSAndroid Build Coastguard Worker
15*61c4878aSAndroid Build Coastguard Worker   #include "hardware/i2c.h"
16*61c4878aSAndroid Build Coastguard Worker
17*61c4878aSAndroid Build Coastguard Worker   constexpr pw::i2c::Rp2040Initiator::Config ki2cConfig{
18*61c4878aSAndroid Build Coastguard Worker     .clock_frequency_hz = 400'000,
19*61c4878aSAndroid Build Coastguard Worker     .sda_pin = 8,
20*61c4878aSAndroid Build Coastguard Worker     .scl_pin = 9,
21*61c4878aSAndroid Build Coastguard Worker   };
22*61c4878aSAndroid Build Coastguard Worker
23*61c4878aSAndroid Build Coastguard Worker    pw::i2c::Rp2040Initiator i2c_bus(ki2cConfig, i2c0);
24*61c4878aSAndroid Build Coastguard Worker    // Calls these Pico SDK functions:
25*61c4878aSAndroid Build Coastguard Worker    // * gpio_set_function(8, GPIO_FUNC_I2C)
26*61c4878aSAndroid Build Coastguard Worker    // * gpio_set_function(9, GPIO_FUNC_I2C)
27*61c4878aSAndroid Build Coastguard Worker    i2c_bus.Enable();
28*61c4878aSAndroid Build Coastguard Worker
29*61c4878aSAndroid Build Coastguard Worker.. pw_i2c_rp2040-example-end
30*61c4878aSAndroid Build Coastguard Worker
31*61c4878aSAndroid Build Coastguard Worker``pw_i2c_rp2040`` implements the :ref:`module-pw_i2c` interface using the
32*61c4878aSAndroid Build Coastguard WorkerRaspberry Pi Pico SDK.
33*61c4878aSAndroid Build Coastguard Worker
34*61c4878aSAndroid Build Coastguard WorkerThe implementation is based on the I2C driver in the Pico SDK. I2C transfers
35*61c4878aSAndroid Build Coastguard Workeruse the blocking driver API which uses busy waiting under the hood.
36*61c4878aSAndroid Build Coastguard Worker
37*61c4878aSAndroid Build Coastguard WorkerImplementation notes
38*61c4878aSAndroid Build Coastguard Worker====================
39*61c4878aSAndroid Build Coastguard WorkerThe ``Enable()`` function calls `gpio_set_function
40*61c4878aSAndroid Build Coastguard Worker<https://www.raspberrypi.com/documentation/pico-sdk/hardware.html#rpipc56748afaf477c99958b>`_
41*61c4878aSAndroid Build Coastguard Workerfrom the Pico SDK.
42*61c4878aSAndroid Build Coastguard Worker
43*61c4878aSAndroid Build Coastguard WorkerUnder the hood this implementation uses the following Pico SDK functions which only
44*61c4878aSAndroid Build Coastguard Workerallow specifying timeouts in microseconds:
45*61c4878aSAndroid Build Coastguard Worker
46*61c4878aSAndroid Build Coastguard Worker- `i2c_read_blocking_until <https://www.raspberrypi.com/documentation/pico-sdk/hardware.html#rpip9cd3e6e1aeea56af6388>`_
47*61c4878aSAndroid Build Coastguard Worker- `i2c_read_timeout_us <https://www.raspberrypi.com/documentation/pico-sdk/hardware.html#rpip0102e3f420f091f30b00>`_
48*61c4878aSAndroid Build Coastguard Worker- `i2c_write_blocking_until <https://www.raspberrypi.com/documentation/pico-sdk/hardware.html#rpip03d01a63251da3cc0588>`_
49*61c4878aSAndroid Build Coastguard Worker- `i2c_write_timeout_us <https://www.raspberrypi.com/documentation/pico-sdk/hardware.html#rpip6ca2b36048b95c5e0b07>`_
50*61c4878aSAndroid Build Coastguard Worker
51*61c4878aSAndroid Build Coastguard WorkerOne additional microsecond is added to each timeout value to ensure reads and
52*61c4878aSAndroid Build Coastguard Workerwrites wait at least the full duration. Ideally a single clock tick would be
53*61c4878aSAndroid Build Coastguard Workeradded but that is not currently possible with the Pico SDK APIs.
54