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
+18 -7
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) {
LOG_ERR("ADS1256 register verify failed (DRATE=0x%02X, expected 0xC0)", drate);
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;
}