xref: /aosp_15_r20/external/pigweed/pw_spi_mcuxpresso/docs.rst (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1.. _module-pw_spi_mcuxpresso:
2
3=================
4pw_spi_mcuxpresso
5=================
6``pw_spi_mcuxpresso`` implements the :ref:`module-pw_spi` interfaces using the
7NXP MCUXpresso SDK.
8
9There are two initiator implementations corresponding to the SPI and FLEXIO_SPI
10drivers in the SDK. SPI transfer can be configured to use a blocking
11(by polling) method or non-blocking under the covers. The API is synchronous
12regardless.
13
14There is a responder implementation ``McuxpressoResponder`` which uses the SPI
15and DMA drivers from the SDK.
16
17-----
18Setup
19-----
20Use of this module requires setting up the MCUXpresso SDK for use with Pigweed. Follow
21the steps in :ref:`module-pw_build_mcuxpresso` to create a ``pw_source_set`` for an
22MCUXpresso SDK. Include the GPIO and PINT driver components in this SDK definition.
23
24This example shows what your SDK setup would look like if using an RT595 EVK.
25
26.. code-block:: text
27
28   import("$dir_pw_third_party/mcuxpresso/mcuxpresso.gni")
29
30   pw_mcuxpresso_sdk("sample_project_sdk") {
31     manifest = "$dir_pw_third_party/mcuxpresso/evkmimxrt595/EVK-MIMXRT595_manifest_v3_13.xml"
32     include = [
33       "component.serial_manager_uart.MIMXRT595S",
34       "platform.drivers.flexio_spi.MIMXRT595S",
35       "platform.drivers.flexspi.MIMXRT595S",
36       "project_template.evkmimxrt595.MIMXRT595S",
37       "utility.debug_console.MIMXRT595S",
38     ]
39   }
40
41Next, specify the ``pw_third_party_mcuxpresso_SDK`` GN global variable to specify
42the name of this source set. Edit your GN args with ``gn args out``.
43
44.. code-block:: text
45
46   pw_third_party_mcuxpresso_SDK = "//targets/mimxrt595_evk:sample_project_sdk"
47
48Then, depend on this module in your BUILD.gn to use.
49
50.. code-block:: text
51
52   deps = [ dir_pw_spi_mcuxpresso ]
53
54--------
55Examples
56--------
57Example write using the FLEXIO_SPI initiator:
58
59.. code-block:: text
60
61   McuxpressoFlexIoInitiator spi(
62      flexio_spi_config, CLOCK_GetFlexioClkFreq(), baud_rate_bps, blocking);
63   spi.Configure(configuration);
64
65   spi.WriteRead(source, destination);
66
67Example use of SPI responder:
68
69.. code-block:: cpp
70
71   #include "pw_dma_mcuxpresso/dma.h"
72   #include "pw_spi_mcuxpresso/responder.h"
73
74   constinit pw::dma::McuxpressoDmaController dma(DMA0_BASE);
75
76   pw::dma::McuxpressoDmaChannel tx_dma = dma.GetChannel(kTxDmaChannel);
77   pw::dma::McuxpressoDmaChannel rx_dma = dma.GetChannel(kRxDmaChannel);
78
79   pw::spi::McuxpressoResponder spi_responder(
80      {
81         // SPI mode 3 (CPOL = 1, CPHA = 1)
82         .polarity = pw::spi::ClockPolarity::kActiveLow,  // CPOL = 1
83         .phase = pw::spi::ClockPhase::kFallingEdge,      // CPHA = 1
84         .bits_per_word = 8,
85         .bit_order = pw::spi::BitOrder::kMsbFirst,
86         .base_address = SPI14_BASE,
87         .handle_cs = true,
88      },
89      tx_dma,
90      rx_dma);
91
92   pw::Status Init() {
93     // Initialize the DMA controller
94     PW_TRY(dma.Init());
95
96     tx_dma.Init();
97     tx_dma.SetPriority(kTxDmaChannelPriority);
98     tx_dma.Enable();
99     tx_dma.EnableInterrupts();
100
101     rx_dma.Init();
102     rx_dma.SetPriority(kRxDmaChannelPriority);
103     rx_dma.Enable();
104     tx_dma.EnableInterrupts();
105
106     PW_TRY(spi_responder.Initialize());
107
108     spi_responder.SetCompletionHandler([this](pw::ByteSpan rx_data, pw::Status status) {
109      // Signal we got some data
110     });
111
112     // Start listen for read
113     PW_TRY(spi_.WriteReadAsync(kTxData, rx_buf));
114   }
115