Anthony DiGirolamo | eea0d77 | 2020-08-06 12:00:36 -0700 | [diff] [blame] | 1 | // Copyright 2019 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 <Arduino.h> |
| 16 | |
| 17 | #include <cinttypes> |
| 18 | #include <cstdint> |
| 19 | |
| 20 | #include "pw_preprocessor/compiler.h" |
| 21 | #include "pw_sys_io/sys_io.h" |
| 22 | |
| 23 | extern "C" void pw_sys_io_Init() { |
| 24 | Serial.begin(115200); |
| 25 | // Wait for serial port to be available |
| 26 | while (!Serial) { |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | namespace pw::sys_io { |
| 31 | |
| 32 | // Wait for a byte to read on USART1. This blocks until a byte is read. This is |
| 33 | // extremely inefficient as it requires the target to burn CPU cycles polling to |
| 34 | // see if a byte is ready yet. |
| 35 | |
| 36 | Status ReadByte(std::byte* dest) { |
| 37 | while (true) { |
| 38 | if (Serial.available()) { |
| 39 | *dest = static_cast<std::byte>(Serial.read()); |
| 40 | break; |
| 41 | } |
| 42 | } |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame^] | 43 | return Status::Ok(); |
Anthony DiGirolamo | eea0d77 | 2020-08-06 12:00:36 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | // Send a byte over USART1. Since this blocks on every byte, it's rather |
| 47 | // inefficient. At the default baud rate of 115200, one byte blocks the CPU for |
| 48 | // ~87 micro seconds. This means it takes only 10 bytes to block the CPU for |
| 49 | // 1ms! |
| 50 | Status WriteByte(std::byte b) { |
| 51 | // Wait for TX buffer to be empty. When the buffer is empty, we can write |
| 52 | // a value to be dumped out of UART. |
| 53 | while (Serial.availableForWrite() < 1) { |
| 54 | } |
| 55 | Serial.write((uint8_t)b); |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame^] | 56 | return Status::Ok(); |
Anthony DiGirolamo | eea0d77 | 2020-08-06 12:00:36 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | // Writes a string using pw::sys_io, and add newline characters at the end. |
| 60 | StatusWithSize WriteLine(const std::string_view& s) { |
| 61 | size_t chars_written = 0; |
| 62 | StatusWithSize result = WriteBytes(std::as_bytes(std::span(s))); |
| 63 | if (!result.ok()) { |
| 64 | return result; |
| 65 | } |
| 66 | chars_written += result.size(); |
| 67 | |
| 68 | // Write trailing newline. |
| 69 | result = WriteBytes(std::as_bytes(std::span("\r\n", 2))); |
| 70 | chars_written += result.size(); |
| 71 | |
| 72 | return StatusWithSize(result.status(), chars_written); |
| 73 | } |
| 74 | |
| 75 | } // namespace pw::sys_io |