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/blocking_adapter.h"
18 #include "pw_uart_mcuxpresso/dma_uart_nonblocking.h"
19 #include "pw_unit_test/framework.h"
20
21 namespace examples {
22
DmaUartNonBlockingBasicExample()23 pw::Status DmaUartNonBlockingBasicExample() {
24 // DOCSTAG: [pw_uart_mcuxpresso-DmaUartNonBlockingBasicExample]
25 const auto kUartBase = USART0;
26 constexpr uint32_t kBaudRate = 115200;
27 constexpr bool kFlowControl = true;
28 std::array<std::byte, 65536> ring_buffer = {};
29 constexpr uint32_t kUartRxDmaCh = 0;
30 constexpr uint32_t kUartTxDmaCh = 1;
31 static pw::dma::McuxpressoDmaController dma(DMA0_BASE);
32 static pw::dma::McuxpressoDmaChannel rx_dma_ch = dma.GetChannel(kUartRxDmaCh);
33 static pw::dma::McuxpressoDmaChannel tx_dma_ch = dma.GetChannel(kUartTxDmaCh);
34
35 PW_TRY(dma.Init());
36 rx_dma_ch.Init();
37 tx_dma_ch.Init();
38
39 const pw::uart::DmaUartMcuxpressoNonBlocking::Config kConfig = {
40 .usart_base = kUartBase,
41 .baud_rate = kBaudRate,
42 .flow_control = kFlowControl,
43 .parity = kUSART_ParityDisabled,
44 .stop_bits = kUSART_OneStopBit,
45 .rx_dma_ch = rx_dma_ch,
46 .tx_dma_ch = tx_dma_ch,
47 .rx_input_mux_dmac_ch_request_en =
48 kINPUTMUX_Flexcomm0RxToDmac0Ch0RequestEna,
49 .tx_input_mux_dmac_ch_request_en =
50 kINPUTMUX_Flexcomm0TxToDmac0Ch1RequestEna,
51 .buffer = pw::ByteSpan(ring_buffer),
52 };
53
54 auto uart = pw::uart::DmaUartMcuxpressoNonBlocking{kConfig};
55
56 PW_TRY(uart.Enable());
57
58 // DOCSTAG: [pw_uart_mcuxpresso-DmaUartNonBlockingBasicExample]
59
60 // Do something else
61
62 return pw::OkStatus();
63 }
64
DmaUartNonBlockingAdapterExample()65 pw::Status DmaUartNonBlockingAdapterExample() {
66 // DOCSTAG: [pw_uart_mcuxpresso-DmaUartNonBlockingAdapterExample]
67 const auto kUartBase = USART0;
68 constexpr uint32_t kBaudRate = 115200;
69 constexpr bool kFlowControl = true;
70 std::array<std::byte, 65536> ring_buffer = {};
71 constexpr uint32_t kUartRxDmaCh = 0;
72 constexpr uint32_t kUartTxDmaCh = 1;
73 static pw::dma::McuxpressoDmaController dma(DMA0_BASE);
74 static pw::dma::McuxpressoDmaChannel rx_dma_ch = dma.GetChannel(kUartRxDmaCh);
75 static pw::dma::McuxpressoDmaChannel tx_dma_ch = dma.GetChannel(kUartTxDmaCh);
76
77 PW_TRY(dma.Init());
78 rx_dma_ch.Init();
79 tx_dma_ch.Init();
80
81 const pw::uart::DmaUartMcuxpressoNonBlocking::Config kConfig = {
82 .usart_base = kUartBase,
83 .baud_rate = kBaudRate,
84 .flow_control = kFlowControl,
85 .parity = kUSART_ParityDisabled,
86 .stop_bits = kUSART_OneStopBit,
87 .rx_dma_ch = rx_dma_ch,
88 .tx_dma_ch = tx_dma_ch,
89 .rx_input_mux_dmac_ch_request_en =
90 kINPUTMUX_Flexcomm0RxToDmac0Ch0RequestEna,
91 .tx_input_mux_dmac_ch_request_en =
92 kINPUTMUX_Flexcomm0TxToDmac0Ch1RequestEna,
93 .buffer = pw::ByteSpan(ring_buffer),
94 };
95
96 auto uart = pw::uart::DmaUartMcuxpressoNonBlocking{kConfig};
97 auto adapted_uart = pw::uart::UartBlockingAdapter(uart);
98
99 PW_TRY(adapted_uart.Enable());
100
101 // DOCSTAG: [pw_uart_mcuxpresso-DmaUartNonBlockingAdapterExample]
102
103 // Do something else
104
105 return pw::OkStatus();
106 }
107
TEST(DmaUartNonBlocking,BasicExample)108 TEST(DmaUartNonBlocking, BasicExample) {
109 pw::Status status = DmaUartNonBlockingBasicExample();
110 EXPECT_EQ(status.code(), PW_STATUS_OK);
111 }
112
TEST(DmaUartNonBlocking,AdapterExample)113 TEST(DmaUartNonBlocking, AdapterExample) {
114 pw::Status status = DmaUartNonBlockingAdapterExample();
115 EXPECT_EQ(status.code(), PW_STATUS_OK);
116 }
117 } // namespace examples
118