From d95cce5cf2fee25a3b616b187ac5d4cb2b06d48d Mon Sep 17 00:00:00 2001 From: pNexus Date: Tue, 19 May 2026 14:50:04 +0800 Subject: [PATCH] =?UTF-8?q?feat(ads1256):=20=E6=B7=BB=E5=8A=A0=20EMI=20?= =?UTF-8?q?=E9=98=B2=E6=8A=A4=E4=B8=8E=E5=AF=84=E5=AD=98=E5=99=A8=E8=87=AA?= =?UTF-8?q?=E6=81=A2=E5=A4=8D=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CS 引脚加内部上拉防止干扰误触发;新增 ads1256_recover() 在检测到 寄存器被改写时重写关键配置并验证恢复。 --- inc/ads1256.h | 1 + src/ads1256.c | 30 +++++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/inc/ads1256.h b/inc/ads1256.h index 12d0131..fbdd91a 100644 --- a/inc/ads1256.h +++ b/inc/ads1256.h @@ -12,3 +12,4 @@ void ads1256_sync_wakeup(void); int ads1256_hwreset(void); int32_t ads1256_read_data(void); void ads1256_write_cmd(uint8_t cmd); +int ads1256_recover(int max_retries); diff --git a/src/ads1256.c b/src/ads1256.c index b5cd474..59c876f 100644 --- a/src/ads1256.c +++ b/src/ads1256.c @@ -179,7 +179,8 @@ int32_t ads1256_read_data(void) { */ int ads1256_init(void) { /* GPIO */ - gpio_pin_configure_dt(&cs_spec, GPIO_OUTPUT_INACTIVE); + /* CS 加内部上拉:EMI 干扰时维持高电平,阻止 ADS1256 误收命令 */ + gpio_pin_configure_dt(&cs_spec, GPIO_OUTPUT_INACTIVE | GPIO_PULL_UP); gpio_pin_configure_dt(&reset_spec, GPIO_OUTPUT_INACTIVE); gpio_pin_configure_dt(&drdy_spec, GPIO_INPUT); @@ -248,3 +249,30 @@ int ads1256_init(void) { LOG_INF("ADS1256 initialized (BUFEN=1, PGA=64, 3750SPS)"); return 0; } + +/** + * @brief EMI 恢复:重写关键寄存器并验证,用于马达干扰后自恢复。 + * + * @param max_retries 最大重试次数,每次间隔 20ms。 + * + * @retval 0 恢复成功。 + * @retval -EIO 达到最大重试次数仍未恢复。 + */ +int ads1256_recover(int max_retries) { + for (int i = 0; i < max_retries; i++) { + ads1256_write_reg(REG_STATUS, 0x06); + ads1256_write_reg(REG_ADCON, 0x07); + ads1256_write_reg(REG_DRATE, 0xC0); + ads1256_sync_wakeup(); + k_msleep(20); + + uint8_t adcon = ads1256_read_reg(REG_ADCON); + uint8_t drate = ads1256_read_reg(REG_DRATE); + if (adcon == 0x07 && drate == 0xC0) { + LOG_WRN("ADS1256 recovered after %d retries", i + 1); + return 0; + } + } + LOG_ERR("ADS1256 recovery failed after %d retries", max_retries); + return -EIO; +}