xref: /aosp_15_r20/external/pigweed/pw_sys_io_mcuxpresso/sys_io.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2021 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_sys_io/sys_io.h"
16 
17 #include <cinttypes>
18 #include <string_view>
19 
20 #include "board.h"
21 #include "fsl_common.h"
22 #include "fsl_debug_console.h"
23 #include "pw_status/status.h"
24 #include "pw_status/status_with_size.h"
25 
pw_sys_io_mcuxpresso_Init(void)26 extern "C" void pw_sys_io_mcuxpresso_Init(void) { BOARD_InitDebugConsole(); }
27 
28 namespace pw::sys_io {
29 
30 // Wait for a byte to read on USART0. This blocks until a byte is read. This is
31 // extremely inefficient as it requires the target to burn CPU cycles polling to
32 // see if a byte is ready yet.
ReadByte(std::byte * dest)33 Status ReadByte(std::byte* dest) {
34   *dest = static_cast<std::byte>(DbgConsole_Getchar());
35   return OkStatus();
36 }
37 
38 #if DEBUG_CONSOLE_TRANSFER_NON_BLOCKING
39 // Read a byte from USART0 if one is available.
TryReadByte(std::byte * dest)40 Status TryReadByte(std::byte* dest) {
41   char ch = 0;
42   status_t status = DbgConsole_TryGetchar(&ch);
43   if (status != kStatus_Success) {
44     return Status::Unavailable();
45   }
46 
47   *dest = static_cast<std::byte>(ch);
48   return OkStatus();
49 }
50 #else   // !DEBUG_CONSOLE_TRANSFER_NONBLOCKING
TryReadByte(std::byte *)51 Status TryReadByte(std::byte* /*dest*/) { return Status::Unimplemented(); }
52 #endif  // DEBUG_CONSOLE_TRANSFER_NONBLOCKING
53 
54 // Send a byte over USART0. Since this blocks on every byte, it's rather
55 // inefficient. At the default baud rate of 115200, one byte blocks the CPU for
56 // ~87 micro seconds. This means it takes only 10 bytes to block the CPU for
57 // 1ms!
WriteByte(std::byte b)58 Status WriteByte(std::byte b) {
59   int ch = static_cast<int>(b);
60   int len = DbgConsole_Putchar(ch);
61   return len == 1 ? OkStatus() : Status::Unknown();
62 }
63 
64 // Writes a string using pw::sys_io, and add newline characters at the end.
WriteLine(std::string_view s)65 StatusWithSize WriteLine(std::string_view s) {
66   size_t chars_written = 0;
67   StatusWithSize result = WriteBytes(as_bytes(span(s)));
68   if (!result.ok()) {
69     return result;
70   }
71   chars_written += result.size();
72 
73   // Write trailing newline.
74   result = WriteBytes(as_bytes(span("\r\n", 2)));
75   chars_written += result.size();
76 
77   return StatusWithSize(result.status(), chars_written);
78 }
79 
80 }  // namespace pw::sys_io
81