generated from hulk/gd32e23x_template_cmake_vscode
fix iic driver
This commit is contained in:
@@ -13,8 +13,8 @@
|
||||
|
||||
/* >>>>>>>>>>>>>>>>>>>>[DEBUG ASSERTIONS DEFINE]<<<<<<<<<<<<<<<<<<<< */
|
||||
|
||||
#define DEBUG_VERBOSE // Debug Assertions Status : Debug Verbose Information
|
||||
// #undef DEBUG_VERBOSE // Debug Assertions Status : No Debug Verbose Information
|
||||
// #define DEBUG_VERBOSE // Debug Assertions Status : Debug Verbose Information
|
||||
#undef DEBUG_VERBOSE // Debug Assertions Status : No Debug Verbose Information
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
|
59
Inc/i2c.h
59
Inc/i2c.h
@@ -32,15 +32,15 @@
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/* I2C status enumeration */
|
||||
/* I2C result enumeration */
|
||||
typedef enum {
|
||||
I2C_STATUS_SUCCESS = 0, /* Operation successful */
|
||||
I2C_STATUS_TIMEOUT, /* Timeout occurred */
|
||||
I2C_STATUS_NACK, /* No acknowledge received */
|
||||
I2C_STATUS_BUS_BUSY, /* Bus is busy */
|
||||
I2C_STATUS_ERROR, /* General error */
|
||||
I2C_STATUS_INVALID_PARAM /* Invalid parameter */
|
||||
} i2c_status_t;
|
||||
I2C_RESULT_SUCCESS = 0, /* Operation successful */
|
||||
I2C_RESULT_TIMEOUT, /* Timeout occurred */
|
||||
I2C_RESULT_NACK, /* No acknowledge received */
|
||||
I2C_RESULT_BUS_BUSY, /* Bus is busy */
|
||||
I2C_RESULT_ERROR, /* General error */
|
||||
I2C_RESULT_INVALID_PARAM /* Invalid parameter */
|
||||
} i2c_result_t;
|
||||
|
||||
/* I2C state machine enumeration */
|
||||
typedef enum {
|
||||
@@ -67,31 +67,26 @@ typedef enum {
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
|
||||
/* Function declarations */
|
||||
|
||||
/*!
|
||||
\brief configure the GPIO ports for I2C
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void i2c_gpio_config(void);
|
||||
|
||||
// TODO I2C Result
|
||||
/*!
|
||||
\brief configure the I2C interface
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval i2c_status_t
|
||||
\retval i2c_result_t
|
||||
*/
|
||||
i2c_status_t i2c_config(void);
|
||||
void i2c_config(void);
|
||||
|
||||
// TODO I2C Result
|
||||
/*!
|
||||
\brief reset I2C bus with proper recovery
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval i2c_status_t
|
||||
\retval i2c_result_t
|
||||
*/
|
||||
i2c_status_t i2c_bus_reset(void);
|
||||
void i2c_bus_reset(void);
|
||||
|
||||
/*!
|
||||
\brief scan I2C bus for devices
|
||||
@@ -101,31 +96,43 @@ i2c_status_t i2c_bus_reset(void);
|
||||
*/
|
||||
void i2c_scan(void);
|
||||
|
||||
// TODO I2C Result
|
||||
/*!
|
||||
\brief write 16-bit data to I2C device
|
||||
\param[in] slave_addr: 7-bit slave address
|
||||
\param[in] reg_addr: register address
|
||||
\param[in] data: pointer to 2-byte data array
|
||||
\param[out] none
|
||||
\retval i2c_status_t
|
||||
\retval i2c_result_t
|
||||
*/
|
||||
i2c_status_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, const uint8_t data[2]);
|
||||
uint8_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[2]);
|
||||
|
||||
// TODO I2C Result
|
||||
/*!
|
||||
\brief read 16-bit data from I2C device
|
||||
\param[in] slave_addr: 7-bit slave address
|
||||
\param[in] reg_addr: register address
|
||||
\param[out] data: pointer to 2-byte data buffer
|
||||
\retval i2c_status_t
|
||||
\retval i2c_result_t
|
||||
*/
|
||||
i2c_status_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data);
|
||||
uint8_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data);
|
||||
|
||||
// TODO I2C Result
|
||||
/*!
|
||||
\brief read 16-bit data from I2C device
|
||||
\param[in] slave_addr: 7-bit slave address
|
||||
\param[in] reg_addr: register address
|
||||
\param[out] data: pointer to 2-byte data buffer
|
||||
\retval i2c_result_t
|
||||
*/
|
||||
uint8_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data);
|
||||
|
||||
/*!
|
||||
\brief get status string for debugging
|
||||
\param[in] status: i2c_status_t value
|
||||
\param[in] status: i2c_result_t value
|
||||
\param[out] none
|
||||
\retval const char* status string
|
||||
*/
|
||||
const char* i2c_get_status_string(i2c_status_t status);
|
||||
const char* i2c_get_status_string(i2c_result_t status);
|
||||
|
||||
#endif //I2C_H
|
||||
|
206
Inc/ldc1612.h
206
Inc/ldc1612.h
@@ -1,6 +1,5 @@
|
||||
//
|
||||
// Created by dell on 24-12-3.
|
||||
// LDC1612 Inductive Sensor Driver Header
|
||||
//
|
||||
|
||||
#ifndef LDC1612_H
|
||||
@@ -15,163 +14,88 @@
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include "board_config.h"
|
||||
#include "soft_i2c.h"
|
||||
#include "i2c.h"
|
||||
|
||||
/******************************************************************************/
|
||||
/* LDC1612 I2C Address */
|
||||
#define LDC1612_ADDR (0x2B) // 7-bit address
|
||||
/***************************************************************************/
|
||||
|
||||
/* Register Addresses */
|
||||
/******************************************************************************/
|
||||
#define LDC1612_DATA_CH0_MSB 0x00
|
||||
#define LDC1612_DATA_CH0_LSB 0x01
|
||||
#define LDC1612_DATA_CH1_MSB 0x02
|
||||
#define LDC1612_DATA_CH1_LSB 0x03
|
||||
#define LDC1612_RCOUNT_CH0 0x08
|
||||
#define LDC1612_RCOUNT_CH1 0x09
|
||||
#define LDC1612_OFFSET_CH0 0x0C
|
||||
#define LDC1612_OFFSET_CH1 0x0D
|
||||
#define LDC1612_SETTLECOUNT_CH0 0x10
|
||||
#define LDC1612_SETTLECOUNT_CH1 0x11
|
||||
#define LDC1612_CLOCK_DIVIDERS_CH0 0x14
|
||||
#define LDC1612_CLOCK_DIVIDERS_CH1 0x15
|
||||
#define LDC1612_STATUS 0x18
|
||||
#define LDC1612_ERROR_CONFIG 0x19
|
||||
#define LDC1612_CONFIG 0x1A
|
||||
#define LDC1612_MUX_CONFIG 0x1B
|
||||
#define LDC1612_RESET_DEV 0x1C
|
||||
#define LDC1612_DRIVE_CURRENT_CH0 0x1E
|
||||
#define LDC1612_DRIVE_CURRENT_CH1 0x1F
|
||||
#define LDC1612_MANUFACTURER_ID 0x7E
|
||||
#define LDC1612_DEVICE_ID 0x7F
|
||||
#define LDC1612_ADDR 0x2B
|
||||
|
||||
/* Channel Definitions */
|
||||
/******************************************************************************/
|
||||
#define LDC1612_CHANNEL_0 0
|
||||
#define LDC1612_CHANNEL_1 1
|
||||
/*Register Rddr*/
|
||||
/***************************************************************************/
|
||||
|
||||
/* Configuration Values */
|
||||
/******************************************************************************/
|
||||
#define LDC1612_CONVERSION_TIME_CH0 0x0546 // 转换时间
|
||||
#define LDC1612_DRIVE_CURRENT_DEFAULT 0x9000 // 驱动电流
|
||||
#define LDC1612_MUX_CONFIG_DEFAULT 0x020C // 无自动扫描,滤波器带宽3.3MHz
|
||||
#define LDC1612_SENSOR_CONFIG_ACTIVE 0x1601 // 激活配置
|
||||
#define LDC1612_SENSOR_CONFIG_SLEEP 0x2801 // 休眠配置
|
||||
#define LDC1612_ERROR_CONFIG_DEFAULT 0x0000 // 错误配置
|
||||
#define LDC1612_SETTLECOUNT_CH0_DEFAULT 0x001E // 稳定时间
|
||||
#define LDC1612_RESET_VALUE 0x8000 // 复位值
|
||||
#define CONVERTION_RESULT_REG_START 0X00
|
||||
#define SET_CONVERSION_TIME_REG_START 0X08
|
||||
#define SET_CONVERSION_OFFSET_REG_START 0X0C
|
||||
#define SET_LC_STABILIZE_REG_START 0X10
|
||||
#define SET_FREQ_REG_START 0X14
|
||||
|
||||
/* Coil Parameters */
|
||||
/******************************************************************************/
|
||||
#define LDC1612_COIL_RP_KOHM 7.2f // 并联电阻 (kΩ)
|
||||
#define LDC1612_COIL_L_UH 33.0f // 电感值 (μH)
|
||||
#define LDC1612_COIL_C_PF 150.0f // 电容值 (pF)
|
||||
#define LDC1612_COIL_Q_FACTOR 35.97f // 品质因数
|
||||
#define LDC1612_COIL_FREQ_HZ 2262000 // 谐振频率 (Hz)
|
||||
#define SENSOR_STATUS_REG 0X18
|
||||
#define ERROR_CONFIG_REG 0X19
|
||||
#define SENSOR_CONFIG_REG 0X1A
|
||||
#define MUL_CONFIG_REG 0X1B
|
||||
#define SENSOR_RESET_REG 0X1C
|
||||
#define SET_DRIVER_CURRENT_REG 0X1E
|
||||
|
||||
/* Error Codes */
|
||||
/******************************************************************************/
|
||||
#define LDC1612_ERROR_NONE 0x00000000
|
||||
#define LDC1612_ERROR_NO_COIL 0xF0000000
|
||||
#define LDC1612_ERROR_UNDER_RANGE 0x80000000
|
||||
#define LDC1612_ERROR_OVER_RANGE 0x40000000
|
||||
#define LDC1612_ERROR_WATCHDOG 0x20000000
|
||||
#define LDC1612_ERROR_AMPLITUDE 0x10000000
|
||||
|
||||
/* Status Definitions */
|
||||
/******************************************************************************/
|
||||
typedef enum {
|
||||
LDC1612_STATUS_SUCCESS = 0,
|
||||
LDC1612_STATUS_ERROR,
|
||||
LDC1612_STATUS_TIMEOUT,
|
||||
LDC1612_STATUS_INVALID_PARAM,
|
||||
LDC1612_STATUS_NO_COIL,
|
||||
LDC1612_STATUS_UNDER_RANGE,
|
||||
LDC1612_STATUS_OVER_RANGE
|
||||
} ldc1612_status_t;
|
||||
|
||||
typedef struct {
|
||||
uint32_t raw_data;
|
||||
uint32_t frequency;
|
||||
float distance_mm;
|
||||
bool error_flag;
|
||||
uint8_t error_code;
|
||||
} ldc1612_result_t;
|
||||
#define READ_MANUFACTURER_ID 0X7E
|
||||
#define READ_DEVICE_ID 0X7F
|
||||
|
||||
/******************************************************************************/
|
||||
/* Function Declarations */
|
||||
|
||||
/*!
|
||||
\brief 初始化LDC1612传感器
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval ldc1612_status_t
|
||||
*/
|
||||
ldc1612_status_t ldc1612_init(void);
|
||||
#define CHANNEL_0 0
|
||||
#define CHANNEL_1 1
|
||||
|
||||
/*!
|
||||
\brief 复位LDC1612传感器
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval ldc1612_status_t
|
||||
*/
|
||||
ldc1612_status_t ldc1612_reset(void);
|
||||
/******************************************************************************/
|
||||
|
||||
/*!
|
||||
\brief 配置单通道模式
|
||||
\param[in] channel: 通道号 (0或1)
|
||||
\param[out] none
|
||||
\retval ldc1612_status_t
|
||||
*/
|
||||
ldc1612_status_t ldc1612_config_single_channel(uint8_t channel);
|
||||
#define LDC1612_CONVERSION_TIME_CH0 0x0546 //0536
|
||||
#define LDC1612_DRIVE_CURRENT 0x9000 //A000
|
||||
#define LDC1612_MUX_CONFIG 0x020C // no auto scan and filter bandwidth 3.3MHz
|
||||
#define LDC1612_SENSOR_CONFIG 0x1601
|
||||
#define LDC1612_SLEEP_MODE 0x2801
|
||||
#define LDC1612_ERROR_CONFIG 0x0000
|
||||
#define LC_STABILIZE_TIME_CH0 0x001E //30
|
||||
#define LDC1612_RESET_DEV 0x8000 //[15:0] 0b1000 0000 0000 0000
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#define COIL_RP_KOM 7.2
|
||||
#define COIL_L_UH 33
|
||||
#define COIL_C_PF 150
|
||||
#define COIL_Q_FACTOR 35.97
|
||||
#define COIL_FREQ_HZ 2262000
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void ldc1612_set_conversion_time(uint8_t channel, uint16_t result);
|
||||
|
||||
void ldc1612_set_conversion_offset(uint8_t channel, uint16_t result);
|
||||
|
||||
void ldc1612_set_LC_stabilize_time(uint8_t channel, uint16_t result);
|
||||
|
||||
void ldc1612_set_freq_divide(uint8_t channel);
|
||||
|
||||
void ldc1612_set_error_config(uint16_t value);
|
||||
|
||||
void ldc1612_set_mux_config(uint16_t value);
|
||||
|
||||
void ldc1612_reset_sensor(void);
|
||||
|
||||
void ldc1612_set_drive_current(uint8_t channel, uint16_t value);
|
||||
|
||||
void ldc1612_set_sensor_config(uint16_t value);
|
||||
|
||||
void ldc1612_single_ch0_config(void);
|
||||
|
||||
void ldc1612_iic_get_sensor_infomation(void);
|
||||
|
||||
/*!
|
||||
\brief 读取制造商ID
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval uint16_t 制造商ID
|
||||
*/
|
||||
uint16_t ldc1612_get_manufacturer_id(void);
|
||||
|
||||
/*!
|
||||
\brief 读取设备ID
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval uint16_t 设备ID
|
||||
*/
|
||||
uint16_t ldc1612_get_device_id(void);
|
||||
uint16_t ldc1612_get_deveice_id(void);
|
||||
|
||||
/*!
|
||||
\brief 读取通道原始数据
|
||||
\param[in] channel: 通道号
|
||||
\param[out] result: 结果结构体指针
|
||||
\retval ldc1612_status_t
|
||||
*/
|
||||
ldc1612_status_t ldc1612_read_channel(uint8_t channel, ldc1612_result_t *result);
|
||||
uint32_t ldc1612_get_raw_channel_result(uint8_t channel);
|
||||
|
||||
/*!
|
||||
\brief 设置驱动电流
|
||||
\param[in] channel: 通道号
|
||||
\param[in] current: 电流值
|
||||
\param[out] none
|
||||
\retval ldc1612_status_t
|
||||
*/
|
||||
ldc1612_status_t ldc1612_set_drive_current(uint8_t channel, uint16_t current);
|
||||
uint32_t ldc1612_parse_raw_result(uint32_t raw_result);
|
||||
|
||||
/*!
|
||||
\brief 自动检测驱动电流
|
||||
\param[in] channel: 通道号
|
||||
\param[out] none
|
||||
\retval ldc1612_status_t
|
||||
*/
|
||||
ldc1612_status_t ldc1612_auto_detect_drive_current(uint8_t channel);
|
||||
|
||||
/*!
|
||||
\brief 获取状态字符串
|
||||
\param[in] status: 状态码
|
||||
\param[out] none
|
||||
\retval const char* 状态字符串
|
||||
*/
|
||||
const char* ldc1612_get_status_string(ldc1612_status_t status);
|
||||
void ldc1612_drvie_current_detect(uint8_t channel);
|
||||
|
||||
#endif //LDC1612_H
|
||||
|
Reference in New Issue
Block a user