BNO08x Driver
API reference
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | List of all members
BNO085< CommType > Class Template Reference

Unified driver for the BNO08x IMU – SH-2 mode, RVC mode, and DFU. More...

#include <bno08x.hpp>

Public Member Functions

 BNO085 (CommType &comm) noexcept
 Construct the driver with a communication interface.
 
 ~BNO085 () noexcept
 Destructor. Closes active SH-2/RVC sessions.
 
RVC Mode API

These methods are valid when GetInterfaceType() returns UARTRVC. RVC mode provides simplified yaw/pitch/roll + acceleration data via UART at 115200 baud without the overhead of the SH-2 protocol.

void SetRvcCallback (RvcCallback cb) noexcept
 Register a callback for decoded RVC frames.
 
bool BeginRvc () noexcept
 Initialise RVC mode.
 
void ServiceRvc () noexcept
 Poll for RVC frames and dispatch callbacks.
 
void CloseRvc () noexcept
 Stop RVC processing and close the transport.
 

Static Public Member Functions

static constexpr const char * GetDriverVersion () noexcept
 Get the compiled driver version string.
 
static constexpr uint8_t GetDriverVersionMajor () noexcept
 Get the compiled driver major version number.
 
static constexpr uint8_t GetDriverVersionMinor () noexcept
 Get the compiled driver minor version number.
 
static constexpr uint8_t GetDriverVersionPatch () noexcept
 Get the compiled driver patch version number.
 

Common API

These methods work regardless of the interface type.

static constexpr uint32_t RESET_STARTUP_MS = 90 + 4
 Wait after releasing NRST before the hub can be addressed, in ms.
 
static constexpr uint8_t I2C_ADDRESS_APP = 0x4A
 Application I2C address with SA0 low (datasheet figure 1-12); 0x4B with SA0 high.
 
static constexpr uint8_t I2C_ADDRESS_BOOTLOADER = 0x28
 Bootloader I2C address with SA0 low (datasheet section 1.4); 0x29 with SA0 high.
 
int GetLastError () const
 Get the last error code from the SH-2 driver.
 
BNO085DriverState GetState () const noexcept
 Get the current high-level driver state.
 
bool HardwareReset (uint32_t lowMs=2) noexcept
 Perform a hardware reset via the RSTN pin.
 
bool SetBootPin (bool state) noexcept
 Drive the BOOTN pin to enter/exit DFU bootloader mode.
 
bool SetWakePin (bool state) noexcept
 Drive the WAKE pin (SPI mode only).
 
bool SelectInterface (BNO085Interface iface) noexcept
 Select the host interface by driving PS0/PS1 pins.
 
int Dfu (const HcBin_t &fw=firmware) noexcept
 Perform a Device Firmware Update (DFU).
 
int DfuWithOptions (const HcBin_t &fw, const DfuOptions &options) noexcept
 Perform DFU with explicit validation and transfer options.
 
int DfuFromMemory (const DfuMemoryImage &image, const DfuOptions &options={}) noexcept
 Perform DFU from an in-memory firmware image.
 
int DfuFromMemory (const uint8_t *data, uint32_t len, const char *partNumber="1000-3608", const DfuOptions &options={}) noexcept
 Convenience overload for in-memory DFU.
 
bool EnterBootloader (uint32_t resetLowMs=10, uint32_t settleMs=50) noexcept
 Enter bootloader mode using BOOTN + reset pin sequence.
 
bool ExitBootloaderAndReboot (uint32_t resetLowMs=2, uint32_t settleMs=100) noexcept
 Exit bootloader mode and reboot into application firmware.
 
int RunDfuFromMemory (const DfuMemoryImage &image, const DfuOptions &options={}, uint32_t enterResetLowMs=10, uint32_t enterSettleMs=50, uint32_t exitResetLowMs=2, uint32_t exitSettleMs=100) noexcept
 Full class-aware DFU workflow: enter bootloader -> transfer firmware -> reboot to app.
 
static constexpr uint8_t BootloaderI2cAddress (uint8_t appAddress) noexcept
 Bootloader I2C address for a port configured at appAddress.
 

SH-2 Mode API

These methods are valid when GetInterfaceType() returns I2C, SPI, or UART. They provide full access to the BNO08x sensor hub via the SH-2 protocol.

bool Begin () noexcept
 Initialise the sensor in SH-2 mode.
 
void Close () noexcept
 Close the currently active session and release transport resources.
 
bool EnableSensor (BNO085Sensor sensor, uint32_t interval_ms, float sensitivity=0.0f) noexcept
 Enable periodic reporting for a sensor.
 
