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 Apr 14, 2024
1 parent b663008 commit e068a9f
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 41 deletions.
34 changes: 17 additions & 17 deletions src/driver/spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,33 +148,33 @@ static inline bool spi_txn_ready(spi_bus_device_t *bus) {
return dev->txn_head == dev->txn_tail;
}

#define spi_seg_submit_wait(bus, segs) \
{ \
static_assert(__builtin_types_compatible_p(spi_txn_segment_t[], typeof(segs)), "spi segment not const array"); \
const uint32_t count = sizeof(segs) / sizeof(spi_txn_segment_t); \
static_assert(count < SPI_TXN_SEG_MAX, "spi segment count > max"); \
spi_seg_submit_wait_ex(bus, segs, count); \
#define spi_seg_submit_wait(_bus, _segs) \
{ \
static_assert(__builtin_types_compatible_p(spi_txn_segment_t[], typeof(_segs)), "spi segment not const array"); \
const uint32_t count = sizeof(_segs) / sizeof(spi_txn_segment_t); \
static_assert(count < SPI_TXN_SEG_MAX, "spi segment count > max"); \
spi_seg_submit_wait_ex(_bus, _segs, count); \
}
#define spi_seg_submit(bus, segs, ...) \
{ \
static_assert(__builtin_types_compatible_p(spi_txn_segment_t[], typeof(segs)), "spi segment not const array"); \
const uint32_t count = sizeof(segs) / sizeof(spi_txn_segment_t); \
static_assert(count < SPI_TXN_SEG_MAX, "spi segment count > max"); \
spi_seg_submit_ex(bus, (spi_txn_opts_t){.segs = segs, .seg_count = count, __VA_ARGS__}); \
#define spi_seg_submit(_bus, _segs, ...) \
{ \
static_assert(__builtin_types_compatible_p(spi_txn_segment_t[], typeof(_segs)), "spi segment not const array"); \
const uint32_t count = sizeof(_segs) / sizeof(spi_txn_segment_t); \
static_assert(count < SPI_TXN_SEG_MAX, "spi segment count > max"); \
spi_seg_submit_ex(_bus, (spi_txn_opts_t){.segs = _segs, .seg_count = count, __VA_ARGS__}); \
}
#define spi_seg_submit_continue(bus, segs, ...) ({ \
spi_seg_submit(bus, segs, __VA_ARGS__); \
spi_txn_continue(bus); \
#define spi_seg_submit_continue(_bus, _segs, ...) ({ \
spi_seg_submit(_bus, _segs, __VA_ARGS__); \
spi_txn_continue(_bus); \
})

static inline void spi_txn_set_done(void *arg) { *((bool *)arg) = true; }
#define spi_seg_submit_check(bus, segs, ...) \
#define spi_seg_submit_check(_bus, _segs, ...) \
({ \
static volatile bool __did_submit__ = false; \
static volatile bool __is_done__ = false; \
if (!__did_submit__) { \
__VA_ARGS__; \
spi_seg_submit_continue(bus, segs, \
spi_seg_submit_continue(_bus, _segs, \
.done_fn = spi_txn_set_done, \
.done_fn_arg = (void *)&__is_done__); \
__did_submit__ = true; \
Expand Down
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 e068a9f

Please sign in to comment.