Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BMI160: Fix magnetometer error check #285

Merged
merged 1 commit into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions lib/bmi160/BMI160.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,10 @@ void BMI160::setMagDeviceAddress(uint8_t addr) {
setRegister(BMI160_RA_MAG_IF_0_DEVADDR, addr << 1); // 0 bit of address is reserved and needs to be shifted
}

bool BMI160::setMagRegister(uint8_t addr, uint8_t value) {
void BMI160::setMagRegister(uint8_t addr, uint8_t value) {
setRegister(BMI160_RA_MAG_IF_4_WRITE_VALUE, value);
setRegister(BMI160_RA_MAG_IF_3_WRITE_RA, addr);
delay(3);
I2CdevMod::readByte(devAddr, BMI160_RA_ERR, buffer);
if (buffer[0] & BMI160_ERR_MASK_I2C_FAIL) {
printf("BMI160: mag register proxy write error\n");
return false;
}
return true;
}

/** Get Device ID.
Expand Down
15 changes: 8 additions & 7 deletions lib/bmi160/BMI160.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ THE SOFTWARE.
#define BMI160_RA_MAG_CONF 0x44
#define BMI160_RA_MAG_IF_0_DEVADDR 0x4B
#define BMI160_RA_MAG_IF_1_MODE 0x4C
#define BMI160_RA_MAG_IF_2_READ_RA 0x4D
#define BMI160_RA_MAG_IF_2_READ_RA 0x4D
#define BMI160_RA_MAG_IF_3_WRITE_RA 0x4E
#define BMI160_RA_MAG_IF_4_WRITE_VALUE 0x4F
#define BMI160_RA_IF_CONF 0x6B
Expand Down Expand Up @@ -343,11 +343,12 @@ typedef enum {
#define BMI160_FIFO_A_LEN 6

#define BMI160_RA_ERR 0x02
#define BMI160_ERR_MASK_MAG_DRDY_ERR 0x10000000
#define BMI160_ERR_MASK_DROP_CMD_ERR 0x01000000
#define BMI160_ERR_MASK_I2C_FAIL 0x00100000
#define BMI160_ERR_MASK_ERROR_CODE 0x00011110
#define BMI160_ERR_MASK_CHIP_NOT_OPERABLE 0x00000001
#define BMI160_ERR_MASK_MAG_DRDY_ERR 0b10000000
#define BMI160_ERR_MASK_DROP_CMD_ERR 0b01000000
// datasheet is unclear - reserved or i2c_fail_err?
// #define BMI160_ERR_MASK_I2C_FAIL 0b00100000
#define BMI160_ERR_MASK_ERROR_CODE 0b00011110
#define BMI160_ERR_MASK_CHIP_NOT_OPERABLE 0b00000001

/**
* Interrupt Latch Mode options
Expand Down Expand Up @@ -755,7 +756,7 @@ class BMI160 {
void waitForMagDrdy();
bool getSensorTime(uint32_t *v_sensor_time_u32);
void setMagDeviceAddress(uint8_t addr);
bool setMagRegister(uint8_t addr, uint8_t value);
void setMagRegister(uint8_t addr, uint8_t value);

bool getErrReg(uint8_t* out);
private:
Expand Down
12 changes: 0 additions & 12 deletions src/sensors/bmi160sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ void BMI160Sensor::initHMC(BMI160MagRate magRate) {
imu.setRegister(BMI160_RA_CMD, BMI160_CMD_MAG_MODE_NORMAL);
delay(60);

imu.setRegister(BMI160_RA_CMD, BMI160_EN_PULL_UP_REG_1);
imu.setRegister(BMI160_RA_CMD, BMI160_EN_PULL_UP_REG_2);
imu.setRegister(BMI160_RA_CMD, BMI160_EN_PULL_UP_REG_3);
imu.setRegister(BMI160_7F, BMI160_EN_PULL_UP_REG_4);
imu.setRegister(BMI160_7F, BMI160_EN_PULL_UP_REG_5);

/* Enable MAG interface */
imu.setRegister(BMI160_RA_IF_CONF, BMI160_IF_CONF_MODE_PRI_AUTO_SEC_MAG);
delay(1);
Expand All @@ -65,12 +59,6 @@ void BMI160Sensor::initQMC(BMI160MagRate magRate) {
imu.setRegister(BMI160_RA_CMD, BMI160_CMD_MAG_MODE_NORMAL);
delay(60);

imu.setRegister(BMI160_RA_CMD, BMI160_EN_PULL_UP_REG_1);
imu.setRegister(BMI160_RA_CMD, BMI160_EN_PULL_UP_REG_2);
imu.setRegister(BMI160_RA_CMD, BMI160_EN_PULL_UP_REG_3);
imu.setRegister(BMI160_7F, BMI160_EN_PULL_UP_REG_4);
imu.setRegister(BMI160_7F, BMI160_EN_PULL_UP_REG_5);

/* Enable MAG interface */
imu.setRegister(BMI160_RA_IF_CONF, BMI160_IF_CONF_MODE_PRI_AUTO_SEC_MAG);
delay(1);
Expand Down
Loading