diff --git a/src/SparkFun_BMI270_Arduino_Library.cpp b/src/SparkFun_BMI270_Arduino_Library.cpp index 952442f..ea2b6b3 100644 --- a/src/SparkFun_BMI270_Arduino_Library.cpp +++ b/src/SparkFun_BMI270_Arduino_Library.cpp @@ -9,7 +9,7 @@ BMI270::BMI270() /// @brief Checks whether sensor is connected, initializes sensor, then sets /// default config parameters /// @return Error code (0 is success, negative is failure, positive is warning) -int8_t BMI270::begin() +int8_t BMI270::begin(bool skip_reset) { // Variable to track errors returned by API calls int8_t err = BMI2_OK; @@ -22,7 +22,7 @@ int8_t BMI270::begin() sensor.read_write_len = 32; // Initialize the sensor - err = bmi270_init(&sensor); + err = bmi270_init(&sensor, skip_reset ? 1 : 0); if(err != BMI2_OK) return err; // Enable the accelerometer and gyroscope @@ -50,7 +50,7 @@ int8_t BMI270::begin() /// @param address I2C address of sensor /// @param wirePort I2C port to use for communication, defaults to Wire /// @return Error code (0 is success, negative is failure, positive is warning) -int8_t BMI270::beginI2C(uint8_t address, TwoWire& wirePort) +int8_t BMI270::beginI2C(uint8_t address, TwoWire& wirePort, bool skip_reset) { // Check whether address is valid option if(address != BMI2_I2C_PRIM_ADDR && address != BMI2_I2C_SEC_ADDR) @@ -68,7 +68,7 @@ int8_t BMI270::beginI2C(uint8_t address, TwoWire& wirePort) interfaceData.interface = BMI2_I2C_INTF; // Initialize sensor - return begin(); + return begin(skip_reset); } /// @brief Begin communication with the sensor over SPI, initialize it, and set @@ -76,7 +76,7 @@ int8_t BMI270::beginI2C(uint8_t address, TwoWire& wirePort) /// @param csPin Chip select pin of sensor /// @param clockFrequency SPI clock frequency /// @return Error code (0 is success, negative is failure, positive is warning) -int8_t BMI270::beginSPI(uint8_t csPin, uint32_t clockFrequency) +int8_t BMI270::beginSPI(uint8_t csPin, uint32_t clockFrequency, bool skip_reset) { // Set up chip select pin interfaceData.spiCSPin = csPin; @@ -93,7 +93,7 @@ int8_t BMI270::beginSPI(uint8_t csPin, uint32_t clockFrequency) sensor.dummy_byte = 1; // Initialize sensor - return begin(); + return begin(skip_reset); } /// @brief Performs a soft reset of the sensor @@ -385,7 +385,7 @@ int8_t BMI270::enableAdvancedPowerSave(bool enable) { return bmi2_set_adv_power_save(BMI2_DISABLE, &sensor); } - + } /// @brief Disables advanced power save mode @@ -664,7 +664,7 @@ int8_t BMI270::setFIFOConfig(BMI270_FIFOConfig config) // Set flag bits err = setFIFOFlags(config.flags, BMI2_ENABLE); if(err != BMI2_OK) return err; - + // Clear unused flag bits err = setFIFOFlags(~config.flags, BMI2_DISABLE); if(err != BMI2_OK) return err; @@ -727,7 +727,7 @@ int8_t BMI270::setFIFOFlags(uint16_t flags, bool enable) { fifoConfigFlags &= ~flags; } - + // Compute number of bytes per FIFO frame bytesPerFIFOData = 0; bytesPerFIFOData += ((fifoConfigFlags & BMI2_FIFO_ACC_EN) != 0) * 6; @@ -827,13 +827,13 @@ int8_t BMI270::extractFIFOData(BMI270_SensorData* data, bmi2_fifo_frame* fifoDat // Sensor is disabled in FIFO, just return return BMI2_OK; } - + // Variable to track errors returned by API calls int8_t err = BMI2_OK; // Create buffer to hold raw data bmi2_sens_axes_data* rawData = (bmi2_sens_axes_data*) malloc((*numFrames) * sizeof(bmi2_sens_axes_data)); - + // Extract raw data from the FIFO data if(sensorSelect == BMI2_ACCEL) { @@ -848,7 +848,7 @@ int8_t BMI270::extractFIFOData(BMI270_SensorData* data, bmi2_fifo_frame* fifoDat free(rawData); return err; } - + // Convert raw data for(int i = 0; i < (*numFrames); i++) { @@ -861,7 +861,7 @@ int8_t BMI270::extractFIFOData(BMI270_SensorData* data, bmi2_fifo_frame* fifoDat convertRawGyroData(&(rawData[i]), &(data[i])); } } - + free(rawData); return err; } @@ -944,7 +944,7 @@ int8_t BMI270::getStepCount(uint32_t* count) featureData.type = BMI2_STEP_COUNTER; err = getFeatureData(&featureData); if(err) return err; - + *count = featureData.sens_data.step_counter_output; return BMI2_OK; } @@ -1002,7 +1002,7 @@ int8_t BMI270::getStepActivity(uint8_t* activity) featureData.type = BMI2_STEP_ACTIVITY; err = getFeatureData(&featureData); if(err) return err; - + *activity = featureData.sens_data.activity_output; return BMI2_OK; } @@ -1026,7 +1026,7 @@ int8_t BMI270::getWristGesture(uint8_t* gesture) featureData.type = BMI2_WRIST_GESTURE; err = getFeatureData(&featureData); if(err) return err; - + *gesture = featureData.sens_data.wrist_gesture_output; return BMI2_OK; } @@ -1284,7 +1284,7 @@ BMI2_INTF_RETURN_TYPE BMI270::writeRegistersI2C(uint8_t regAddress, const uint8_ // Write the address interfaceData->i2cPort->write(regAddress); - + // Write all the data for(uint32_t i = 0; i < numBytes; i++) { @@ -1311,10 +1311,10 @@ BMI2_INTF_RETURN_TYPE BMI270::writeRegistersSPI(uint8_t regAddress, const uint8_ // Begin transmission SPI.beginTransaction(SPISettings(interfaceData->spiClockFrequency, MSBFIRST, SPI_MODE0)); digitalWrite(interfaceData->spiCSPin, LOW); - + // Write the address SPI.transfer(regAddress); - + // Write all the data for(uint32_t i = 0; i < numBytes; i++) { diff --git a/src/SparkFun_BMI270_Arduino_Library.h b/src/SparkFun_BMI270_Arduino_Library.h index 505f89c..582b0c4 100644 --- a/src/SparkFun_BMI270_Arduino_Library.h +++ b/src/SparkFun_BMI270_Arduino_Library.h @@ -147,8 +147,8 @@ class BMI270 BMI270(); // Sensor initialization, must specify communication interface - int8_t beginI2C(uint8_t address = BMI2_I2C_PRIM_ADDR, TwoWire& wirePort = Wire); - int8_t beginSPI(uint8_t csPin, uint32_t clockFrequency = 100000); + int8_t beginI2C(uint8_t address = BMI2_I2C_PRIM_ADDR, TwoWire& wirePort = Wire, bool skip_reset = false); + int8_t beginSPI(uint8_t csPin, uint32_t clockFrequency = 100000, bool skip_reset = false); // Sensor control int8_t reset(); @@ -231,7 +231,7 @@ class BMI270 private: // Sensor initialization, after communication interface has been selected - int8_t begin(); + int8_t begin(bool skip_reset = false); // Convert from raw data (bmi2_sens_data) to g's (BMI270_SensorData) void convertRawAccelData(bmi2_sens_axes_data* rawData, BMI270_SensorData* data); diff --git a/src/bmi270_api/bmi2.c b/src/bmi270_api/bmi2.c index 5dce0ec..1f606d0 100644 --- a/src/bmi270_api/bmi2.c +++ b/src/bmi270_api/bmi2.c @@ -1880,6 +1880,11 @@ static int8_t validate_foc_accel_axis(int16_t avg_foc_data, struct bmi2_dev *dev * chip-id of the sensor. */ int8_t bmi2_sec_init(struct bmi2_dev *dev) +{ + return bmi2_sec_init_with_opt(dev, 0); +} + +int8_t bmi2_sec_init_with_opt(struct bmi2_dev *dev, uint8_t skip_reset) { /* Variable to define error */ int8_t rslt; @@ -1928,6 +1933,8 @@ int8_t bmi2_sec_init(struct bmi2_dev *dev) */ dev->remap = axes_remap; + if (skip_reset) return BMI2_OK; + /* Perform soft-reset to bring all register values to their * default values */ diff --git a/src/bmi270_api/bmi2.h b/src/bmi270_api/bmi2.h index 1aa7800..f475f4a 100644 --- a/src/bmi270_api/bmi2.h +++ b/src/bmi270_api/bmi2.h @@ -89,6 +89,8 @@ extern "C" { */ int8_t bmi2_sec_init(struct bmi2_dev *dev); +int8_t bmi2_sec_init_with_opt(struct bmi2_dev *dev, uint8_t skip_reset); + /*! * \ingroup bmi2ApiInit * \page bmi2_api_bmi2_set_spi_en bmi2_set_spi_en diff --git a/src/bmi270_api/bmi270.c b/src/bmi270_api/bmi270.c index 4f2548a..614b23a 100644 --- a/src/bmi270_api/bmi270.c +++ b/src/bmi270_api/bmi270.c @@ -1351,7 +1351,7 @@ static int8_t disable_sensor_features(uint64_t sensor_sel, struct bmi2_dev *dev) * 4) Updates the feature offset parameters in the device structure. * 5) Updates the maximum number of pages, in the device structure. */ -int8_t bmi270_init(struct bmi2_dev *dev) +int8_t bmi270_init(struct bmi2_dev *dev, uint8_t skip_reset) { /* Variable to define error */ int8_t rslt; @@ -1389,7 +1389,7 @@ int8_t bmi270_init(struct bmi2_dev *dev) } /* Initialize BMI2 sensor */ - rslt = bmi2_sec_init(dev); + rslt = bmi2_sec_init_with_opt(dev, skip_reset); if (rslt == BMI2_OK) { /* Assign the offsets of the feature input diff --git a/src/bmi270_api/bmi270.h b/src/bmi270_api/bmi270.h index 4c98067..b4d84db 100644 --- a/src/bmi270_api/bmi270.h +++ b/src/bmi270_api/bmi270.h @@ -150,7 +150,7 @@ extern "C" { * @retval 0 -> Success * @retval < 0 -> Fail */ -int8_t bmi270_init(struct bmi2_dev *dev); +int8_t bmi270_init(struct bmi2_dev *dev, uint8_t skip_reset); /** * \ingroup bmi270