xref: /aosp_15_r20/external/pigweed/pw_uart_mcuxpresso/dma_uart_example.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2024 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include "pw_dma_mcuxpresso/dma.h"
16 #include "pw_status/try.h"
17 #include "pw_uart_mcuxpresso/dma_uart.h"
18 #include "pw_unit_test/framework.h"
19 
20 namespace examples {
21 
DmaUartExample()22 pw::Status DmaUartExample() {
23   // DOCSTAG: [pw_uart_mcuxpresso-DmaUartExample]
24   const auto kUartBase = USART0;
25   constexpr uint32_t kBaudRate = 115200;
26   constexpr bool kFlowControl = true;
27   std::array<std::byte, 65536> ring_buffer = {};
28   constexpr uint32_t kUartRxDmaCh = 0;
29   constexpr uint32_t kUartTxDmaCh = 1;
30   static pw::dma::McuxpressoDmaController dma(DMA0_BASE);
31   static pw::dma::McuxpressoDmaChannel rx_dma_ch = dma.GetChannel(kUartRxDmaCh);
32   static pw::dma::McuxpressoDmaChannel tx_dma_ch = dma.GetChannel(kUartTxDmaCh);
33 
34   const pw::uart::DmaUartMcuxpresso::Config kConfig = {
35       .usart_base = kUartBase,
36       .baud_rate = kBaudRate,
37       .flow_control = kFlowControl,
38       .parity = kUSART_ParityDisabled,
39       .stop_bits = kUSART_OneStopBit,
40       .rx_dma_ch = rx_dma_ch,
41       .tx_dma_ch = tx_dma_ch,
42       .rx_input_mux_dmac_ch_request_en =
43           kINPUTMUX_Flexcomm0RxToDmac0Ch0RequestEna,
44       .tx_input_mux_dmac_ch_request_en =
45           kINPUTMUX_Flexcomm0TxToDmac0Ch1RequestEna,
46       .buffer = pw::ByteSpan(ring_buffer),
47   };
48 
49   auto uart = pw::uart::DmaUartMcuxpresso{kConfig};
50 
51   PW_TRY(uart.Enable());
52 
53   // DOCSTAG: [pw_uart_mcuxpresso-DmaUartExample]
54 
55   // Do something else
56 
57   return pw::OkStatus();
58 }
59 
TEST(DmaUart,Example)60 TEST(DmaUart, Example) {
61   pw::Status status = DmaUartExample();
62   EXPECT_EQ(status.code(), PW_STATUS_OK);
63 }
64 }  // namespace examples
65