feat(ads1256): 添加寄存器快照读取与日志记录功能,增强恢复过程的可追踪性
This commit is contained in:
@@ -22,6 +22,13 @@ LOG_MODULE_REGISTER(ads1256, LOG_LEVEL_INF);
|
||||
#define REG_MUX 0x01
|
||||
#define REG_ADCON 0x02
|
||||
#define REG_DRATE 0x03
|
||||
#define REG_IO 0x04
|
||||
#define REG_OFC0 0x05
|
||||
#define REG_OFC1 0x06
|
||||
#define REG_OFC2 0x07
|
||||
#define REG_FSC0 0x08
|
||||
#define REG_FSC1 0x09
|
||||
#define REG_FSC2 0x0A
|
||||
|
||||
#define SPI_OP (SPI_OP_MODE_MASTER | SPI_MODE_CPHA | SPI_WORD_SET(8) | SPI_LINES_SINGLE)
|
||||
|
||||
@@ -32,6 +39,20 @@ static const struct gpio_dt_spec cs_spec = GPIO_DT_SPEC_GET(DT_ALIAS(ads_cs),
|
||||
static const struct gpio_dt_spec drdy_spec = GPIO_DT_SPEC_GET(DT_ALIAS(ads_drdy), gpios);
|
||||
static const struct gpio_dt_spec reset_spec = GPIO_DT_SPEC_GET(DT_ALIAS(ads_reset), gpios);
|
||||
|
||||
struct ads1256_reg_snapshot {
|
||||
uint8_t status;
|
||||
uint8_t mux;
|
||||
uint8_t adcon;
|
||||
uint8_t drate;
|
||||
uint8_t io;
|
||||
uint8_t ofc0;
|
||||
uint8_t ofc1;
|
||||
uint8_t ofc2;
|
||||
uint8_t fsc0;
|
||||
uint8_t fsc1;
|
||||
uint8_t fsc2;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief 等待 DRDY 拉低(转换结果可读),带超时保护。
|
||||
*
|
||||
@@ -95,6 +116,71 @@ uint8_t ads1256_read_reg(uint8_t reg) {
|
||||
return rx_val;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 连续读取 ADS1256 关键寄存器,形成一次诊断快照。
|
||||
*
|
||||
* @param snapshot 输出快照对象,不能为空。
|
||||
*/
|
||||
static void ads1256_read_snapshot(struct ads1256_reg_snapshot *snapshot) {
|
||||
snapshot->status = ads1256_read_reg(REG_STATUS);
|
||||
snapshot->mux = ads1256_read_reg(REG_MUX);
|
||||
snapshot->adcon = ads1256_read_reg(REG_ADCON);
|
||||
snapshot->drate = ads1256_read_reg(REG_DRATE);
|
||||
snapshot->io = ads1256_read_reg(REG_IO);
|
||||
snapshot->ofc0 = ads1256_read_reg(REG_OFC0);
|
||||
snapshot->ofc1 = ads1256_read_reg(REG_OFC1);
|
||||
snapshot->ofc2 = ads1256_read_reg(REG_OFC2);
|
||||
snapshot->fsc0 = ads1256_read_reg(REG_FSC0);
|
||||
snapshot->fsc1 = ads1256_read_reg(REG_FSC1);
|
||||
snapshot->fsc2 = ads1256_read_reg(REG_FSC2);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 仅打印恢复前后发生变化的寄存器。
|
||||
*
|
||||
* @param tag 日志标签,用于区分恢复成功与失败后的变化。
|
||||
* @param before 恢复前寄存器快照。
|
||||
* @param after 恢复后寄存器快照。
|
||||
*/
|
||||
static void ads1256_log_snapshot_delta(
|
||||
const char *tag,
|
||||
const struct ads1256_reg_snapshot *before,
|
||||
const struct ads1256_reg_snapshot *after) {
|
||||
if (before->status != after->status) {
|
||||
LOG_WRN("%s STATUS: 0x%02X -> 0x%02X", tag, before->status, after->status);
|
||||
}
|
||||
if (before->mux != after->mux) {
|
||||
LOG_WRN("%s MUX: 0x%02X -> 0x%02X", tag, before->mux, after->mux);
|
||||
}
|
||||
if (before->adcon != after->adcon) {
|
||||
LOG_WRN("%s ADCON: 0x%02X -> 0x%02X", tag, before->adcon, after->adcon);
|
||||
}
|
||||
if (before->drate != after->drate) {
|
||||
LOG_WRN("%s DRATE: 0x%02X -> 0x%02X", tag, before->drate, after->drate);
|
||||
}
|
||||
if (before->io != after->io) {
|
||||
LOG_WRN("%s IO: 0x%02X -> 0x%02X", tag, before->io, after->io);
|
||||
}
|
||||
if (before->ofc0 != after->ofc0) {
|
||||
LOG_WRN("%s OFC0: 0x%02X -> 0x%02X", tag, before->ofc0, after->ofc0);
|
||||
}
|
||||
if (before->ofc1 != after->ofc1) {
|
||||
LOG_WRN("%s OFC1: 0x%02X -> 0x%02X", tag, before->ofc1, after->ofc1);
|
||||
}
|
||||
if (before->ofc2 != after->ofc2) {
|
||||
LOG_WRN("%s OFC2: 0x%02X -> 0x%02X", tag, before->ofc2, after->ofc2);
|
||||
}
|
||||
if (before->fsc0 != after->fsc0) {
|
||||
LOG_WRN("%s FSC0: 0x%02X -> 0x%02X", tag, before->fsc0, after->fsc0);
|
||||
}
|
||||
if (before->fsc1 != after->fsc1) {
|
||||
LOG_WRN("%s FSC1: 0x%02X -> 0x%02X", tag, before->fsc1, after->fsc1);
|
||||
}
|
||||
if (before->fsc2 != after->fsc2) {
|
||||
LOG_WRN("%s FSC2: 0x%02X -> 0x%02X", tag, before->fsc2, after->fsc2);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 发送单字节命令到 ADS1256。
|
||||
*
|
||||
@@ -259,6 +345,9 @@ int ads1256_init(void) {
|
||||
* @retval -EIO 达到最大重试次数仍未恢复。
|
||||
*/
|
||||
int ads1256_recover(int max_retries) {
|
||||
struct ads1256_reg_snapshot before_snapshot;
|
||||
ads1256_read_snapshot(&before_snapshot);
|
||||
|
||||
for (int i = 0; i < max_retries; i++) {
|
||||
ads1256_write_reg(REG_STATUS, 0x06);
|
||||
ads1256_write_reg(REG_ADCON, 0x07);
|
||||
@@ -269,10 +358,17 @@ int ads1256_recover(int max_retries) {
|
||||
uint8_t adcon = ads1256_read_reg(REG_ADCON);
|
||||
uint8_t drate = ads1256_read_reg(REG_DRATE);
|
||||
if (adcon == 0x07 && drate == 0xC0) {
|
||||
struct ads1256_reg_snapshot after_snapshot;
|
||||
ads1256_read_snapshot(&after_snapshot);
|
||||
ads1256_log_snapshot_delta("ADS1256 recover reg change", &before_snapshot, &after_snapshot);
|
||||
LOG_WRN("ADS1256 recovered after %d retries", i + 1);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
struct ads1256_reg_snapshot failed_snapshot;
|
||||
ads1256_read_snapshot(&failed_snapshot);
|
||||
ads1256_log_snapshot_delta(
|
||||
"ADS1256 recover reg change after retries exhausted", &before_snapshot, &failed_snapshot);
|
||||
LOG_ERR("ADS1256 recovery failed after %d retries", max_retries);
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user