Skip to content

Commit

Permalink
gyro: use async transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
bkleiner committed Nov 2, 2023
1 parent 157c372 commit 2c060ef
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
16 changes: 9 additions & 7 deletions src/driver/spi_bmi270.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,9 @@ void bmi270_read_data(uint8_t reg, uint8_t *data, uint32_t size) {

void bmi270_read_gyro_data(gyro_data_t *data) {
spi_bus_device_reconfigure(&gyro_bus, SPI_MODE_TRAILING_EDGE, SPI_SPEED_FAST);
spi_txn_wait(&gyro_bus);

uint8_t buf[12];
const spi_txn_segment_t gyro_segs[] = {
spi_make_seg_const(BMI270_REG_ACC_DATA_X_LSB | 0x80),
spi_make_seg_const(0xFF),
spi_make_seg_buffer(buf, NULL, 12),
};
spi_seg_submit_wait(&gyro_bus, gyro_segs);
static uint8_t buf[12];

data->accel.axis[0] = -(int16_t)((buf[1] << 8) | buf[0]);
data->accel.axis[1] = -(int16_t)((buf[3] << 8) | buf[2]);
Expand All @@ -233,6 +228,13 @@ void bmi270_read_gyro_data(gyro_data_t *data) {
data->gyro.axis[2] = gyro_data[2];

data->temp = 0;

const spi_txn_segment_t segs[] = {
spi_make_seg_const(BMI270_REG_ACC_DATA_X_LSB | 0x80),
spi_make_seg_const(0xFF),
spi_make_seg_buffer(buf, NULL, 12),
};
spi_seg_submit_continue(&gyro_bus, NULL, segs);
}

const uint8_t bmi270_config_file[8192] = {
Expand Down
2 changes: 2 additions & 0 deletions src/driver/spi_gyro.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ uint8_t gyro_spi_init() {
break;
}

gyro_spi_read(); // dummy read to fill buffers

return gyro_type;
}

Expand Down
14 changes: 8 additions & 6 deletions src/driver/spi_icm42605.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,9 @@ void icm42605_write(uint8_t reg, uint8_t data) {

void icm42605_read_gyro_data(gyro_data_t *data) {
spi_bus_device_reconfigure(&gyro_bus, SPI_MODE_TRAILING_EDGE, SPI_SPEED_FAST);
spi_txn_wait(&gyro_bus);

uint8_t buf[14];
const spi_txn_segment_t segs[] = {
spi_make_seg_const(ICM42605_TEMP_DATA1 | 0x80),
spi_make_seg_buffer(buf, NULL, 14),
};
spi_seg_submit_wait(&gyro_bus, segs);
static uint8_t buf[14];

data->temp = (float)((int16_t)((buf[0] << 8) | buf[1])) / 132.48f + 25.f;

Expand All @@ -126,5 +122,11 @@ void icm42605_read_gyro_data(gyro_data_t *data) {
data->gyro.axis[1] = (int16_t)((buf[8] << 8) | buf[9]);
data->gyro.axis[0] = (int16_t)((buf[10] << 8) | buf[11]);
data->gyro.axis[2] = (int16_t)((buf[12] << 8) | buf[13]);

const spi_txn_segment_t segs[] = {
spi_make_seg_const(ICM42605_TEMP_DATA1 | 0x80),
spi_make_seg_buffer(buf, NULL, 14),
};
spi_seg_submit_continue(&gyro_bus, NULL, segs);
}
#endif
14 changes: 8 additions & 6 deletions src/driver/spi_mpu6xxx.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,9 @@ void mpu6xxx_write(uint8_t reg, uint8_t data) {

void mpu6xxx_read_gyro_data(gyro_data_t *data) {
spi_bus_device_reconfigure(&gyro_bus, SPI_MODE_TRAILING_EDGE, mpu6xxx_fast_divider());
spi_txn_wait(&gyro_bus);

uint8_t buf[14];
const spi_txn_segment_t segs[] = {
spi_make_seg_const(MPU_RA_ACCEL_XOUT_H | 0x80),
spi_make_seg_buffer(buf, NULL, 14),
};
spi_seg_submit_wait(&gyro_bus, segs);
static uint8_t buf[14];

data->accel.axis[0] = -(int16_t)((buf[0] << 8) | buf[1]);
data->accel.axis[1] = -(int16_t)((buf[2] << 8) | buf[3]);
Expand All @@ -131,6 +127,12 @@ void mpu6xxx_read_gyro_data(gyro_data_t *data) {
data->gyro.axis[1] = (int16_t)((buf[8] << 8) | buf[9]);
data->gyro.axis[0] = (int16_t)((buf[10] << 8) | buf[11]);
data->gyro.axis[2] = (int16_t)((buf[12] << 8) | buf[13]);

const spi_txn_segment_t segs[] = {
spi_make_seg_const(MPU_RA_ACCEL_XOUT_H | 0x80),
spi_make_seg_buffer(buf, NULL, 14),
};
spi_seg_submit_continue(&gyro_bus, NULL, segs);
}

#endif

0 comments on commit 2c060ef

Please sign in to comment.