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 Jul 4, 2024
1 parent 48b1d00 commit 33f8b76
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 24 deletions.
16 changes: 10 additions & 6 deletions src/driver/spi_bmi270.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,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, 0xFF),
spi_make_seg_buffer(buf, NULL, 12),
};
spi_seg_submit_wait(&gyro_bus, gyro_segs);
static uint8_t buf[12];

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

data->temp = 0;

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

const uint8_t bmi270_config_file[8192] = {
Expand Down
16 changes: 10 additions & 6 deletions src/driver/spi_bmi323.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,9 @@ void bmi3_read_data(uint8_t reg, uint8_t *data, uint32_t size) {

void bmi323_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(BMI323_REG_ACC_DATA_X_LSB | 0x80, 0xFF),
spi_make_seg_buffer(buf, NULL, 12),
};
spi_seg_submit_wait(&gyro_bus, gyro_segs);
static uint8_t buf[12];

data->accel.pitch = -(int16_t)((buf[1] << 8) | buf[0]);
data->accel.roll = -(int16_t)((buf[3] << 8) | buf[2]);
Expand All @@ -209,6 +205,14 @@ void bmi323_read_gyro_data(gyro_data_t *data) {
data->gyro.yaw = gyro_data[2];

data->temp = 0;

const spi_txn_segment_t segs[] = {
spi_make_seg_const(BMI323_REG_ACC_DATA_X_LSB | 0x80, 0xFF),
spi_make_seg_buffer(buf, NULL, 12),
};
spi_seg_submit(&gyro_bus, segs);
while (!spi_txn_continue(&gyro_bus))
;
}

#endif
2 changes: 2 additions & 0 deletions src/driver/spi_gyro.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ uint8_t gyro_spi_init() {
break;
}

gyro_spi_read(); // dummy read to fill buffers

return gyro_type;
}

Expand Down
16 changes: 10 additions & 6 deletions src/driver/spi_icm42605.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,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 @@ -125,5 +121,13 @@ void icm42605_read_gyro_data(gyro_data_t *data) {
data->gyro.pitch = (int16_t)((buf[8] << 8) | buf[9]);
data->gyro.roll = (int16_t)((buf[10] << 8) | buf[11]);
data->gyro.yaw = (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(&gyro_bus, segs);
while (!spi_txn_continue(&gyro_bus))
;
}
#endif
16 changes: 10 additions & 6 deletions src/driver/spi_mpu6xxx.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,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.pitch = -(int16_t)((buf[0] << 8) | buf[1]);
data->accel.roll = -(int16_t)((buf[2] << 8) | buf[3]);
Expand All @@ -130,6 +126,14 @@ void mpu6xxx_read_gyro_data(gyro_data_t *data) {
data->gyro.pitch = (int16_t)((buf[8] << 8) | buf[9]);
data->gyro.roll = (int16_t)((buf[10] << 8) | buf[11]);
data->gyro.yaw = (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(&gyro_bus, segs);
while (!spi_txn_continue(&gyro_bus))
;
}

#endif

0 comments on commit 33f8b76

Please sign in to comment.