bool DisableSensor (BNO085Sensor sensor) noexcept
 Disable reporting for a sensor.
 
void SetCallback (SensorCallback cb) noexcept
 Register a callback for SH-2 sensor events.
 
bool HasNewData (BNO085Sensor sensor) const
 Check if new data is available for a sensor.
 
SensorEvent GetLatest (BNO085Sensor sensor) noexcept
 Retrieve the most recent event for a sensor.
 
void Update () noexcept
 Pump the SH-2 service loop.
 
static bool Sh2SessionInUse () noexcept
 True while any BNO085 object in the process holds the SH-2 session.
 

Detailed Description

template<typename CommType>
class BNO085< CommType >

Unified driver for the BNO08x IMU – SH-2 mode, RVC mode, and DFU.

A single template class that adapts to the transport type at compile time. The CommInterface::GetInterfaceType() method determines which mode of operation is valid:

Interface Type Valid Operations
I2C / SPI / UART Begin(), Update(), EnableSensor(), Dfu(), etc.
UARTRVC BeginRvc(), ServiceRvc(), CloseRvc()

Calling a mode-incompatible method returns false or SH2_ERR gracefully. The active mode is tracked by an internal state machine exposed via GetState(). Illegal transitions are rejected with SH2_ERR_OP_IN_PROGRESS or SH2_ERR_BAD_PARAM.

Error policy:

The driver automatically re-enables configured sensors after a sensor reset (handled internally by the async event callback).

Template Parameters
CommTypePlatform-specific transport. Must inherit from bno08x::CommInterface<CommType>.
Note
The CommType instance passed to the constructor must outlive this object. The driver stores a reference, not a copy.
This class is not thread-safe. Callers must serialize access externally if used from multiple tasks/threads.
One SH-2 session per process: the CEVA SH-2 library keeps global state, so a second object's Begin() is refused with BNO08X_ERR_SH2_SESSION_IN_USE while another object's session is open. UART-RVC objects are not limited.

Constructor & Destructor Documentation

◆ BNO085()

template<typename CommType >
BNO085< CommType >::BNO085 ( CommType &  comm)
inlineexplicitnoexcept

Construct the driver with a communication interface.

Parameters
[in]commReference to the transport (must outlive this object).

◆ ~BNO085()

template<typename CommType >
BNO085< CommType >::~BNO085 ( )
inlinenoexcept

Destructor. Closes active SH-2/RVC sessions.

Member Function Documentation

◆ Begin()

template<typename CommType >
bool BNO085< CommType >::Begin ( )
noexcept

Initialise the sensor in SH-2 mode.

Opens the communication bus, connects to the SH-2 layer, sends the reinitialize command, and registers the internal sensor/event callbacks.

Only one BNO085 object in the process can hold an SH-2 session at a time (the CEVA library keeps a single global session). While another object holds it, Begin() returns false with BNO08X_ERR_SH2_SESSION_IN_USE and touches neither the library nor the bus. Close() or destroying the owner frees the session. UART-RVC and DFU do not use the library.

Precondition
Interface type must NOT be UARTRVC.
Returns
true on success, false on failure or wrong interface type.

◆ BeginRvc()

template<typename CommType >
bool BNO085< CommType >::BeginRvc ( )
noexcept

Initialise RVC mode.

Open the UART transport and start the RVC frame parser.

Opens the UART transport and starts the internal frame parser. The sensor must have PS0/PS1 set for RVC mode at boot time.

Precondition
Interface type must be UARTRVC.
Returns
true on success, false on failure or wrong interface type.
Precondition
io_.GetInterfaceType() must be BNO085Interface::UARTRVC.
Returns
true on success, false on failure or incompatible transport.

◆ BootloaderI2cAddress()

template<typename CommType >
static constexpr uint8_t BNO085< CommType >::BootloaderI2cAddress ( uint8_t  appAddress)
inlinestaticconstexprnoexcept

Bootloader I2C address for a port configured at appAddress.

The bootloader answers at 0x28 | SA0 instead of 0x4A | SA0 (datasheet 1000-3927 section 1.4 and figure 1-12), so SA0 is taken from bit 0 of the application address. A bootloader address maps to itself.

Returns
0x28 / 0x29, or 0 if appAddress is none of 0x4A, 0x4B, 0x28, 0x29.

◆ Close()

template<typename CommType >
void BNO085< CommType >::Close ( )
noexcept

Close the currently active session and release transport resources.

If SH-2 is active, calls sh2_close() and closes the transport. If RVC is active, closes the UART transport and stops the parser. If DFU is in progress, the call is rejected and last_error_ is set to SH2_ERR_OP_IN_PROGRESS. Safe to call multiple times.

