split out magnetometer

This commit is contained in:
2024-07-04 23:30:11 -06:00
parent 9a2419223a
commit d8ac413a9a
7 changed files with 226 additions and 112 deletions

View File

@ -90,44 +90,11 @@ const uint8_t ACCEL_REG_TRIM_GP1 = 0x3C;
const uint8_t ACCEL_REG_FIFO_CONFIG_1 = 0x3E;
const uint8_t ACCEL_REG_FIFO_DATA = 0x3F;
const uint8_t MAG_REG_CHIP_ID = 0x40;
const uint8_t MAG_REG_DATA_X_LSB = 0x42;
const uint8_t MAG_REG_DATA_X_MSB = 0x43;
const uint8_t MAG_REG_DATA_Y_LSB = 0x44;
const uint8_t MAG_REG_DATA_Y_MSB = 0x45;
const uint8_t MAG_REG_DATA_Z_LSB = 0x46;
const uint8_t MAG_REG_DATA_Z_MSB = 0x47;
const uint8_t MAG_REG_RHALL_LSB = 0x48;
const uint8_t MAG_REG_RHALL_MSB = 0x49;
const uint8_t MAG_REG_INT_STATUS = 0x4A;
const uint8_t MAG_REG_POWER = 0x4B;
typedef enum mag_power_t
{
MAG_POWER_POWER_ON = 0x01,
MAG_POWER_RESET = 0x82,
} mag_power_t;
const uint8_t MAG_REG_MODE = 0x4C;
typedef enum mag_mode_t
{
MAG_MODE_OPMODE_NORMAL = 0x00 << 1,
MAG_MODE_OPMODE_FORCED = 0x01 << 1,
MAG_MODE_OPMODE_SLEEP_MODE = 0x11 << 1,
} mag_mode_t;
const uint8_t MAG_REG_INT_ENABLE = 0x4D;
const uint8_t MAG_REG_INT_SETTINGS = 0x4E;
const uint8_t MAG_REG_LOW_THRESH = 0x4F;
const uint8_t MAG_REG_HIGH_THRESH = 0x50;
const uint8_t MAG_REG_REP_XY = 0x51;
const uint8_t MAG_REG_REP_Z = 0x52;
struct bma255_data
{
int16_t accel_x;
int16_t accel_y;
int16_t accel_z;
int16_t mag_x;
int16_t mag_y;
int16_t mag_z;
int8_t temperature;
};
@ -160,19 +127,6 @@ static int bma255_sample_fetch(const struct device *dev,
data->accel_y = ((int16_t)sys_get_le16(&accel[2])) >> 4;
data->accel_z = ((int16_t)sys_get_le16(&accel[4])) >> 4;
uint8_t mag[6];
struct i2c_dt_spec mag_bus = config->bus;
mag_bus.addr = 0x10;
ret = i2c_burst_read_dt(&mag_bus, MAG_REG_DATA_X_LSB, mag, sizeof(mag));
if (ret < 0)
{
LOG_ERR("Failed to read mag registers!");
return ret;
}
data->mag_x = ((int16_t)sys_get_le16(&mag[0])) >> 1;
data->mag_y = ((int16_t)sys_get_le16(&mag[2])) >> 1;
data->mag_z = ((int16_t)sys_get_le16(&mag[4])) >> 1;
ret = i2c_burst_read_dt(&config->bus, ACCEL_REG_ACCD_TEMP, &data->temperature, sizeof(data->temperature));
if (ret < 0)
{
@ -225,28 +179,6 @@ static int bma255_channel_get(const struct device *dev,
val->val2 = (accel - val->val1) * 1000000;
break;
}
// Gauss
case SENSOR_CHAN_MAGN_X:
{
float rate = data->mag_x * 1300.0 / (2 << 15) / 100; // to uT, to gauss
val->val1 = rate;
val->val2 = (rate - val->val1) * 1000000;
break;
}
case SENSOR_CHAN_MAGN_Y:
{
float rate = data->mag_y * 1300.0 / (2 << 15) / 100; // to uT, to gauss
val->val1 = rate;
val->val2 = (rate - val->val1) * 1000000;
break;
}
case SENSOR_CHAN_MAGN_Z:
{
float rate = data->mag_z * 2500.0 / (2 << 15) / 100; // to uT, to gauss
val->val1 = rate;
val->val2 = (rate - val->val1) * 1000000;
break;
}
default:
return -ENOTSUP;
}
@ -308,37 +240,6 @@ static int bma255_init(const struct device *dev)
uint8_t hbw = (1 << 7); // data_high_bw (read filtered data) and enable lsb/msb shadowing
i2c_burst_write_dt(&config->bus, ACCEL_REG_ACCD_HBW, &hbw, sizeof(hbw));
struct i2c_dt_spec mag_bus = config->bus;
mag_bus.addr = 0x10;
uint8_t mag_power = MAG_POWER_POWER_ON;
ret = i2c_burst_write_dt(&mag_bus, MAG_REG_POWER, &mag_power, sizeof(mag_power));
if (ret < 0)
{
LOG_ERR("Failed to power up magnetometer");
return ret;
}
uint8_t mag_mode = MAG_MODE_OPMODE_NORMAL;
ret = i2c_burst_write_dt(&mag_bus, MAG_REG_MODE, &mag_mode, sizeof(mag_mode));
if (ret < 0)
{
LOG_ERR("Failed to start magnetometer");
return ret;
}
uint8_t mag_chip_id;
ret = i2c_burst_read_dt(&mag_bus, MAG_REG_CHIP_ID, &mag_chip_id, sizeof(mag_chip_id));
if (ret < 0)
{
LOG_ERR("Failed to read magnetometer chip ID register!");
return ret;
}
const uint8_t MAG_CHIP_ID = 0x32;
if (mag_chip_id != MAG_CHIP_ID)
{
LOG_ERR("Mag chip ID read from %s incorrect. Read 0x%02X, expected 0x%02X", dev->name, mag_chip_id, MAG_CHIP_ID);
}
return 0;
}