refactor(ads1256): 修改等待 DRDY 和硬件复位函数返回值,增加超时处理

This commit is contained in:
2026-05-14 21:17:53 +08:00
parent 16683e40d8
commit 93e7990fdf
3 changed files with 32 additions and 19 deletions
+4 -2
View File
@@ -13,8 +13,9 @@ int ads1256_init(void);
/**
* @brief 等待 DRDY 拉低(转换结果可读),带超时保护。
* @param timeout_ms 超时时间(毫秒)。
* @return 0 成功,-ETIMEDOUT 超时。
*/
void ads1256_wait_drdy(uint16_t timeout_ms);
int ads1256_wait_drdy(uint16_t timeout_ms);
/**
* @brief 向 ADS1256 写单个寄存器。
@@ -37,8 +38,9 @@ void ads1256_sync_wakeup(void);
/**
* @brief 硬件复位 ADS1256(通过 RESET 引脚),等待复位后自校准完成。
* @return 0 成功,-ETIMEDOUT 芯片无响应。
*/
void ads1256_hwreset(void);
int ads1256_hwreset(void);
/**
* @brief 读取当前 24 位转换结果,符号扩展为 int32_t。
+2 -2
View File
@@ -18,8 +18,8 @@
/* ─── 板面几何 & 传感器标定 ─── */
/* 传感器到板面中心的半宽、半长 (cm)。 */
#define BOARD_HALF_WIDTH_CM 10.0f
#define BOARD_HALF_LENGTH_CM 10.0f
#define BOARD_HALF_WIDTH_CM 42.5f
#define BOARD_HALF_LENGTH_CM 16.0f
/*
* ADC → kg 换算推导:
+17 -6
View File
@@ -32,16 +32,17 @@ 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);
void ads1256_wait_drdy(uint16_t timeout_ms) {
int ads1256_wait_drdy(uint16_t timeout_ms) {
/* drdy_spec 配了 GPIO_ACTIVE_LOW,逻辑 1 表示 DRDY 有效(物理拉低) */
int64_t deadline = k_uptime_get() + timeout_ms;
while (gpio_pin_get_dt(&drdy_spec) == 0) {
k_usleep(10);
if (k_uptime_get() >= deadline) {
LOG_WRN("DRDY timeout (%u ms)", timeout_ms);
break;
return -ETIMEDOUT;
}
}
return 0;
}
void ads1256_write_reg(uint8_t reg, uint8_t val) {
@@ -97,7 +98,7 @@ void ads1256_sync_wakeup(void) {
gpio_pin_set_dt(&cs_spec, 0);
}
void ads1256_hwreset(void) {
int ads1256_hwreset(void) {
/* reset_spec 配了 GPIO_ACTIVE_LOW:逻辑 1 = 物理 LOW = 断言复位 */
gpio_pin_set_dt(&reset_spec, 1);
/* t16: 最小复位脉宽 4 × tCLKIN ≈ 0.5µs,取 1ms 余量 */
@@ -105,7 +106,7 @@ void ads1256_hwreset(void) {
gpio_pin_set_dt(&reset_spec, 0);
/* 复位释放后芯片执行自校准,等待完成 */
k_msleep(10);
ads1256_wait_drdy(500);
return ads1256_wait_drdy(500);
}
int32_t ads1256_read_data(void) {
@@ -123,7 +124,7 @@ int32_t ads1256_read_data(void) {
gpio_pin_set_dt(&cs_spec, 0);
int32_t val = ((int32_t)rx_buf[0] << 16) | ((int32_t)rx_buf[1] << 8) | rx_buf[2];
if (val & 0x800000) val |= 0xFF000000;
return val;
return -val;
}
int ads1256_init(void) {
@@ -143,7 +144,11 @@ int ads1256_init(void) {
return -ENODEV;
}
ads1256_hwreset();
int ret = ads1256_hwreset();
if (ret) {
LOG_ERR("ADS1256 not detected (DRDY no response after reset)");
return ret;
}
/* 关闭连续输出模式,进入命令模式 */
ads1256_write_cmd(CMD_SDATAC);
@@ -180,7 +185,13 @@ int ads1256_init(void) {
LOG_INF("ADS1256 regs: STATUS=0x%02X ADCON=0x%02X DRATE=0x%02X", status, adcon, drate);
if (drate != 0xC0) {
if (status == 0x00 && adcon == 0x00 && drate == 0x00) {
LOG_ERR("ADS1256 SPI read failure (all regs 0x00) — check MISO wiring");
} else if (status == 0xFF && adcon == 0xFF && drate == 0xFF) {
LOG_ERR("ADS1256 SPI read failure (all regs 0xFF) — MISO floating or not connected");
} else {
LOG_ERR("ADS1256 register verify failed (DRATE=0x%02X, expected 0xC0)", drate);
}
return -EIO;
}