first
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
add_subdirectory(drivers)
|
||||
@@ -0,0 +1,3 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
rsource "drivers/Kconfig"
|
||||
@@ -0,0 +1,49 @@
|
||||
# zephyr-qmc5883p — QST QMC5883P 磁力计驱动(Zephyr 模块)
|
||||
|
||||
标准 Zephyr out-of-tree 模块:自定义 binding + Kconfig + 驱动实现。
|
||||
Zephyr 上游只有 QMC5883L 驱动,寄存器与 QMC5883P 不兼容,本模块按
|
||||
QMC5883P 数据手册(Doc# 13-52-19 Rev A)自行实现。
|
||||
|
||||
## 目录结构
|
||||
|
||||
zephyr-qmc5883p/
|
||||
├── zephyr/module.yml
|
||||
├── CMakeLists.txt / Kconfig
|
||||
├── dts/bindings/sensor/misaki,qmc5883p.yaml
|
||||
└── drivers/sensor/qmc5883p/
|
||||
├── Kconfig # CONFIG_QMC5883P
|
||||
├── CMakeLists.txt
|
||||
└── qmc5883p.c
|
||||
|
||||
## 接入步骤
|
||||
|
||||
1. 构建时传 `-DZEPHYR_EXTRA_MODULES=/path/to/zephyr-qmc5883p`
|
||||
(本项目已通过 `CMakePresets.json` 挂载)。
|
||||
2. 板级 dts 加节点(默认地址 0x2C):
|
||||
|
||||
&i2c3 {
|
||||
qmc5883p: qmc5883p@2c {
|
||||
compatible = "misaki,qmc5883p";
|
||||
reg = <0x2c>;
|
||||
range = <8>; /* ±8 Gauss */
|
||||
};
|
||||
};
|
||||
|
||||
3. prj.conf 追加 `CONFIG_QMC5883P=y`(节点 enable 后再开,否则 Kconfig
|
||||
依赖不满足直接中止构建)。
|
||||
|
||||
## 关键设计点
|
||||
|
||||
- 上电默认 **Suspend 模式**;init 顺序:校验 chip ID(0x00=0x80) → 软复位
|
||||
(CTRL2=0x80) → 写轴符号寄存器 0x29=0x06(数据手册示例固定值)→
|
||||
CTRL2 配置量程 + Set/Reset On → CTRL1 进入 Normal 模式(OSR2=8、
|
||||
OSR1=8、ODR=100Hz)。
|
||||
- 数据寄存器 0x01~0x06 为 **16bit 二进制补码、小端**(LSB 在低地址)。
|
||||
- `fetch` 轮询状态寄存器 0x09 的 DRDY(**读状态寄存器即清 DRDY**),
|
||||
就绪后一次 burst 读 6 字节。
|
||||
- `range` 属性(2/8/12/30 Gauss)选择 CTRL2 的 RNG 位与灵敏度
|
||||
(15000/3750/2500/1000 LSB/G)。
|
||||
- 芯片内置温度补偿与 Set/Reset 偏置消除,输出已是补偿后数据。
|
||||
- 应用层:`sensor_sample_fetch()` + `sensor_channel_get()`
|
||||
(SENSOR_CHAN_MAGN_X/Y/Z),单位 **Gauss**(val1.val2 合成)。
|
||||
硬磁/软磁校准、轴对齐由应用层完成。
|
||||
@@ -0,0 +1,3 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
add_subdirectory_ifdef(CONFIG_QMC5883P sensor/qmc5883p)
|
||||
@@ -0,0 +1,3 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
rsource "sensor/qmc5883p/Kconfig"
|
||||
@@ -0,0 +1,4 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
zephyr_library()
|
||||
zephyr_library_sources(qmc5883p.c)
|
||||
@@ -0,0 +1,25 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
menuconfig QMC5883P
|
||||
bool "QST QMC5883P 3-axis magnetometer"
|
||||
depends on DT_HAS_MISAKI_QMC5883P_ENABLED
|
||||
select I2C
|
||||
select SENSOR
|
||||
default y
|
||||
help
|
||||
QST QMC5883P 3-axis magnetometer over I2C.
|
||||
Zephyr has no upstream driver (the QMC5883L register map is
|
||||
incompatible), so this module implements the QMC5883P registers
|
||||
per its datasheet: normal mode at 100 Hz, 16-bit two's complement
|
||||
output, field range configurable via the 'range' devicetree
|
||||
property. Output is standard SENSOR_CHAN_MAGN_XYZ in Gauss.
|
||||
|
||||
if QMC5883P
|
||||
|
||||
config QMC5883P_LOG_LEVEL
|
||||
int "QMC5883P log level"
|
||||
depends on LOG
|
||||
range 0 4
|
||||
default 3
|
||||
|
||||
endif # QMC5883P
|
||||
@@ -0,0 +1,234 @@
|
||||
/*
|
||||
* 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)
|
||||
@@ -0,0 +1,37 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
#
|
||||
# QST QMC5883P 三轴磁力计 binding(I2C)。
|
||||
# Zephyr 上游无驱动(QMC5883L 寄存器不兼容),本模块按 QMC5883P 数据手册实现。
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# &i2c3 {
|
||||
# qmc5883p: qmc5883p@2c {
|
||||
# compatible = "misaki,qmc5883p";
|
||||
# reg = <0x2c>;
|
||||
# range = <8>;
|
||||
# status = "okay";
|
||||
# };
|
||||
# };
|
||||
|
||||
description: QST QMC5883P 3-axis magnetometer (I2C)
|
||||
|
||||
compatible: "misaki,qmc5883p"
|
||||
|
||||
include: [sensor-device.yaml, i2c-device.yaml]
|
||||
|
||||
properties:
|
||||
reg:
|
||||
required: true
|
||||
description: |
|
||||
I2C address. QMC5883P default is 0x2C.
|
||||
|
||||
range:
|
||||
type: int
|
||||
default: 8
|
||||
enum: [2, 8, 12, 30]
|
||||
description: |
|
||||
Magnetic field full-scale range in Gauss (RNG bits of CTRL2).
|
||||
Sensitivity is 15000 / 3750 / 2500 / 1000 LSB/G for ±2/8/12/30 G
|
||||
respectively. Smaller range gives finer resolution but saturates
|
||||
sooner (relevant with large hard-iron offsets / motor currents).
|
||||
@@ -0,0 +1,6 @@
|
||||
name: qmc5883p
|
||||
build:
|
||||
cmake: .
|
||||
kconfig: Kconfig
|
||||
settings:
|
||||
dts_root: .
|
||||
Reference in New Issue
Block a user