From 9c214b740f0764d239d270df9a0fa1f75f412a8a Mon Sep 17 00:00:00 2001 From: bkleiner Date: Wed, 1 Nov 2023 20:56:34 +0100 Subject: [PATCH] gyro: use async transaction --- src/driver/spi_bmi270.c | 16 ++++++++++------ src/driver/spi_bmi323.c | 16 ++++++++++------ src/driver/spi_gyro.c | 2 ++ src/driver/spi_icm42605.c | 16 ++++++++++------ src/driver/spi_mpu6xxx.c | 16 ++++++++++------ 5 files changed, 42 insertions(+), 24 deletions(-) diff --git a/src/driver/spi_bmi270.c b/src/driver/spi_bmi270.c index cddb785df..5c662551a 100644 --- a/src/driver/spi_bmi270.c +++ b/src/driver/spi_bmi270.c @@ -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]); @@ -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] = { diff --git a/src/driver/spi_bmi323.c b/src/driver/spi_bmi323.c index 9c53fb086..b3f021da4 100644 --- a/src/driver/spi_bmi323.c +++ b/src/driver/spi_bmi323.c @@ -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]); @@ -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 \ No newline at end of file diff --git a/src/driver/spi_gyro.c b/src/driver/spi_gyro.c index adf9a1c24..e50c99e71 100644 --- a/src/driver/spi_gyro.c +++ b/src/driver/spi_gyro.c @@ -108,6 +108,8 @@ uint8_t gyro_spi_init() { break; } + gyro_spi_read(); // dummy read to fill buffers + return gyro_type; } diff --git a/src/driver/spi_icm42605.c b/src/driver/spi_icm42605.c index 47b51daff..9db82a074 100644 --- a/src/driver/spi_icm42605.c +++ b/src/driver/spi_icm42605.c @@ -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; @@ -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 \ No newline at end of file diff --git a/src/driver/spi_mpu6xxx.c b/src/driver/spi_mpu6xxx.c index b26120535..58b35224b 100644 --- a/src/driver/spi_mpu6xxx.c +++ b/src/driver/spi_mpu6xxx.c @@ -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]); @@ -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 \ No newline at end of file