xref: /aosp_15_r20/external/pigweed/pw_spi_rp2040/docs.rst (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1.. _module-pw_spi_rp2040:
2
3==============
4 pw_spi_rp2040
5==============
6.. pigweed-module::
7   :name: pw_spi_rp2040
8
9``pw_spi_rp2040`` implements the :ref:`module-pw_spi` interface using the
10`Raspberry Pi Pico SDK <https://github.com/raspberrypi/pico-sdk/>`_.
11
12The implementation is based on the SPI driver from the Pico SDK. SPI transfers
13use the blocking driver API which uses busy waiting under the hood.
14
15The PicoSDK's ``spi_init()`` must be called before using the initiator in order
16to enable the SPI peripheral and set its bus speed.
17
18.. note::
19   There is currently no support for RP2040 hardware CSn
20   pins. :cpp:class:`pw::spi::DigitalOutChipSelector` is
21   used instead.
22
23Usage
24=====
25.. code-block:: cpp
26
27   #include "hardware/spi.h"
28   #include "pw_digital_io/polarity.h"
29   #include "pw_digital_io_rp2040/digital_io.h"
30   #include "pw_spi/chip_selector_digital_out.h"
31   #include "pw_spi/device.h"
32   #include "pw_sync/borrow.h"
33   #include "pw_sync/mutex.h"
34
35   constexpr pw::spi::Config kSpiConfig8Bit{
36       .polarity = pw::spi::ClockPolarity::kActiveHigh,
37       .phase = pw::spi::ClockPhase::kFallingEdge,
38       .bits_per_word = pw::spi::BitsPerWord(8),
39       .bit_order = pw::spi::BitOrder::kMsbFirst,
40   };
41
42   pw::digital_io::Rp2040DigitalInOut display_cs_pin({
43      .pin = 12,
44      .polarity = pw::digital_io::Polarity::kActiveLow,
45   });
46   DigitalOutChipSelector spi_chip_selector(display_cs_pin);
47
48   Rp2040Initiator spi_initiator(spi0);
49   VirtualMutex spi_initiator_mutex;
50   Borrowable<Initiator> borrowable_spi_initiator(spi_initiator,
51                                                  spi_initiator_mutex);
52   pw::spi::Device spi_8_bit(borrowable_spi_initiator,
53                             kSpiConfig8Bit,
54                             spi_chip_selector);
55