SparkFun BMI330 Arduino Library  v1.0.0-2-gdbddb99
Arduino Library for the SparkFun Qwiic BMI330 6-DoF IMU (Bosch BMI330)
Loading...
Searching...
No Matches
SparkFun_BMI330.h
Go to the documentation of this file.
1
43#pragma once
44
45// clang-format off
46#include <SparkFun_Toolkit.h>
47#include "sfTk/sfDevBMI330.h"
48#include <Arduino.h>
49// clang-format on
50
58#define SFE_BMI330_SPI_CLOCK_DEFAULT 4000000
59
61// SfeBMI330ArdI2C
63
77{
78 public:
80 {
81 }
82
100 bool begin(uint8_t address = SFE_BMI330_DEFAULT_ADDRESS, TwoWire &wirePort = Wire)
101 {
102 if (_theI2CBus.init(wirePort, address) != ksfTkErrOk)
103 return false;
104
105 // Confirm something is at this address before reading registers, so a missing board reports
106 // a bus failure rather than a chip ID mismatch.
107 if (_theI2CBus.ping() != ksfTkErrOk)
108 return false;
109
110 return sfDevBMI330::begin(&_theI2CBus) == ksfTkErrOk;
111 }
112
122 bool begin(TwoWire &wirePort)
123 {
124 return begin(SFE_BMI330_DEFAULT_ADDRESS, wirePort);
125 }
126
131 uint8_t getAddress(void)
132 {
133 return _theI2CBus.address();
134 }
135
147 static bool findDevice(uint8_t &address, TwoWire &wirePort = Wire)
148 {
149 static const uint8_t kAddresses[2] = {SFE_BMI330_DEFAULT_ADDRESS, SFE_BMI330_ALT_ADDRESS};
150
151 for (uint8_t i = 0; i < 2; i++)
152 {
153 SfeBMI330ArdI2C candidate;
154
155 if (candidate._theI2CBus.init(wirePort, kAddresses[i]) != ksfTkErrOk)
156 continue;
157
158 if (candidate._theI2CBus.ping() != ksfTkErrOk)
159 continue;
160
161 candidate.setCommunicationBus(&candidate._theI2CBus);
162
163 if (!candidate.isConnected())
164 continue;
165
166 address = kAddresses[i];
167 return true;
168 }
169
170 return false;
171 }
172
173 private:
175 sfTkArdI2C _theI2CBus;
176};
177
179// SfeBMI330ArdSPI
181
201{
202 public:
204 {
205 }
206
222 bool begin(uint8_t csPin, uint32_t clockFrequency = SFE_BMI330_SPI_CLOCK_DEFAULT, SPIClass &spiPort = SPI)
223 {
224 // High first, then output. Reversing these glitches CS low for a moment on most cores.
225 digitalWrite(csPin, HIGH);
226 pinMode(csPin, OUTPUT);
227 digitalWrite(csPin, HIGH);
228
229 // Mode 0. The BMI330 also accepts mode 3, but mode 0 is what the BMI270 library uses and
230 // keeps behavior consistent across SparkFun's Bosch IMU boards.
231 SPISettings settings(clockFrequency, MSBFIRST, SPI_MODE0);
232
233 if (_theSPIBus.init(spiPort, settings, csPin, true) != ksfTkErrOk)
234 return false;
235
236 return sfDevBMI330::begin(&_theSPIBus) == ksfTkErrOk;
237 }
238
243 uint8_t getCSPin(void)
244 {
245 return _theSPIBus.cs();
246 }
247
248 private:
250 sfTkArdSPI _theSPIBus;
251};
#define SFE_BMI330_SPI_CLOCK_DEFAULT
Default SPI clock for SfeBMI330ArdSPI, in hertz.
Definition: SparkFun_BMI330.h:58
Arduino I2C implementation for the SparkFun Qwiic BMI330 breakout.
Definition: SparkFun_BMI330.h:77
bool begin(uint8_t address=SFE_BMI330_DEFAULT_ADDRESS, TwoWire &wirePort=Wire)
Initialize the board over I2C.
Definition: SparkFun_BMI330.h:100
bool begin(TwoWire &wirePort)
Initialize the board over I2C on an explicit Wire port at the default address.
Definition: SparkFun_BMI330.h:122
static bool findDevice(uint8_t &address, TwoWire &wirePort=Wire)
Check whether a BMI330 answers at either factory address on a given bus.
Definition: SparkFun_BMI330.h:147
SfeBMI330ArdI2C()
Definition: SparkFun_BMI330.h:79
uint8_t getAddress(void)
Get the 7-bit I2C address this object is using.
Definition: SparkFun_BMI330.h:131
Arduino SPI implementation for the SparkFun Qwiic BMI330 breakout.
Definition: SparkFun_BMI330.h:201
SfeBMI330ArdSPI()
Definition: SparkFun_BMI330.h:203
bool begin(uint8_t csPin, uint32_t clockFrequency=SFE_BMI330_SPI_CLOCK_DEFAULT, SPIClass &spiPort=SPI)
Initialize the board over SPI.
Definition: SparkFun_BMI330.h:222
uint8_t getCSPin(void)
Get the chip select pin this object is using.
Definition: SparkFun_BMI330.h:243
Platform-independent driver for the Bosch BMI330 6-DoF IMU.
Definition: sfDevBMI330.h:728
void setCommunicationBus(sfTkIBus *theBus)
Set the communication bus without initializing the device.
Definition: sfDevBMI330.cpp:72
bool isConnected(void)
Check that a BMI330 is responding.
Definition: sfDevBMI330.cpp:84
sfTkError_t begin(sfTkIBus *theBus=nullptr)
Adopt a bus, verify the device, and bring the feature engine up.
Definition: sfDevBMI330.cpp:37
Header file for the SparkFun Bosch BMI330 6-DoF IMU Driver.
#define SFE_BMI330_DEFAULT_ADDRESS
Factory-default 7-bit I2C address (SDO / ADR pin low).
Definition: sfDevBMI330.h:62
#define SFE_BMI330_ALT_ADDRESS
Alternate 7-bit I2C address, selected by the board's ADR jumper (SDO / ADR pin high).
Definition: sfDevBMI330.h:65