diff --git a/inc/ads1256.h b/inc/ads1256.h index 4b37b53..115ae72 100644 --- a/inc/ads1256.h +++ b/inc/ads1256.h @@ -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。 diff --git a/inc/protocol.h b/inc/protocol.h index 8496b6f..1d9ceea 100644 --- a/inc/protocol.h +++ b/inc/protocol.h @@ -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 换算推导: @@ -47,14 +47,14 @@ * [AA 55 01 flags cop_x(2) cop_y(2) force(2) crc] */ struct cop_frame_t { - uint8_t sync0; /* = PROTO_SYNC0 */ - uint8_t sync1; /* = PROTO_SYNC1 */ - uint8_t type; /* = PROTO_TYPE_COP */ - uint8_t flags; /* bit0: force_valid */ - int16_t cop_x; /* 压力中心 X (cm),板面坐标系 */ - int16_t cop_y; /* 压力中心 Y (cm),板面坐标系 */ - int16_t force; /* 总重量 (kg) */ - uint8_t crc; /* CRC-8/MAXIM, 校验 type~force */ + uint8_t sync0; /* = PROTO_SYNC0 */ + uint8_t sync1; /* = PROTO_SYNC1 */ + uint8_t type; /* = PROTO_TYPE_COP */ + uint8_t flags; /* bit0: force_valid */ + int16_t cop_x; /* 压力中心 X (cm),板面坐标系 */ + int16_t cop_y; /* 压力中心 Y (cm),板面坐标系 */ + int16_t force; /* 总重量 (kg) */ + uint8_t crc; /* CRC-8/MAXIM, 校验 type~force */ } __attribute__((packed)); union cop_pkt_t { diff --git a/src/ads1256.c b/src/ads1256.c index eb2fa8e..4c89b71 100644 --- a/src/ads1256.c +++ b/src/ads1256.c @@ -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; }