Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / examples / platform / esp32 / pw_sys_io / sys_io_esp32.cc
1 /*
2  *
3  *    Copyright (c) 2021 Project CHIP Authors
4  *
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #include "argtable3/argtable3.h"
19 #include "driver/uart.h"
20 #include "esp_console.h"
21 #include "esp_err.h"
22 #include "esp_log.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"
28 #include <cassert>
29 #include <cinttypes>
30 #include <stdio.h>
31 #include <string.h>
32
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)
37
38 #define ECHO_UART_PORT_NUM (CONFIG_EXAMPLE_UART_PORT_NUM)
39 #define ECHO_UART_BAUD_RATE (CONFIG_EXAMPLE_UART_BAUD_RATE)
40
41 int console_getchar(uint8_t * chr)
42 {
43     return uart_read_bytes(ECHO_UART_PORT_NUM, chr, 1, portMAX_DELAY);
44 }
45
46 int console_putchar(const char * chr)
47 {
48     return uart_write_bytes(ECHO_UART_PORT_NUM, chr, 1);
49 }
50
51 void console_init(void)
52 {
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,
62     };
63     int intr_alloc_flags = 0;
64
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));
68
69     return;
70 }
71
72 extern "C" void pw_sys_io_Init()
73 {
74     console_init();
75 }
76
77 namespace pw::sys_io {
78
79 Status ReadByte(std::byte * dest)
80 {
81     if (!dest)
82         return Status::InvalidArgument();
83
84     int ret = console_getchar(reinterpret_cast<uint8_t *>(dest));
85     return ret < 0 ? Status::FailedPrecondition() : OkStatus();
86 }
87
88 Status WriteByte(std::byte b)
89 {
90     int ret = console_putchar(reinterpret_cast<const char *>(&b));
91     return ret < 0 ? Status::FailedPrecondition() : OkStatus();
92 }
93
94 // Writes a string using pw::sys_io, and add newline characters at the end.
95 StatusWithSize WriteLine(const std::string_view & s)
96 {
97     size_t chars_written  = 0;
98     StatusWithSize result = WriteBytes(std::as_bytes(std::span(s)));
99     if (!result.ok())
100     {
101         return result;
102     }
103     chars_written += result.size();
104     result = WriteBytes(std::as_bytes(std::span("\r\n", 2)));
105     chars_written += result.size();
106
107     return StatusWithSize(result.status(), chars_written);
108 }
109
110 } // namespace pw::sys_io