generated from hulk/gd32e23x_template_cmake_vscode
sync: 合并内网 LDC1612_last_version 代码变更
- 覆盖 Src/Inc/SDK/LD/doc 等源码文件 - 保留 cmake 构建配置和 Git 历史不变 - 来源: 内网 LDC1612_last_version
This commit is contained in:
Vendored
+1
-5
@@ -4,13 +4,9 @@
|
|||||||
"Git Bash": {
|
"Git Bash": {
|
||||||
"path": "C:\\Program Files\\Git\\bin\\bash.exe",
|
"path": "C:\\Program Files\\Git\\bin\\bash.exe",
|
||||||
"icon": "terminal-bash"
|
"icon": "terminal-bash"
|
||||||
},
|
|
||||||
"Git-Bash": {
|
|
||||||
"path": "D:\\Git\\bin\\bash.exe",
|
|
||||||
"icon": "terminal-bash"
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"terminal.integrated.defaultProfile.windows": "Git-Bash",
|
"terminal.integrated.defaultProfile.windows": "Git Bash",
|
||||||
"clang-format.assumeFilename": ".clang-format",
|
"clang-format.assumeFilename": ".clang-format",
|
||||||
"clang-format.executable": "clang-format",
|
"clang-format.executable": "clang-format",
|
||||||
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools",
|
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools",
|
||||||
|
|||||||
+6
-12
@@ -1,8 +1,6 @@
|
|||||||
#ifndef BOARD_CONFIG_H
|
#ifndef BOARD_CONFIG_H
|
||||||
#define BOARD_CONFIG_H
|
#define BOARD_CONFIG_H
|
||||||
|
|
||||||
#include "version.h"
|
|
||||||
|
|
||||||
#define GD32E23XF4 0x10
|
#define GD32E23XF4 0x10
|
||||||
#define GD32E23XF6 0x20
|
#define GD32E23XF6 0x20
|
||||||
#define GD32E23XF8 0x40
|
#define GD32E23XF8 0x40
|
||||||
@@ -27,18 +25,14 @@
|
|||||||
// #define EDDY_DRIVE_CURRENT_DETECTION // Eddy Drive Current Detection : Enable
|
// #define EDDY_DRIVE_CURRENT_DETECTION // Eddy Drive Current Detection : Enable
|
||||||
#undef EDDY_DRIVE_CURRENT_DETECTION // Eddy Drive Current Detection : Disable
|
#undef EDDY_DRIVE_CURRENT_DETECTION // Eddy Drive Current Detection : Disable
|
||||||
|
|
||||||
/* >>>>>>>>>>>>>>>>>>>>[COMMAND DEBUG]<<<<<<<<<<<<<<<<<<<< */
|
|
||||||
|
|
||||||
// #define COM_DEBUG // Enable Command Debug Information
|
|
||||||
#undef COM_DEBUG // Disable Command Debug Information
|
|
||||||
|
|
||||||
/* >>>>>>>>>>>>>>>>>>>>>[LDC1612 DEBUG]<<<<<<<<<<<<<<<<<<<< */
|
|
||||||
|
|
||||||
// #define LDC_DEBUG // LDC1612 Driver Debug : Enable
|
|
||||||
#undef LDC_DEBUG // LDC1612 Driver Debug : Disable
|
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
|
#define MCU_CODE 24U
|
||||||
|
|
||||||
|
#define FW_VERSION_MAJOR 1
|
||||||
|
#define FW_VERSION_MINOR 1
|
||||||
|
#define FW_VERSION_PATCH 3
|
||||||
|
|
||||||
/* Dynamic USART Configuration Structure */
|
/* Dynamic USART Configuration Structure */
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uint32_t rcu_usart;
|
uint32_t rcu_usart;
|
||||||
|
|||||||
@@ -105,8 +105,4 @@ void eddy_current_report(void);
|
|||||||
|
|
||||||
void temperature_raw_value_report(void);
|
void temperature_raw_value_report(void);
|
||||||
|
|
||||||
void eddy_current_compensated_report(void);
|
|
||||||
|
|
||||||
void calibration_data_report(void);
|
|
||||||
|
|
||||||
#endif // COMMAND_H
|
#endif // COMMAND_H
|
||||||
|
|||||||
@@ -107,76 +107,15 @@ i2c_result_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data
|
|||||||
*/
|
*/
|
||||||
i2c_result_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data);
|
i2c_result_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data);
|
||||||
|
|
||||||
/* Generic read/write functions with configurable length */
|
|
||||||
/*!
|
/*!
|
||||||
\brief write data to I2C device with configurable length
|
\brief read 16-bit data from I2C device
|
||||||
\param[in] slave_addr: slave device address (7-bit)
|
\param[in] slave_addr: 7-bit slave address
|
||||||
\param[in] reg_addr: register address
|
\param[in] reg_addr: register address
|
||||||
\param[in] data: pointer to data buffer
|
\param[out] data: pointer to 2-byte data buffer
|
||||||
\param[in] length: number of bytes to write (1-255)
|
\retval i2c_result_t
|
||||||
\param[out] none
|
|
||||||
\retval i2c_result_t: operation result
|
|
||||||
*/
|
*/
|
||||||
i2c_result_t i2c_write(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8_t length);
|
i2c_result_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data);
|
||||||
|
|
||||||
/*!
|
|
||||||
\brief read data from I2C device with configurable length
|
|
||||||
\param[in] slave_addr: slave device address (7-bit)
|
|
||||||
\param[in] reg_addr: register address
|
|
||||||
\param[out] data: pointer to data buffer
|
|
||||||
\param[in] length: number of bytes to read (1-255)
|
|
||||||
\retval i2c_result_t: operation result
|
|
||||||
*/
|
|
||||||
i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8_t length);
|
|
||||||
|
|
||||||
/* Convenience functions for common operations */
|
|
||||||
/*!
|
|
||||||
\brief write single byte to I2C device
|
|
||||||
\param[in] slave_addr: slave device address (7-bit)
|
|
||||||
\param[in] reg_addr: register address
|
|
||||||
\param[in] data: data byte to write
|
|
||||||
\retval i2c_result_t: operation result
|
|
||||||
*/
|
|
||||||
i2c_result_t i2c_write_8bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data);
|
|
||||||
|
|
||||||
/*!
|
|
||||||
\brief read single byte from I2C device
|
|
||||||
\param[in] slave_addr: slave device address (7-bit)
|
|
||||||
\param[in] reg_addr: register address
|
|
||||||
\param[out] data: pointer to data byte
|
|
||||||
\retval i2c_result_t: operation result
|
|
||||||
*/
|
|
||||||
i2c_result_t i2c_read_8bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data);
|
|
||||||
|
|
||||||
/*!
|
|
||||||
\brief write 32-bit data to I2C device
|
|
||||||
\param[in] slave_addr: slave device address (7-bit)
|
|
||||||
\param[in] reg_addr: register address
|
|
||||||
\param[in] data: pointer to 4-byte data array
|
|
||||||
\retval i2c_result_t: operation result
|
|
||||||
*/
|
|
||||||
i2c_result_t i2c_write_32bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[4]);
|
|
||||||
|
|
||||||
/*!
|
|
||||||
\brief read 32-bit data from I2C device
|
|
||||||
\param[in] slave_addr: slave device address (7-bit)
|
|
||||||
\param[in] reg_addr: register address
|
|
||||||
\param[out] data: pointer to 4-byte data buffer
|
|
||||||
\retval i2c_result_t: operation result
|
|
||||||
*/
|
|
||||||
i2c_result_t i2c_read_32bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data);
|
|
||||||
|
|
||||||
/*!
|
|
||||||
\brief read display panel parameters (multi-byte)
|
|
||||||
\param[in] slave_addr: slave device address (7-bit)
|
|
||||||
\param[in] reg_addr: register address
|
|
||||||
\param[out] data: pointer to data buffer
|
|
||||||
\param[in] length: number of bytes to read (1-13)
|
|
||||||
\retval i2c_result_t: operation result
|
|
||||||
*/
|
|
||||||
i2c_result_t i2c_read_display_params(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8_t length);
|
|
||||||
|
|
||||||
#ifdef DEBUG_VERBOSE
|
|
||||||
/*!
|
/*!
|
||||||
\brief get status string for debugging
|
\brief get status string for debugging
|
||||||
\param[in] status: i2c_result_t value
|
\param[in] status: i2c_result_t value
|
||||||
@@ -184,6 +123,5 @@ i2c_result_t i2c_read_display_params(uint8_t slave_addr, uint8_t reg_addr, uint8
|
|||||||
\retval const char* status string
|
\retval const char* status string
|
||||||
*/
|
*/
|
||||||
const char* i2c_get_status_string(i2c_result_t status);
|
const char* i2c_get_status_string(i2c_result_t status);
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif //I2C_H
|
#endif //I2C_H
|
||||||
|
|||||||
+110
-385
@@ -10,6 +10,7 @@
|
|||||||
#include "systick.h"
|
#include "systick.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include "board_config.h"
|
#include "board_config.h"
|
||||||
@@ -28,32 +29,25 @@
|
|||||||
#define LDC1612_IIC_TYPE_STR "Hardware IIC"
|
#define LDC1612_IIC_TYPE_STR "Hardware IIC"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>[EXT CLK(MHz)]<<<<<<<<<<<<<<<<<<<< */
|
|
||||||
|
|
||||||
#define LDC1612_EXT_CLK_MHZ 40
|
|
||||||
|
|
||||||
/***************************************************************************/
|
/***************************************************************************/
|
||||||
|
|
||||||
#define LDC1612_ADDR (0x2B)
|
#define LDC1612_ADDR (0x2B)
|
||||||
|
|
||||||
/******************************************************************************/
|
|
||||||
|
|
||||||
#define COIL_L_UH 40.9
|
|
||||||
#define COIL_C_PF 180
|
|
||||||
|
|
||||||
/************************Register Addr***************************************/
|
/************************Register Addr***************************************/
|
||||||
|
|
||||||
#define CONVERSION_RESULT_REG_START 0X00
|
#define CONVERTION_RESULT_REG_START 0X00
|
||||||
#define SET_CONVERSION_TIME_REG_START 0X08
|
#define SET_CONVERSION_TIME_REG_START 0X08
|
||||||
#define SET_CONVERSION_OFFSET_REG_START 0X0C
|
#define SET_CONVERSION_OFFSET_REG_START 0X0C
|
||||||
#define SET_SETTLECOUNT_REG_START 0X10
|
#define SET_SETTLECOUNT_REG_START 0X10
|
||||||
#define SET_FREQ_REG_START 0X14
|
#define SET_FREQ_REG_START 0X14
|
||||||
|
|
||||||
#define SENSOR_STATUS_REG 0X18
|
#define SENSOR_STATUS_REG 0X18
|
||||||
#define ERROR_CONFIG_REG 0X19
|
#define ERROR_CONFIG_REG 0X19
|
||||||
#define SENSOR_CONFIG_REG 0X1A
|
#define SENSOR_CONFIG_REG 0X1A
|
||||||
#define MUX_CONFIG_REG 0X1B
|
#define MUX_CONFIG_REG 0X1B
|
||||||
#define SENSOR_RESET_REG 0X1C
|
#define SENSOR_RESET_REG 0X1C
|
||||||
#define SET_DRIVER_CURRENT_REG 0X1E
|
#define SET_DRIVER_CURRENT_REG 0X1E
|
||||||
|
|
||||||
#define READ_MANUFACTURER_ID 0X7E
|
#define READ_MANUFACTURER_ID 0X7E
|
||||||
#define READ_DEVICE_ID 0X7F
|
#define READ_DEVICE_ID 0X7F
|
||||||
|
|
||||||
@@ -62,398 +56,131 @@
|
|||||||
#define CHANNEL_0 0
|
#define CHANNEL_0 0
|
||||||
#define CHANNEL_1 1
|
#define CHANNEL_1 1
|
||||||
|
|
||||||
/**************************DATA (0x00-0x03)*******************************************/
|
/*************************MUX_CONFIG********************************************
|
||||||
/*
|
* 0x0209 AutoScanEN: 0 / RR_SEQ: 00 / RESERVED: 0 0010 0000 1 / Deglitch: 001( 1MHz)
|
||||||
* 作用: 存储28位的传感器转换结果。结果分为高字节(MSB)和低字节(LSB)两个寄存器。
|
* 0x020C AutoScanEN: 0 / RR_SEQ: 00 / RESERVED: 0 0010 0000 1 / Deglitch: 100(3.3MHz)
|
||||||
*
|
* 0x020D AutoScanEN: 0 / RR_SEQ: 00 / RESERVED: 0 0010 0000 1 / Deglitch: 100( 10MHz)
|
||||||
* 结构说明:
|
* 0x020F AutoScanEN: 0 / RR_SEQ: 00 / RESERVED: 0 0010 0000 1 / Deglitch: 100( 33MHz)
|
||||||
* - DATA_CHx_MSB: 包含错误标志和数据的高12位 [27:16]。
|
*/
|
||||||
* - DATA_CHx_LSB: 包含数据的低16位 [15:0]。
|
#define LDC1612_MUX_CONFIG 0x020C
|
||||||
*
|
|
||||||
* MSB寄存器位域:
|
/***********************SENSOR_CONFIG********************************************
|
||||||
* [15] ERR_UR: 转换下溢错误标志 (1 = 发生错误)
|
* 0x1601 Active CH0: 00 / SLEEP: 0 / OVERDRIVE: 1 / LowPowerMode: 0 / AutoAmpDis 1 / CLK(ext): 1 / RESERVED: 0 / INTB_Dis : 0 / HighCurrent: 0 / RESERVED: 00 0001
|
||||||
* [14] ERR_OR: 转换上溢错误标志 (1 = 发生错误)
|
* 0x1201 Active CH0: 00 / SLEEP: 0 / OVERDRIVE: 1 / LowPowerMode: 0 / AutoAmpDis 0 / CLK(ext): 1 / RESERVED: 0 / INTB_Dis : 0 / HighCurrent: 0 / RESERVED: 00 0001
|
||||||
* [13] ERR_WD: 看门狗超时错误标志 (1 = 发生错误)
|
* 0x1641 Active CH0: 00 / SLEEP: 0 / OVERDRIVE: 1 / LowPowerMode: 0 / AutoAmpDis 1 / CLK(ext): 1 / RESERVED: 0 / INTB_Dis : 0 / HighCurrent: 1 / RESERVED: 00 0001
|
||||||
* [12] ERR_AE: 振幅错误标志 (高或低) (1 = 发生错误)
|
* 0x1241 Active CH0: 00 / SLEEP: 0 / OVERDRIVE: 1 / LowPowerMode: 0 / AutoAmpDis 0 / CLK(ext): 1 / RESERVED: 0 / INTB_Dis : 0 / HighCurrent: 1 / RESERVED: 00 0001
|
||||||
* [11:0] DATA[27:16]: 数据的高12位
|
|
||||||
*
|
|
||||||
* 注意:
|
|
||||||
* - 仅当ERROR_CONFIG寄存器中对应的ERR2OUT位置1时,这些错误标志才会在MSB寄存器中被设置。
|
|
||||||
* - 读取数据时,应先读取LSB,再读取MSB,以确保数据的一致性。
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* --- 数据寄存器错误标志位掩码 --- */
|
#ifdef EDDY_DRIVE_CURRENT_DETECTION
|
||||||
#define LDC1612_DATA_ERR_UR (1 << 15)
|
#define LDC1612_SENSOR_CONFIG_CH0 0x1241
|
||||||
#define LDC1612_DATA_ERR_OR (1 << 14)
|
#else
|
||||||
#define LDC1612_DATA_ERR_WD (1 << 13)
|
#define LDC1612_SENSOR_CONFIG_CH0 0x1641
|
||||||
#define LDC1612_DATA_ERR_AE (1 << 12)
|
#endif
|
||||||
#define LDC1612_DATA_ERR_MASK (0xF000)
|
#define LDC1612_SLEEP_MODE 0x2801
|
||||||
#define LDC1612_DATA_MSB_MASK (0x0FFF)
|
|
||||||
|
|
||||||
/**************************RCOUNT (0x08, 0x09)****************************************/
|
/****************************CONVERSION_TIME************************************
|
||||||
/*
|
* Freq_ref = 40MHz / CHx_FREF_DIVIDER
|
||||||
* 作用: 设置参考计数器值,决定了传感器的转换时间,从而影响测量分辨率。
|
* ******RCOUNT_CHx*******
|
||||||
|
* Reference Count Conversion Interval Time
|
||||||
|
* 0x0005 ~ 0xFFFF
|
||||||
|
* default: 0x0080
|
||||||
|
* RCOUNT_CHx * 16 / Freq_ref = Conversion Interval Time
|
||||||
*
|
*
|
||||||
* 位域说明:
|
* ******SETTLECOUNT_CHx*******
|
||||||
* [15:0] RCOUNT: 参考计数值。
|
* Conversion Settling Time
|
||||||
|
* 0x0000 ~ 0xFFFF
|
||||||
|
* default: 0x0000
|
||||||
|
* SETTLECOUNT_CHx * 16 / Freq_ref = Conversion Settling Time
|
||||||
|
* 0x1000 4096*16个时钟周期
|
||||||
|
* 0x0100 256*16个时钟周期
|
||||||
|
* 0x0000/0x0001 32*16个时钟周期
|
||||||
*
|
*
|
||||||
* 计算公式:
|
* ******RCOUNT_CHx*******
|
||||||
* t_CONVERSION = (RCOUNT * 16) / f_REF
|
|
||||||
*
|
|
||||||
* 注意:
|
|
||||||
* - RCOUNT值必须 ≥ 0x0004。
|
|
||||||
* - 该寄存器在复位后值为 0x0080。
|
|
||||||
*
|
|
||||||
* 配置建议:
|
|
||||||
* - 需要高采样率: 使用较小的RCOUNT值。
|
|
||||||
* - 需要高分辨率: 使用较大的RCOUNT值。
|
|
||||||
*/
|
*/
|
||||||
|
#define LDC1612_RCOUNT_TIME_CH0 0x1000 // 0x1000=4096个时钟周期
|
||||||
|
#define LDC1612_SETTLECOUNT_CH0 0x0100
|
||||||
|
|
||||||
/* --- 预设配置示例 --- */
|
/**************************DRIVE_CURRENT****************************************
|
||||||
// 高速采样配置 (分辨率较低)
|
* 0xA000 CH_IDRIVE: 1010 0 / CH_INIT_IDRIVE: 000 00 / RESERVED: 00 0000
|
||||||
#define LDC1612_RCOUNT_HIGH_SPEED (0x04D6) // 1238, 约 1kSPS @ 40MHz/2
|
* 0x9000 CH_IDRIVE: 1001 0 / CH_INIT_IDRIVE: 000 00 / RESERVED: 00 0000
|
||||||
|
|
||||||
// 平衡配置 (常用)
|
|
||||||
#define LDC1612_RCOUNT_BALANCED (0x1000) // 4096, 约 380SPS @ 40MHz/2
|
|
||||||
|
|
||||||
// 高分辨率配置 (采样率较低)
|
|
||||||
#define LDC1612_RCOUNT_HIGH_RESOLUTION (0xFFFF) // 65535, 约 24SPS @ 40MHz/2
|
|
||||||
|
|
||||||
// 默认配置
|
|
||||||
#define LDC1612_RCOUNT_TIME_CH0 LDC1612_RCOUNT_BALANCED // 0x1000=4096个时钟周期
|
|
||||||
|
|
||||||
/**************************OFFSET (0x0C, 0x0D)****************************************/
|
|
||||||
/*
|
|
||||||
* 作用: 设置一个16位的数字偏移量,该值会从原始转换结果中减去。
|
|
||||||
*
|
|
||||||
* 位域说明:
|
|
||||||
* [15:0] OFFSET: 数据偏移值。
|
|
||||||
*
|
|
||||||
* 计算公式:
|
|
||||||
* 最终数据 = 原始转换数据 - OFFSET
|
|
||||||
*
|
|
||||||
* 注意:
|
|
||||||
* - 如果减法结果为负,将触发下溢错误 (ERR_UR)。
|
|
||||||
* - 该寄存器在复位后值为 0x0000。
|
|
||||||
*
|
|
||||||
* 应用场景:
|
|
||||||
* - 消除传感器或环境的固有基线偏移。
|
|
||||||
* - 实现“去皮”(Tare)功能,将当前读数设为新的零点。
|
|
||||||
*/
|
|
||||||
|
|
||||||
// 默认配置: 不设置偏移
|
|
||||||
#define SET_CONVERSION_OFFSET_CH0 0x0000
|
|
||||||
|
|
||||||
/**************************SETTLECOUNT (0x10, 0x11)***********************************/
|
|
||||||
/*
|
|
||||||
* 作用: 设置传感器振荡器在开始转换前所需的建立时间。
|
|
||||||
*
|
|
||||||
* 位域说明:
|
|
||||||
* [15:0] SETTLECOUNT: 建立时间计数值。
|
|
||||||
*
|
|
||||||
* 计算公式:
|
|
||||||
* t_SETTLE 的计算方式取决于SETTLECOUNT的值:
|
|
||||||
* - 当 SETTLECOUNT = 0x0000 或 0x0001 时, t_SETTLE = 32 / f_REF
|
|
||||||
* - 当 SETTLECOUNT ≥ 0x0002 时, t_SETTLE = (SETTLECOUNT * 16) / f_REF
|
|
||||||
*
|
|
||||||
* 配置建议:
|
|
||||||
* - 传感器的Q值越高,所需的建立时间越短 (SETTLECOUNT值可以越小)。
|
|
||||||
* - 值过小可能导致传感器未充分稳定,数据不准确。
|
|
||||||
* - 值过大则会不必要地增加总转换时间,降低采样率。
|
|
||||||
* - 对于大多数应用,0x0100 (256) 是一个很好的起始值。
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* --- 预设配置示例 --- */
|
|
||||||
// 适用于高Q值传感器 (建立时间短)
|
|
||||||
#define LDC1612_SETTLECOUNT_HIGH_Q (0x000A) // 约 4µs @ 40MHz/2
|
|
||||||
|
|
||||||
// 适用于中等Q值传感器 (通用)
|
|
||||||
#define LDC1612_SETTLECOUNT_MEDIUM_Q (0x0100) // 约 102µs @ 40MHz/2
|
|
||||||
|
|
||||||
// 适用于低Q值传感器 (建立时间长)
|
|
||||||
#define LDC1612_SETTLECOUNT_LOW_Q (0x0400) // 约 410µs @ 40MHz/2
|
|
||||||
|
|
||||||
// 默认配置
|
|
||||||
#define LDC1612_SETTLECOUNT_CH0 LDC1612_SETTLECOUNT_MEDIUM_Q
|
|
||||||
|
|
||||||
/**************************CLOCK_DIVIDER (0x14, 0x15)***********************************/
|
|
||||||
/*
|
|
||||||
* 作用: 配置传感器输入频率(f_sensor)和参考时钟(f_ref)的分频器。
|
|
||||||
*
|
|
||||||
* 位域说明:
|
|
||||||
* [15:12] FIN_DIVIDER: 传感器输入分频器。
|
|
||||||
* [11:10] RESERVED: 必须为00。
|
|
||||||
* [9:0] FREF_DIVIDER: 参考时钟分频器。
|
|
||||||
*
|
|
||||||
* 配置逻辑:
|
|
||||||
* 1. FIN_DIVIDER: 根据传感器的谐振频率 f_sensor 选择。
|
|
||||||
* - 目标是使 f_sensor / FIN_DIVIDER <= 8.75MHz。
|
|
||||||
* - 例如: 如果 f_sensor = 15MHz, 则 FIN_DIVIDER 必须 >= 2。
|
|
||||||
*
|
|
||||||
* 2. FREF_DIVIDER: 根据外部时钟 f_clk 和工作模式选择。
|
|
||||||
* - 目标是使 f_ref = f_clk / FREF_DIVIDER。
|
|
||||||
* - 单通道模式且 f_clk <= 35MHz: FREF_DIVIDER = 1。
|
|
||||||
* - 双通道模式或 f_clk > 35MHz: FREF_DIVIDER = 2。
|
|
||||||
*
|
|
||||||
* 最终寄存器值 = (FIN_DIVIDER << 12) | FREF_DIVIDER;
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* --- 位域选项宏 --- */
|
|
||||||
// [15:12] Sensor Input Divider (FIN_DIVIDER)
|
|
||||||
#define LDC1612_FIN_DIV_1 (0x1 << 12) // for f_sensor <= 8.75MHz
|
|
||||||
#define LDC1612_FIN_DIV_2 (0x2 << 12) // for 8.75MHz < f_sensor <= 17.5MHz
|
|
||||||
#define LDC1612_FIN_DIV_4 (0x3 << 12) // for 17.5MHz < f_sensor <= 35MHz
|
|
||||||
|
|
||||||
// [9:0] Reference Clock Divider (FREF_DIVIDER)
|
|
||||||
#define LDC1612_FREF_DIV_1 (0x001)
|
|
||||||
#define LDC1612_FREF_DIV_2 (0x002)
|
|
||||||
|
|
||||||
/* --- 组合宏 --- */
|
|
||||||
#define LDC1612_CLOCK_DIVIDER_GEN(fin_div, fref_div) ((fin_div) | (fref_div))
|
|
||||||
|
|
||||||
/* --- 预设配置示例 (基于40MHz外部时钟) --- */
|
|
||||||
// 适用于 f_sensor <= 8.75MHz
|
|
||||||
#define LDC1612_CLOCK_DIVIDER_DEFAULT LDC1612_CLOCK_DIVIDER_GEN(LDC1612_FIN_DIV_1, LDC1612_FREF_DIV_2) // 0x1002
|
|
||||||
|
|
||||||
|
|
||||||
/**************************STATUS (0x18) MACROS****************************************
|
|
||||||
*
|
|
||||||
* 作用: 定义STATUS寄存器的位掩码,用于解析设备状态。
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
#define LDC1612_STATUS_DRDY (1 << 6) // 数据就绪
|
|
||||||
#define LDC1612_STATUS_UNREAD_CH0 (1 << 3) // 通道0有未读数据
|
|
||||||
#define LDC1612_STATUS_UNREAD_CH1 (1 << 2) // 通道1有未读数据
|
|
||||||
|
|
||||||
#define LDC1612_STATUS_ERR_ZC (1 << 8) // 零计数错误
|
|
||||||
#define LDC1612_STATUS_ERR_ALE (1 << 9) // 振幅过低
|
|
||||||
#define LDC1612_STATUS_ERR_AHE (1 << 10) // 振幅过高
|
|
||||||
#define LDC1612_STATUS_ERR_WD (1 << 11) // 看门狗超时
|
|
||||||
#define LDC1612_STATUS_ERR_OR (1 << 12) // 转换上溢
|
|
||||||
#define LDC1612_STATUS_ERR_UR (1 << 13) // 转换下溢
|
|
||||||
#define LDC1612_STATUS_ERR_CHAN_MASK (3 << 14) // 错误通道掩码
|
|
||||||
|
|
||||||
/**************************ERROR_CONFIG (0x19)****************************************/
|
|
||||||
/*
|
|
||||||
* 作用: 配置状态或错误输出或者触发INTB引脚中断。
|
|
||||||
*
|
|
||||||
* 位域说明:
|
|
||||||
* [15] UR_ERR2OUT: 1 = 转换下溢错误输出到DATA_CHx寄存器
|
|
||||||
* [14] OR_ERR2OUT: 1 = 转换上溢错误输出到DATA_CHx寄存器
|
|
||||||
* [13] WD_ERR2OUT: 1 = 看门狗超时错误输出到DATA_CHx寄存器
|
|
||||||
* [12] AH_ERR2OUT: 1 = 振幅过高错误输出到DATA_CHx寄存器
|
|
||||||
* [11] AL_ERR2OUT: 1 = 振幅过低错误输出到DATA_CHx寄存器
|
|
||||||
* [10:8] RESERVED
|
|
||||||
* [7] UR_ERR2INT: 1 = 转换下溢错误触发INTB
|
|
||||||
* [6] OR_ERR2INT: 1 = 转换上溢错误触发INTB
|
|
||||||
* [5] WD_ERR2INT: 1 = 看门狗超时错误触发INTB
|
|
||||||
* [4] AH_ERR2INT: 1 = 振幅过高错误触发INTB
|
|
||||||
* [3] AL_ERR2INT: 1 = 振幅过低错误触发INTB
|
|
||||||
* [2] ZC_ERR2INT: 1 = 零计数错误触发INTB
|
|
||||||
* [1] RESERVED
|
|
||||||
* [0] DRDY_2INT: 1 = 数据就绪标志触发INTB
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* --- 位域选项宏 --- */
|
|
||||||
|
|
||||||
// --- 中断触发 (ERR2INT) ---
|
|
||||||
#define LDC1612_ERR_CFG_DRDY_INT_EN (1 << 0) // 数据就绪中断使能
|
|
||||||
#define LDC1612_ERR_CFG_ZC_INT_EN (1 << 2) // 零计数错误中断使能
|
|
||||||
#define LDC1612_ERR_CFG_AL_INT_EN (1 << 3) // 振幅过低错误中断使能
|
|
||||||
#define LDC1612_ERR_CFG_AH_INT_EN (1 << 4) // 振幅过高错误中断使能
|
|
||||||
#define LDC1612_ERR_CFG_WD_INT_EN (1 << 5) // 看门狗超时中断使能
|
|
||||||
#define LDC1612_ERR_CFG_OR_INT_EN (1 << 6) // 转换上溢中断使能
|
|
||||||
#define LDC1612_ERR_CFG_UR_INT_EN (1 << 7) // 转换下溢中断使能
|
|
||||||
|
|
||||||
// --- 错误报告至数据寄存器 (ERR2OUT) ---
|
|
||||||
#define LDC1612_ERR_CFG_AL_OUT_EN (1 << 11) // 振幅过低错误报告使能
|
|
||||||
#define LDC1612_ERR_CFG_AH_OUT_EN (1 << 12) // 振幅过高错误报告使能
|
|
||||||
#define LDC1612_ERR_CFG_WD_OUT_EN (1 << 13) // 看门狗超时错误报告使能
|
|
||||||
#define LDC1612_ERR_CFG_OR_OUT_EN (1 << 14) // 转换上溢错误报告使能
|
|
||||||
#define LDC1612_ERR_CFG_UR_OUT_EN (1 << 15) // 转换下溢错误报告使能
|
|
||||||
|
|
||||||
// 常用配置: 仅使能 "数据就绪" 中断
|
|
||||||
#define LDC1612_ERROR_CONFIG_DRDY_ONLY (LDC1612_ERR_CFG_DRDY_INT_EN) // 结果: 0x0001
|
|
||||||
|
|
||||||
// 常用配置: 使能所有错误报告
|
|
||||||
#define LDC1612_ERROR_CONFIG_OUT_ONLY (LDC1612_ERR_CFG_AL_OUT_EN | \
|
|
||||||
LDC1612_ERR_CFG_AH_OUT_EN | \
|
|
||||||
LDC1612_ERR_CFG_WD_OUT_EN | \
|
|
||||||
LDC1612_ERR_CFG_OR_OUT_EN | \
|
|
||||||
LDC1612_ERR_CFG_UR_OUT_EN) // 结果: 0xF800
|
|
||||||
|
|
||||||
// 调试配置: 使能所有错误中断和错误报告
|
|
||||||
#define LDC1612_ERROR_CONFIG_DEBUG_ALL (LDC1612_ERR_CFG_DRDY_INT_EN | \
|
|
||||||
LDC1612_ERR_CFG_ZC_INT_EN | \
|
|
||||||
LDC1612_ERR_CFG_AL_INT_EN | \
|
|
||||||
LDC1612_ERR_CFG_AH_INT_EN | \
|
|
||||||
LDC1612_ERR_CFG_WD_INT_EN | \
|
|
||||||
LDC1612_ERR_CFG_OR_INT_EN | \
|
|
||||||
LDC1612_ERR_CFG_UR_INT_EN | \
|
|
||||||
LDC1612_ERR_CFG_AL_OUT_EN | \
|
|
||||||
LDC1612_ERR_CFG_AH_OUT_EN | \
|
|
||||||
LDC1612_ERR_CFG_WD_OUT_EN | \
|
|
||||||
LDC1612_ERR_CFG_OR_OUT_EN | \
|
|
||||||
LDC1612_ERR_CFG_UR_OUT_EN) // 结果: 0xF8FD
|
|
||||||
|
|
||||||
// 默认配置: 所有功能都禁用
|
|
||||||
#define LDC1612_ERROR_CONFIG_DEFAULT (0x0000)
|
|
||||||
|
|
||||||
/**************************SENSOR_CONFIG (0x1A) MACROS***************************************/
|
|
||||||
/*
|
|
||||||
* CONFIG寄存器位域宏定义,用于灵活组合生成配置值。
|
|
||||||
* 使用方法: LDC1612_CONFIG_GEN(ACTIVE_CHAN, SLEEP_MODE, RP_OVERRIDE, AUTO_AMP, CLK_SRC, INTB, CURRENT_DRV)
|
|
||||||
*
|
|
||||||
* 位域说明 (根据 LDC1612_REG_LIST.md):
|
|
||||||
* [15:14] ACTIVE_CHAN: 激活通道选择 (仅在 AUTOSCAN_EN=0 时有效)
|
|
||||||
* [13] SLEEP_MODE_EN: 1 = 睡眠模式使能
|
|
||||||
* [12] RP_OVERRIDE_EN: 1 = 禁用自动校准 (使用手动的IDRIVE设置)
|
|
||||||
* [11] SENSOR_ACTIVATE_SEL: 传感器激活电流选择 (0:低电流, 1:高电流)
|
|
||||||
* [10] AUTO_AMP_DIS: 1 = 禁用自动幅度校正
|
|
||||||
* [9] REF_CLK_SRC: 1 = 使用外部CLKIN时钟
|
|
||||||
* [8] RESERVED: 必须为0
|
|
||||||
* [7] INTB_DIS: 1 = 禁用INTB中断引脚
|
|
||||||
* [6] HIGH_CURRENT_DRV: 1 = 通道0高电流驱动模式
|
|
||||||
* [5:0] RESERVED: 必须写入 0x01
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* --- 位域选项宏 --- */
|
|
||||||
// [15:14] Active Channel Selection
|
|
||||||
#define LDC1612_CONFIG_ACTIVE_CHAN_CH0 (0x00 << 14)
|
|
||||||
#define LDC1612_CONFIG_ACTIVE_CHAN_CH1 (0x01 << 14)
|
|
||||||
|
|
||||||
// [13] Sleep Mode Enable
|
|
||||||
#define LDC1612_CONFIG_SLEEP_MODE_DISABLE (0x00 << 13)
|
|
||||||
#define LDC1612_CONFIG_SLEEP_MODE_ENABLE (0x01 << 13)
|
|
||||||
|
|
||||||
// [12] RP Override Enable (Auto-Calibration Disable)
|
|
||||||
#define LDC1612_CONFIG_RP_OVERRIDE_DISABLE (0x00 << 12) // 启用自动校准
|
|
||||||
#define LDC1612_CONFIG_RP_OVERRIDE_ENABLE (0x01 << 12) // 禁用自动校准
|
|
||||||
|
|
||||||
// [11] Sensor Activation Current Selection
|
|
||||||
#define LDC1612_CONFIG_SENSOR_ACT_LOW_I (0x00 << 11) // 低电流激活
|
|
||||||
#define LDC1612_CONFIG_SENSOR_ACT_HIGH_I (0x01 << 11) // 高电流激活
|
|
||||||
|
|
||||||
// [10] Auto Amplitude Correction Disable
|
|
||||||
#define LDC1612_CONFIG_AUTO_AMP_ENABLE (0x00 << 10) // 启用自动幅度校正
|
|
||||||
#define LDC1612_CONFIG_AUTO_AMP_DISABLE (0x01 << 10) // 禁用自动幅度校正
|
|
||||||
|
|
||||||
// [9] Reference Clock Source
|
|
||||||
#define LDC1612_CONFIG_CLK_SRC_INTERNAL (0x00 << 9)
|
|
||||||
#define LDC1612_CONFIG_CLK_SRC_EXTERNAL (0x01 << 9)
|
|
||||||
|
|
||||||
// [7] INTB Pin Disable
|
|
||||||
#define LDC1612_CONFIG_INTB_ENABLE (0x00 << 7)
|
|
||||||
#define LDC1612_CONFIG_INTB_DISABLE (0x01 << 7)
|
|
||||||
|
|
||||||
// [6] High Current Drive (Channel 0)
|
|
||||||
#define LDC1612_CONFIG_HIGH_CURRENT_DISABLE (0x00 << 6)
|
|
||||||
#define LDC1612_CONFIG_HIGH_CURRENT_ENABLE (0x01 << 6)
|
|
||||||
|
|
||||||
/* --- 组合宏 --- */
|
|
||||||
// 将所有位域组合成一个16位值。注意,保留位0x01被固定添加。
|
|
||||||
#define LDC1612_CONFIG_GEN(active_chan, sleep, rp_override, sensor_act, auto_amp, clk_src, intb, high_current) \
|
|
||||||
( (active_chan) | (sleep) | (rp_override) | (sensor_act) | (auto_amp) | (clk_src) | (intb) | (high_current) | 0x0001 )
|
|
||||||
|
|
||||||
/* --- 预设配置示例 --- */
|
|
||||||
// CH0连续转换, 外部时钟, 高驱动电流, 禁用自动幅度修正(适用于电流检测)
|
|
||||||
#define LDC1612_SENSOR_CONFIG_CH0 LDC1612_CONFIG_GEN( \
|
|
||||||
LDC1612_CONFIG_ACTIVE_CHAN_CH0, \
|
|
||||||
LDC1612_CONFIG_SLEEP_MODE_DISABLE, \
|
|
||||||
LDC1612_CONFIG_RP_OVERRIDE_ENABLE, /* Rp覆盖开启 */ \
|
|
||||||
LDC1612_CONFIG_SENSOR_ACT_LOW_I, /* 低功耗启动 */ \
|
|
||||||
LDC1612_CONFIG_AUTO_AMP_DISABLE, /* 禁用自动幅度校正 */ \
|
|
||||||
LDC1612_CONFIG_CLK_SRC_EXTERNAL, /* 外部时钟 */ \
|
|
||||||
LDC1612_CONFIG_INTB_ENABLE, /* 启用INTB引脚 */ \
|
|
||||||
LDC1612_CONFIG_HIGH_CURRENT_ENABLE /* 大电流模式 */ ) // 结果: 0x1641
|
|
||||||
// TODO 对比1601的不同(大电流与标准电流)
|
|
||||||
|
|
||||||
// 睡眠模式, 外部时钟
|
|
||||||
#define LDC1612_SLEEP_MODE LDC1612_CONFIG_GEN( \
|
|
||||||
LDC1612_CONFIG_ACTIVE_CHAN_CH0, \
|
|
||||||
LDC1612_CONFIG_SLEEP_MODE_ENABLE, \
|
|
||||||
LDC1612_CONFIG_RP_OVERRIDE_DISABLE, \
|
|
||||||
LDC1612_CONFIG_SENSOR_ACT_HIGH_I, \
|
|
||||||
LDC1612_CONFIG_AUTO_AMP_ENABLE, \
|
|
||||||
LDC1612_CONFIG_CLK_SRC_INTERNAL, \
|
|
||||||
LDC1612_CONFIG_INTB_ENABLE, \
|
|
||||||
LDC1612_CONFIG_HIGH_CURRENT_DISABLE ) // 结果: 0x2801
|
|
||||||
|
|
||||||
/*************************MUX_CONFIG (0x1B) MACROS***************************************/
|
|
||||||
/*
|
|
||||||
* MUX_CONFIG寄存器位域宏定义,用于灵活组合生成配置值。
|
|
||||||
*
|
|
||||||
* 位域说明:
|
|
||||||
* [15] AUTOSCAN_EN: 1 = 自动顺序扫描模式使能
|
|
||||||
* [14:13] RR_SEQUENCE: 扫描序列 (00: CH0, CH1)
|
|
||||||
* [12:3] RESERVED: 必须写入 0x041
|
|
||||||
* [2:0] DEGLITCH: 输入消抖滤波器带宽
|
|
||||||
*/
|
|
||||||
/* --- 位域选项宏 --- */
|
|
||||||
// [15] Auto Scan Mode
|
|
||||||
#define LDC1612_MUX_AUTOSCAN_DISABLE (0x00 << 15) // 单通道连续模式
|
|
||||||
#define LDC1612_MUX_AUTOSCAN_ENABLE (0x01 << 15) // 自动扫描模式
|
|
||||||
|
|
||||||
// [14:13] Round Robin Sequence
|
|
||||||
#define LDC1612_MUX_RR_SEQ_CH0_CH1 (0x00 << 13) // 扫描 CH0, CH1
|
|
||||||
|
|
||||||
// [2:0] Deglitch Filter Bandwidth
|
|
||||||
#define LDC1612_MUX_DEGLITCH_1MHZ (0x01)
|
|
||||||
#define LDC1612_MUX_DEGLITCH_3_3MHZ (0x04)
|
|
||||||
#define LDC1612_MUX_DEGLITCH_10MHZ (0x05)
|
|
||||||
#define LDC1612_MUX_DEGLITCH_33MHZ (0x07)
|
|
||||||
|
|
||||||
/* --- 组合宏 --- */
|
|
||||||
// 将所有位域组合成一个16位值。注意,保留位0x0208 (0x041 << 3)被固定添加。
|
|
||||||
#define LDC1612_MUX_CONFIG_GEN(autoscan, sequence, deglitch) \
|
|
||||||
( (autoscan) | (sequence) | (deglitch) | 0x0208 )
|
|
||||||
|
|
||||||
/* --- 预设配置示例 --- */
|
|
||||||
// 单通道模式, 3.3MHz 滤波
|
|
||||||
#define LDC1612_MUX_CONFIG LDC1612_MUX_CONFIG_GEN( \
|
|
||||||
LDC1612_MUX_AUTOSCAN_DISABLE, \
|
|
||||||
LDC1612_MUX_RR_SEQ_CH0_CH1, /* 此模式下无效,但保持定义 */ \
|
|
||||||
LDC1612_MUX_DEGLITCH_3_3MHZ ) // 0x020C
|
|
||||||
|
|
||||||
/***********************RESET DEVICE (0x1C)***********************************/
|
|
||||||
/*
|
|
||||||
* 向RESET_DEV寄存器写入 LDC1612_RESET_CMD 会触发软件复位。
|
|
||||||
* 复位后,所有寄存器将恢复为默认值,设备进入睡眠模式。
|
|
||||||
* 需要大约10ms的稳定时间后才能重新配置。
|
|
||||||
*/
|
|
||||||
#define LDC1612_RESET_DEV 0x8000
|
|
||||||
|
|
||||||
/**************************DRIVE_CURRENT (0x1E, 0x1F)****************************************/
|
|
||||||
/*
|
|
||||||
* 作用: 设置传感器的驱动电流,以确保振荡幅度(Vosc)在1.2V到1.8V之间。
|
|
||||||
*
|
|
||||||
* 位域说明:
|
|
||||||
* [15:11] IDRIVE: 当前驱动电流设置值 (0-31)。
|
|
||||||
* [10:6] INIT_IDRIVE: 初始驱动电流设置值 (0-31)。
|
|
||||||
* [5:0] RESERVED: 必须为0。
|
|
||||||
*
|
|
||||||
* 配置建议:
|
|
||||||
* 1. 初始阶段可启用自动校准 (CONFIG.RP_OVERRIDE_EN = 0),让芯片自动寻找合适的IDRIVE值。
|
|
||||||
* 2. 读取DRIVE_CURRENT寄存器,获得自动校准后的IDRIVE值。
|
|
||||||
* 3. 在最终代码中,禁用自动校准 (CONFIG.RP_OVERRIDE_EN = 1),并手动写入这个调试好的IDRIVE值。
|
|
||||||
*
|
|
||||||
* CH_INIT_IDRIVE will update when every conversion systick ==>AutoAmpDis is 0
|
* CH_INIT_IDRIVE will update when every conversion systick ==>AutoAmpDis is 0
|
||||||
* CH_INIT_IDRIVE will store init drive current calculated ==> AutoAmpDis is 1
|
* CH_INIT_IDRIVE will store init drive current calculated ==> AutoAmpDis is 1
|
||||||
*/
|
*/
|
||||||
|
#define LDC1612_DRIVE_CURRENT 0x9000
|
||||||
|
|
||||||
/* --- 驱动电流值生成宏 --- */
|
/**************************SENSOR_CONFIG***************************************/
|
||||||
// 参数 idrive: 0-31之间的整数
|
|
||||||
#define LDC1612_DRIVE_CURRENT_GEN(idrive) ( (uint16_t)(idrive) << 11 )
|
|
||||||
|
|
||||||
#define LDC1612_DRIVE_CURRENT LDC1612_DRIVE_CURRENT_GEN(18) // 0x9000
|
|
||||||
|
|
||||||
/**************************IDs (Read Only 0x7E 0x7F)***********************************/
|
|
||||||
|
/**************************ERROR_CONFIG****************************************
|
||||||
|
* [15] Under-Range ERR to OUT (DATA_CHx.CHx_ERR_UR)
|
||||||
|
* [14] Over-Range ERR to OUT (DATA_CHx.CHx_ERR_OR)
|
||||||
|
* [13] Watchdog-Timeout ERR to OUT (DATA_CHx.CHx_ERR_WD)
|
||||||
|
* [12] Amplitude-High-Error ERR to OUT (DATA_CHx.CHx_ERR_AE)
|
||||||
|
* [11] Amplitude-Low-Error ERR to OUT (DATA_CHx.CHx_ERR_AE)
|
||||||
|
* [10] RESERVED
|
||||||
|
* [ 9] RESERVED
|
||||||
|
* [ 8] RESERVED
|
||||||
|
* [ 7] Under-Range ERR to INTB (STATUS.ERR_UR)
|
||||||
|
* [ 6] Over-Range ERR to INTB (STATUS.ERR_OR)
|
||||||
|
* [ 5] Watchdog-Timeout ERR to INTB (STATUS.ERR_WD)
|
||||||
|
* [ 4] Amplitude-High-Error ERR to INTB (STATUS.ERR_AHE)
|
||||||
|
* [ 3] Amplitude-Low-Error ERR to INTB (STATUS.ERR_ALE)
|
||||||
|
* [ 2] Zero_Count_Error ERR to INTB (STATUS.ERR_ZC)
|
||||||
|
* [ 1] RESERVED
|
||||||
|
* [ 0] Data_Ready_Flag to INTB (STATUS.DRDY)
|
||||||
|
*
|
||||||
|
* 0x0000 No ERR to OUT or INTB
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define LDC1612_ERROR_CONFIG 0x0000
|
||||||
|
|
||||||
|
/**************************STATUS****************************************
|
||||||
|
* [15]
|
||||||
|
* [14] Error Channel 0b00: CH0 / 0b01: CH1 / 0b10: CH2 / 0b11: CH3
|
||||||
|
* [13] Conversion Under-Range Error 0b0: No / 0b1: Yes
|
||||||
|
* [12] Conversion Over-Range Error 0b0: No / 0b1: Yes
|
||||||
|
* [11] Watchdog Timeout Error 0b0: No / 0b1: Yes
|
||||||
|
* [10] Amplitude High Error 0b0: No / 0b1: Yes
|
||||||
|
* [ 9] Amplitude Low Error 0b0: No / 0b1: Yes
|
||||||
|
* [ 8] Zero Count Error 0b0: No / 0b1: Yes
|
||||||
|
* [ 7] RESERVED
|
||||||
|
* [ 6] Ddata Ready Flag 0b0: No new results / 0b1: New results available
|
||||||
|
* [ 5] RESERVED
|
||||||
|
* [ 4] RESERVED
|
||||||
|
* [ 3] CH0 Unread Conversion Result 0b0: No / 0b1: Yes(DATA_CH0)
|
||||||
|
* [ 2] CH1 Unread Conversion Result 0b0: No / 0b1: Yes(DATA_CH1)
|
||||||
|
* [ 1] CH2 Unread Conversion Result 0b0: No / 0b1: Yes(DATA_CH2)
|
||||||
|
* [ 0] CH3 Unread Conversion Result 0b0: No / 0b1: Yes(DATA_CH3)
|
||||||
|
*
|
||||||
|
* 0x0000 No ERR to OUT or INTB
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*****************CONVERSION_OFFSET_CONFIG****************************************/
|
||||||
|
|
||||||
|
#define SET_CONVERSION_OFFSET_CH0 0x0000
|
||||||
|
|
||||||
|
/***********************RESET DEVICE********************************************
|
||||||
|
0x8000 RESET_DEV: 1 / RESERVED: 000 0000 0000 0000
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define LDC1612_RESET_DEV 0x8000 //[15:0] 0b1000 0000 0000 0000
|
||||||
|
|
||||||
|
/***********************IDs****************************************************/
|
||||||
|
|
||||||
#define LDC1612_MANUFACTURER_ID 0x5449
|
#define LDC1612_MANUFACTURER_ID 0x5449
|
||||||
#define LDC1612_DEVICE_ID 0x3055
|
#define LDC1612_DEVICE_ID 0x3055
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
|
#define COIL_RP_KOM 7.2
|
||||||
|
#define COIL_L_UH 11.22
|
||||||
|
#define COIL_C_PF 150
|
||||||
|
#define COIL_Q_FACTOR 31.09
|
||||||
|
#define COIL_FREQ_HZ 5323770
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
LDC1612_STATUS_SUCCESS = 0,
|
LDC1612_STATUS_SUCCESS = 0,
|
||||||
LDC1612_STATUS_ERROR,
|
LDC1612_STATUS_ERROR,
|
||||||
@@ -483,6 +210,4 @@ uint16_t ldc1612_get_sensor_status(void);
|
|||||||
|
|
||||||
bool ldc1612_is_data_ready(uint8_t channel);
|
bool ldc1612_is_data_ready(uint8_t channel);
|
||||||
|
|
||||||
uint16_t ldc1612_check_status_and_log_errors(void);
|
|
||||||
|
|
||||||
#endif //LDC1612_H
|
#endif //LDC1612_H
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ _Min_Stack_Size = 0x400; /* required amount of stack */
|
|||||||
/* Memories definition */
|
/* Memories definition */
|
||||||
MEMORY
|
MEMORY
|
||||||
{
|
{
|
||||||
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 32K
|
FLASH (rx) : ORIGIN = 0x08002000, LENGTH = 32K
|
||||||
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 8K
|
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 8K
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+14
-153
@@ -74,15 +74,6 @@
|
|||||||
#define RESP_TYPE_LEN_ERR 0xF4 /**< 长度错误 */
|
#define RESP_TYPE_LEN_ERR 0xF4 /**< 长度错误 */
|
||||||
/** @} */
|
/** @} */
|
||||||
|
|
||||||
/* 温度补偿参数定义
|
|
||||||
* TEMP_COMP_REF_TEMP_X10000: 参考温度,单位 0.0001°C
|
|
||||||
* TEMP_COMP_COEFF_NUMERATOR / TEMP_COMP_COEFF_DENOMINATOR: 温漂系数,默认表示 counts/°C
|
|
||||||
* 补偿公式: compensated = raw - (temp - ref) * coeff
|
|
||||||
*/
|
|
||||||
#define TEMP_COMP_REF_TEMP_X10000 (250000L)
|
|
||||||
#define TEMP_COMP_COEFF_NUMERATOR (0L)
|
|
||||||
#define TEMP_COMP_COEFF_DENOMINATOR (10000L)
|
|
||||||
|
|
||||||
/* ============================================================================
|
/* ============================================================================
|
||||||
* 模块内部变量
|
* 模块内部变量
|
||||||
* ============================================================================ */
|
* ============================================================================ */
|
||||||
@@ -90,23 +81,12 @@
|
|||||||
/** @brief 传感器周期上报使能标志 */
|
/** @brief 传感器周期上报使能标志 */
|
||||||
volatile bool g_eddy_current_sensor_report_enabled = false;
|
volatile bool g_eddy_current_sensor_report_enabled = false;
|
||||||
|
|
||||||
/* Debug output control */
|
|
||||||
#ifdef COM_DEBUG
|
|
||||||
#include <stdio.h>
|
|
||||||
#define COMMAND_DEBUG(fmt, ...) printf("[COMMAND] " fmt "\n", ##__VA_ARGS__)
|
|
||||||
#else
|
|
||||||
#define COMMAND_DEBUG(fmt, ...)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/** @name 预设响应数据
|
/** @name 预设响应数据
|
||||||
* @{ */
|
* @{ */
|
||||||
static const uint8_t s_report_status_ok[] = { 'o', 'k' }; /**< 成功响应数据 */
|
static const uint8_t s_report_status_ok[] = { 'o', 'k' }; /**< 成功响应数据 */
|
||||||
static const uint8_t s_report_status_err[] = { 'e','r','r' }; /**< 错误响应数据 */
|
static const uint8_t s_report_status_err[] = { 'e','r','r' }; /**< 错误响应数据 */
|
||||||
/** @} */
|
/** @} */
|
||||||
|
|
||||||
static int32_t tmp112_get_temperature_x10000(void);
|
|
||||||
static int64_t calculate_temperature_compensation_offset(int32_t temperature_x10000);
|
|
||||||
|
|
||||||
/* ============================================================================
|
/* ============================================================================
|
||||||
* 公共接口函数
|
* 公共接口函数
|
||||||
* ============================================================================ */
|
* ============================================================================ */
|
||||||
@@ -217,36 +197,6 @@ static void send_response(uint8_t type, const uint8_t *payload, uint8_t len)
|
|||||||
*/
|
*/
|
||||||
static inline bool is_dec_digit(uint8_t c) { return (c >= '0' && c <= '9'); }
|
static inline bool is_dec_digit(uint8_t c) { return (c >= '0' && c <= '9'); }
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 将一个无符号整数转换为字符串并追加到缓冲区。
|
|
||||||
* @param value 要转换的数字。
|
|
||||||
* @param buffer 指向目标缓冲区的指针,转换后的字符串将写入此处。
|
|
||||||
* @return uint8_t 写入的字符数。
|
|
||||||
*/
|
|
||||||
static uint8_t uint_to_str(uint32_t value, char *buffer) {
|
|
||||||
char temp[10]; // 32位无符号整数最多10位
|
|
||||||
int i = 0;
|
|
||||||
|
|
||||||
if (value == 0) {
|
|
||||||
buffer[0] = '0';
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 将数字逆序转换为字符存入临时数组
|
|
||||||
while (value > 0) {
|
|
||||||
temp[i++] = (char)((value % 10) + '0');
|
|
||||||
value /= 10;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 将逆序的字符串反转并存入目标缓冲区
|
|
||||||
uint8_t len = (uint8_t)i;
|
|
||||||
for (int j = 0; j < len; j++) {
|
|
||||||
buffer[j] = temp[--i];
|
|
||||||
}
|
|
||||||
|
|
||||||
return len;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 从缓冲区解析十进制无符号整数。
|
* @brief 从缓冲区解析十进制无符号整数。
|
||||||
* @details 从指定位置开始连续读取十进制数字字符,累加构成32位无符号整数。
|
* @details 从指定位置开始连续读取十进制数字字符,累加构成32位无符号整数。
|
||||||
@@ -333,7 +283,6 @@ void handle_command(const uint8_t *frame, uint8_t len) {
|
|||||||
|
|
||||||
case 2u: // M2: disable sensor report
|
case 2u: // M2: disable sensor report
|
||||||
set_eddy_sensor_report_status(false);
|
set_eddy_sensor_report_status(false);
|
||||||
send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok));
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case 3u:
|
case 3u:
|
||||||
@@ -344,41 +293,15 @@ void handle_command(const uint8_t *frame, uint8_t len) {
|
|||||||
temperature_raw_value_report();
|
temperature_raw_value_report();
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case 5u:
|
|
||||||
eddy_current_compensated_report();
|
|
||||||
return;
|
|
||||||
|
|
||||||
case 6u:
|
|
||||||
calibration_data_report();
|
|
||||||
return;
|
|
||||||
|
|
||||||
// case 201u: // M201命令
|
// case 201u: // M201命令
|
||||||
// send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok));
|
// send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok));
|
||||||
// return;
|
// return;
|
||||||
|
|
||||||
/* ==========================================
|
case 999u:
|
||||||
* M999 输出固件版本号命令
|
|
||||||
* ========================================== */
|
|
||||||
case 999u: //M999: 输出固件版本号
|
|
||||||
{
|
|
||||||
char version_str[16];
|
char version_str[16];
|
||||||
char *p = version_str;
|
int n = snprintf(version_str, sizeof(version_str), "v%u.%u.%u.%u", MCU_CODE, FW_VERSION_MAJOR, FW_VERSION_MINOR, FW_VERSION_PATCH);
|
||||||
|
send_response(RESP_TYPE_OK, (uint8_t *)version_str, (uint8_t)n);
|
||||||
*p++ = 'v';
|
return;
|
||||||
p += uint_to_str(BOARD_TYPE_CODE, p);
|
|
||||||
*p++ = '.';
|
|
||||||
p += uint_to_str(FW_VERSION_MAJOR, p);
|
|
||||||
*p++ = '.';
|
|
||||||
p += uint_to_str(FW_VERSION_MINOR, p);
|
|
||||||
*p++ = '.';
|
|
||||||
p += uint_to_str(FW_VERSION_PATCH, p);
|
|
||||||
*p = '\0'; // null-terminate for printf safety
|
|
||||||
|
|
||||||
uint8_t n = (uint8_t)(p - version_str);
|
|
||||||
send_response(RESP_TYPE_OK, (uint8_t *)version_str, n);
|
|
||||||
COMMAND_DEBUG("Firmware Version: %s", version_str);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
|
|
||||||
case 9999u:
|
case 9999u:
|
||||||
__disable_irq();
|
__disable_irq();
|
||||||
@@ -533,82 +456,20 @@ void eddy_current_report(void) {
|
|||||||
send_response(RESP_TYPE_OK, sensor_data, sizeof(sensor_data));
|
send_response(RESP_TYPE_OK, sensor_data, sizeof(sensor_data));
|
||||||
}
|
}
|
||||||
|
|
||||||
static int32_t tmp112_get_temperature_x10000(void)
|
|
||||||
{
|
|
||||||
uint8_t value[2] = {0};
|
|
||||||
int16_t temp_raw;
|
|
||||||
|
|
||||||
tmp112a_get_raw_temperature_value(value);
|
|
||||||
|
|
||||||
temp_raw = (int16_t)(((uint16_t)value[0] << 8) | value[1]);
|
|
||||||
temp_raw >>= 4;
|
|
||||||
|
|
||||||
return (int32_t)temp_raw * 625;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int64_t calculate_temperature_compensation_offset(int32_t temperature_x10000)
|
|
||||||
{
|
|
||||||
int32_t delta_temp_x10000 = temperature_x10000 - TEMP_COMP_REF_TEMP_X10000;
|
|
||||||
|
|
||||||
return ((int64_t)delta_temp_x10000 * TEMP_COMP_COEFF_NUMERATOR) /
|
|
||||||
TEMP_COMP_COEFF_DENOMINATOR;
|
|
||||||
}
|
|
||||||
|
|
||||||
void temperature_raw_value_report(void) {
|
void temperature_raw_value_report(void) {
|
||||||
|
// if (!g_temperature_sensor_report_enabled) return;
|
||||||
uint8_t raw_result[4];
|
uint8_t raw_result[4];
|
||||||
int32_t raw_value = tmp112_get_temperature_x10000();
|
uint8_t value[2] = {0};
|
||||||
|
uint32_t raw_value = 0;
|
||||||
|
|
||||||
raw_result[0] = (uint8_t)((uint32_t)raw_value >> 24);
|
// i2c_read_16bits(TMP112A_ADDR, TMP112A_TEMP_REG, value);
|
||||||
raw_result[1] = (uint8_t)((uint32_t)raw_value >> 16);
|
tmp112a_get_raw_temperature_value(value);
|
||||||
raw_result[2] = (uint8_t)((uint32_t)raw_value >> 8);
|
raw_value = (uint32_t)((uint16_t) (value[0] << 4) | (value[1]>>4)) * 625;
|
||||||
raw_result[3] = (uint8_t)((uint32_t)raw_value & 0xFF);
|
raw_result[0] = (uint8_t)(raw_value >> 24);
|
||||||
|
raw_result[1] = (uint8_t)(raw_value >> 16);
|
||||||
|
raw_result[2] = (uint8_t)(raw_value >> 8);
|
||||||
|
raw_result[3] = (uint8_t)(raw_value & 0xFF);
|
||||||
|
|
||||||
send_response(RESP_TYPE_OK, raw_result, sizeof(raw_result));
|
send_response(RESP_TYPE_OK, raw_result, sizeof(raw_result));
|
||||||
}
|
}
|
||||||
|
|
||||||
void eddy_current_compensated_report(void)
|
|
||||||
{
|
|
||||||
uint8_t sensor_data[4];
|
|
||||||
uint32_t raw_result = ldc1612_get_raw_channel_result(CHANNEL_0);
|
|
||||||
|
|
||||||
if ((raw_result & 0xF0000000u) == 0u) {
|
|
||||||
int32_t temperature_x10000 = tmp112_get_temperature_x10000();
|
|
||||||
int64_t compensated_value = (int64_t)raw_result -
|
|
||||||
calculate_temperature_compensation_offset(temperature_x10000);
|
|
||||||
|
|
||||||
if (compensated_value < 0) {
|
|
||||||
raw_result = 0;
|
|
||||||
} else if (compensated_value > 0x0FFFFFFFLL) {
|
|
||||||
raw_result = 0x0FFFFFFFu;
|
|
||||||
} else {
|
|
||||||
raw_result = (uint32_t)compensated_value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
sensor_data[0] = (uint8_t)(raw_result >> 24);
|
|
||||||
sensor_data[1] = (uint8_t)(raw_result >> 16);
|
|
||||||
sensor_data[2] = (uint8_t)(raw_result >> 8);
|
|
||||||
sensor_data[3] = (uint8_t)(raw_result & 0xFF);
|
|
||||||
|
|
||||||
send_response(RESP_TYPE_OK, sensor_data, sizeof(sensor_data));
|
|
||||||
}
|
|
||||||
|
|
||||||
void calibration_data_report(void)
|
|
||||||
{
|
|
||||||
uint8_t payload[9];
|
|
||||||
uint32_t eddy_raw_value = ldc1612_get_raw_channel_result(CHANNEL_0);
|
|
||||||
int32_t temperature_raw_value = tmp112_get_temperature_x10000();
|
|
||||||
|
|
||||||
payload[0] = (uint8_t)(eddy_raw_value >> 24);
|
|
||||||
payload[1] = (uint8_t)(eddy_raw_value >> 16);
|
|
||||||
payload[2] = (uint8_t)(eddy_raw_value >> 8);
|
|
||||||
payload[3] = (uint8_t)(eddy_raw_value & 0xFF);
|
|
||||||
payload[4] = 0xFF;
|
|
||||||
payload[5] = (uint8_t)((uint32_t)temperature_raw_value >> 24);
|
|
||||||
payload[6] = (uint8_t)((uint32_t)temperature_raw_value >> 16);
|
|
||||||
payload[7] = (uint8_t)((uint32_t)temperature_raw_value >> 8);
|
|
||||||
payload[8] = (uint8_t)((uint32_t)temperature_raw_value & 0xFF);
|
|
||||||
|
|
||||||
send_response(RESP_TYPE_OK, payload, sizeof(payload));
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -649,487 +649,6 @@ i2c_result_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data
|
|||||||
return I2C_RESULT_TIMEOUT;
|
return I2C_RESULT_TIMEOUT;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
|
||||||
\brief write data to I2C device with configurable length
|
|
||||||
\param[in] slave_addr: slave device address (7-bit)
|
|
||||||
\param[in] reg_addr: register address
|
|
||||||
\param[in] data: pointer to data buffer
|
|
||||||
\param[in] length: number of bytes to write (1-255)
|
|
||||||
\param[out] none
|
|
||||||
\retval i2c_result_t: operation result
|
|
||||||
*/
|
|
||||||
i2c_result_t i2c_write(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8_t length) {
|
|
||||||
i2c_state_t state = I2C_STATE_START;
|
|
||||||
uint16_t timeout = 0;
|
|
||||||
uint8_t retry_count = 0;
|
|
||||||
uint8_t data_index = 0;
|
|
||||||
|
|
||||||
/* parameter validation */
|
|
||||||
if (data == NULL || slave_addr > 0x7F || length == 0) {
|
|
||||||
return I2C_RESULT_INVALID_PARAM;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (retry_count < I2C_MAX_RETRY) {
|
|
||||||
switch (state) {
|
|
||||||
case I2C_STATE_START:
|
|
||||||
timeout = 0;
|
|
||||||
data_index = 0;
|
|
||||||
|
|
||||||
/* wait for bus to be idle */
|
|
||||||
while (i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
state = I2C_STATE_ERROR;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
i2c_start_on_bus(I2C0);
|
|
||||||
timeout = 0;
|
|
||||||
state = I2C_STATE_SEND_ADDRESS;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case I2C_STATE_SEND_ADDRESS:
|
|
||||||
/* wait for start condition to be sent. SBSEND flag */
|
|
||||||
while((!i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) && (timeout < I2C_TIME_OUT)) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
state = I2C_STATE_ERROR;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* send slave address */
|
|
||||||
i2c_master_addressing(I2C0, slave_addr << 1, I2C_TRANSMITTER);
|
|
||||||
timeout = 0;
|
|
||||||
state = I2C_STATE_CLEAR_ADDRESS;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case I2C_STATE_CLEAR_ADDRESS:
|
|
||||||
/* wait for address to be acknowledged.ADDSEND set means i2c slave sends ACK */
|
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) && (!i2c_flag_get(I2C0, I2C_FLAG_AERR)) && (timeout < I2C_TIME_OUT)) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
state = I2C_STATE_ERROR;
|
|
||||||
break;
|
|
||||||
} else if (i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) {
|
|
||||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
|
||||||
timeout = 0;
|
|
||||||
state = I2C_STATE_TRANSMIT_REG;
|
|
||||||
break;
|
|
||||||
} else {
|
|
||||||
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
|
||||||
timeout = 0;
|
|
||||||
#ifdef DEBUG_VERBOSE
|
|
||||||
printf("IIC write failed for Error Slave Address. \n");
|
|
||||||
#endif
|
|
||||||
return I2C_RESULT_NACK;
|
|
||||||
}
|
|
||||||
|
|
||||||
case I2C_STATE_TRANSMIT_REG:
|
|
||||||
/* wait until the transmit data buffer is empty */
|
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
state = I2C_STATE_ERROR;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* send register address */
|
|
||||||
i2c_data_transmit(I2C0, reg_addr);
|
|
||||||
timeout = 0;
|
|
||||||
state = I2C_STATE_TRANSMIT_DATA;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case I2C_STATE_TRANSMIT_DATA:
|
|
||||||
/* wait until the transmit data buffer is empty */
|
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
state = I2C_STATE_ERROR;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* send data byte */
|
|
||||||
i2c_data_transmit(I2C0, data[data_index]);
|
|
||||||
data_index++;
|
|
||||||
|
|
||||||
/* check for errors */
|
|
||||||
if (i2c_flag_get(I2C0, I2C_FLAG_AERR)) {
|
|
||||||
i2c_stop_on_bus(I2C0);
|
|
||||||
return I2C_RESULT_NACK;
|
|
||||||
} else if (i2c_flag_get(I2C0, I2C_FLAG_BERR) || i2c_flag_get(I2C0, I2C_FLAG_LOSTARB)) {
|
|
||||||
i2c_stop_on_bus(I2C0);
|
|
||||||
return I2C_RESULT_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* check if all data has been sent */
|
|
||||||
if (data_index >= length) {
|
|
||||||
/* wait until BTC bit is set for last byte */
|
|
||||||
timeout = 0;
|
|
||||||
while (!i2c_flag_get(I2C0, I2C_FLAG_BTC) && (timeout < I2C_TIME_OUT)) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
state = I2C_STATE_ERROR;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
state = I2C_STATE_STOP;
|
|
||||||
}
|
|
||||||
timeout = 0;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case I2C_STATE_STOP:
|
|
||||||
/* send a stop condition to I2C bus */
|
|
||||||
i2c_stop_on_bus(I2C0);
|
|
||||||
|
|
||||||
timeout = 0;
|
|
||||||
while ((I2C_CTL0(I2C0) & I2C_CTL0_STOP) && (timeout < I2C_TIME_OUT)) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
state = I2C_STATE_ERROR;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* success */
|
|
||||||
return I2C_RESULT_SUCCESS;
|
|
||||||
|
|
||||||
case I2C_STATE_ERROR:
|
|
||||||
/* send a stop condition to I2C bus */
|
|
||||||
i2c_stop_on_bus(I2C0);
|
|
||||||
|
|
||||||
timeout = 0;
|
|
||||||
while ((I2C_CTL0(I2C0) & I2C_CTL0_STOP) && (timeout < I2C_TIME_OUT)) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
|
|
||||||
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
|
||||||
i2c_flag_clear(I2C0, I2C_FLAG_BERR);
|
|
||||||
i2c_flag_clear(I2C0, I2C_FLAG_LOSTARB);
|
|
||||||
|
|
||||||
retry_count++;
|
|
||||||
if (retry_count >= I2C_MAX_RETRY) {
|
|
||||||
#ifdef DEBUG_VERBOSE
|
|
||||||
printf("IIC write failed after %d retries\n", I2C_MAX_RETRY);
|
|
||||||
#endif
|
|
||||||
return I2C_RESULT_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* reset state machine for retry */
|
|
||||||
state = I2C_STATE_START;
|
|
||||||
timeout = 0;
|
|
||||||
|
|
||||||
/* small delay before retry */
|
|
||||||
delay_10us(10);
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
state = I2C_STATE_START;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return I2C_RESULT_TIMEOUT;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
|
||||||
\brief read data from I2C device with configurable length
|
|
||||||
\param[in] slave_addr: slave device address (7-bit)
|
|
||||||
\param[in] reg_addr: register address
|
|
||||||
\param[out] data: pointer to data buffer
|
|
||||||
\param[in] length: number of bytes to read (1-255)
|
|
||||||
\retval i2c_result_t: operation result
|
|
||||||
*/
|
|
||||||
i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8_t length) {
|
|
||||||
i2c_state_t state = I2C_STATE_START;
|
|
||||||
uint16_t timeout = 0;
|
|
||||||
uint8_t retry_count = 0;
|
|
||||||
bool write_phase = true;
|
|
||||||
uint8_t data_index = 0;
|
|
||||||
|
|
||||||
/* parameter validation */
|
|
||||||
if (data == NULL || slave_addr > 0x7F || length == 0) {
|
|
||||||
return I2C_RESULT_INVALID_PARAM;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* enable acknowledge */
|
|
||||||
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
|
||||||
|
|
||||||
while (retry_count < (uint8_t)I2C_MAX_RETRY) {
|
|
||||||
switch (state) {
|
|
||||||
case I2C_STATE_START:
|
|
||||||
timeout = 0;
|
|
||||||
data_index = 0;
|
|
||||||
|
|
||||||
/* wait for bus to be idle */
|
|
||||||
while (i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
state = I2C_STATE_ERROR;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* send start condition */
|
|
||||||
i2c_start_on_bus(I2C0);
|
|
||||||
state = I2C_STATE_SEND_ADDRESS;
|
|
||||||
timeout = 0;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case I2C_STATE_SEND_ADDRESS:
|
|
||||||
/* wait for start condition to be sent */
|
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) && (timeout < I2C_TIME_OUT)) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
state = I2C_STATE_ERROR;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* send slave address */
|
|
||||||
if (write_phase) {
|
|
||||||
/* write phase: send address with write bit */
|
|
||||||
i2c_master_addressing(I2C0, (slave_addr << 1), I2C_TRANSMITTER);
|
|
||||||
} else {
|
|
||||||
/* read phase: send address with read bit */
|
|
||||||
i2c_master_addressing(I2C0, (slave_addr << 1) | 0x01, I2C_RECEIVER);
|
|
||||||
}
|
|
||||||
|
|
||||||
state = I2C_STATE_CLEAR_ADDRESS;
|
|
||||||
timeout = 0;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case I2C_STATE_CLEAR_ADDRESS:
|
|
||||||
/* wait for address to be acknowledged */
|
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) && (timeout < I2C_TIME_OUT)) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
state = I2C_STATE_ERROR;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (write_phase) {
|
|
||||||
/* clear address flag (write phase) */
|
|
||||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
|
||||||
state = I2C_STATE_TRANSMIT_DATA;
|
|
||||||
} else {
|
|
||||||
/* read phase setup based on length */
|
|
||||||
if (length == 1) {
|
|
||||||
/* single byte read: disable ACK before clearing ADDR */
|
|
||||||
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
|
||||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
|
||||||
/* send STOP immediately after clearing ADDR for single byte */
|
|
||||||
i2c_stop_on_bus(I2C0);
|
|
||||||
} else if (length == 2) {
|
|
||||||
/* two bytes read: set POS=NEXT and disable ACK before clearing ADDR */
|
|
||||||
i2c_ackpos_config(I2C0, I2C_ACKPOS_NEXT);
|
|
||||||
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
|
||||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
|
||||||
} else {
|
|
||||||
/* multi-byte read: clear ADDR with ACK enabled */
|
|
||||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
|
||||||
}
|
|
||||||
state = I2C_STATE_RECEIVE_DATA;
|
|
||||||
}
|
|
||||||
timeout = 0;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case I2C_STATE_TRANSMIT_DATA:
|
|
||||||
/* wait for transmit buffer to be empty */
|
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
state = I2C_STATE_ERROR;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* send register address */
|
|
||||||
i2c_data_transmit(I2C0, reg_addr);
|
|
||||||
state = I2C_STATE_RESTART;
|
|
||||||
timeout = 0;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case I2C_STATE_RESTART:
|
|
||||||
/* wait for byte transfer complete */
|
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
state = I2C_STATE_ERROR;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* generate repeated start condition */
|
|
||||||
i2c_start_on_bus(I2C0);
|
|
||||||
|
|
||||||
/* wait for repeated start condition to be sent */
|
|
||||||
timeout = 0;
|
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) && (timeout < I2C_TIME_OUT)) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
state = I2C_STATE_ERROR;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* send slave address with read bit */
|
|
||||||
i2c_master_addressing(I2C0, (slave_addr << 1), I2C_RECEIVER);
|
|
||||||
|
|
||||||
/* switch to read phase */
|
|
||||||
write_phase = false;
|
|
||||||
state = I2C_STATE_CLEAR_ADDRESS;
|
|
||||||
timeout = 0;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case I2C_STATE_RECEIVE_DATA:
|
|
||||||
if (length == 1) {
|
|
||||||
/* single byte read */
|
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_RBNE)) && (timeout < I2C_TIME_OUT)) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
state = I2C_STATE_ERROR;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
data[0] = i2c_data_receive(I2C0);
|
|
||||||
state = I2C_STATE_STOP;
|
|
||||||
} else if (length == 2) {
|
|
||||||
/* two bytes read */
|
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
state = I2C_STATE_ERROR;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
/* send STOP before reading the last two bytes */
|
|
||||||
i2c_stop_on_bus(I2C0);
|
|
||||||
/* read the two bytes back-to-back */
|
|
||||||
data[0] = i2c_data_receive(I2C0);
|
|
||||||
data[1] = i2c_data_receive(I2C0);
|
|
||||||
state = I2C_STATE_STOP;
|
|
||||||
} else {
|
|
||||||
/* multi-byte read (length > 2) */
|
|
||||||
while (data_index < length) {
|
|
||||||
/* wait for RBNE (receive buffer not empty) */
|
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_RBNE)) && (timeout < I2C_TIME_OUT)) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
state = I2C_STATE_ERROR;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* special handling for last 3 bytes */
|
|
||||||
if (data_index == length - 3) {
|
|
||||||
/* wait for BTF (byte transfer finished) before reading N-2 */
|
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
state = I2C_STATE_ERROR;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
/* disable ACK for last 2 bytes */
|
|
||||||
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* read data byte */
|
|
||||||
data[data_index] = i2c_data_receive(I2C0);
|
|
||||||
data_index++;
|
|
||||||
|
|
||||||
/* send STOP after reading N-1 byte */
|
|
||||||
if (data_index == length - 1) {
|
|
||||||
i2c_stop_on_bus(I2C0);
|
|
||||||
}
|
|
||||||
|
|
||||||
timeout = 0;
|
|
||||||
}
|
|
||||||
state = I2C_STATE_STOP;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case I2C_STATE_STOP:
|
|
||||||
/* wait for stop condition to complete */
|
|
||||||
while ((I2C_CTL0(I2C0) & I2C_CTL0_STOP) && (timeout < I2C_TIME_OUT)) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
state = I2C_STATE_ERROR;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* reset ACK configuration for next operation */
|
|
||||||
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
|
||||||
i2c_ackpos_config(I2C0, I2C_ACKPOS_CURRENT);
|
|
||||||
|
|
||||||
/* success */
|
|
||||||
return I2C_RESULT_SUCCESS;
|
|
||||||
|
|
||||||
case I2C_STATE_ERROR:
|
|
||||||
/* send stop condition to release bus */
|
|
||||||
i2c_stop_on_bus(I2C0);
|
|
||||||
|
|
||||||
/* reset ACK configuration */
|
|
||||||
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
|
||||||
i2c_ackpos_config(I2C0, I2C_ACKPOS_CURRENT);
|
|
||||||
|
|
||||||
retry_count++;
|
|
||||||
if (retry_count >= I2C_MAX_RETRY) {
|
|
||||||
#ifdef DEBUG_VERBOSE
|
|
||||||
printf("I2C read failed after %d retries\n", I2C_MAX_RETRY);
|
|
||||||
#endif
|
|
||||||
return I2C_RESULT_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* reset state machine for retry */
|
|
||||||
state = I2C_STATE_START;
|
|
||||||
write_phase = true;
|
|
||||||
timeout = 0;
|
|
||||||
|
|
||||||
/* small delay before retry */
|
|
||||||
delay_10us(10);
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
state = I2C_STATE_START;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return I2C_RESULT_TIMEOUT;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* convenience functions for common operations */
|
|
||||||
i2c_result_t i2c_write_8bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data) {
|
|
||||||
return i2c_write(slave_addr, reg_addr, &data, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
i2c_result_t i2c_read_8bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data) {
|
|
||||||
return i2c_read(slave_addr, reg_addr, data, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
i2c_result_t i2c_write_32bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[4]) {
|
|
||||||
return i2c_write(slave_addr, reg_addr, data, 4);
|
|
||||||
}
|
|
||||||
|
|
||||||
i2c_result_t i2c_read_32bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data) {
|
|
||||||
return i2c_read(slave_addr, reg_addr, data, 4);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* 显示面板专用函数 - 支持读取显示界面参数 */
|
|
||||||
i2c_result_t i2c_read_display_params(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8_t length) {
|
|
||||||
/* 针对显示面板的多字节读取,支持13字节的完整参数读取 */
|
|
||||||
if (length > 13) {
|
|
||||||
return I2C_RESULT_INVALID_PARAM;
|
|
||||||
}
|
|
||||||
return i2c_read(slave_addr, reg_addr, data, length);
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef DEBUG_VERBOSE
|
#ifdef DEBUG_VERBOSE
|
||||||
/*!
|
/*!
|
||||||
\brief get status string for debugging
|
\brief get status string for debugging
|
||||||
|
|||||||
+12
-101
@@ -4,13 +4,6 @@
|
|||||||
|
|
||||||
#include "ldc1612.h"
|
#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 写入寄存器
|
\brief 写入寄存器
|
||||||
\param[in] reg_addr: 寄存器地址
|
\param[in] reg_addr: 寄存器地址
|
||||||
@@ -60,38 +53,16 @@ static uint16_t ldc1612_calculate_freq_divider(uint8_t channel) {
|
|||||||
float sensor_freq;
|
float sensor_freq;
|
||||||
|
|
||||||
sensor_freq = 1 / (2 * 3.14 * sqrt(COIL_L_UH * COIL_C_PF * pow(10, -18))) * pow(10, -6);
|
sensor_freq = 1 / (2 * 3.14 * sqrt(COIL_L_UH * COIL_C_PF * pow(10, -18))) * pow(10, -6);
|
||||||
if (sensor_freq <= 8.75) {
|
fin_div = (uint16_t) (sensor_freq / 8.75 + 1);
|
||||||
fin_div = LDC1612_FIN_DIV_1;
|
|
||||||
} else if (sensor_freq <= 17.5) {
|
if (fin_div * 4 < 40) {
|
||||||
fin_div = LDC1612_FIN_DIV_2;
|
freq_div = 2;
|
||||||
} else if (sensor_freq <= 35.0) {
|
|
||||||
fin_div = LDC1612_FIN_DIV_4;
|
|
||||||
} else {
|
} else {
|
||||||
LDC1612_DEBUG("Error: Sensor frequency (%.2f MHz) exceeds maximum limit!", sensor_freq);
|
freq_div = 4;
|
||||||
return 0;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
value = fin_div << 12;
|
||||||
Fref为参考时钟频率,单位MHz,必须小于35MHz,如果输入时钟为外部时钟40MHz,则需要分频
|
value |= freq_div;
|
||||||
LDC1612_EXT_CLK_MHZ为外部时钟频率,单位MHz
|
|
||||||
Fin为传感器谐振频率,单位MHz。
|
|
||||||
必须满足:Fin < Fref / 4
|
|
||||||
通常高精度应用,采用外部40MHz,2分频,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;
|
return value;
|
||||||
}
|
}
|
||||||
@@ -182,7 +153,7 @@ ldc1612_status_t ldc1612_config_single_channel(uint8_t channel) {
|
|||||||
ldc1612_write_register(MUX_CONFIG_REG, LDC1612_MUX_CONFIG);
|
ldc1612_write_register(MUX_CONFIG_REG, LDC1612_MUX_CONFIG);
|
||||||
|
|
||||||
/* Step 8: 配置错误输出 */
|
/* Step 8: 配置错误输出 */
|
||||||
ldc1612_write_register(ERROR_CONFIG_REG, LDC1612_ERROR_CONFIG_DEFAULT);
|
ldc1612_write_register(ERROR_CONFIG_REG, LDC1612_ERROR_CONFIG);
|
||||||
|
|
||||||
/* Step 9: 最后启动传感器 - 必须最后一步 */
|
/* Step 9: 最后启动传感器 - 必须最后一步 */
|
||||||
status = ldc1612_write_register(SENSOR_CONFIG_REG, LDC1612_SENSOR_CONFIG_CH0);
|
status = ldc1612_write_register(SENSOR_CONFIG_REG, LDC1612_SENSOR_CONFIG_CH0);
|
||||||
@@ -203,11 +174,11 @@ uint32_t ldc1612_get_raw_channel_result(uint8_t channel) {
|
|||||||
uint8_t value[2] = {0};
|
uint8_t value[2] = {0};
|
||||||
|
|
||||||
/* Read MSW */
|
/* Read MSW */
|
||||||
LDC1612_IIC_READ_16BITS(LDC1612_ADDR, CONVERSION_RESULT_REG_START + (channel * 2), value);
|
LDC1612_IIC_READ_16BITS(LDC1612_ADDR, CONVERTION_RESULT_REG_START + (channel * 2), value);
|
||||||
raw_value |= (uint32_t)(((uint16_t)value[0] << 8) | value[1]) << 16;
|
raw_value |= (uint32_t)(((uint16_t)value[0] << 8) | value[1]) << 16;
|
||||||
|
|
||||||
/* Read LSW */
|
/* Read LSW */
|
||||||
LDC1612_IIC_READ_16BITS(LDC1612_ADDR, CONVERSION_RESULT_REG_START + 1 + (channel * 2), value);
|
LDC1612_IIC_READ_16BITS(LDC1612_ADDR, CONVERTION_RESULT_REG_START + 1 + (channel * 2), value);
|
||||||
raw_value |= (uint32_t)(((uint16_t)value[0] << 8) | value[1]);
|
raw_value |= (uint32_t)(((uint16_t)value[0] << 8) | value[1]);
|
||||||
|
|
||||||
|
|
||||||
@@ -215,7 +186,7 @@ uint32_t ldc1612_get_raw_channel_result(uint8_t channel) {
|
|||||||
if (calibration_value == 0x0FFFFFFF) {
|
if (calibration_value == 0x0FFFFFFF) {
|
||||||
return 0xF0000000; /* No coil */
|
return 0xF0000000; /* No coil */
|
||||||
}
|
}
|
||||||
if (LDC1612_ERROR_CONFIG_DEFAULT & 0xF800) {
|
if (LDC1612_ERROR_CONFIG & 0xF800) {
|
||||||
uint8_t error_code = (uint8_t)(raw_value >> 24);
|
uint8_t error_code = (uint8_t)(raw_value >> 24);
|
||||||
if (error_code & 0x80) return 0x80000000; /* Under range */
|
if (error_code & 0x80) return 0x80000000; /* Under range */
|
||||||
if (error_code & 0x40) return 0x40000000; /* Over range */
|
if (error_code & 0x40) return 0x40000000; /* Over range */
|
||||||
@@ -248,7 +219,7 @@ void ldc1612_drvie_current_detect(uint8_t channel) {
|
|||||||
|
|
||||||
init_value = (((data[0] << 8) | data[1]) >> 6) & 0x1F;
|
init_value = (((data[0] << 8) | data[1]) >> 6) & 0x1F;
|
||||||
drive_current = (init_value << 11) | 0x0000;
|
drive_current = (init_value << 11) | 0x0000;
|
||||||
LDC1612_DEBUG("init value: 0x%x\tdrive current: 0x%x\n", init_value, drive_current);
|
printf("init value: 0x%x\tdrive current: 0x%x\n", init_value, drive_current);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @brief Get sensor status register
|
/** @brief Get sensor status register
|
||||||
@@ -273,63 +244,3 @@ bool ldc1612_is_data_ready(uint8_t channel) {
|
|||||||
}
|
}
|
||||||
return false;
|
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;
|
|
||||||
}
|
|
||||||
+1
-1
@@ -51,7 +51,7 @@ OF SUCH DAMAGE.
|
|||||||
*/
|
*/
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
// nvic_vector_table_set(NVIC_VECTTAB_FLASH, 0x2000);
|
nvic_vector_table_set(NVIC_VECTTAB_FLASH, 0x2000);
|
||||||
|
|
||||||
led_init();
|
led_init();
|
||||||
mcu_detect_and_config();
|
mcu_detect_and_config();
|
||||||
|
|||||||
+1
-2
@@ -86,8 +86,7 @@ tmp112a_status_t tmp112a_read_temperature(tmp112a_result_t *result) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void tmp112a_get_raw_temperature_value(uint8_t *value) {
|
void tmp112a_get_raw_temperature_value(uint8_t *value) {
|
||||||
// i2c_read_16bits(TMP112A_ADDR, TMP112A_TEMP_REG, value);
|
i2c_read_16bits(TMP112A_ADDR, TMP112A_TEMP_REG, value);
|
||||||
i2c_read(TMP112A_ADDR, TMP112A_TEMP_REG, value, 2);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user