◆ CloseRvc()

template<typename CommType >
void BNO085< CommType >::CloseRvc ( )
noexcept

Stop RVC processing and close the transport.

Close the UART transport and deactivate the RVC parser.

Convenience API for explicit RVC teardown. Close() can also be used to close whichever mode is currently active.

◆ Dfu()

template<typename CommType >
int BNO085< CommType >::Dfu ( const HcBin_t &  fw = firmware)
noexcept

Perform a Device Firmware Update (DFU).

Execute DFU with default validation options.

The sensor must already be in bootloader mode (hold BOOTN LOW during reset). The method validates the firmware image, then transfers it in packets with CRC-16 and ACK/NAK retry logic.

Over I2C the bootloader answers at 0x28 / 0x29 (datasheet section 1.4). When the port has the optional GetI2cAddress() / SetI2cAddress() hooks, the driver switches it to BootloaderI2cAddress() of its current address for the transfer and restores the address afterwards, on every path (SH2_ERR_IO if a switch fails, SH2_ERR_BAD_PARAM if the current address is not a BNO08x address).

Precondition
Interface type must NOT be UARTRVC.
Parameters
[in]fwFirmware image. Defaults to the compiled-in stub from firmware-bno.c. Use MemoryFirmware for runtime images.
Returns
SH2_OK (0) on success, or a negative SH-2 error code.
See also
MemoryFirmware For loading firmware from memory at runtime.

◆ DfuFromMemory() [1/2]

template<typename CommType >
int BNO085< CommType >::DfuFromMemory ( const DfuMemoryImage &  image,
const DfuOptions &  options = {} 
)
noexcept

Perform DFU from an in-memory firmware image.

Execute DFU from an in-memory image descriptor.

Wraps image in a MemoryFirmware adapter internally and executes the same DFU protocol as DfuWithOptions().

◆ DfuFromMemory() [2/2]

template<typename CommType >
int BNO085< CommType >::DfuFromMemory ( const uint8_t *  data,
uint32_t  len,
const char *  partNumber = "1000-3608",
const DfuOptions &  options = {} 
)
noexcept

Convenience overload for in-memory DFU.

Convenience overload for memory DFU from raw pointers.

◆ DfuWithOptions()

template<typename CommType >
int BNO085< CommType >::DfuWithOptions ( const HcBin_t &  fw,
const DfuOptions &  options 
)
noexcept

Perform DFU with explicit validation and transfer options.

Execute the full DFU firmware transfer with configurable policy.

Uses the same HcBin image interface as Dfu(), but allows metadata policy, packet-size override, and progress reporting customization.

Validates firmware metadata/length, opens the bootloader transport, sends app size + packet size, streams firmware data with CRC+ACK retry, and optionally reports progress.

◆ DisableSensor()

template<typename CommType >
bool BNO085< CommType >::DisableSensor ( BNO085Sensor  sensor)
noexcept

Disable reporting for a sensor.

Disable reporting for a sensor by setting its interval to zero.

Parameters
[in]sensorWhich sensor to disable.
Returns
true if successful.

◆ EnableSensor()

template<typename CommType >
bool BNO085< CommType >::EnableSensor ( BNO085Sensor  sensor,
uint32_t  interval_ms,
float  sensitivity = 0.0f 
)
noexcept

Enable periodic reporting for a sensor.

Parameters
[in]sensorWhich sensor to enable.
[in]interval_msReport interval in milliseconds (e.g. 10 = 100 Hz). Use 0 for on-change sensors (step counter, tap, etc.).
[in]sensitivityChange sensitivity threshold for on-change sensors. Set to 0.0 to disable sensitivity filtering.
Returns
true if the sensor was configured successfully.

Converts the millisecond interval to microseconds and forwards to the SH-2 configuration API. The interval and sensitivity are cached so they can be automatically re-applied after a sensor reset.

◆ EnterBootloader()

template<typename CommType >
bool BNO085< CommType >::EnterBootloader ( uint32_t  resetLowMs = 10,
uint32_t  settleMs = 50 
)
noexcept

Enter bootloader mode using BOOTN + reset pin sequence.

Enter bootloader mode via BOOTN+reset sequence.

Holds BOOTN low, pulses reset, then releases BOOTN. Requires a transport with BOOTN/RSTN wiring support.

Returns
true on success, false if interface/state is incompatible (SH2_ERR_BAD_PARAM / SH2_ERR_OP_IN_PROGRESS), the port did not open (SH2_ERR) or a control pin could not be driven (SH2_ERR_IO; the port is closed again).

◆ ExitBootloaderAndReboot()

