idfxx 1.0.0
Modern C++23 components for ESP-IDF
Loading...
Searching...
No Matches
uart.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Chris Leishman
3
4#pragma once
5
16#include <hal/uart_types.h>
17#include <soc/soc_caps.h>
18#include <string>
19
20namespace idfxx {
21
33enum class uart_port : int {
36#if SOC_UART_HP_NUM > 2
38#endif
39#if SOC_UART_HP_NUM > 3
41#endif
42#if SOC_UART_HP_NUM > 4
44#endif
45#if SOC_UART_LP_NUM >= 1
47#endif
48};
49
56[[nodiscard]] inline std::string to_string(uart_port p) {
57 switch (p) {
59 return "UART0";
61 return "UART1";
62#if SOC_UART_HP_NUM > 2
63 case uart_port::uart2:
64 return "UART2";
65#endif
66#if SOC_UART_HP_NUM > 3
67 case uart_port::uart3:
68 return "UART3";
69#endif
70#if SOC_UART_HP_NUM > 4
71 case uart_port::uart4:
72 return "UART4";
73#endif
74#if SOC_UART_LP_NUM >= 1
75 case uart_port::lp_uart0:
76 return "LP_UART0";
77#endif
78 default:
79 return "unknown(" + std::to_string(static_cast<int>(p)) + ")";
80 }
81}
82
// end of idfxx_uart
84
85} // namespace idfxx
86
87#include "sdkconfig.h"
88#ifdef CONFIG_IDFXX_STD_FORMAT
90#include <algorithm>
91#include <format>
92namespace std {
93template<>
94struct formatter<idfxx::uart_port> {
95 constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
96
97 template<typename FormatContext>
98 auto format(idfxx::uart_port p, FormatContext& ctx) const {
99 auto s = to_string(p);
100 return std::copy(s.begin(), s.end(), ctx.out());
101 }
102};
103} // namespace std
105#endif // CONFIG_IDFXX_STD_FORMAT
std::string to_string(core_id c)
Returns a string representation of a CPU core identifier.
Definition cpu.hpp:52
uart_port
Identifies a UART port.
Definition uart.hpp:33
std::expected< T, std::error_code > result
result type wrapping a value or error code.
Definition error.hpp:120