xref: /aosp_15_r20/external/pigweed/pw_stream_uart_mcuxpresso/public/pw_stream_uart_mcuxpresso/stream.h (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2023 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 #pragma once
15 
16 #include "fsl_usart_freertos.h"
17 #include "pw_bytes/span.h"
18 #include "pw_clock_tree/clock_tree.h"
19 #include "pw_status/status.h"
20 #include "pw_stream/stream.h"
21 
22 namespace pw::stream {
23 
24 class UartStreamMcuxpresso : public NonSeekableReaderWriter {
25  public:
UartStreamMcuxpresso(USART_Type * base,uint32_t baudrate,usart_parity_mode_t parity,usart_stop_bit_count_t stopbits,ByteSpan buffer,pw::clock_tree::ClockTree & clock_tree,pw::clock_tree::Element & clock_tree_element)26   UartStreamMcuxpresso(USART_Type* base,
27                        uint32_t baudrate,
28                        usart_parity_mode_t parity,
29                        usart_stop_bit_count_t stopbits,
30                        ByteSpan buffer,
31                        pw::clock_tree::ClockTree& clock_tree,
32                        pw::clock_tree::Element& clock_tree_element)
33       : base_(base),
34         config_{.base = base_,
35                 .srcclk = 0,
36                 .baudrate = baudrate,
37                 .parity = parity,
38                 .stopbits = stopbits,
39                 .buffer = reinterpret_cast<uint8_t*>(buffer.data()),
40                 .buffer_size = buffer.size()},
41         element_controller_(&clock_tree, &clock_tree_element) {}
42 
UartStreamMcuxpresso(USART_Type * base,uint32_t baudrate,usart_parity_mode_t parity,usart_stop_bit_count_t stopbits,ByteSpan buffer)43   UartStreamMcuxpresso(USART_Type* base,
44                        uint32_t baudrate,
45                        usart_parity_mode_t parity,
46                        usart_stop_bit_count_t stopbits,
47                        ByteSpan buffer)
48       : base_(base),
49         config_{.base = base_,
50                 .srcclk = 0,
51                 .baudrate = baudrate,
52                 .parity = parity,
53                 .stopbits = stopbits,
54                 .buffer = reinterpret_cast<uint8_t*>(buffer.data()),
55                 .buffer_size = buffer.size()} {}
56 
57   ~UartStreamMcuxpresso();
58 
59   pw::Status Init(uint32_t srcclk);
60 
61  private:
62   StatusWithSize DoRead(ByteSpan) override;
63   Status DoWrite(ConstByteSpan) override;
64 
65   USART_Type* base_;
66   struct rtos_usart_config config_;
67   usart_rtos_handle_t handle_;
68   usart_handle_t uart_handle_;
69   pw::clock_tree::ElementController element_controller_;
70 };
71 
72 }  // namespace pw::stream
73