BNO08x Driver
API reference
Loading...
Searching...
No Matches
bno08x_comm_interface.hpp
Go to the documentation of this file.
1#pragma once
7#include <cstdint>
8#include <type_traits>
9#include <utility>
10
11// ============================================================================
12// Interface Type Enum
13// ============================================================================
14
27enum class BNO085Interface : uint8_t {
28 I2C = 0,
29 UARTRVC = 1,
30 UART = 2,
31 SPI = 3
32};
33
34// ============================================================================
35// CommInterface CRTP Base
36// ============================================================================
37
38namespace bno08x {
39
40// ============================================================================
41// GPIO Enums -- Standardised Control Pin Model
42// ============================================================================
43
60enum class CtrlPin : uint8_t {
61 RSTN = 0,
62 BOOTN,
63 WAKE,
64 PS0,
65 PS1
66};
67
76enum class GpioSignal : uint8_t {
77 INACTIVE = 0,
78 ACTIVE = 1
79};
80
81namespace detail {
82
84template <typename Port>
86 decltype(std::declval<Port&>().GpioSet(std::declval<CtrlPin>(), std::declval<GpioSignal>()));
87
89template <typename Port>
90inline constexpr bool kGpioSetReportsStatus =
91 std::is_same_v<std::remove_cv_t<std::remove_reference_t<GpioSetResult<Port>>>, bool>;
92
101template <typename Port>
102inline bool GpioSet(Port& port, CtrlPin pin, GpioSignal signal) noexcept {
103 using Result = GpioSetResult<Port>;
104 static_assert(std::is_void_v<Result> || kGpioSetReportsStatus<Port>,
105 "bno08x: the port's GpioSet(pin, signal) must return bool (true = pin driven, "
106 "false = GPIO error), or void for a port that never reports errors");
107 if constexpr (std::is_void_v<Result>) {
108 port.GpioSet(pin, signal);
109 return true;
110 } else {
111 return static_cast<bool>(port.GpioSet(pin, signal));
112 }
113}
114
116template <typename Port, typename = void>
117struct HasI2cAddressHooks : std::false_type {};
118
119template <typename Port>
120struct HasI2cAddressHooks<
121 Port, std::void_t<decltype(std::declval<Port&>().GetI2cAddress()),
122 decltype(std::declval<Port&>().SetI2cAddress(std::declval<uint8_t>()))>>
123 : std::true_type {};
125
135template <typename Port>
136inline constexpr bool kHasI2cAddressHooks = HasI2cAddressHooks<Port>::value;
137
138} // namespace detail
139
161template <typename Derived>
163public:
164 // --------------------------------------------------------------------------
167
177 return static_cast<Derived*>(this)->GetInterfaceType();
178 }
179
181
182 // --------------------------------------------------------------------------
185
195 bool Open() noexcept {
196 return static_cast<Derived*>(this)->Open();
197 }
198
208 void Close() noexcept {
209 static_cast<Derived*>(this)->Close();
210 }
211
222 int Write(const uint8_t* data, uint32_t length) noexcept {
223 return static_cast<Derived*>(this)->Write(data, length);
224 }
225
238 int Read(uint8_t* data, uint32_t length) noexcept {
239 return static_cast<Derived*>(this)->Read(data, length);
240 }
241
250 bool DataAvailable() noexcept {
251 return static_cast<Derived*>(this)->DataAvailable();
252 }
253
262 void Delay(uint32_t ms) noexcept {
263 static_cast<Derived*>(this)->Delay(ms);
264 }
265
275 uint32_t GetTimeUs() noexcept {
276 return static_cast<Derived*>(this)->GetTimeUs();
277 }
278
280
281 // --------------------------------------------------------------------------
288
304 bool GpioSet(CtrlPin pin, GpioSignal signal) noexcept {
305 return detail::GpioSet(*static_cast<Derived*>(this), pin, signal);
306 }
307
316 bool GpioSetActive(CtrlPin pin) noexcept {
317 return GpioSet(pin, GpioSignal::ACTIVE);
318 }
319
328 bool GpioSetInactive(CtrlPin pin) noexcept {
329 return GpioSet(pin, GpioSignal::INACTIVE);
330 }
331
333
334protected:
336 CommInterface() = default;
338 ~CommInterface() = default;
339
340 CommInterface(const CommInterface&) = delete;
342 CommInterface(CommInterface&&) noexcept = default;
343 CommInterface& operator=(CommInterface&&) noexcept = default;
344};
345
346} // namespace bno08x
BNO085Interface
Identifies the host interface type that a CommInterface provides.
Definition bno08x_comm_interface.hpp:27
@ UARTRVC
UART in RVC mode (PS1=0, PS0=1). RVC frames only.
@ SPI
SPI bus (PS1=1, PS0=1). Supports SH-2 and DFU.
@ UART
UART in SH-2 mode (PS1=1, PS0=0). Supports SH-2 and DFU.
@ I2C
I2C bus (PS1=0, PS0=0). Supports SH-2 and DFU.
CRTP base class for BNO08x communication interfaces.
Definition bno08x_comm_interface.hpp:162
CommInterface()=default
Protected constructor – prevents direct instantiation.
bool DataAvailable() noexcept
Check whether the sensor has data ready.
Definition bno08x_comm_interface.hpp:250
CommInterface(CommInterface &&) noexcept=default
Movable.
CommInterface & operator=(const CommInterface &)=delete
Non-copyable.
void Close() noexcept
Close the communication bus and release resources.
Definition bno08x_comm_interface.hpp:208
CommInterface(const CommInterface &)=delete
Non-copyable.
uint32_t GetTimeUs() noexcept
Return the current monotonic time in microseconds.
Definition bno08x_comm_interface.hpp:275
bool GpioSet(CtrlPin pin, GpioSignal signal) noexcept
Set a control pin to the specified signal state.
Definition bno08x_comm_interface.hpp:304
int Write(const uint8_t *data, uint32_t length) noexcept
Write raw bytes to the sensor.
Definition bno08x_comm_interface.hpp:222
bool Open() noexcept
Open and initialise the communication bus.
Definition bno08x_comm_interface.hpp:195
~CommInterface()=default
Protected destructor – prevent polymorphic deletion through base.
void Delay(uint32_t ms) noexcept
Block for a specified duration.
Definition bno08x_comm_interface.hpp:262
int Read(uint8_t *data, uint32_t length) noexcept
Read raw bytes from the sensor.
Definition bno08x_comm_interface.hpp:238
bool GpioSetInactive(CtrlPin pin) noexcept
Deassert a control pin (set to INACTIVE).
Definition bno08x_comm_interface.hpp:328
bool GpioSetActive(CtrlPin pin) noexcept
Assert a control pin (set to ACTIVE).
Definition bno08x_comm_interface.hpp:316
BNO085Interface GetInterfaceType() noexcept
Report the transport type this implementation provides.
Definition bno08x_comm_interface.hpp:176
constexpr bool kGpioSetReportsStatus
True when the port's GpioSet() reports a status (returns bool).
Definition bno08x_comm_interface.hpp:90
constexpr bool kHasI2cAddressHooks
True when the port provides the optional I2C address hooks.
Definition bno08x_comm_interface.hpp:136
decltype(std::declval< Port & >().GpioSet(std::declval< CtrlPin >(), std::declval< GpioSignal >())) GpioSetResult
Return type of Port::GpioSet(CtrlPin, GpioSignal).
Definition bno08x_comm_interface.hpp:86
bool GpioSet(Port &port, CtrlPin pin, GpioSignal signal) noexcept
Call the port's GpioSet() and return its status.
Definition bno08x_comm_interface.hpp:102
Definition bno08x.hpp:35
CtrlPin
Identifies the hardware control pins of the BNO08x.
Definition bno08x_comm_interface.hpp:60
@ PS0
Protocol select bit 0 (active-high on the physical pin)
@ BOOTN
Bootloader entry (active-low on the physical pin)
@ RSTN
Hardware reset (active-low on the physical pin)
@ PS1
Protocol select bit 1 (active-high on the physical pin)
@ WAKE
Wake from suspend, SPI mode only (active-low on the physical pin)
GpioSignal
Abstract signal level for control pins.
Definition bno08x_comm_interface.hpp:76
@ ACTIVE
Pin function is asserted.
@ INACTIVE
Pin function is deasserted.