first
This commit is contained in:
@@ -0,0 +1,521 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* SPL06-001 / DPS310 barometric pressure sensor driver (I2C).
|
||||
*
|
||||
* SPL06-001(歌尔)与 DPS310(Infineon)为同一颗 die:寄存器布局、校准
|
||||
* 系数、补偿公式、缩放因子 k 完全相同。本驱动一份代码同时支持两种
|
||||
* compatible:
|
||||
* - "misaki,spl06" SPL06-001 节点
|
||||
* - "infineon,dps310" DPS310 节点(复用 Zephyr 上游 binding,
|
||||
* 通过 CONFIG_DPS310=n 让本驱动接管该节点)
|
||||
*
|
||||
* 配置对齐 Betaflight barometer_dps310.c:后台连续测量(MEAS_CFG=0x07),
|
||||
* 气压/温度默认 16x 过采样、32Hz,结果右移 6 位(P_SHIFT/T_SHIFT),
|
||||
* 二阶级数补偿在软件侧完成。
|
||||
*
|
||||
* 输出遵循 Zephyr sensor API:
|
||||
* - SENSOR_CHAN_PRESS 单位 kPa(val1 + val2/1e6)
|
||||
* - SENSOR_CHAN_AMBIENT_TEMP 单位 °C(val1 + val2/1e6)
|
||||
*/
|
||||
|
||||
#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(spl06, CONFIG_SPL06_LOG_LEVEL);
|
||||
|
||||
// 寄存器地址(SPL06-001 / DPS310 通用)
|
||||
#define SPL06_REG_PRS_B2 0x00
|
||||
#define SPL06_REG_TMP_B2 0x03
|
||||
#define SPL06_REG_PRS_CFG 0x06
|
||||
#define SPL06_REG_TMP_CFG 0x07
|
||||
#define SPL06_REG_MEAS_CFG 0x08
|
||||
#define SPL06_REG_CFG 0x09
|
||||
#define SPL06_REG_RESET 0x0C
|
||||
#define SPL06_REG_ID 0x0D
|
||||
#define SPL06_REG_COEF 0x10
|
||||
#define SPL06_REG_COEF_SRCE 0x28
|
||||
|
||||
// 芯片 ID:0x10 = DPS310 / SPL06-001(同 die,读 ID 无法区分,靠总线/地址
|
||||
// 区分);0x11 = SPL07_003(同族扩展片,多 c31/c40 两个系数,本驱动兼容)。
|
||||
#define SPL06_ID_DPS310_SPL06 0x10
|
||||
#define SPL06_ID_SPL07_003 0x11
|
||||
|
||||
// MEAS_CFG(0x08) 位定义
|
||||
#define MEAS_CFG_COEF_RDY BIT(7) // 校准系数已就绪
|
||||
#define MEAS_CFG_SENSOR_RDY BIT(6) // 传感器初始化完成
|
||||
#define MEAS_CFG_TMP_RDY BIT(5) // 温度结果就绪(读结果寄存器后自动清零)
|
||||
#define MEAS_CFG_PRS_RDY BIT(4) // 气压结果就绪(读结果寄存器后自动清零)
|
||||
#define MEAS_CTRL_BG_PRS_TMP 0x07 // 后台连续测量:气压 + 温度
|
||||
#define MEAS_CFG_RDY_BOTH (MEAS_CFG_PRS_RDY | MEAS_CFG_TMP_RDY)
|
||||
|
||||
// PRS_CFG/TMP_CFG bit[6:4] 测量速率编码,编码值 = log2(Hz):000=1Hz ... 111=128Hz
|
||||
#define SPL06_RATE_4HZ 2
|
||||
#define SPL06_RATE_8HZ 3
|
||||
#define SPL06_RATE_16HZ 4
|
||||
#define SPL06_RATE_32HZ 5
|
||||
|
||||
// TMP_CFG(0x07) bit7:温度源选择。1 = 压力 MEMS 元件温度,0 = ASIC 内部温度。
|
||||
// 校准系数对哪个源标定,测量就必须用哪个源,否则温度系统性偏差(并连带
|
||||
// 影响气压补偿)。
|
||||
#define TMP_CFG_TMP_EXT BIT(7)
|
||||
|
||||
// CFG_REG(0x09):过采样 >8x 时结果寄存器会溢出 24bit,必须右移 6 位。
|
||||
#define CFG_REG_T_SHIFT BIT(3)
|
||||
#define CFG_REG_P_SHIFT BIT(2)
|
||||
|
||||
// 补偿缩放因子 k(datasheet Table 7/9),下标 = log2(OSR)。
|
||||
// OSR<=8 时结果不移位;OSR>8 时结果右移 6 位,k 用移位版(故 8x 与 16x
|
||||
// 之间断档)。k 的选择必须与 CFG_REG 的移位配置严格配对,混用会导致结果
|
||||
// 变成天文数字。
|
||||
static const uint32_t spl06_k[8] = {
|
||||
524288, // 1x
|
||||
1572864, // 2x
|
||||
3670016, // 4x
|
||||
7864320, // 8x
|
||||
253952, // 16x = 2^19 * 31 >> 6
|
||||
516096, // 32x = 2^19 * 63 >> 6
|
||||
1040384, // 64x = 2^19 * 127 >> 6
|
||||
2088960, // 128x = 2^19 * 255 >> 6
|
||||
};
|
||||
|
||||
// 校准系数(datasheet 8.11),全部为二进制补码。
|
||||
// c31/c40 仅 SPL07_003 存在,其余芯片置 0(补偿公式中对应项自然消失)。
|
||||
struct spl06_calib {
|
||||
int16_t c0;
|
||||
int16_t c1;
|
||||
int32_t c00;
|
||||
int32_t c10;
|
||||
int16_t c01;
|
||||
int16_t c11;
|
||||
int16_t c20;
|
||||
int16_t c21;
|
||||
int16_t c30;
|
||||
int16_t c31;
|
||||
int16_t c40;
|
||||
};
|
||||
|
||||
struct spl06_data {
|
||||
struct spl06_calib calib;
|
||||
int32_t raw_prs;
|
||||
int32_t raw_tmp;
|
||||
};
|
||||
|
||||
struct spl06_config {
|
||||
struct i2c_dt_spec i2c;
|
||||
uint8_t press_idx; // log2(press-osr),索引 spl06_k 与移位配置
|
||||
uint8_t temp_idx; // log2(temp-osr)
|
||||
bool force_mems_temp; // true:强制 TMP_EXT=1。SPL06-001 的出厂校准
|
||||
// 系数按 MEMS 元件温度源标定,但其 COEF_SRCE
|
||||
// 标志位读出不可信,故按芯片类型强制;DPS310
|
||||
// 标志位可信,运行时跟随 COEF_SRCE。
|
||||
bool dps310_hw_bug_fix; // true:DPS310 需应用高增益温度 bug 修补。
|
||||
// 部分 DPS310 的温度高增益 trim 值错误,
|
||||
// 表现为温度读成 ~60°C(实际 ~20°C)。
|
||||
// SPL06 无此问题,仅在 DPS310 节点调用。
|
||||
};
|
||||
|
||||
/**
|
||||
* 将低 bits 位的二进制补码数扩展为有符号 int32_t。
|
||||
* @param raw 待扩展的无符号原始值(低位对齐)。
|
||||
* @param bits 有效位宽(如 c0 为 12、c00 为 20)。
|
||||
* @return 扩展后的有符号值。
|
||||
*/
|
||||
static int32_t spl06_twos_complement(uint32_t raw, uint8_t bits)
|
||||
{
|
||||
if (raw & BIT(bits - 1)) {
|
||||
return (int32_t)raw - ((int32_t)1 << bits);
|
||||
}
|
||||
|
||||
return (int32_t)raw;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 3 字节大端原始值转换为 24bit 二进制补码有符号数。
|
||||
* @param buf 指向 3 字节大端数据。
|
||||
* @return 有符号 24bit 数值。
|
||||
*/
|
||||
static int32_t spl06_raw24(const uint8_t *buf)
|
||||
{
|
||||
int32_t v = ((int32_t)buf[0] << 16) | ((int32_t)buf[1] << 8) | buf[2];
|
||||
|
||||
return spl06_twos_complement((uint32_t)v & 0xFFFFFF, 24);
|
||||
}
|
||||
|
||||
/**
|
||||
* 选择与过采样率匹配的测量速率,保证 datasheet 约束
|
||||
* Rate * T_conv < 1(OSR 越高单次转换越久,速率必须相应降低)。
|
||||
* @param osr_idx log2(OSR)。
|
||||
* @return PRS_CFG/TMP_CFG 的速率编码。
|
||||
*/
|
||||
static uint8_t spl06_rate_for_osr(uint8_t osr_idx)
|
||||
{
|
||||
if (osr_idx <= 4) {
|
||||
return SPL06_RATE_32HZ; // OSR <= 16x,T_conv ~28ms
|
||||
}
|
||||
if (osr_idx == 5) {
|
||||
return SPL06_RATE_16HZ; // 32x
|
||||
}
|
||||
if (osr_idx == 6) {
|
||||
return SPL06_RATE_8HZ; // 64x
|
||||
}
|
||||
return SPL06_RATE_4HZ; // 128x,T_conv ~207ms
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析 datasheet 8.11 的 18/22 字节校准系数。
|
||||
* @param c 系数寄存器原始字节,至少 22 字节(不足部分须为 0)。
|
||||
* @param k 输出系数结构体。
|
||||
*/
|
||||
static void spl06_parse_coef(const uint8_t *c, struct spl06_calib *k)
|
||||
{
|
||||
k->c0 = (int16_t)spl06_twos_complement((c[0] << 4) | (c[1] >> 4), 12);
|
||||
k->c1 = (int16_t)spl06_twos_complement(((c[1] & 0x0F) << 8) | c[2], 12);
|
||||
k->c00 = spl06_twos_complement((c[3] << 12) | (c[4] << 4) | (c[5] >> 4), 20);
|
||||
k->c10 = spl06_twos_complement(((c[5] & 0x0F) << 16) | (c[6] << 8) | c[7], 20);
|
||||
k->c01 = (int16_t)((c[8] << 8) | c[9]);
|
||||
k->c11 = (int16_t)((c[10] << 8) | c[11]);
|
||||
k->c20 = (int16_t)((c[12] << 8) | c[13]);
|
||||
k->c21 = (int16_t)((c[14] << 8) | c[15]);
|
||||
k->c30 = (int16_t)((c[16] << 8) | c[17]);
|
||||
k->c31 = (int16_t)spl06_twos_complement((c[18] << 4) | (c[19] >> 4), 12);
|
||||
k->c40 = (int16_t)spl06_twos_complement(((c[19] & 0x0F) << 8) | c[20], 12);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取校准系数。部分芯片一次性 burst 读全 18 字节会失败,按 Betaflight
|
||||
* 做法分块(每块 9 字节)读取。
|
||||
* @param dev 传感器设备。
|
||||
* @param id 芯片 ID,0x11(SPL07_003) 需多读 4 字节 c31/c40。
|
||||
* @return 0 成功,负值为 I2C 错误码。
|
||||
*/
|
||||
static int spl06_read_coef(const struct device *dev, uint8_t id)
|
||||
{
|
||||
struct spl06_data *d = dev->data;
|
||||
const struct spl06_config *cfg = dev->config;
|
||||
uint8_t c[22] = {0};
|
||||
const size_t total = (id == SPL06_ID_SPL07_003) ? 22 : 18;
|
||||
int ret;
|
||||
|
||||
for (size_t off = 0; off < total; off += 9) {
|
||||
const size_t chunk = MIN((size_t)9, total - off);
|
||||
|
||||
ret = i2c_burst_read_dt(&cfg->i2c, SPL06_REG_COEF + off,
|
||||
&c[off], chunk);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("%s: read coef @%zu failed: %d", dev->name, off, ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
LOG_HEXDUMP_DBG(c, total, "coef chunked");
|
||||
spl06_parse_coef(c, &d->calib);
|
||||
LOG_DBG("%s: coef c0=%d c1=%d c00=%d c10=%d c01=%d c11=%d c20=%d c21=%d c30=%d c31=%d c40=%d",
|
||||
dev->name, d->calib.c0, d->calib.c1, d->calib.c00,
|
||||
d->calib.c10, d->calib.c01, d->calib.c11, d->calib.c20,
|
||||
d->calib.c21, d->calib.c30, d->calib.c31, d->calib.c40);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 触发一次后台模式下的气压/温度数据读取。
|
||||
* @param dev 传感器设备。
|
||||
* @param chan 请求的通道(本驱动忽略,始终同时读取气压与温度)。
|
||||
* @return 0 成功,负值为错误码。
|
||||
*/
|
||||
static int spl06_fetch(const struct device *dev, enum sensor_channel chan)
|
||||
{
|
||||
struct spl06_data *d = dev->data;
|
||||
const struct spl06_config *cfg = dev->config;
|
||||
uint8_t buf[6];
|
||||
uint8_t st;
|
||||
int ret;
|
||||
int tries;
|
||||
|
||||
ARG_UNUSED(chan);
|
||||
|
||||
// 结果寄存器读走后 PRS_RDY/TMP_RDY 自动清零,故轮询到两者同时就绪读到的
|
||||
// 一定是同一轮的新数据。带超时避免芯片异常时死等;超时后仍读取,寄存器
|
||||
// 中保留的是上一次有效转换值。
|
||||
for (tries = 0; tries < 100; tries++) {
|
||||
ret = i2c_reg_read_byte_dt(&cfg->i2c, SPL06_REG_MEAS_CFG, &st);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
if ((st & MEAS_CFG_RDY_BOTH) == MEAS_CFG_RDY_BOTH) {
|
||||
break;
|
||||
}
|
||||
k_msleep(1);
|
||||
}
|
||||
if (tries == 100) {
|
||||
LOG_DBG("DRDY timeout, reading last conversion");
|
||||
}
|
||||
|
||||
ret = i2c_burst_read_dt(&cfg->i2c, SPL06_REG_PRS_B2, buf, sizeof(buf));
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
d->raw_prs = spl06_raw24(&buf[0]);
|
||||
d->raw_tmp = spl06_raw24(&buf[3]);
|
||||
LOG_DBG("%s: raw bytes %02x %02x %02x %02x %02x %02x -> prs=%d tmp=%d",
|
||||
dev->name, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5],
|
||||
d->raw_prs, d->raw_tmp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取补偿后的气压/温度值。
|
||||
* @param dev 传感器设备。
|
||||
* @param chan SENSOR_CHAN_PRESS 返回 kPa;SENSOR_CHAN_AMBIENT_TEMP 返回 °C。
|
||||
* @param val 输出 sensor_value(val1 整数部分 + val2 百万分之一小数部分)。
|
||||
* @return 0 成功,-ENOTSUP 表示不支持的通道。
|
||||
*/
|
||||
static int spl06_get(const struct device *dev, enum sensor_channel chan,
|
||||
struct sensor_value *val)
|
||||
{
|
||||
struct spl06_data *d = dev->data;
|
||||
const struct spl06_config *cfg = dev->config;
|
||||
const float s = (float)d->raw_prs / (float)spl06_k[cfg->press_idx];
|
||||
const float u = (float)d->raw_tmp / (float)spl06_k[cfg->temp_idx];
|
||||
float out;
|
||||
|
||||
if (chan == SENSOR_CHAN_AMBIENT_TEMP) {
|
||||
// datasheet 4.9.2:T[°C] = c0/2 + c1*u
|
||||
out = (float)d->calib.c0 * 0.5f + (float)d->calib.c1 * u;
|
||||
LOG_DBG("%s: temp u=%.6f c0=%d c1=%d -> %.2f C",
|
||||
dev->name, (double)u, d->calib.c0, d->calib.c1,
|
||||
(double)out);
|
||||
} else if (chan == SENSOR_CHAN_PRESS) {
|
||||
// datasheet 4.9.1:二阶级数补偿,结果为 Pa
|
||||
const float pa = (float)d->calib.c00
|
||||
+ s * ((float)d->calib.c10 + s * ((float)d->calib.c20
|
||||
+ s * ((float)d->calib.c30 + s * (float)d->calib.c40)))
|
||||
+ u * (float)d->calib.c01
|
||||
+ u * s * ((float)d->calib.c11 + s * ((float)d->calib.c21
|
||||
+ s * (float)d->calib.c31));
|
||||
|
||||
out = pa / 1000.0f; // Pa -> kPa
|
||||
LOG_DBG("%s: press s=%.6f u=%.6f c00=%d -> %.3f kPa",
|
||||
dev->name, (double)s, (double)u, d->calib.c00,
|
||||
(double)out);
|
||||
} else {
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
val->val1 = (int32_t)out;
|
||||
val->val2 = (int32_t)((out - (float)val->val1) * 1000000.0f);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修补部分 DPS310 的温度高增益 trim 错误(temperature 读成 ~60°C)。
|
||||
* 通过签名解锁保留寄存器 0x62 写入正确的高增益值,然后重新上锁。
|
||||
* @param dev 传感器设备。
|
||||
* @return 0 成功,负值为 I2C 错误码。
|
||||
*/
|
||||
static int spl06_dps310_hw_bug_fix(const struct device *dev)
|
||||
{
|
||||
const struct spl06_config *cfg = dev->config;
|
||||
// {寄存器, 值}:0x0E/0x0F 写入解锁签名,0x62 写入温度高增益,再清签名上锁
|
||||
static const uint8_t seq[5][2] = {
|
||||
{0x0E, 0xA5},
|
||||
{0x0F, 0x96},
|
||||
{0x62, 0x02},
|
||||
{0x0E, 0x00},
|
||||
{0x0F, 0x00},
|
||||
};
|
||||
|
||||
for (int i = 0; i < ARRAY_SIZE(seq); i++) {
|
||||
int ret = i2c_reg_write_byte_dt(&cfg->i2c, seq[i][0], seq[i][1]);
|
||||
|
||||
if (ret < 0) {
|
||||
LOG_WRN("DPS310 hw bug fix step %d failed: %d", i, ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
LOG_DBG("DPS310 high-gain temperature bug fix applied");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化传感器:复位、等待校准系数就绪、读取系数、配置过采样/速率/温度源,
|
||||
* 最后进入后台连续测量模式。
|
||||
* @param dev 传感器设备。
|
||||
* @return 0 成功,负值为错误码。
|
||||
*/
|
||||
static int spl06_init(const struct device *dev)
|
||||
{
|
||||
const struct spl06_config *cfg = dev->config;
|
||||
uint8_t id;
|
||||
uint8_t st;
|
||||
uint8_t coef_srce;
|
||||
uint8_t tmp_ext;
|
||||
uint8_t cfg_reg;
|
||||
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, SPL06_REG_ID, &id);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
if (id != SPL06_ID_DPS310_SPL06 && id != SPL06_ID_SPL07_003) {
|
||||
LOG_ERR("unexpected chip id 0x%02x", id);
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
// 软复位(RESET=0x09),随后 COEF_RDY 最长 40ms 置位。
|
||||
ret = i2c_reg_write_byte_dt(&cfg->i2c, SPL06_REG_RESET, 0x09);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
for (int tries = 0; tries < 20; tries++) {
|
||||
k_msleep(5);
|
||||
ret = i2c_reg_read_byte_dt(&cfg->i2c, SPL06_REG_MEAS_CFG, &st);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
if ((st & (MEAS_CFG_COEF_RDY | MEAS_CFG_SENSOR_RDY)) ==
|
||||
(MEAS_CFG_COEF_RDY | MEAS_CFG_SENSOR_RDY)) {
|
||||
break;
|
||||
}
|
||||
if (tries == 19) {
|
||||
LOG_ERR("COEF_RDY/SENSOR_RDY timeout");
|
||||
return -ETIMEDOUT;
|
||||
}
|
||||
}
|
||||
|
||||
ret = spl06_read_coef(dev, id);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
// DPS310 高增益温度 bug 修补(SPL06 无此问题,不执行)
|
||||
if (cfg->dps310_hw_bug_fix) {
|
||||
ret = spl06_dps310_hw_bug_fix(dev);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
// 温度源策略:SPL06-001 强制 MEMS 源;DPS310 运行时跟随 COEF_SRCE。
|
||||
ret = i2c_reg_read_byte_dt(&cfg->i2c, SPL06_REG_COEF_SRCE, &coef_srce);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
tmp_ext = (cfg->force_mems_temp || (coef_srce & TMP_CFG_TMP_EXT))
|
||||
? TMP_CFG_TMP_EXT : 0;
|
||||
LOG_DBG("%s: COEF_SRCE=0x%02x -> TMP_EXT=%d", dev->name, coef_srce,
|
||||
(tmp_ext & TMP_CFG_TMP_EXT) ? 1 : 0);
|
||||
|
||||
// PRS_CFG:气压速率 + 过采样(编码 = log2(osr))
|
||||
ret = i2c_reg_write_byte_dt(&cfg->i2c, SPL06_REG_PRS_CFG,
|
||||
(spl06_rate_for_osr(cfg->press_idx) << 4) |
|
||||
cfg->press_idx);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
// TMP_CFG:温度源 + 温度速率 + 过采样(编码 = log2(osr))
|
||||
ret = i2c_reg_write_byte_dt(&cfg->i2c, SPL06_REG_TMP_CFG,
|
||||
tmp_ext | (spl06_rate_for_osr(cfg->temp_idx) << 4) |
|
||||
cfg->temp_idx);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
// CFG_REG:OSR>8x 时结果右移 6 位,防止 24bit 溢出(与 spl06_k 配套)
|
||||
cfg_reg = 0;
|
||||
if (cfg->press_idx > 3) {
|
||||
cfg_reg |= CFG_REG_P_SHIFT;
|
||||
}
|
||||
if (cfg->temp_idx > 3) {
|
||||
cfg_reg |= CFG_REG_T_SHIFT;
|
||||
}
|
||||
ret = i2c_reg_write_byte_dt(&cfg->i2c, SPL06_REG_CFG, cfg_reg);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
// MEAS_CFG:进入后台连续测量(气压 + 温度)
|
||||
ret = i2c_reg_write_byte_dt(&cfg->i2c, SPL06_REG_MEAS_CFG,
|
||||
MEAS_CTRL_BG_PRS_TMP);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
// 回读配置寄存器,确认写入生效(诊断用)
|
||||
{
|
||||
uint8_t rb_prs = 0, rb_tmp = 0, rb_cfg = 0, rb_meas = 0;
|
||||
|
||||
(void)i2c_reg_read_byte_dt(&cfg->i2c, SPL06_REG_PRS_CFG, &rb_prs);
|
||||
(void)i2c_reg_read_byte_dt(&cfg->i2c, SPL06_REG_TMP_CFG, &rb_tmp);
|
||||
(void)i2c_reg_read_byte_dt(&cfg->i2c, SPL06_REG_CFG, &rb_cfg);
|
||||
(void)i2c_reg_read_byte_dt(&cfg->i2c, SPL06_REG_MEAS_CFG, &rb_meas);
|
||||
LOG_DBG("%s: readback PRS_CFG=0x%02x TMP_CFG=0x%02x CFG=0x%02x MEAS=0x%02x",
|
||||
dev->name, rb_prs, rb_tmp, rb_cfg, rb_meas);
|
||||
}
|
||||
|
||||
LOG_INF("ready: id=0x%02x press %dx temp %dx TMP_EXT=%d",
|
||||
id, 1 << cfg->press_idx, 1 << cfg->temp_idx,
|
||||
(tmp_ext & TMP_CFG_TMP_EXT) ? 1 : 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct sensor_driver_api spl06_api = {
|
||||
.sample_fetch = spl06_fetch,
|
||||
.channel_get = spl06_get,
|
||||
};
|
||||
|
||||
// press-osr/temp-osr 支持的值 -> log2 索引
|
||||
#define SPL06_LOG2(osr) ((osr) == 1 ? 0 : (osr) == 2 ? 1 : (osr) == 4 ? 2 : \
|
||||
(osr) == 8 ? 3 : (osr) == 16 ? 4 : (osr) == 32 ? 5 : \
|
||||
(osr) == 64 ? 6 : 7)
|
||||
|
||||
// 双 compatible 各自从实例 0 编号,静态符号名必须拼入 compatible 前缀,
|
||||
// 否则 misaki,spl06 与 infineon,dps310 的同名实例会符号冲突。
|
||||
#define SPL06_PASTE_(a, b) a##b
|
||||
#define SPL06_PASTE(a, b) SPL06_PASTE_(a, b)
|
||||
|
||||
// SPL06_DEFINE1 负责把 DT_DRV_COMPAT 宏展开成具体 compatible 名,再交给
|
||||
// SPL06_DEFINE2 做 ## 拼接(## 的操作数不会宏展开,必须分两层)。
|
||||
#define SPL06_DEFINE2(compat, inst) \
|
||||
static struct spl06_data \
|
||||
SPL06_PASTE(spl06_##compat##_data_, inst); \
|
||||
static const struct spl06_config \
|
||||
SPL06_PASTE(spl06_##compat##_cfg_, inst) = { \
|
||||
.i2c = I2C_DT_SPEC_INST_GET(inst), \
|
||||
.press_idx = SPL06_LOG2(DT_INST_PROP_OR(inst, press_osr, 16)), \
|
||||
.temp_idx = SPL06_LOG2(DT_INST_PROP_OR(inst, temp_osr, 16)), \
|
||||
.force_mems_temp = \
|
||||
DT_NODE_HAS_COMPAT(DT_DRV_INST(inst), misaki_spl06), \
|
||||
.dps310_hw_bug_fix = \
|
||||
DT_NODE_HAS_COMPAT(DT_DRV_INST(inst), infineon_dps310), \
|
||||
}; \
|
||||
DEVICE_DT_INST_DEFINE(inst, spl06_init, NULL, \
|
||||
&SPL06_PASTE(spl06_##compat##_data_, inst), \
|
||||
&SPL06_PASTE(spl06_##compat##_cfg_, inst), \
|
||||
POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, \
|
||||
&spl06_api)
|
||||
|
||||
#define SPL06_DEFINE1(compat, inst) SPL06_DEFINE2(compat, inst)
|
||||
#define SPL06_DEFINE(inst) SPL06_DEFINE1(DT_DRV_COMPAT, inst)
|
||||
|
||||
#define DT_DRV_COMPAT misaki_spl06
|
||||
DT_INST_FOREACH_STATUS_OKAY(SPL06_DEFINE)
|
||||
#undef DT_DRV_COMPAT
|
||||
|
||||
#if defined(CONFIG_SPL06_CLAIM_DPS310)
|
||||
#define DT_DRV_COMPAT infineon_dps310
|
||||
DT_INST_FOREACH_STATUS_OKAY(SPL06_DEFINE)
|
||||
#endif
|
||||
Reference in New Issue
Block a user