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