idfxx 1.0.0
Modern C++23 components for ESP-IDF
Loading...
Searching...
No Matches
cpu.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 <soc/soc_caps.h>
17#include <string>
18
19namespace idfxx {
20
39enum class core_id : unsigned int {
40 core_0 = 0,
41#if SOC_CPU_CORES_NUM > 1
42 core_1 = 1,
43#endif
44};
45
52[[nodiscard]] inline std::string to_string(core_id c) {
53 switch (c) {
54 case core_id::core_0:
55 return "CORE_0";
56#if SOC_CPU_CORES_NUM > 1
57 case core_id::core_1:
58 return "CORE_1";
59#endif
60 default:
61 return "unknown(" + std::to_string(static_cast<unsigned int>(c)) + ")";
62 }
63}
64
85public:
88 : _value(0) {}
89
97 constexpr task_priority(unsigned int value) noexcept
98 : _value(value) {}
99
104 [[nodiscard]] constexpr unsigned int value() const noexcept { return _value; }
105
107 [[nodiscard]] constexpr auto operator<=>(const task_priority&) const noexcept = default;
108
109private:
110 unsigned int _value;
111};
112
119[[nodiscard]] inline std::string to_string(task_priority p) {
120 return std::to_string(p.value());
121}
122
// end of idfxx_cpu
124
125} // namespace idfxx
126
127#include "sdkconfig.h"
128#ifdef CONFIG_IDFXX_STD_FORMAT
130#include <algorithm>
131#include <format>
132namespace std {
133template<>
134struct formatter<idfxx::core_id> {
135 constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
136
137 template<typename FormatContext>
138 auto format(idfxx::core_id c, FormatContext& ctx) const {
139 auto s = to_string(c);
140 return std::copy(s.begin(), s.end(), ctx.out());
141 }
142};
143
144template<>
145struct formatter<idfxx::task_priority> {
146 constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
147
148 template<typename FormatContext>
149 auto format(idfxx::task_priority p, FormatContext& ctx) const {
150 auto s = to_string(p);
151 return std::copy(s.begin(), s.end(), ctx.out());
152 }
153};
154} // namespace std
156#endif // CONFIG_IDFXX_STD_FORMAT
Type-safe wrapper for FreeRTOS task priority values.
Definition cpu.hpp:84
constexpr task_priority() noexcept
Default constructor.
Definition cpu.hpp:87
constexpr auto operator<=>(const task_priority &) const noexcept=default
Default three-way comparison.
constexpr task_priority(unsigned int value) noexcept
Constructs from an unsigned integer value.
Definition cpu.hpp:97
constexpr unsigned int value() const noexcept
Returns the raw priority value.
Definition cpu.hpp:104
core_id
Identifies a specific CPU core.
Definition cpu.hpp:39
std::string to_string(core_id c)
Returns a string representation of a CPU core identifier.
Definition cpu.hpp:52
std::expected< T, std::error_code > result
result type wrapping a value or error code.
Definition error.hpp:120