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