Skip to content

Commit ab158f9

Browse files
authored
add CAS compensation for bmi270 (#4)
合并入 主干
1 parent 928087d commit ab158f9

File tree

1 file changed

+50
-6
lines changed

1 file changed

+50
-6
lines changed

src/main/drivers/accgyro/accgyro_spi_bmi270.c

+50-6
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ extern const uint8_t bmi270_maximum_fifo_config_file[BMI270_CONFIG_SIZE];
5252

5353
#define BMI270_CHIP_ID 0x24
5454

55+
#define BMI270_GYRO_CAS_SIGN_BIT_MASK 0x0040
56+
5557
// BMI270 registers (not the complete list)
5658
typedef enum {
5759
BMI270_REG_CHIP_ID = 0x00,
@@ -71,6 +73,8 @@ typedef enum {
7173
BMI270_REG_FIFO_LENGTH_LSB = 0x24,
7274
BMI270_REG_FIFO_LENGTH_MSB = 0x25,
7375
BMI270_REG_FIFO_DATA = 0x26,
76+
BMI270_REG_FEAT_PAGE = 0x2F,
77+
BMI270_REG_FEATURES_C = 0x3C,
7478
BMI270_REG_ACC_CONF = 0x40,
7579
BMI270_REG_ACC_RANGE = 0x41,
7680
BMI270_REG_GYRO_CONF = 0x42,
@@ -103,6 +107,7 @@ typedef enum {
103107
BMI270_VAL_CMD_FIFOFLUSH = 0xB0,
104108
BMI270_VAL_PWR_CTRL = 0x0E, // enable gyro, acc and temp sensors
105109
BMI270_VAL_PWR_CONF = 0x02, // disable advanced power save, enable FIFO self-wake
110+
BMI270_VAL_PAGE_0 = 0x00, // select page 0
106111
BMI270_VAL_ACC_CONF_ODR800 = 0x0B, // set acc sample rate to 800hz
107112
BMI270_VAL_ACC_CONF_ODR1600 = 0x0C, // set acc sample rate to 1600hz
108113
BMI270_VAL_ACC_CONF_BWP = 0x02, // set acc filter in normal mode
@@ -154,6 +159,16 @@ static void bmi270RegisterWrite(const extDevice_t *dev, bmi270Register_e registe
154159
}
155160
}
156161

162+
static int16_t bmi270GetGyroCas(int16_t gyroCasRaw)
163+
{
164+
if (gyroCasRaw & BMI270_GYRO_CAS_SIGN_BIT_MASK ){
165+
return (int16_t)(((int16_t)gyroCasRaw) - 128);
166+
} else {
167+
return (int16_t)(gyroCasRaw);
168+
}
169+
return 0;
170+
}
171+
157172
// Toggle the CS to switch the device into SPI mode.
158173
// Device switches initializes as I2C and switches to SPI on a low to high CS transition
159174
static void bmi270EnableSPI(const extDevice_t *dev)
@@ -232,6 +247,9 @@ static void bmi270Config(gyroDev_t *gyro)
232247
bmi270RegisterWrite(dev, BMI270_REG_FIFO_WTM_1, BMI270_VAL_FIFO_WTM_1, 1);
233248
}
234249

250+
// Configure the feature register page to page 0
251+
bmi270RegisterWrite(dev, BMI270_REG_FEAT_PAGE, BMI270_VAL_PAGE_0, 1);
252+
235253
// Configure the accelerometer
236254
bmi270RegisterWrite(dev, BMI270_REG_ACC_CONF, (BMI270_VAL_ACC_CONF_HP << 7) | (BMI270_VAL_ACC_CONF_BWP << 4) | BMI270_VAL_ACC_CONF_ODR800, 1);
237255

@@ -380,7 +398,7 @@ static bool bmi270GyroReadRegister(gyroDev_t *gyro)
380398
case GYRO_EXTI_INIT:
381399
{
382400
// Initialise the tx buffer to all 0x00
383-
memset(gyro->dev.txBuf, 0x00, 14);
401+
memset(gyro->dev.txBuf, 0x00, 18);
384402
#ifdef USE_GYRO_EXTI
385403
// Check that minimum number of interrupts have been detected
386404

@@ -412,8 +430,8 @@ static bool bmi270GyroReadRegister(gyroDev_t *gyro)
412430
case GYRO_EXTI_INT:
413431
case GYRO_EXTI_NO_INT:
414432
{
433+
// gyro data reading
415434
gyro->dev.txBuf[0] = BMI270_REG_GYR_DATA_X_LSB | 0x80;
416-
417435
busSegment_t segments[] = {
418436
{.u.buffers = {NULL, NULL}, 8, true, NULL},
419437
{.u.link = {NULL, NULL}, 0, true, NULL},
@@ -426,7 +444,21 @@ static bool bmi270GyroReadRegister(gyroDev_t *gyro)
426444
// Wait for completion
427445
spiWait(&gyro->dev);
428446

429-
gyro->gyroADCRaw[X] = gyroData[1];
447+
// GYRO_CAS rading
448+
gyro->dev.txBuf[9] = BMI270_REG_FEATURES_C | 0x80;
449+
busSegment_t segmentsCas[] = {
450+
{.u.buffers = {NULL, NULL}, 4, true, NULL},
451+
{.u.link = {NULL, NULL}, 0, true, NULL},
452+
};
453+
segmentsCas[0].u.buffers.txData = &gyro->dev.txBuf[9];
454+
segmentsCas[0].u.buffers.rxData = &gyro->dev.rxBuf[9];
455+
456+
spiSequence(&gyro->dev, &segmentsCas[0]);
457+
// Wait for completion
458+
spiWait(&gyro->dev);
459+
460+
int16_t cas = bmi270GetGyroCas((int16_t)(gyroData[5]));
461+
gyro->gyroADCRaw[X] = gyroData[1] - (cas * gyroData[3] / 512);
430462
gyro->gyroADCRaw[Y] = gyroData[2];
431463
gyro->gyroADCRaw[Z] = gyroData[3];
432464

@@ -435,9 +467,21 @@ static bool bmi270GyroReadRegister(gyroDev_t *gyro)
435467

436468
case GYRO_EXTI_INT_DMA:
437469
{
438-
// If read was triggered in interrupt don't bother waiting. The worst that could happen is that we pick
439-
// up an old value.
440-
gyro->gyroADCRaw[X] = gyroData[4];
470+
// GYRO_CAS rading
471+
gyro->dev.txBuf[15] = BMI270_REG_FEATURES_C | 0x80;
472+
busSegment_t segmentsCas[] = {
473+
{.u.buffers = {NULL, NULL}, 4, true, NULL},
474+
{.u.link = {NULL, NULL}, 0, true, NULL},
475+
};
476+
segmentsCas[0].u.buffers.txData = &gyro->dev.txBuf[15];
477+
segmentsCas[0].u.buffers.rxData = &gyro->dev.rxBuf[15];
478+
479+
spiSequence(&gyro->dev, &segmentsCas[0]);
480+
// Wait for completion
481+
spiWait(&gyro->dev);
482+
483+
int16_t cas = bmi270GetGyroCas((int16_t)(gyroData[8]));
484+
gyro->gyroADCRaw[X] = gyroData[4] - (cas * gyroData[6] / 512);
441485
gyro->gyroADCRaw[Y] = gyroData[5];
442486
gyro->gyroADCRaw[Z] = gyroData[6];
443487
break;

0 commit comments

Comments
 (0)