This commit is contained in:
2026-02-02 10:33:43 +08:00
9 changed files with 1210 additions and 187 deletions

View File

@@ -80,7 +80,6 @@
/** @brief 传感器周期上报使能标志 */
volatile bool g_eddy_current_sensor_report_enabled = false;
volatile bool g_temperature_sensor_report_enabled = false;
/** @name 预设响应数据
* @{ */
@@ -114,28 +113,6 @@ void set_eddy_sensor_report_status(bool status)
g_eddy_current_sensor_report_enabled = status;
}
/**
* @brief 查询温度传感器是否启用周期性传感器上报。
* @return true 表示启用false 表示禁用。
* @ingroup Command
*/
bool get_temp_sensor_report_enabled(void)
{
return g_temperature_sensor_report_enabled;
}
/**
* @brief 设置温度传感器是否启用周期性传感器上报标志。
* @details 本模块内部保存的布尔状态,供其他逻辑决定是否进行周期性数据上报;
* 推荐通过本函数修改而非直接访问全局/静态变量,以便后续扩展(如加锁/回调)。
* @param status true 启用周期上报false 禁用。
* @ingroup Command
*/
void set_temp_sensor_report_status(bool status)
{
g_temperature_sensor_report_enabled = status;
}
/**
* @brief 计算协议包的 8 位累加校验值Checksum
* @details 对输入缓冲区逐字节累加并取低 8 位,累加范围为 data[1] 至 data[len-2]
@@ -310,12 +287,11 @@ void handle_command(const uint8_t *frame, uint8_t len) {
return;
case 3u:
set_temp_sensor_report_status(true);
eddy_current_report();
return;
case 4u:
set_temp_sensor_report_status(false);
send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok));
temperature_raw_value_report();
return;
// case 201u: // M201命令
@@ -347,10 +323,9 @@ void handle_command(const uint8_t *frame, uint8_t len) {
return;
case 9999u:
// M9999: 重启系统
__disable_irq(); // 关闭全局中断GD32E230 CMSIS标准函数
nvic_system_reset(); // 系统复位GD32E230标准库函数
break;
__disable_irq();
NVIC_SystemReset();
return;
default:
// 其它无参数命令在此扩展示例M100处理逻辑该如何待定

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;
}

View File

@@ -51,6 +51,8 @@ OF SUCH DAMAGE.
*/
int main(void)
{
// nvic_vector_table_set(NVIC_VECTTAB_FLASH, 0x2000);
led_init();
mcu_detect_and_config();
@@ -95,14 +97,8 @@ int main(void)
#ifndef EDDY_DRIVE_CURRENT_DETECTION
command_process();
delay_ms(10);
if (g_eddy_current_sensor_report_enabled)
// if (g_eddy_current_sensor_report_enabled)
eddy_current_report();
if (g_temperature_sensor_report_enabled)
temperature_raw_value_report();
#else
ldc1612_drvie_current_detect(CHANNEL_0);