Skip to content

Commit

Permalink
gyro: refactor update functions
Browse files Browse the repository at this point in the history
  • Loading branch information
bkleiner committed Nov 1, 2023
1 parent afbf6f2 commit d5b928c
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 38 deletions.
5 changes: 1 addition & 4 deletions src/driver/spi_bmi270.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,12 @@ void bmi270_read_gyro_data(gyro_data_t *data) {
spi_bus_device_reconfigure(&gyro_bus, SPI_MODE_TRAILING_EDGE, SPI_SPEED_FAST);

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(&gyro_bus, NULL, gyro_segs);

spi_txn_wait(&gyro_bus);
spi_seg_submit_wait(&gyro_bus, gyro_segs);

data->accel.axis[0] = -(int16_t)((buf[1] << 8) | buf[0]);
data->accel.axis[1] = -(int16_t)((buf[3] << 8) | buf[2]);
Expand Down
29 changes: 3 additions & 26 deletions src/driver/spi_gyro.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ uint8_t gyro_spi_init() {
}

gyro_data_t gyro_spi_read() {
gyro_data_t data;
static gyro_data_t data;

switch (gyro_type) {
case GYRO_TYPE_MPU6000:
Expand All @@ -112,36 +112,13 @@ gyro_data_t gyro_spi_read() {
case GYRO_TYPE_ICM20602:
case GYRO_TYPE_ICM20608:
case GYRO_TYPE_ICM20689: {
uint8_t buf[14];
mpu6xxx_read_data(MPU_RA_ACCEL_XOUT_H, buf, 14);

data.accel.axis[0] = -(int16_t)((buf[0] << 8) | buf[1]);
data.accel.axis[1] = -(int16_t)((buf[2] << 8) | buf[3]);
data.accel.axis[2] = (int16_t)((buf[4] << 8) | buf[5]);

data.temp = (float)((int16_t)((buf[6] << 8) | buf[7])) / 333.87f + 21.f;

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]);
mpu6xxx_read_gyro_data(&data);
break;
}

case GYRO_TYPE_ICM42605:
case GYRO_TYPE_ICM42688P: {
uint8_t buf[14];
icm42605_read_data(ICM42605_TEMP_DATA1, buf, 14);

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

data.accel.axis[0] = -(int16_t)((buf[2] << 8) | buf[3]);
data.accel.axis[1] = -(int16_t)((buf[4] << 8) | buf[5]);
data.accel.axis[2] = (int16_t)((buf[6] << 8) | buf[7]);

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]);

icm42605_read_gyro_data(&data);
break;
}

Expand Down
17 changes: 14 additions & 3 deletions src/driver/spi_icm42605.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,24 @@ void icm42605_write(uint8_t reg, uint8_t data) {
spi_seg_submit_wait(&gyro_bus, segs);
}

void icm42605_read_data(uint8_t reg, uint8_t *data, uint32_t size) {
void icm42605_read_gyro_data(gyro_data_t *data) {
spi_bus_device_reconfigure(&gyro_bus, SPI_MODE_TRAILING_EDGE, SPI_SPEED_FAST);

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

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

data->accel.axis[0] = -(int16_t)((buf[2] << 8) | buf[3]);
data->accel.axis[1] = -(int16_t)((buf[4] << 8) | buf[5]);
data->accel.axis[2] = (int16_t)((buf[6] << 8) | buf[7]);

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]);
}
#endif
4 changes: 3 additions & 1 deletion src/driver/spi_icm42605.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <stdint.h>

#include "driver/spi_gyro.h"

// Bank 0
#define ICM42605_DEVICE_CONFIG 0x11
#define ICM42605_DRIVE_CONFIG 0x13
Expand Down Expand Up @@ -196,4 +198,4 @@ void icm42605_configure();
void icm42605_write(uint8_t reg, uint8_t data);

uint8_t icm42605_read(uint8_t reg);
void icm42605_read_data(uint8_t reg, uint8_t *data, uint32_t size);
void icm42605_read_gyro_data(gyro_data_t *data);
17 changes: 14 additions & 3 deletions src/driver/spi_mpu6xxx.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,25 @@ void mpu6xxx_write(uint8_t reg, uint8_t data) {
spi_seg_submit_wait(&gyro_bus, segs);
}

void mpu6xxx_read_data(uint8_t reg, uint8_t *data, uint32_t size) {
void mpu6xxx_read_gyro_data(gyro_data_t *data) {
spi_bus_device_reconfigure(&gyro_bus, SPI_MODE_TRAILING_EDGE, mpu6xxx_fast_divider());

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

data->accel.axis[0] = -(int16_t)((buf[0] << 8) | buf[1]);
data->accel.axis[1] = -(int16_t)((buf[2] << 8) | buf[3]);
data->accel.axis[2] = (int16_t)((buf[4] << 8) | buf[5]);

data->temp = (float)((int16_t)((buf[6] << 8) | buf[7])) / 333.87f + 21.f;

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]);
}

#endif
4 changes: 3 additions & 1 deletion src/driver/spi_mpu6xxx.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <stdint.h>

#include "driver/spi_gyro.h"

#define MPU_BIT_SLEEP 0x40
#define MPU_BIT_H_RESET 0x80
#define MPU_BITS_CLKSEL 0x07
Expand Down Expand Up @@ -131,4 +133,4 @@ void mpu6xxx_configure();
void mpu6xxx_write(uint8_t reg, uint8_t data);

uint8_t mpu6xxx_read(uint8_t reg);
void mpu6xxx_read_data(uint8_t reg, uint8_t *data, uint32_t size);
void mpu6xxx_read_gyro_data(gyro_data_t *data);

0 comments on commit d5b928c

Please sign in to comment.