3 * Copyright (c) 2021 Project CHIP Authors
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include "argtable3/argtable3.h"
19 #include "driver/uart.h"
20 #include "esp_console.h"
23 #include "esp_system.h"
24 #include "esp_vfs_dev.h"
25 #include "esp_vfs_fat.h"
26 #include "linenoise/linenoise.h"
27 #include "pw_sys_io/sys_io.h"
33 #define ECHO_TEST_TXD (CONFIG_EXAMPLE_UART_TXD)
34 #define ECHO_TEST_RXD (CONFIG_EXAMPLE_UART_RXD)
35 #define ECHO_TEST_RTS (UART_PIN_NO_CHANGE)
36 #define ECHO_TEST_CTS (UART_PIN_NO_CHANGE)
38 #define ECHO_UART_PORT_NUM (CONFIG_EXAMPLE_UART_PORT_NUM)
39 #define ECHO_UART_BAUD_RATE (CONFIG_EXAMPLE_UART_BAUD_RATE)
41 int console_getchar(uint8_t * chr)
43 return uart_read_bytes(ECHO_UART_PORT_NUM, chr, 1, portMAX_DELAY);
46 int console_putchar(const char * chr)
48 return uart_write_bytes(ECHO_UART_PORT_NUM, chr, 1);
51 void console_init(void)
53 /* Configure parameters of an UART driver,
54 * communication pins and install the driver */
55 uart_config_t uart_config = {
56 .baud_rate = ECHO_UART_BAUD_RATE,
57 .data_bits = UART_DATA_8_BITS,
58 .parity = UART_PARITY_DISABLE,
59 .stop_bits = UART_STOP_BITS_1,
60 .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
61 .source_clk = UART_SCLK_APB,
63 int intr_alloc_flags = 0;
65 ESP_ERROR_CHECK(uart_driver_install(ECHO_UART_PORT_NUM, 256, 0, 0, NULL, intr_alloc_flags));
66 ESP_ERROR_CHECK(uart_param_config(ECHO_UART_PORT_NUM, &uart_config));
67 ESP_ERROR_CHECK(uart_set_pin(ECHO_UART_PORT_NUM, ECHO_TEST_TXD, ECHO_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS));
72 extern "C" void pw_sys_io_Init()
77 namespace pw::sys_io {
79 Status ReadByte(std::byte * dest)
82 return Status::InvalidArgument();
84 int ret = console_getchar(reinterpret_cast<uint8_t *>(dest));
85 return ret < 0 ? Status::FailedPrecondition() : OkStatus();
88 Status WriteByte(std::byte b)
90 int ret = console_putchar(reinterpret_cast<const char *>(&b));
91 return ret < 0 ? Status::FailedPrecondition() : OkStatus();
94 // Writes a string using pw::sys_io, and add newline characters at the end.
95 StatusWithSize WriteLine(const std::string_view & s)
97 size_t chars_written = 0;
98 StatusWithSize result = WriteBytes(std::as_bytes(std::span(s)));
103 chars_written += result.size();
104 result = WriteBytes(std::as_bytes(std::span("\r\n", 2)));
105 chars_written += result.size();
107 return StatusWithSize(result.status(), chars_written);
110 } // namespace pw::sys_io