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