oveRTOS C++ API
C++20 RAII wrappers for the oveRTOS C API
Loading...
Searching...
No Matches
spi.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_SPI
17
18#include <ove/spi.h>
19#include <ove/types.hpp>
20#include <ove/error.hpp>
21
22namespace ove
23{
24
31class Spi
32{
33 public:
38 explicit Spi(const struct ove_spi_cfg &cfg)
39 {
40#ifdef CONFIG_OVE_ZERO_HEAP
41 int err = ove_spi_init(&handle_, &storage_, &cfg);
42#else
43 int err = ove_spi_create(&handle_, &cfg);
44#endif
45 OVE_STATIC_INIT_ASSERT(err == OVE_OK);
46 }
47
48 ~Spi() noexcept
49 {
50 if (!handle_)
51 return;
52#ifdef CONFIG_OVE_ZERO_HEAP
53 ove_spi_deinit(handle_);
54#else
55 ove_spi_destroy(handle_);
56#endif
57 }
58
59 Spi(const Spi &) = delete;
60 Spi &operator=(const Spi &) = delete;
61
62#ifdef CONFIG_OVE_ZERO_HEAP
63 Spi(Spi &&) = delete;
64 Spi &operator=(Spi &&) = delete;
65#else
67 Spi(Spi &&o) noexcept : handle_(o.handle_)
68 {
69 o.handle_ = nullptr;
70 }
72 Spi &operator=(Spi &&o) noexcept
73 {
74 if (this != &o) {
75 if (handle_)
76 ove_spi_destroy(handle_);
77 handle_ = o.handle_;
78 o.handle_ = nullptr;
79 }
80 return *this;
81 }
82#endif
83
85 [[nodiscard]] Result<void>
86 transfer(const struct ove_spi_cs *cs, const void *tx, void *rx, size_t len,
87 std::chrono::nanoseconds timeout = wait_forever) noexcept
88 {
89 return from_rc(ove_spi_transfer(handle_, cs, tx, rx, len, to_timeout_ns(timeout)));
90 }
91
93 [[nodiscard]] Result<void> write(const struct ove_spi_cs *cs, const void *data, size_t len,
94 std::chrono::nanoseconds timeout = wait_forever) noexcept
95 {
96 return from_rc(ove_spi_write(handle_, cs, data, len, to_timeout_ns(timeout)));
97 }
98
100 [[nodiscard]] Result<void> read(const struct ove_spi_cs *cs, void *buf, size_t len,
101 std::chrono::nanoseconds timeout = wait_forever) noexcept
102 {
103 return from_rc(ove_spi_read(handle_, cs, buf, len, to_timeout_ns(timeout)));
104 }
105
107 [[nodiscard]] Result<void>
108 transfer_seq(const struct ove_spi_cs *cs, const struct ove_spi_xfer *xfers,
109 unsigned int num_xfers,
110 std::chrono::nanoseconds timeout = wait_forever) noexcept
111 {
112 return from_rc(ove_spi_transfer_seq(handle_, cs, xfers, num_xfers,
113 to_timeout_ns(timeout)));
114 }
115
117 ove_spi_t handle() const
118 {
119 return handle_;
120 }
121
122 private:
123 ove_spi_t handle_ = nullptr;
124#ifdef CONFIG_OVE_ZERO_HEAP
125 ove_spi_storage_t storage_{};
126#endif
127};
128
129} /* namespace ove */
130
131#endif /* CONFIG_OVE_SPI */
RAII wrapper around an oveRTOS SPI bus controller.
Definition spi.hpp:32
Spi & operator=(Spi &&o) noexcept
Move-assignment — destroys current bus, then takes o's handle.
Definition spi.hpp:72
Spi(Spi &&o) noexcept
Move constructor — transfers handle; source becomes empty.
Definition spi.hpp:67
Result< void > transfer_seq(const struct ove_spi_cs *cs, const struct ove_spi_xfer *xfers, unsigned int num_xfers, std::chrono::nanoseconds timeout=wait_forever) noexcept
Execute a sequence of transfers under a single CS assertion.
Definition spi.hpp:108
ove_spi_t handle() const
Returns the underlying C handle.
Definition spi.hpp:117
Result< void > read(const struct ove_spi_cs *cs, void *buf, size_t len, std::chrono::nanoseconds timeout=wait_forever) noexcept
Read-only transfer — transmit sends zeros.
Definition spi.hpp:100
Spi(const struct ove_spi_cfg &cfg)
Construct and initialise the SPI bus from cfg.
Definition spi.hpp:38
Result< void > transfer(const struct ove_spi_cs *cs, const void *tx, void *rx, size_t len, std::chrono::nanoseconds timeout=wait_forever) noexcept
Full-duplex transfer — sends tx and receives into rx.
Definition spi.hpp:86
Result< void > write(const struct ove_spi_cs *cs, const void *data, size_t len, std::chrono::nanoseconds timeout=wait_forever) noexcept
Write-only transfer — receive data is discarded.
Definition spi.hpp:93
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.