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
21namespace ove {
22
29class Spi {
30public:
31 explicit Spi(const struct ove_spi_cfg &cfg) {
32#ifdef CONFIG_OVE_ZERO_HEAP
33 int err = ove_spi_init(&handle_, &storage_, &cfg);
34#else
35 int err = ove_spi_create(&handle_, &cfg);
36#endif
37 OVE_STATIC_INIT_ASSERT(err == OVE_OK);
38 }
39
40 ~Spi() {
41 if (!handle_) return;
42#ifdef CONFIG_OVE_ZERO_HEAP
43 ove_spi_deinit(handle_);
44#else
45 ove_spi_destroy(handle_);
46#endif
47 }
48
49 Spi(const Spi &) = delete;
50 Spi &operator=(const Spi &) = delete;
51
52#ifdef CONFIG_OVE_ZERO_HEAP
53 Spi(Spi &&) = delete;
54 Spi &operator=(Spi &&) = delete;
55#else
56 Spi(Spi &&o) noexcept : handle_(o.handle_) { o.handle_ = nullptr; }
57 Spi &operator=(Spi &&o) noexcept {
58 if (this != &o) {
59 if (handle_) ove_spi_destroy(handle_);
60 handle_ = o.handle_;
61 o.handle_ = nullptr;
62 }
63 return *this;
64 }
65#endif
66
67 [[nodiscard]] int transfer(const struct ove_spi_cs *cs,
68 const void *tx, void *rx, size_t len,
69 uint32_t timeout_ms = OVE_WAIT_FOREVER) {
70 return ove_spi_transfer(handle_, cs, tx, rx, len, timeout_ms);
71 }
72
73 [[nodiscard]] int write(const struct ove_spi_cs *cs,
74 const void *data, size_t len,
75 uint32_t timeout_ms = OVE_WAIT_FOREVER) {
76 return ove_spi_write(handle_, cs, data, len, timeout_ms);
77 }
78
79 [[nodiscard]] int read(const struct ove_spi_cs *cs,
80 void *buf, size_t len,
81 uint32_t timeout_ms = OVE_WAIT_FOREVER) {
82 return ove_spi_read(handle_, cs, buf, len, timeout_ms);
83 }
84
85 [[nodiscard]] int transfer_seq(const struct ove_spi_cs *cs,
86 const struct ove_spi_xfer *xfers,
87 unsigned int num_xfers,
88 uint32_t timeout_ms = OVE_WAIT_FOREVER) {
89 return ove_spi_transfer_seq(handle_, cs, xfers, num_xfers,
90 timeout_ms);
91 }
92
93 ove_spi_t native_handle() const { return handle_; }
94
95private:
96 ove_spi_t handle_ = nullptr;
97#ifdef CONFIG_OVE_ZERO_HEAP
98 ove_spi_storage_t storage_{};
99#endif
100};
101
102} /* namespace ove */
103
104#endif /* CONFIG_OVE_SPI */
void write(const char *data, unsigned int len)
Writes a buffer of bytes to the console output.
Definition console.hpp:71
int read(const char *key, void *buf, size_t len, size_t *out)
Reads the value associated with a key from NVS.
Definition nvs.hpp:55
Top-level namespace for all oveRTOS C++ abstractions.
Definition app.hpp:19
Common type definitions and concepts for the C++ wrapper layer.