/* * Copyright (c) 2021 Nordic Semiconductor ASA * SPDX-License-Identifier: Apache-2.0 */ #define DT_DRV_COMPAT bosch_bma255 #include #include #include #include #include #include LOG_MODULE_REGISTER(bma255, CONFIG_SENSOR_LOG_LEVEL); // Accelerometer registers const uint8_t ACCEL_REG_BWG_CHIPID = 0x00; const uint8_t ACCEL_REG_ACCD_X_LSB = 0x02; const uint8_t ACCEL_REG_ACCD_X_MSB = 0x03; const uint8_t ACCEL_REG_ACCD_Y_LSB = 0x04; const uint8_t ACCEL_REG_ACCD_Y_MSB = 0x05; const uint8_t ACCEL_REG_ACCD_Z_LSB = 0x06; const uint8_t ACCEL_REG_ACCD_Z_MSB = 0x07; const uint8_t ACCEL_REG_ACCD_TEMP = 0x08; const uint8_t ACCEL_REG_INT_STATUS_0 = 0x09; const uint8_t ACCEL_REG_INT_STATUS_1 = 0x0A; const uint8_t ACCEL_REG_INT_STATUS_2 = 0x0B; const uint8_t ACCEL_REG_INT_STATUS_3 = 0x0C; const uint8_t ACCEL_REG_FIFO_STATUS = 0x0E; const uint8_t ACCEL_REG_PMU_RANGE = 0x0F; typedef enum accel_pmu_range_t { PMU_RANGE_2G = 0b0011, ///< DEFAULT PMU_RANGE_4G = 0b0101, PMU_RANGE_8G = 0b1000, PMU_RANGE_16G = 0b1100, } accel_pmu_range_t; const uint8_t ACCEL_REG_PMU_BW = 0x10; typedef enum accel_pmu_bw_t { PMU_BW_8HZ = 0b01000, PMU_BW_16HZ = 0b01001, PMU_BW_31HZ = 0b01010, PMU_BW_63HZ = 0b01011, PMU_BW_125HZ = 0b01100, PMU_BW_250HZ = 0b01101, PMU_BW_500HZ = 0b01110, PMU_BW_1000HZ = 0b01111, } accel_pmu_bw_t; const uint8_t ACCEL_REG_PMU_LPW = 0x11; const uint8_t ACCEL_REG_PMU_LOW_POWER = 0x12; const uint8_t ACCEL_REG_ACCD_HBW = 0x13; const uint8_t ACCEL_REG_BGW_SOFTRESET = 0x14; const uint8_t BGW_SOFTRESET = 0xB6; ///< Soft reset occurs when this value is written to ACCEL_REG_BGW_SOFTRESET const uint8_t ACCEL_REG_INT_EN_0 = 0x16; const uint8_t ACCEL_REG_INT_EN_1 = 0x17; const uint8_t ACCEL_REG_INT_EN_2 = 0x18; const uint8_t ACCEL_REG_INT_MAP_0 = 0x19; const uint8_t ACCEL_REG_INT_MAP_1 = 0x1A; const uint8_t ACCEL_REG_INT_MAP_2 = 0x1B; const uint8_t ACCEL_REG_INT_SRC = 0x1E; const uint8_t ACCEL_REG_INT_OUT_CTRL = 0x20; const uint8_t ACCEL_REG_INT_RST_LATCH = 0x21; const uint8_t ACCEL_REG_INT_0 = 0x22; const uint8_t ACCEL_REG_INT_1 = 0x23; const uint8_t ACCEL_REG_INT_2 = 0x24; const uint8_t ACCEL_REG_INT_3 = 0x25; const uint8_t ACCEL_REG_INT_4 = 0x26; const uint8_t ACCEL_REG_INT_5 = 0x27; const uint8_t ACCEL_REG_INT_6 = 0x28; const uint8_t ACCEL_REG_INT_7 = 0x29; const uint8_t ACCEL_REG_INT_8 = 0x2A; const uint8_t ACCEL_REG_INT_9 = 0x2B; const uint8_t ACCEL_REG_INT_A = 0x2C; const uint8_t ACCEL_REG_INT_B = 0x2D; const uint8_t ACCEL_REG_INT_C = 0x2E; const uint8_t ACCEL_REG_INT_D = 0x2F; const uint8_t ACCEL_REG_FIFO_CONFIG_0 = 0x30; const uint8_t ACCEL_REG_PMU_SELF_TEST = 0x32; const uint8_t ACCEL_REG_TRIM_NVM_CTRL = 0x33; const uint8_t ACCEL_REG_BGW_SPI3_WDT = 0x34; const uint8_t ACCEL_REG_OFC_CTRL = 0x36; const uint8_t ACCEL_REG_OFC_SETTING = 0x37; const uint8_t ACCEL_REG_OFC_OFFSET_X = 0x38; const uint8_t ACCEL_REG_OFC_OFFSET_Y = 0x39; const uint8_t ACCEL_REG_OFC_OFFSET_Z = 0x3A; const uint8_t ACCEL_REG_TRIM_GP0 = 0x3B; 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; }; struct bma255_config { struct i2c_dt_spec bus; #ifdef CONFIG_INA230_TRIGGER bool trig_enabled; uint16_t mask; const struct gpio_dt_spec alert_gpio; uint16_t alert_limit; #endif /* CONFIG_INA230_TRIGGER */ }; static int bma255_sample_fetch(const struct device *dev, enum sensor_channel chan) { const struct bma255_config *config = dev->config; struct bma255_data *data = dev->data; int ret; uint8_t accel[6]; ret = i2c_burst_read_dt(&config->bus, ACCEL_REG_ACCD_X_LSB, accel, sizeof(accel)); if (ret < 0) { LOG_ERR("Failed to read acceleration registers!"); return ret; } data->accel_x = ((int16_t)sys_get_le16(&accel[0])) >> 4; 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) { LOG_ERR("Failed to read temperature register!"); return ret; } return 0; } static int bma255_channel_get(const struct device *dev, enum sensor_channel chan, struct sensor_value *val) { struct bma255_data *data = dev->data; switch (chan) { // degrees C case SENSOR_CHAN_DIE_TEMP: // 0.5K/LSB, center temperature is 23C val->val1 = 23 + data->temperature / 2; val->val2 = 0; // TODO: don't throw out LSB break; // m/s^2 case SENSOR_CHAN_ACCEL_X: { float accel = data->accel_x * 0.00098 * 9.80665; // to gees, to m/s^2 val->val1 = accel; val->val2 = (accel - val->val1) * 1000000; break; } case SENSOR_CHAN_ACCEL_Y: { float accel = data->accel_y * 0.00098 * 9.80665; // to gees, to m/s^2 val->val1 = accel; val->val2 = (accel - val->val1) * 1000000; break; } case SENSOR_CHAN_ACCEL_Z: { // For now assume 2g since that's the default value // 2g 0.98mg/LSB // 4g 1.95mg/LSB // 8g 3.91mg/LSB // 16g 7.81mg/LSB // 1 g = 9.80665 m/s^2 float accel = data->accel_z * 0.00098 * 9.80665; // to gees, to m/s^2 val->val1 = accel; 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; } return 0; } static const struct sensor_driver_api bma255_api = { .sample_fetch = &bma255_sample_fetch, .channel_get = &bma255_channel_get, }; static int bma255_init(const struct device *dev) { const struct bma255_config *const config = dev->config; int ret; if (!device_is_ready(config->bus.bus)) { LOG_ERR("I2C bus %s is not ready", config->bus.bus->name); return -ENODEV; } uint8_t chip_id; ret = i2c_burst_read_dt(&config->bus, ACCEL_REG_BWG_CHIPID, &chip_id, sizeof(chip_id)); if (ret < 0) { LOG_ERR("Failed to read chip ID register!"); return ret; } const uint8_t CHIP_ID = 0xfa; if (chip_id != CHIP_ID) { LOG_ERR("Chip ID read from %s incorrect. Read 0x%02X, expected 0x%02X", dev->name, chip_id, CHIP_ID); } // // Reset the sensor // uint8_t reset_val = BGW_SOFTRESET; // i2c_burst_write_dt(&config->bus, ACCEL_REG_BGW_SOFTRESET, &reset_val, sizeof(reset_val)); // // Wait for device to reset uint8_t lpw; ret = i2c_burst_read_dt(&config->bus, ACCEL_REG_PMU_LPW, &lpw, sizeof(lpw)); if (ret < 0) { LOG_ERR("Failed to read LPW register!"); return ret; } LOG_INF("LPW register: 0x%02X", lpw); // Write configuration uint8_t accel_range = PMU_RANGE_2G; i2c_burst_write_dt(&config->bus, ACCEL_REG_PMU_RANGE, &accel_range, sizeof(accel_range)); uint8_t accel_bw = PMU_BW_8HZ; i2c_burst_write_dt(&config->bus, ACCEL_REG_PMU_BW, &accel_bw, sizeof(accel_bw)); 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; } #define BMA255_INIT(i) \ static struct bma255_data bma255_data_##i; \ \ static const struct bma255_config bma255_config_##i = { \ .bus = I2C_DT_SPEC_INST_GET(i), \ }; \ \ SENSOR_DEVICE_DT_INST_DEFINE(i, &bma255_init, NULL, \ &bma255_data_##i, \ &bma255_config_##i, POST_KERNEL, \ CONFIG_SENSOR_INIT_PRIORITY, &bma255_api); DT_INST_FOREACH_STATUS_OKAY(BMA255_INIT)