oveRTOS C++ API
C++20 RAII wrappers for the oveRTOS C API
Loading...
Searching...
No Matches
i2c.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 Kamil Lulko <kamil.lulko@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 *
6 * This file is part of oveRTOS.
7 */
8
14#pragma once
15
16#ifdef CONFIG_OVE_I2C
17
18#include <ove/i2c.h>
19#include <ove/types.hpp>
20#include <ove/error.hpp>
21
22namespace ove
23{
24
31class I2c
32{
33 public:
38 explicit I2c(const struct ove_i2c_cfg &cfg)
39 {
40#ifdef CONFIG_OVE_ZERO_HEAP
41 int err = ove_i2c_init(&handle_, &storage_, &cfg);
42#else
43 int err = ove_i2c_create(&handle_, &cfg);
44#endif
45 OVE_STATIC_INIT_ASSERT(err == OVE_OK);
46 }
47
48 ~I2c() noexcept
49 {
50 if (!handle_)
51 return;
52#ifdef CONFIG_OVE_ZERO_HEAP
53 ove_i2c_deinit(handle_);
54#else
55 ove_i2c_destroy(handle_);
56#endif
57 }
58
59 I2c(const I2c &) = delete;
60 I2c &operator=(const I2c &) = delete;
61
62#ifdef CONFIG_OVE_ZERO_HEAP
63 I2c(I2c &&) = delete;
64 I2c &operator=(I2c &&) = delete;
65#else
67 I2c(I2c &&o) noexcept : handle_(o.handle_)
68 {
69 o.handle_ = nullptr;
70 }
72 I2c &operator=(I2c &&o) noexcept
73 {
74 if (this != &o) {
75 if (handle_)
76 ove_i2c_destroy(handle_);
77 handle_ = o.handle_;
78 o.handle_ = nullptr;
79 }
80 return *this;
81 }
82#endif
83
85 [[nodiscard]] Result<void> write(uint16_t addr, const void *data, size_t len,
86 std::chrono::nanoseconds timeout = wait_forever) noexcept
87 {
88 return from_rc(ove_i2c_write(handle_, addr, data, len, to_timeout_ns(timeout)));
89 }
90
92 [[nodiscard]] Result<void> read(uint16_t addr, void *buf, size_t len,
93 std::chrono::nanoseconds timeout = wait_forever) noexcept
94 {
95 return from_rc(ove_i2c_read(handle_, addr, buf, len, to_timeout_ns(timeout)));
96 }
97
99 [[nodiscard]] Result<void>
100 write_read(uint16_t addr, const void *tx, size_t tx_len, void *rx, size_t rx_len,
101 std::chrono::nanoseconds timeout = wait_forever) noexcept
102 {
103 return from_rc(ove_i2c_write_read(handle_, addr, tx, tx_len, rx, rx_len,
104 to_timeout_ns(timeout)));
105 }
106
108 [[nodiscard]] Result<void>
109 reg_write(uint16_t addr, uint8_t reg, const void *data, size_t len,
110 std::chrono::nanoseconds timeout = wait_forever) noexcept
111 {
112 return from_rc(
113 ove_i2c_reg_write(handle_, addr, reg, data, len, to_timeout_ns(timeout)));
114 }
115
117 [[nodiscard]] Result<void>
118 reg_read(uint16_t addr, uint8_t reg, void *buf, size_t len,
119 std::chrono::nanoseconds timeout = wait_forever) noexcept
120 {
121 return from_rc(
122 ove_i2c_reg_read(handle_, addr, reg, buf, len, to_timeout_ns(timeout)));
123 }
124
126 [[nodiscard]] Result<void> probe(uint16_t addr,
127 std::chrono::nanoseconds timeout = wait_forever) noexcept
128 {
129 return from_rc(ove_i2c_probe(handle_, addr, to_timeout_ns(timeout)));
130 }
131
133 ove_i2c_t handle() const
134 {
135 return handle_;
136 }
137
138 private:
139 ove_i2c_t handle_ = nullptr;
140#ifdef CONFIG_OVE_ZERO_HEAP
141 ove_i2c_storage_t storage_{};
142#endif
143};
144
145} /* namespace ove */
146
147#endif /* CONFIG_OVE_I2C */
RAII wrapper around an oveRTOS I2C bus controller.
Definition i2c.hpp:32
Result< void > write_read(uint16_t addr, const void *tx, size_t tx_len, void *rx, size_t rx_len, std::chrono::nanoseconds timeout=wait_forever) noexcept
Combined write-then-read transaction with a repeated start.
Definition i2c.hpp:100
I2c & operator=(I2c &&o) noexcept
Move-assignment — destroys current bus, then takes o's handle.
Definition i2c.hpp:72
ove_i2c_t handle() const
Returns the underlying C handle.
Definition i2c.hpp:133
I2c(I2c &&o) noexcept
Move constructor — transfers handle; source becomes empty.
Definition i2c.hpp:67
Result< void > read(uint16_t addr, void *buf, size_t len, std::chrono::nanoseconds timeout=wait_forever) noexcept
Read len bytes from slave addr into buf.
Definition i2c.hpp:92
Result< void > reg_read(uint16_t addr, uint8_t reg, void *buf, size_t len, std::chrono::nanoseconds timeout=wait_forever) noexcept
Read len bytes from register reg on slave addr.
Definition i2c.hpp:118
I2c(const struct ove_i2c_cfg &cfg)
Construct and initialise the I2C bus from cfg.
Definition i2c.hpp:38
Result< void > reg_write(uint16_t addr, uint8_t reg, const void *data, size_t len, std::chrono::nanoseconds timeout=wait_forever) noexcept
Write len bytes to register reg on slave addr.
Definition i2c.hpp:109
Result< void > write(uint16_t addr, const void *data, size_t len, std::chrono::nanoseconds timeout=wait_forever) noexcept
Write len bytes to slave addr.
Definition i2c.hpp:85
Result< void > probe(uint16_t addr, std::chrono::nanoseconds timeout=wait_forever) noexcept
Probe slave addr — empty Result<void> if the device ACKs.
Definition i2c.hpp:126
Strong ove::Error type, Result<T> alias, and std::error_code interop for the oveRTOS C++ binding.
Top-level namespace for all oveRTOS C++ abstractions.
Definition app.hpp:20
constexpr uint64_t to_timeout_ns(std::chrono::duration< Rep, Period > d) noexcept
Convert a chrono duration to uint64_t nanoseconds for the C API.
Definition types.hpp:129
constexpr std::chrono::nanoseconds wait_forever
Sentinel duration meaning "block indefinitely".
Definition types.hpp:119
Result< void > from_rc(int rc) noexcept
Lifts a substrate rc-code into a Result<void>.
Definition error.hpp:254
std::expected< T, Error > Result
std::expected-based result alias.
Definition error.hpp:139
Common type definitions and concepts for the C++ wrapper layer.