235 lines
6.8 KiB
C
235 lines
6.8 KiB
C
/*
|
||
* SPDX-License-Identifier: Apache-2.0
|
||
*
|
||
* QMC5883P 3-axis magnetometer driver (I2C).
|
||
*
|
||
* Zephyr 上游只有 QMC5883L 驱动(寄存器不兼容),本驱动按 QMC5883P 数据手册
|
||
* (Doc# 13-52-19 Rev A)实现:
|
||
* - 上电默认 Suspend 模式,软复位后配置量程/ODR/过采样并进入 Normal 模式
|
||
* - 输出为 16bit 二进制补码,**小端**(LSB 在低地址)
|
||
* - 芯片内置温度补偿与 Set/Reset 偏置消除,补偿后数据直接放输出寄存器
|
||
*
|
||
* 输出遵循 Zephyr sensor API:SENSOR_CHAN_MAGN_X/Y/Z,单位 Gauss(val1 +
|
||
* val2/1e6)。mag 为无方向偏置的原始场强,硬/软磁校准由应用层完成。
|
||
*/
|
||
|
||
#include <zephyr/device.h>
|
||
#include <zephyr/drivers/i2c.h>
|
||
#include <zephyr/drivers/sensor.h>
|
||
#include <zephyr/kernel.h>
|
||
#include <zephyr/logging/log.h>
|
||
#include <zephyr/sys/util.h>
|
||
|
||
LOG_MODULE_REGISTER(qmc5883p, CONFIG_QMC5883P_LOG_LEVEL);
|
||
|
||
// 寄存器地址
|
||
#define REG_CHIP_ID 0x00
|
||
#define REG_DATA_X_LSB 0x01 // X 低字节,MSB 在 +1
|
||
#define REG_DATA_Y_LSB 0x03
|
||
#define REG_DATA_Z_LSB 0x05
|
||
#define REG_STATUS 0x09 // bit0 DRDY,bit1 OVFL
|
||
#define REG_CTRL1 0x0A // OSR2<7:6> OSR1<5:4> ODR<3:2> MODE<1:0>
|
||
#define REG_CTRL2 0x0B // SOFT_RST<7> SELF_TEST<6> RNG<3:2> SET/RESET<1:0>
|
||
#define REG_AXIS_SIGN 0x29 // 手册示例要求写 0x06(定义 XYZ 轴符号)
|
||
|
||
#define CHIP_ID_QMC5883P 0x80
|
||
|
||
#define STATUS_DRDY BIT(0)
|
||
|
||
// CTRL1:过采样/速率/模式(编码见数据手册 Table 17)
|
||
#define CTRL1_OSR2_8 (0x3 << 6) // OSR2=8(最强降采样,噪声最低)
|
||
#define CTRL1_OSR1_8 (0x0 << 4) // OSR1=8
|
||
#define CTRL1_ODR_100HZ (0x2 << 2) // 100Hz 输出速率
|
||
#define MODE_NORMAL 0x1 // Normal 模式(连续测量,带间歇睡眠)
|
||
|
||
// CTRL2:软复位 / 量程 / Set-Reset(编码见数据手册 Table 18)
|
||
#define CTRL2_SOFT_RST BIT(7)
|
||
#define RNG_30G (0x0 << 2)
|
||
#define RNG_12G (0x1 << 2)
|
||
#define RNG_8G (0x2 << 2)
|
||
#define RNG_2G (0x3 << 2)
|
||
#define SETRESET_ON (0x0 << 0) // Set and Reset On(每次测量刷新偏置)
|
||
|
||
// 数据手册示例固定的轴符号寄存器值
|
||
#define AXIS_SIGN_VAL 0x06
|
||
|
||
// devicetree 的 range 属性(Gauss)-> CTRL2 RNG 位
|
||
#define QMC_RNG_BITS(range) ((range) == 30 ? RNG_30G : (range) == 12 ? RNG_12G : \
|
||
(range) == 8 ? RNG_8G : RNG_2G)
|
||
|
||
// devicetree 的 range 属性(Gauss)-> 灵敏度 LSB/Gauss
|
||
#define QMC_SENSITIVITY(range) ((range) == 30 ? 1000.0f : (range) == 12 ? 2500.0f : \
|
||
(range) == 8 ? 3750.0f : 15000.0f)
|
||
|
||
struct qmc5883p_data {
|
||
int16_t x;
|
||
int16_t y;
|
||
int16_t z;
|
||
};
|
||
|
||
struct qmc5883p_config {
|
||
struct i2c_dt_spec i2c;
|
||
uint8_t rng; // CTRL2 的 RNG<1:0> 位
|
||
float sensitivity; // LSB/Gauss
|
||
};
|
||
|
||
/**
|
||
* 读取并触发一次三轴磁场测量。
|
||
* @param dev 传感器设备。
|
||
* @param chan 请求的通道(本驱动忽略,始终读取 XYZ)。
|
||
* @return 0 成功,负值为错误码。
|
||
*/
|
||
static int qmc5883p_fetch(const struct device *dev, enum sensor_channel chan)
|
||
{
|
||
struct qmc5883p_data *d = dev->data;
|
||
const struct qmc5883p_config *cfg = dev->config;
|
||
uint8_t buf[6];
|
||
uint8_t st;
|
||
int ret;
|
||
|
||
ARG_UNUSED(chan);
|
||
|
||
// 轮询 DRDY(读状态寄存器会清 DRDY)。100Hz 下周期 10ms,超时留足。
|
||
for (int tries = 0; tries < 50; tries++) {
|
||
ret = i2c_reg_read_byte_dt(&cfg->i2c, REG_STATUS, &st);
|
||
if (ret < 0) {
|
||
return ret;
|
||
}
|
||
if (st & STATUS_DRDY) {
|
||
break;
|
||
}
|
||
k_msleep(1);
|
||
}
|
||
|
||
ret = i2c_burst_read_dt(&cfg->i2c, REG_DATA_X_LSB, buf, sizeof(buf));
|
||
if (ret < 0) {
|
||
return ret;
|
||
}
|
||
|
||
// 小端:低地址为 LSB,高地址为 MSB;16bit 二进制补码
|
||
d->x = (int16_t)((buf[1] << 8) | buf[0]);
|
||
d->y = (int16_t)((buf[3] << 8) | buf[2]);
|
||
d->z = (int16_t)((buf[5] << 8) | buf[4]);
|
||
LOG_DBG("raw x=%d y=%d z=%d", d->x, d->y, d->z);
|
||
return 0;
|
||
}
|
||
|
||
/**
|
||
* 读取某一轴的磁场值(Gauss)。
|
||
* @param dev 传感器设备。
|
||
* @param chan SENSOR_CHAN_MAGN_X/Y/Z。
|
||
* @param val 输出 sensor_value(整数 Gauss + 百万分之一 Gauss)。
|
||
* @return 0 成功,-ENOTSUP 表示不支持的通道。
|
||
*/
|
||
static int qmc5883p_get(const struct device *dev, enum sensor_channel chan,
|
||
struct sensor_value *val)
|
||
{
|
||
struct qmc5883p_data *d = dev->data;
|
||
const struct qmc5883p_config *cfg = dev->config;
|
||
float g;
|
||
|
||
switch (chan) {
|
||
case SENSOR_CHAN_MAGN_X:
|
||
g = (float)d->x / cfg->sensitivity;
|
||
break;
|
||
case SENSOR_CHAN_MAGN_Y:
|
||
g = (float)d->y / cfg->sensitivity;
|
||
break;
|
||
case SENSOR_CHAN_MAGN_Z:
|
||
g = (float)d->z / cfg->sensitivity;
|
||
break;
|
||
default:
|
||
return -ENOTSUP;
|
||
}
|
||
|
||
val->val1 = (int32_t)g;
|
||
val->val2 = (int32_t)((g - (float)val->val1) * 1000000.0f);
|
||
return 0;
|
||
}
|
||
|
||
/**
|
||
* 初始化磁力计:校验 ID、软复位、写轴符号、配置量程与工作模式。
|
||
* @param dev 传感器设备。
|
||
* @return 0 成功,负值为错误码。
|
||
*/
|
||
static int qmc5883p_init(const struct device *dev)
|
||
{
|
||
const struct qmc5883p_config *cfg = dev->config;
|
||
uint8_t id;
|
||
int ret;
|
||
|
||
if (!i2c_is_ready_dt(&cfg->i2c)) {
|
||
LOG_ERR("I2C bus not ready");
|
||
return -ENODEV;
|
||
}
|
||
|
||
ret = i2c_reg_read_byte_dt(&cfg->i2c, REG_CHIP_ID, &id);
|
||
if (ret < 0) {
|
||
return ret;
|
||
}
|
||
if (id != CHIP_ID_QMC5883P) {
|
||
LOG_ERR("unexpected chip id 0x%02x (want 0x%02x)", id,
|
||
CHIP_ID_QMC5883P);
|
||
return -EIO;
|
||
}
|
||
|
||
// 软复位:恢复所有寄存器默认值并进入 Suspend 模式
|
||
ret = i2c_reg_write_byte_dt(&cfg->i2c, REG_CTRL2, CTRL2_SOFT_RST);
|
||
if (ret < 0) {
|
||
return ret;
|
||
}
|
||
k_msleep(10);
|
||
|
||
// 轴符号(数据手册示例固定值)
|
||
ret = i2c_reg_write_byte_dt(&cfg->i2c, REG_AXIS_SIGN, AXIS_SIGN_VAL);
|
||
if (ret < 0) {
|
||
return ret;
|
||
}
|
||
|
||
// CTRL2:量程 + Set/Reset On(刷新偏置消除)
|
||
ret = i2c_reg_write_byte_dt(&cfg->i2c, REG_CTRL2,
|
||
cfg->rng | SETRESET_ON);
|
||
if (ret < 0) {
|
||
return ret;
|
||
}
|
||
|
||
// CTRL1:进入 Normal 模式,OSR2=8/OSR1=8/ODR=100Hz
|
||
ret = i2c_reg_write_byte_dt(&cfg->i2c, REG_CTRL1,
|
||
CTRL1_OSR2_8 | CTRL1_OSR1_8 | CTRL1_ODR_100HZ |
|
||
MODE_NORMAL);
|
||
if (ret < 0) {
|
||
return ret;
|
||
}
|
||
|
||
// 等待首次测量完成
|
||
k_msleep(20);
|
||
|
||
LOG_INF("ready: id=0x%02x range=%dG sens=%.0f LSB/G", id,
|
||
(int)(cfg->sensitivity == 1000.0f ? 30 :
|
||
cfg->sensitivity == 2500.0f ? 12 :
|
||
cfg->sensitivity == 3750.0f ? 8 : 2),
|
||
(double)cfg->sensitivity);
|
||
return 0;
|
||
}
|
||
|
||
static const struct sensor_driver_api qmc5883p_api = {
|
||
.sample_fetch = qmc5883p_fetch,
|
||
.channel_get = qmc5883p_get,
|
||
};
|
||
|
||
#define QMC5883P_DEFINE(inst) \
|
||
static struct qmc5883p_data qmc5883p_data_##inst; \
|
||
static const struct qmc5883p_config qmc5883p_cfg_##inst = { \
|
||
.i2c = I2C_DT_SPEC_INST_GET(inst), \
|
||
.rng = QMC_RNG_BITS(DT_INST_PROP(inst, range)), \
|
||
.sensitivity = QMC_SENSITIVITY(DT_INST_PROP(inst, range)), \
|
||
}; \
|
||
DEVICE_DT_INST_DEFINE(inst, qmc5883p_init, NULL, \
|
||
&qmc5883p_data_##inst, \
|
||
&qmc5883p_cfg_##inst, \
|
||
POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, \
|
||
&qmc5883p_api)
|
||
|
||
#define DT_DRV_COMPAT misaki_qmc5883p
|
||
DT_INST_FOREACH_STATUS_OKAY(QMC5883P_DEFINE)
|