template<typename CommType >
bool BNO085< CommType >::ExitBootloaderAndReboot ( uint32_t  resetLowMs = 2,
uint32_t  settleMs = 100 
)
noexcept

Exit bootloader mode and reboot into application firmware.

Exit bootloader mode and reboot application firmware.

Ensures BOOTN is released high, pulses reset, then waits for boot. Fails like EnterBootloader(), including SH2_ERR_IO on a GPIO error.

◆ GetDriverVersion()

template<typename CommType >
static constexpr const char * BNO085< CommType >::GetDriverVersion ( )
inlinestaticconstexprnoexcept

Get the compiled driver version string.

◆ GetDriverVersionMajor()

template<typename CommType >
static constexpr uint8_t BNO085< CommType >::GetDriverVersionMajor ( )
inlinestaticconstexprnoexcept

Get the compiled driver major version number.

◆ GetDriverVersionMinor()

template<typename CommType >
static constexpr uint8_t BNO085< CommType >::GetDriverVersionMinor ( )
inlinestaticconstexprnoexcept

Get the compiled driver minor version number.

◆ GetDriverVersionPatch()

template<typename CommType >
static constexpr uint8_t BNO085< CommType >::GetDriverVersionPatch ( )
inlinestaticconstexprnoexcept

Get the compiled driver patch version number.

◆ GetLastError()

template<typename CommType >
int BNO085< CommType >::GetLastError ( ) const
inline

Get the last error code from the SH-2 driver.

Returns
SH-2 error code (0 = no error). See sh2_err.h for values.

◆ GetLatest()

template<typename CommType >
SensorEvent BNO085< CommType >::GetLatest ( BNO085Sensor  sensor)
noexcept

Retrieve the most recent event for a sensor.

Retrieve the most recent event for a sensor and clear unread flag.

Returns a decoded SensorEvent populated with the latest values. Which fields are meaningful depends on the sensor type.

Parameters
[in]sensorWhich sensor to query. Calling this method clears the unread-data flag for sensor. If the driver is not in Sh2Active state, returns a default event and leaves cached SH-2 flags unchanged.
Returns
Copy of the latest SensorEvent.

◆ GetState()

template<typename CommType >
BNO085DriverState BNO085< CommType >::GetState ( ) const
inlinenoexcept

Get the current high-level driver state.

◆ HardwareReset()

template<typename CommType >
bool BNO085< CommType >::HardwareReset ( uint32_t  lowMs = 2)
noexcept

Perform a hardware reset via the RSTN pin.

Assert RSTN for lowMs, then release and wait for boot.

Drives RSTN LOW for lowMs milliseconds, then releases it HIGH and waits RESET_STARTUP_MS (94 ms) for the sensor to boot: t1 (internal initialization, min 90 ms) plus t2 (internal configuration, typ 4 ms) of datasheet 1000-3927 section 6.5.3, figure 6-8.

Parameters
[in]lowMsDuration to hold RSTN LOW (default 2 ms; the datasheet minimum t_nrst is 10 ns).
Returns
false if the port's GpioSet() reported an error on RSTN.

After NRST rises the hub needs t1 (internal initialization, min 90 ms) plus t2 (internal configuration, typ 4 ms) before it asserts H_INTN and can be addressed (datasheet 1000-3927 section 6.5.3, figures 6-8 and 6-9). The driver waits RESET_STARTUP_MS = t1 + t2.

If asserting RSTN fails nothing else is done. If releasing it fails the hub may still be held in reset, so the start-up wait is skipped.

◆ HasNewData()

template<typename CommType >
bool BNO085< CommType >::HasNewData ( BNO085Sensor  sensor) const

Check if new data is available for a sensor.

Check the new-data flag for a sensor.

The flag is set when a report arrives and cleared when GetLatest() is called. Callback dispatch does not clear this flag, which allows mixed callback + polling usage.

Returns false unless the driver is currently in Sh2Active state.

Parameters
[in]sensorWhich sensor to check.
Returns
true if unread data is available.

◆ RunDfuFromMemory()

template<typename CommType >
int BNO085< CommType >::RunDfuFromMemory ( const DfuMemoryImage &  image,
const DfuOptions &  options = {},
uint32_t  enterResetLowMs = 10,
uint32_t  enterSettleMs = 50,
uint32_t  exitResetLowMs = 2,
uint32_t  exitSettleMs = 100 
)
noexcept

Full class-aware DFU workflow: enter bootloader -> transfer firmware -> reboot to app.

Full workflow: enter bootloader, transfer memory image, reboot app.

◆ SelectInterface()

