整理LDC1612驱动头文件相关定义

This commit is contained in:
2026-02-02 00:34:21 +08:00
parent 233576fd9a
commit 38490f6897
9 changed files with 1219 additions and 183 deletions

View File

@@ -4,6 +4,13 @@
#include "ldc1612.h"
#ifdef LDC_DEBUG
#include <stdio.h>
#define LDC1612_DEBUG(fmt, ...) printf("[LDC1612] " fmt "\n", ##__VA_ARGS__)
#else
#define LDC1612_DEBUG(fmt, ...)
#endif
/*!
\brief 写入寄存器
\param[in] reg_addr: 寄存器地址
@@ -53,16 +60,38 @@ static uint16_t ldc1612_calculate_freq_divider(uint8_t channel) {
float sensor_freq;
sensor_freq = 1 / (2 * 3.14 * sqrt(COIL_L_UH * COIL_C_PF * pow(10, -18))) * pow(10, -6);
fin_div = (uint16_t) (sensor_freq / 8.75 + 1);
if (fin_div * 4 < 40) {
freq_div = 2;
if (sensor_freq <= 8.75) {
fin_div = LDC1612_FIN_DIV_1;
} else if (sensor_freq <= 17.5) {
fin_div = LDC1612_FIN_DIV_2;
} else if (sensor_freq <= 35.0) {
fin_div = LDC1612_FIN_DIV_4;
} else {
freq_div = 4;
LDC1612_DEBUG("Error: Sensor frequency (%.2f MHz) exceeds maximum limit!", sensor_freq);
return 0;
}
value = fin_div << 12;
value |= freq_div;
/*
Fref为参考时钟频率单位MHz必须小于35MHz如果输入时钟为外部时钟40MHz则需要分频
LDC1612_EXT_CLK_MHZ为外部时钟频率单位MHz
Fin为传感器谐振频率单位MHz。
必须满足Fin < Fref / 4
通常高精度应用采用外部40MHz2分频Fin不应超5MHz。
*/
if (LDC1612_EXT_CLK_MHZ >= 35)
{
freq_div = LDC1612_FREF_DIV_2;
} else {
freq_div = LDC1612_FREF_DIV_1;
}
if (sensor_freq >= (LDC1612_EXT_CLK_MHZ / freq_div) / 4)
{
LDC1612_DEBUG("Warning: Sensor frequency (%.2f MHz) is too high for the given reference clock (%.2f MHz)!\n", sensor_freq, (float)(LDC1612_EXT_CLK_MHZ / freq_div));
}
value = LDC1612_CLOCK_DIVIDER_GEN(fin_div, freq_div);
return value;
}
@@ -153,7 +182,7 @@ ldc1612_status_t ldc1612_config_single_channel(uint8_t channel) {
ldc1612_write_register(MUX_CONFIG_REG, LDC1612_MUX_CONFIG);
/* Step 8: 配置错误输出 */
ldc1612_write_register(ERROR_CONFIG_REG, LDC1612_ERROR_CONFIG);
ldc1612_write_register(ERROR_CONFIG_REG, LDC1612_ERROR_CONFIG_DEFAULT);
/* Step 9: 最后启动传感器 - 必须最后一步 */
status = ldc1612_write_register(SENSOR_CONFIG_REG, LDC1612_SENSOR_CONFIG_CH0);
@@ -174,11 +203,11 @@ uint32_t ldc1612_get_raw_channel_result(uint8_t channel) {
uint8_t value[2] = {0};
/* Read MSW */
LDC1612_IIC_READ_16BITS(LDC1612_ADDR, CONVERTION_RESULT_REG_START + (channel * 2), value);
LDC1612_IIC_READ_16BITS(LDC1612_ADDR, CONVERSION_RESULT_REG_START + (channel * 2), value);
raw_value |= (uint32_t)(((uint16_t)value[0] << 8) | value[1]) << 16;
/* Read LSW */
LDC1612_IIC_READ_16BITS(LDC1612_ADDR, CONVERTION_RESULT_REG_START + 1 + (channel * 2), value);
LDC1612_IIC_READ_16BITS(LDC1612_ADDR, CONVERSION_RESULT_REG_START + 1 + (channel * 2), value);
raw_value |= (uint32_t)(((uint16_t)value[0] << 8) | value[1]);
@@ -186,7 +215,7 @@ uint32_t ldc1612_get_raw_channel_result(uint8_t channel) {
if (calibration_value == 0x0FFFFFFF) {
return 0xF0000000; /* No coil */
}
if (LDC1612_ERROR_CONFIG & 0xF800) {
if (LDC1612_ERROR_CONFIG_DEFAULT & 0xF800) {
uint8_t error_code = (uint8_t)(raw_value >> 24);
if (error_code & 0x80) return 0x80000000; /* Under range */
if (error_code & 0x40) return 0x40000000; /* Over range */
@@ -219,7 +248,7 @@ void ldc1612_drvie_current_detect(uint8_t channel) {
init_value = (((data[0] << 8) | data[1]) >> 6) & 0x1F;
drive_current = (init_value << 11) | 0x0000;
printf("init value: 0x%x\tdrive current: 0x%x\n", init_value, drive_current);
LDC1612_DEBUG("init value: 0x%x\tdrive current: 0x%x\n", init_value, drive_current);
}
/** @brief Get sensor status register
@@ -243,4 +272,64 @@ bool ldc1612_is_data_ready(uint8_t channel) {
return (status & 0x0080) != 0; // DRDY_1 bit
}
return false;
}
/*!
\brief 检查并记录LDC1612的状态和错误
\param[in] none
\param[out] none
\retval 读取到的原始状态寄存器值
*/
uint16_t ldc1612_check_status_and_log_errors(void) {
uint16_t status;
i2c_result_t i2c_status = ldc1612_read_register(SENSOR_STATUS_REG, &status);
if (i2c_status != I2C_RESULT_SUCCESS) {
LDC1612_DEBUG("Failed to read STATUS register!");
return 0;
}
LDC1612_DEBUG("--- LDC1612 Status Check (Value: 0x%04X) ---", status);
// 检查数据就绪状态
if (status & LDC1612_STATUS_DRDY) {
LDC1612_DEBUG(" [OK] Data is ready.");
}
if (status & LDC1612_STATUS_UNREAD_CH0) {
LDC1612_DEBUG(" [INFO] Channel 0 has unread data.");
}
if (status & LDC1612_STATUS_UNREAD_CH1) {
LDC1612_DEBUG(" [INFO] Channel 1 has unread data.");
}
// 检查是否有任何错误标志
if ((status & 0x3F00) == 0) { // 检查所有错误位的掩码
LDC1612_DEBUG(" [OK] No errors detected.");
} else {
uint8_t err_chan = (status & LDC1612_STATUS_ERR_CHAN_MASK) >> 14;
LDC1612_DEBUG(" [ERROR] An error occurred on Channel %d.", err_chan);
if (status & LDC1612_STATUS_ERR_UR) {
LDC1612_DEBUG(" - Underflow Error: Conversion result is less than OFFSET.");
}
if (status & LDC1612_STATUS_ERR_OR) {
LDC1612_DEBUG(" - Overflow Error: Conversion result is at maximum.");
}
if (status & LDC1612_STATUS_ERR_WD) {
LDC1612_DEBUG(" - Watchdog Timeout: Sensor failed to complete conversion in time.");
}
if (status & LDC1612_STATUS_ERR_AHE) {
LDC1612_DEBUG(" - Amplitude High Error: Sensor oscillation amplitude > 1.8V.");
}
if (status & LDC1612_STATUS_ERR_ALE) {
LDC1612_DEBUG(" - Amplitude Low Error: Sensor oscillation amplitude < 1.2V.");
}
if (status & LDC1612_STATUS_ERR_ZC) {
LDC1612_DEBUG(" - Zero-Count Error: Reference count is zero, check clock.");
}
}
LDC1612_DEBUG("-------------------------------------------------");
// 读取STATUS寄存器会自动清除错误标志但不会清除DRDY和UNREADCONV标志
return status;
}