template<typename CommType >
bool BNO085< CommType >::SelectInterface ( BNO085Interface  iface)
noexcept

Select the host interface by driving PS0/PS1 pins.

Drive PS0/PS1 pins to select the host interface.

This is only useful if the PS pins are connected to controllable GPIOs (rare – most boards hard-wire them).

Parameters
[in]ifaceDesired interface.
Returns
false if the port's GpioSet() reported an error on PS0 or PS1.

PS pins are sampled at reset. This is only useful if the pins are connected to controllable GPIOs. Levels follow datasheet 1000-3927 figure 1-5 (Protocol selection): PS1/PS0 = 00 I2C, 01 UART-RVC, 10 UART (SHTP), 11 SPI. Both pins are always driven; the result is false if either failed.

◆ ServiceRvc()

template<typename CommType >
void BNO085< CommType >::ServiceRvc ( )
noexcept

Poll for RVC frames and dispatch callbacks.

Read all available UART bytes and parse RVC frames.

Reads all available UART bytes, feeds them into the frame parser, and invokes the RVC callback for each complete valid frame. Call this in your main loop or an RTOS task.

Note
No-op if BeginRvc() has not been called.

Each byte is fed into a 19-byte sliding window. When a valid frame is detected (0xAA 0xAA header + matching checksum), the fields are extracted, decoded to floating-point, and dispatched to the RVC callback.

◆ SetBootPin()

template<typename CommType >
bool BNO085< CommType >::SetBootPin ( bool  state)
noexcept

Drive the BOOTN pin to enter/exit DFU bootloader mode.

Forward BOOTN control to the CommInterface.

Parameters
[in]statetrue = drive LOW (enter bootloader), false = HIGH.
Returns
false if the port's GpioSet() reported an error.

◆ SetCallback()

template<typename CommType >
void BNO085< CommType >::SetCallback ( SensorCallback  cb)
noexcept

Register a callback for SH-2 sensor events.

Register a callback invoked for every received SH-2 sensor event.

The callback is invoked from within Update() whenever the SH-2 library delivers a new sensor report. Only one callback can be registered at a time; calling this again replaces the previous callback.

Parameters
[in]cbCallback function or lambda. Pass {} to clear.

◆ SetRvcCallback()

template<typename CommType >
void BNO085< CommType >::SetRvcCallback ( RvcCallback  cb)
noexcept

Register a callback for decoded RVC frames.

Register a callback invoked for every decoded RVC frame.

The callback is invoked from within ServiceRvc() each time a valid 19-byte RVC frame is received and decoded.

Parameters
[in]cbCallback function or lambda. Pass {} to clear.

◆ SetWakePin()

template<typename CommType >
bool BNO085< CommType >::SetWakePin ( bool  state)
noexcept

Drive the WAKE pin (SPI mode only).

Forward WAKE control to the CommInterface (SPI only).

Parameters
[in]statetrue = drive LOW (wake sensor), false = release.
Returns
false if the port's GpioSet() reported an error.

◆ Sh2SessionInUse()

template<typename CommType >
static bool BNO085< CommType >::Sh2SessionInUse ( )
inlinestaticnoexcept

True while any BNO085 object in the process holds the SH-2 session.

◆ Update()

template<typename CommType >
void BNO085< CommType >::Update ( )
noexcept

Pump the SH-2 service loop.

Service the SH-2 protocol. Invokes sensor and async callbacks.

Must be called frequently (every 5-10 ms or faster) to keep data flowing between the host and the sensor hub. Sensor callbacks and async event handlers are invoked from within this method.

Note
No-op if Begin() has not been called or if in RVC mode.

Member Data Documentation

◆ I2C_ADDRESS_APP

template<typename CommType >
constexpr uint8_t BNO085< CommType >::I2C_ADDRESS_APP = 0x4A
staticconstexpr

Application I2C address with SA0 low (datasheet figure 1-12); 0x4B with SA0 high.

◆ I2C_ADDRESS_BOOTLOADER

template<typename CommType >
constexpr uint8_t BNO085< CommType >::I2C_ADDRESS_BOOTLOADER = 0x28
staticconstexpr

Bootloader I2C address with SA0 low (datasheet section 1.4); 0x29 with SA0 high.

◆ RESET_STARTUP_MS

template<typename CommType >
constexpr uint32_t BNO085< CommType >::RESET_STARTUP_MS = 90 + 4
staticconstexpr

Wait after releasing NRST before the hub can be addressed, in ms.

t1 (internal initialization, min 90 ms) + t2 (internal configuration, typ 4 ms), datasheet 1000-3927 section 6.5.3, figures 6-8 and 6-9.


The documentation for this class was generated from the following files: