generated from hulk/gd32e23x_template_cmake_vscode
Compare commits
6 Commits
8adabcd08d
...
main
Author | SHA1 | Date | |
---|---|---|---|
749cc2d3e8 | |||
399b81005a | |||
633b2583b2 | |||
b24c0853c9 | |||
77a6525168 | |||
6cc7b2dae2 |
22
.vscode/tasks.json
vendored
22
.vscode/tasks.json
vendored
@@ -11,7 +11,11 @@
|
||||
"Build",
|
||||
"Flash MCU"
|
||||
],
|
||||
"dependsOrder": "sequence"
|
||||
"dependsOrder": "sequence",
|
||||
"icon": {
|
||||
"id": "insert",
|
||||
"tooltip": "Build and Flash"
|
||||
}
|
||||
},
|
||||
{
|
||||
"label": "Flash MCU",
|
||||
@@ -31,6 +35,10 @@
|
||||
},
|
||||
"presentation": {
|
||||
"clear": true
|
||||
},
|
||||
"icon": {
|
||||
"id": "gather",
|
||||
"tooltip": "Flash MCU"
|
||||
}
|
||||
},
|
||||
{
|
||||
@@ -51,6 +59,10 @@
|
||||
},
|
||||
"presentation": {
|
||||
"clear": true
|
||||
},
|
||||
"icon": {
|
||||
"id": "discard",
|
||||
"tooltip": "Reset MCU"
|
||||
}
|
||||
},
|
||||
{
|
||||
@@ -71,6 +83,10 @@
|
||||
},
|
||||
"presentation": {
|
||||
"clear": true
|
||||
},
|
||||
"icon": {
|
||||
"id": "clear-all",
|
||||
"tooltip": "Erase MCU"
|
||||
}
|
||||
},
|
||||
{
|
||||
@@ -119,6 +135,10 @@
|
||||
},
|
||||
"presentation": {
|
||||
"clear": true
|
||||
},
|
||||
"icon": {
|
||||
"id": "code",
|
||||
"tooltip": "Build"
|
||||
}
|
||||
}
|
||||
]
|
||||
|
@@ -30,9 +30,6 @@ set(TARGET_SRC
|
||||
Src/uart_ring_buffer.c
|
||||
Src/command.c
|
||||
Src/i2c.c
|
||||
Src/ldc1612.c
|
||||
# Src/tmp112.c
|
||||
# Src/sensor_example.c
|
||||
)
|
||||
|
||||
# 设置输出目录
|
||||
|
@@ -1,139 +0,0 @@
|
||||
# I2C驱动改进总结
|
||||
|
||||
## 🔧 主要改进内容
|
||||
|
||||
### 1. **状态机重构**
|
||||
- **原问题**: 状态机逻辑混乱,使用复杂的read_cycle变量
|
||||
- **改进方案**:
|
||||
- 使用清晰的`i2c_state_t`枚举定义状态
|
||||
- 分离写入和读取的状态流程
|
||||
- 每个状态职责单一,逻辑清晰
|
||||
|
||||
```c
|
||||
typedef enum {
|
||||
I2C_STATE_IDLE = 0, /* 空闲状态 */
|
||||
I2C_STATE_START, /* 生成起始条件 */
|
||||
I2C_STATE_SEND_ADDRESS, /* 发送从设备地址 */
|
||||
I2C_STATE_CLEAR_ADDRESS, /* 清除地址标志 */
|
||||
I2C_STATE_TRANSMIT_REG, /* 发送寄存器地址 */
|
||||
I2C_STATE_TRANSMIT_DATA, /* 发送数据 */
|
||||
I2C_STATE_RESTART, /* 生成重启条件 */
|
||||
I2C_STATE_RECEIVE_DATA, /* 接收数据 */
|
||||
I2C_STATE_STOP, /* 生成停止条件 */
|
||||
I2C_STATE_ERROR /* 错误状态 */
|
||||
} i2c_state_t;
|
||||
```
|
||||
|
||||
### 2. **错误处理改进**
|
||||
- **原问题**: 函数总是返回成功,无法区分错误类型
|
||||
- **改进方案**:
|
||||
- 定义详细的状态码枚举
|
||||
- 添加参数验证
|
||||
- 实现重试机制
|
||||
|
||||
```c
|
||||
typedef enum {
|
||||
I2C_STATUS_SUCCESS = 0, /* 操作成功 */
|
||||
I2C_STATUS_TIMEOUT, /* 超时 */
|
||||
I2C_STATUS_NACK, /* 无应答 */
|
||||
I2C_STATUS_BUS_BUSY, /* 总线忙 */
|
||||
I2C_STATUS_ERROR, /* 一般错误 */
|
||||
I2C_STATUS_INVALID_PARAM /* 无效参数 */
|
||||
} i2c_status_t;
|
||||
```
|
||||
|
||||
### 3. **超时处理优化**
|
||||
- **原问题**: 超时后无限循环重试
|
||||
- **改进方案**:
|
||||
- 限制最大重试次数 (`I2C_MAX_RETRY = 3`)
|
||||
- 超时后进入错误状态
|
||||
- 重试前添加延时
|
||||
|
||||
### 4. **总线重置完善**
|
||||
- **原问题**: 总线重置不完整,可能无法恢复卡死状态
|
||||
- **改进方案**:
|
||||
- 实现标准的9时钟脉冲恢复
|
||||
- 生成正确的停止条件
|
||||
- 重新配置GPIO和I2C外设
|
||||
|
||||
```c
|
||||
/* 生成9个时钟脉冲释放卡死的从设备 */
|
||||
for (i = 0; i < I2C_RECOVERY_CLOCKS; i++) {
|
||||
gpio_bit_reset(I2C_SCL_PORT, I2C_SCL_PIN);
|
||||
delay_us(I2C_DELAY_US);
|
||||
gpio_bit_set(I2C_SCL_PORT, I2C_SCL_PIN);
|
||||
delay_us(I2C_DELAY_US);
|
||||
}
|
||||
```
|
||||
|
||||
### 5. **配置问题修复**
|
||||
- **原问题**: 硬编码从设备地址0xA0
|
||||
- **改进方案**: 主机地址设为0x00,从设备地址作为参数传入
|
||||
|
||||
### 6. **代码结构优化**
|
||||
- **原问题**: 状态机中有大量重复代码
|
||||
- **改进方案**:
|
||||
- 统一的超时检查模式
|
||||
- 清晰的状态转换逻辑
|
||||
- 一致的错误处理流程
|
||||
|
||||
## 📋 新增功能
|
||||
|
||||
### 1. **状态字符串函数**
|
||||
```c
|
||||
const char* i2c_get_status_string(i2c_status_t status);
|
||||
```
|
||||
用于调试时获取状态描述字符串。
|
||||
|
||||
### 2. **参数验证**
|
||||
```c
|
||||
if (data == NULL || slave_addr > 0x7F) {
|
||||
return I2C_STATUS_INVALID_PARAM;
|
||||
}
|
||||
```
|
||||
|
||||
### 3. **调试信息**
|
||||
使用`DEBUG_VERBOSE`宏控制调试输出。
|
||||
|
||||
## 🔍 状态机流程
|
||||
|
||||
### 写入流程:
|
||||
```
|
||||
START → SEND_ADDRESS → CLEAR_ADDRESS → TRANSMIT_REG →
|
||||
TRANSMIT_DATA → STOP → SUCCESS
|
||||
```
|
||||
|
||||
### 读取流程:
|
||||
```
|
||||
写阶段: START → SEND_ADDRESS → CLEAR_ADDRESS → TRANSMIT_REG → RESTART
|
||||
读阶段: START → SEND_ADDRESS → CLEAR_ADDRESS → RECEIVE_DATA → STOP → SUCCESS
|
||||
```
|
||||
|
||||
## 🚀 使用示例
|
||||
|
||||
```c
|
||||
// 写入16位数据
|
||||
uint8_t write_data[2] = {0x12, 0x34};
|
||||
i2c_status_t status = i2c_write_16bits(0x48, 0x01, write_data);
|
||||
if (status != I2C_STATUS_SUCCESS) {
|
||||
printf("Write failed: %s\r\n", i2c_get_status_string(status));
|
||||
}
|
||||
|
||||
// 读取16位数据
|
||||
uint8_t read_data[2];
|
||||
status = i2c_read_16bits(0x48, 0x01, read_data);
|
||||
if (status == I2C_STATUS_SUCCESS) {
|
||||
printf("Read data: 0x%02X%02X\r\n", read_data[0], read_data[1]);
|
||||
} else {
|
||||
printf("Read failed: %s\r\n", i2c_get_status_string(status));
|
||||
}
|
||||
```
|
||||
|
||||
## 📝 注意事项
|
||||
|
||||
1. **编译选项**: 确保包含`<stdbool.h>`以支持bool类型
|
||||
2. **调试输出**: 定义`DEBUG_VERBOSE`宏启用调试信息
|
||||
3. **延时函数**: 确保`delay_us()`函数可用
|
||||
4. **兼容性**: 保留了原有的函数接口以保持向后兼容
|
||||
|
||||
这些改进大大提高了I2C驱动的可靠性、可维护性和调试能力。
|
@@ -16,6 +16,9 @@
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @brief 传感器周期上报使能标志 */
|
||||
extern volatile bool g_sensor_report_enabled;
|
||||
|
||||
/**
|
||||
* @section Command_Protocol 协议格式
|
||||
* 接收命令帧格式:
|
||||
|
101
Inc/ldc1612.h
101
Inc/ldc1612.h
@@ -1,101 +0,0 @@
|
||||
//
|
||||
// Created by dell on 24-12-3.
|
||||
//
|
||||
|
||||
#ifndef LDC1612_H
|
||||
#define LDC1612_H
|
||||
|
||||
#include "gd32e23x_it.h"
|
||||
#include "gd32e23x.h"
|
||||
#include "systick.h"
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include "board_config.h"
|
||||
#include "soft_i2c.h"
|
||||
#include "i2c.h"
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#define LDC1612_ADDR 0x2B
|
||||
|
||||
/*Register Rddr*/
|
||||
/***************************************************************************/
|
||||
|
||||
#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
|
||||
|
||||
#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
|
||||
|
||||
#define READ_MANUFACTURER_ID 0X7E
|
||||
#define READ_DEVICE_ID 0X7F
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#define CHANNEL_0 0
|
||||
#define CHANNEL_1 1
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#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);
|
||||
|
||||
uint16_t ldc1612_get_manufacturer_id(void);
|
||||
|
||||
uint16_t ldc1612_get_deveice_id(void);
|
||||
|
||||
uint32_t ldc1612_get_raw_channel_result(uint8_t channel);
|
||||
|
||||
uint32_t ldc1612_parse_raw_result(uint32_t raw_result);
|
||||
|
||||
void ldc1612_drvie_current_detect(uint8_t channel);
|
||||
|
||||
#endif //LDC1612_H
|
155
Inc/tmp112.h
155
Inc/tmp112.h
@@ -1,155 +0,0 @@
|
||||
//
|
||||
// Created by dell on 24-12-20.
|
||||
// TMP112A Temperature Sensor Driver Header
|
||||
//
|
||||
|
||||
#ifndef TMP112_H
|
||||
#define TMP112_H
|
||||
|
||||
#include "gd32e23x_it.h"
|
||||
#include "gd32e23x.h"
|
||||
#include "systick.h"
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include "board_config.h"
|
||||
#include "i2c.h"
|
||||
|
||||
/******************************************************************************/
|
||||
/* TMP112A I2C Address */
|
||||
#define TMP112A_ADDR (0x48) // 7-bit address (ADD0=GND)
|
||||
|
||||
/* Register Addresses */
|
||||
/******************************************************************************/
|
||||
#define TMP112A_TEMP_REG 0x00 // 温度寄存器
|
||||
#define TMP112A_CONFIG_REG 0x01 // 配置寄存器
|
||||
#define TMP112A_TLOW_REG 0x02 // 低温阈值寄存器
|
||||
#define TMP112A_THIGH_REG 0x03 // 高温阈值寄存器
|
||||
|
||||
/* Configuration Register Bits */
|
||||
/******************************************************************************/
|
||||
#define TMP112A_CONFIG_OS (1 << 15) // One-shot
|
||||
#define TMP112A_CONFIG_R1 (1 << 14) // Converter resolution bit 1
|
||||
#define TMP112A_CONFIG_R0 (1 << 13) // Converter resolution bit 0
|
||||
#define TMP112A_CONFIG_F1 (1 << 12) // Fault queue bit 1
|
||||
#define TMP112A_CONFIG_F0 (1 << 11) // Fault queue bit 0
|
||||
#define TMP112A_CONFIG_POL (1 << 10) // Polarity
|
||||
#define TMP112A_CONFIG_TM (1 << 9) // Thermostat mode
|
||||
#define TMP112A_CONFIG_SD (1 << 8) // Shutdown
|
||||
#define TMP112A_CONFIG_CR1 (1 << 7) // Conversion rate bit 1
|
||||
#define TMP112A_CONFIG_CR0 (1 << 6) // Conversion rate bit 0
|
||||
#define TMP112A_CONFIG_AL (1 << 5) // Alert
|
||||
#define TMP112A_CONFIG_EM (1 << 4) // Extended mode
|
||||
|
||||
/* Resolution Settings */
|
||||
/******************************************************************************/
|
||||
#define TMP112A_RESOLUTION_9BIT 0x0000 // 9-bit (0.5°C)
|
||||
#define TMP112A_RESOLUTION_10BIT 0x2000 // 10-bit (0.25°C)
|
||||
#define TMP112A_RESOLUTION_11BIT 0x4000 // 11-bit (0.125°C)
|
||||
#define TMP112A_RESOLUTION_12BIT 0x6000 // 12-bit (0.0625°C)
|
||||
|
||||
/* Conversion Rate Settings */
|
||||
/******************************************************************************/
|
||||
#define TMP112A_RATE_0_25HZ 0x0000 // 0.25 Hz (4s)
|
||||
#define TMP112A_RATE_1HZ 0x0040 // 1 Hz (1s)
|
||||
#define TMP112A_RATE_4HZ 0x0080 // 4 Hz (250ms)
|
||||
#define TMP112A_RATE_8HZ 0x00C0 // 8 Hz (125ms)
|
||||
|
||||
/* Default Configuration */
|
||||
/******************************************************************************/
|
||||
#define TMP112A_CONFIG_DEFAULT (TMP112A_RESOLUTION_12BIT | TMP112A_RATE_4HZ)
|
||||
|
||||
/* Temperature Conversion Constants */
|
||||
/******************************************************************************/
|
||||
#define TMP112A_TEMP_RESOLUTION 0.0625f // 12-bit resolution (°C/LSB)
|
||||
#define TMP112A_TEMP_MIN -55.0f // 最低温度 (°C)
|
||||
#define TMP112A_TEMP_MAX 125.0f // 最高温度 (°C)
|
||||
|
||||
/* Status Definitions */
|
||||
/******************************************************************************/
|
||||
typedef enum {
|
||||
TMP112A_STATUS_SUCCESS = 0,
|
||||
TMP112A_STATUS_ERROR,
|
||||
TMP112A_STATUS_TIMEOUT,
|
||||
TMP112A_STATUS_INVALID_PARAM,
|
||||
TMP112A_STATUS_OUT_OF_RANGE
|
||||
} tmp112a_status_t;
|
||||
|
||||
typedef struct {
|
||||
uint16_t raw_data;
|
||||
float temperature_c;
|
||||
float temperature_f;
|
||||
bool alert_flag;
|
||||
} tmp112a_result_t;
|
||||
|
||||
/******************************************************************************/
|
||||
/* Function Declarations */
|
||||
|
||||
/*!
|
||||
\brief 初始化TMP112A传感器
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval tmp112a_status_t
|
||||
*/
|
||||
tmp112a_status_t tmp112a_init(void);
|
||||
|
||||
/*!
|
||||
\brief 配置TMP112A传感器
|
||||
\param[in] config: 配置值
|
||||
\param[out] none
|
||||
\retval tmp112a_status_t
|
||||
*/
|
||||
tmp112a_status_t tmp112a_config(uint16_t config);
|
||||
|
||||
/*!
|
||||
\brief 读取温度
|
||||
\param[in] none
|
||||
\param[out] result: 结果结构体指针
|
||||
\retval tmp112a_status_t
|
||||
*/
|
||||
tmp112a_status_t tmp112a_read_temperature(tmp112a_result_t *result);
|
||||
|
||||
/*!
|
||||
\brief 设置温度阈值
|
||||
\param[in] low_temp: 低温阈值 (°C)
|
||||
\param[in] high_temp: 高温阈值 (°C)
|
||||
\param[out] none
|
||||
\retval tmp112a_status_t
|
||||
*/
|
||||
tmp112a_status_t tmp112a_set_thresholds(float low_temp, float high_temp);
|
||||
|
||||
/*!
|
||||
\brief 进入关机模式
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval tmp112a_status_t
|
||||
*/
|
||||
tmp112a_status_t tmp112a_shutdown(void);
|
||||
|
||||
/*!
|
||||
\brief 退出关机模式
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval tmp112a_status_t
|
||||
*/
|
||||
tmp112a_status_t tmp112a_wakeup(void);
|
||||
|
||||
/*!
|
||||
\brief 单次转换
|
||||
\param[in] none
|
||||
\param[out] result: 结果结构体指针
|
||||
\retval tmp112a_status_t
|
||||
*/
|
||||
tmp112a_status_t tmp112a_one_shot(tmp112a_result_t *result);
|
||||
|
||||
/*!
|
||||
\brief 获取状态字符串
|
||||
\param[in] status: 状态码
|
||||
\param[out] none
|
||||
\retval const char* 状态字符串
|
||||
*/
|
||||
const char* tmp112a_get_status_string(tmp112a_status_t status);
|
||||
|
||||
#endif //TMP112_H
|
438
Src/command.c
438
Src/command.c
@@ -77,7 +77,7 @@
|
||||
* ============================================================================ */
|
||||
|
||||
/** @brief 传感器周期上报使能标志 */
|
||||
static volatile bool s_sensor_report_enabled = false;
|
||||
volatile bool g_sensor_report_enabled = false;
|
||||
|
||||
/** @name 预设响应数据
|
||||
* @{ */
|
||||
@@ -96,7 +96,7 @@ static const uint8_t s_report_status_err[] = { 'e','r','r' }; /**< 错误响应
|
||||
*/
|
||||
bool get_sensor_report_enabled(void)
|
||||
{
|
||||
return s_sensor_report_enabled;
|
||||
return g_sensor_report_enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -108,7 +108,7 @@ bool get_sensor_report_enabled(void)
|
||||
*/
|
||||
void set_sensor_report_status(bool status)
|
||||
{
|
||||
s_sensor_report_enabled = status;
|
||||
g_sensor_report_enabled = status;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -234,8 +234,8 @@ static uint8_t parse_uint_dec(const uint8_t *s, uint8_t n, uint32_t *out)
|
||||
* - 带参数命令:M<数字>S<参数>(如 M100S123,参数为十进制)
|
||||
*
|
||||
* 支持的命令:
|
||||
* - M1: 开启LED,启用传感器上报
|
||||
* - M2: 关闭LED,禁用传感器上报
|
||||
* - M1: 启用传感器上报
|
||||
* - M2: 禁用传感器上报
|
||||
* - M100S<value>: 设置PWM值(示例)
|
||||
*
|
||||
* @param frame 指向完整命令帧的缓冲区(从包头0xD5开始)。
|
||||
@@ -275,29 +275,425 @@ void handle_command(const uint8_t *frame, uint8_t len) {
|
||||
if (cmd_index == cmd_len) {
|
||||
// 仅基础命令,如 M1, M2, M3
|
||||
switch (base_cmd) {
|
||||
case 1u: // M1命令
|
||||
case 1u: // M1: enable sensor report
|
||||
set_sensor_report_status(true);
|
||||
// send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok));
|
||||
uint8_t test_response1[] = { 0xAA, 0xBB, 0xCC, 0xDD };
|
||||
send_response(RESP_TYPE_OK, test_response1, sizeof(test_response1));
|
||||
send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok));
|
||||
return;
|
||||
case 2u: // M2命令
|
||||
case 2u: // M2: disable sensor report
|
||||
set_sensor_report_status(false);
|
||||
// send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok));
|
||||
uint8_t test_response2[] = { 0xDD, 0xCC, 0xBB, 0xAA };
|
||||
send_response(RESP_TYPE_OK, test_response2, sizeof(test_response2));
|
||||
send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok));
|
||||
return;
|
||||
|
||||
// 示例:M3、M10、M201、M100 等(按需添加)
|
||||
case 3u: // M3命令
|
||||
send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok));
|
||||
return;
|
||||
case 4u: // M4命令
|
||||
send_response(RESP_TYPE_OK, s_report_status_err, sizeof(s_report_status_err));
|
||||
return;
|
||||
// case 10u: // M10命令
|
||||
// send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok));
|
||||
// case 3u: // M3命令 - 高电流驱动测试
|
||||
// /**
|
||||
// * M3命令:使用更高驱动电流测试线圈响应
|
||||
// * 响应格式:6字节状态信息
|
||||
// *
|
||||
// * 响应数据解析:
|
||||
// * [0-1]: 传感器状态寄存器(大端序)
|
||||
// * bit[15-8]: 预留
|
||||
// * bit[7]: DRDY_1 - 通道1数据就绪
|
||||
// * bit[6]: DRDY_0 - 通道0数据就绪
|
||||
// * bit[5]: UNREAD_CONV - 未读转换结果
|
||||
// * bit[4]: ERR_ZC - 零计数错误
|
||||
// * bit[3]: ERR_AE - 幅度错误(重点关注)
|
||||
// * bit[2]: ERR_WD - 看门狗超时
|
||||
// * bit[1]: ERR_OR - 过量程错误
|
||||
// * bit[0]: ERR_UR - 欠量程错误
|
||||
// * [2]: 数据就绪标志 (0x01=就绪, 0x00=未就绪)
|
||||
// * [3]: 0xA0 - 高电流测试标记
|
||||
// * [4]: 幅度错误专用标志 (0xAE=有幅度错误, 0x00=无)
|
||||
// * [5]: 0x33 - M3命令标记
|
||||
// *
|
||||
// * 分析要点:
|
||||
// * - 如果[0-1]从0x0008变为其他值,说明高电流有效果
|
||||
// * - 如果[2]变为0x01,说明数据开始就绪
|
||||
// * - 如果[4]变为0x00,说明幅度错误消失
|
||||
// */
|
||||
// // 重置传感器
|
||||
// ldc1612_reset_sensor();
|
||||
// delay_ms(50);
|
||||
|
||||
// // 使用更高的驱动电流重新配置
|
||||
// // ldc1612_write_register(SET_DRIVER_CURRENT_REG, 0xA000);
|
||||
// delay_ms(10);
|
||||
|
||||
// // 重新配置其他参数
|
||||
// ldc1612_config_single_channel(CHANNEL_0);
|
||||
// delay_ms(200); // 更长稳定时间
|
||||
|
||||
// // 检查结果
|
||||
// uint16_t status_m3 = ldc1612_get_sensor_status();
|
||||
// bool ready_m3 = ldc1612_is_data_ready(CHANNEL_0);
|
||||
|
||||
// uint8_t m3_info[6];
|
||||
// m3_info[0] = (uint8_t)(status_m3 >> 8);
|
||||
// m3_info[1] = (uint8_t)(status_m3 & 0xFF);
|
||||
// m3_info[2] = ready_m3 ? 0x01 : 0x00;
|
||||
// m3_info[3] = 0xA0; // 高电流标记
|
||||
// m3_info[4] = (status_m3 & 0x0008) ? 0xAE : 0x00; // 幅度错误标志
|
||||
// m3_info[5] = 0x33; // M3命令标记
|
||||
|
||||
// send_response(RESP_TYPE_OK, m3_info, sizeof(m3_info));
|
||||
// return;
|
||||
// case 4u: // M4命令 - 寄存器诊断
|
||||
// /**
|
||||
// * M4命令:读取关键寄存器进行配置诊断
|
||||
// * 响应格式:8字节寄存器信息
|
||||
// *
|
||||
// * 响应数据解析:
|
||||
// * [0-1]: 状态寄存器 (0x18) - 当前传感器状态
|
||||
// * [2-3]: 传感器配置寄存器 (0x1A) - 传感器工作模式
|
||||
// * 期望值: 0x1601 (活动模式,单通道)
|
||||
// * [4-5]: 驱动电流寄存器 (0x1E) - 当前驱动电流设置
|
||||
// * 常见值: 0x9000(默认), 0xA000(高), 0xF800(最高)
|
||||
// * [6]: I2C读取状态 (0x4F='O'=成功, 0xEE=失败)
|
||||
// * [7]: 0x44 - M4命令标记
|
||||
// *
|
||||
// * 分析要点:
|
||||
// * - [2-3]应该是0x1601,如果不是说明配置异常
|
||||
// * - [4-5]显示实际的驱动电流设置
|
||||
// * - [6]必须是0x4F,否则I2C通信有问题
|
||||
// */
|
||||
// // 简化版本,只读取最关键的寄存器,避免I2C超时
|
||||
// uint16_t status_reg = ldc1612_get_sensor_status(); // 0x18
|
||||
|
||||
// // 逐一安全读取关键寄存器
|
||||
// uint8_t data_buf[2] = {0};
|
||||
// uint16_t sensor_config = 0;
|
||||
// uint16_t drive_current = 0;
|
||||
|
||||
// // 尝试读取传感器配置寄存器
|
||||
// bool result1_ok = (LDC1612_IIC_READ_16BITS(LDC1612_ADDR, SENSOR_CONFIG_REG, data_buf) == I2C_RESULT_SUCCESS);
|
||||
// if (result1_ok) {
|
||||
// sensor_config = (data_buf[0] << 8) | data_buf[1];
|
||||
// }
|
||||
|
||||
// // 尝试读取驱动电流寄存器
|
||||
// bool result2_ok = (LDC1612_IIC_READ_16BITS(LDC1612_ADDR, SET_DRIVER_CURRENT_REG, data_buf) == I2C_RESULT_SUCCESS);
|
||||
// if (result2_ok) {
|
||||
// drive_current = (data_buf[0] << 8) | data_buf[1];
|
||||
// }
|
||||
|
||||
// // 构造8字节简化诊断信息
|
||||
// uint8_t diag_info[8];
|
||||
// diag_info[0] = (uint8_t)(status_reg >> 8); // 状态寄存器高位
|
||||
// diag_info[1] = (uint8_t)(status_reg & 0xFF); // 状态寄存器低位
|
||||
// diag_info[2] = (uint8_t)(sensor_config >> 8); // 传感器配置寄存器高位
|
||||
// diag_info[3] = (uint8_t)(sensor_config & 0xFF); // 传感器配置寄存器低位
|
||||
// diag_info[4] = (uint8_t)(drive_current >> 8); // 驱动电流寄存器高位
|
||||
// diag_info[5] = (uint8_t)(drive_current & 0xFF); // 驱动电流寄存器低位
|
||||
// diag_info[6] = (result1_ok && result2_ok) ? 0x4F : 0xEE; // I2C状态
|
||||
// diag_info[7] = 0x44; // M4命令标记
|
||||
|
||||
// send_response(RESP_TYPE_OK, diag_info, sizeof(diag_info));
|
||||
// return;
|
||||
// case 5u: // M5命令 - 最高电流启动测试
|
||||
// // 命令: D5 03 02 4D 35 87
|
||||
// // 响应: B5 F0 08 [状态2字节][就绪标志][电流设置2字节][幅度错误标志][M5标记][最高电流标记] CRC
|
||||
// // 响应格式:
|
||||
// // [0-1]: 传感器状态寄存器(启动后状态)
|
||||
// // [2]: 数据就绪标志 (0x01=就绪, 0x00=未就绪)
|
||||
// // [3-4]: 实际驱动电流设置值(应该是0xF800)
|
||||
// // [5]: 幅度错误专用标志 (0xAE=仍有错误, 0x00=错误消失)
|
||||
// // [6]: 0x55 - M5命令标记
|
||||
// // [7]: 0xF8 - 最高电流标记
|
||||
// // 重置传感器
|
||||
// ldc1612_reset_sensor();
|
||||
// delay_ms(100);
|
||||
|
||||
// // 使用最高驱动电流并固定配置
|
||||
// // ldc1612_write_register(SET_DRIVER_CURRENT_REG, 0xF800);
|
||||
// delay_ms(10);
|
||||
|
||||
// // 手动配置其他必要寄存器,避免被覆盖
|
||||
// // 配置频率分频器为较低频率 (更容易起振)
|
||||
// uint8_t freq_data[2] = {0x10, 0x00}; // 较低分频
|
||||
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SET_FREQ_REG_START + CHANNEL_0, freq_data);
|
||||
// delay_ms(10);
|
||||
|
||||
// // 设置较长的LC稳定时间
|
||||
// uint8_t lc_data[2] = {0x04, 0x00}; // 更长稳定时间
|
||||
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SET_LC_STABILIZE_REG_START + CHANNEL_0, lc_data);
|
||||
// delay_ms(10);
|
||||
|
||||
// // 配置MUX为单通道模式
|
||||
// // ldc1612_configure_mux_register(0, CHANNEL_0, LDC1612_MUX_RR_SEQUENCE_1, LDC1612_MUX_FILTER_1MHz);
|
||||
// delay_ms(10);
|
||||
|
||||
// // 启动传感器
|
||||
// uint8_t sensor_cfg_data[2] = {0x16, 0x01}; // 活动模式,单通道
|
||||
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SENSOR_CONFIG_REG, sensor_cfg_data);
|
||||
// delay_ms(200); // 更长稳定时间
|
||||
|
||||
// // 读取结果
|
||||
// uint16_t status_m5 = ldc1612_get_sensor_status();
|
||||
// bool ready_m5 = ldc1612_is_data_ready(CHANNEL_0);
|
||||
|
||||
// // 再次确认驱动电流设置
|
||||
// uint8_t curr_data[2];
|
||||
// LDC1612_IIC_READ_16BITS(LDC1612_ADDR, SET_DRIVER_CURRENT_REG, curr_data);
|
||||
// uint16_t actual_current = (curr_data[0] << 8) | curr_data[1];
|
||||
|
||||
// uint8_t m5_info[8];
|
||||
// m5_info[0] = (uint8_t)(status_m5 >> 8);
|
||||
// m5_info[1] = (uint8_t)(status_m5 & 0xFF);
|
||||
// m5_info[2] = ready_m5 ? 0x01 : 0x00;
|
||||
// m5_info[3] = (uint8_t)(actual_current >> 8); // 实际电流设置高位
|
||||
// m5_info[4] = (uint8_t)(actual_current & 0xFF); // 实际电流设置低位
|
||||
// m5_info[5] = (status_m5 & 0x0008) ? 0xAE : 0x00; // 幅度错误标志
|
||||
// m5_info[6] = 0x55; // M5命令标记
|
||||
// m5_info[7] = 0xF8; // 最高电流标记
|
||||
|
||||
// send_response(RESP_TYPE_OK, m5_info, sizeof(m5_info));
|
||||
// return;
|
||||
// case 6u: // M6命令 - 芯片功能验证
|
||||
// // 命令: D5 03 02 4D 36 88
|
||||
// // 响应: B5 F0 0C [写入值2字节][读取值2字节][制造商ID2字节][设备ID2字节][状态2字节][ID读取状态][M6标记] CRC
|
||||
// // 响应格式:
|
||||
// // [0-1]: 写入测试值 (0x9000)
|
||||
// // [2-3]: 读取回的值
|
||||
// // [4-5]: 制造商ID (应该是0x5449="TI")
|
||||
// // [6-7]: 设备ID (应该是0x3055)
|
||||
// // [8-9]: 当前状态寄存器
|
||||
// // [10]: ID读取状态 (0x4F=成功, 0xEE=失败)
|
||||
// // [11]: 0x66 - M6命令标记
|
||||
// // 测试1: 写入和读取特定值到驱动电流寄存器
|
||||
// uint8_t test_current_data[2] = {0x90, 0x00}; // 写入0x9000
|
||||
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SET_DRIVER_CURRENT_REG, test_current_data);
|
||||
// delay_ms(10);
|
||||
|
||||
// // 读取验证
|
||||
// uint8_t read_current_data[2];
|
||||
// LDC1612_IIC_READ_16BITS(LDC1612_ADDR, SET_DRIVER_CURRENT_REG, read_current_data);
|
||||
// uint16_t read_current = (read_current_data[0] << 8) | read_current_data[1];
|
||||
|
||||
// // 测试2: 读取制造商ID和设备ID
|
||||
// uint8_t manufacturer_data[2];
|
||||
// uint8_t device_data[2];
|
||||
// bool id_read_ok = true;
|
||||
|
||||
// if (LDC1612_IIC_READ_16BITS(LDC1612_ADDR, 0x7E, manufacturer_data) != I2C_RESULT_SUCCESS) {
|
||||
// id_read_ok = false;
|
||||
// }
|
||||
// if (LDC1612_IIC_READ_16BITS(LDC1612_ADDR, 0x7F, device_data) != I2C_RESULT_SUCCESS) {
|
||||
// id_read_ok = false;
|
||||
// }
|
||||
|
||||
// uint16_t manufacturer_id = id_read_ok ? ((manufacturer_data[0] << 8) | manufacturer_data[1]) : 0x0000;
|
||||
// uint16_t device_id = id_read_ok ? ((device_data[0] << 8) | device_data[1]) : 0x0000;
|
||||
|
||||
// // 测试3: 检查当前状态
|
||||
// uint16_t current_status = ldc1612_get_sensor_status();
|
||||
|
||||
// // 构造12字节测试结果
|
||||
// uint8_t test_info[12];
|
||||
// test_info[0] = 0x90; // 写入的值高位
|
||||
// test_info[1] = 0x00; // 写入的值低位
|
||||
// test_info[2] = (uint8_t)(read_current >> 8); // 读取的值高位
|
||||
// test_info[3] = (uint8_t)(read_current & 0xFF); // 读取的值低位
|
||||
// test_info[4] = (uint8_t)(manufacturer_id >> 8);
|
||||
// test_info[5] = (uint8_t)(manufacturer_id & 0xFF);
|
||||
// test_info[6] = (uint8_t)(device_id >> 8);
|
||||
// test_info[7] = (uint8_t)(device_id & 0xFF);
|
||||
// test_info[8] = (uint8_t)(current_status >> 8);
|
||||
// test_info[9] = (uint8_t)(current_status & 0xFF);
|
||||
// test_info[10] = id_read_ok ? 0x4F : 0xEE; // ID读取状态
|
||||
// test_info[11] = 0x66; // M6命令标记
|
||||
|
||||
// send_response(RESP_TYPE_OK, test_info, sizeof(test_info));
|
||||
// return;
|
||||
// case 7u: // M7命令 - 保守参数测试
|
||||
// // 命令: D5 03 02 4D 37 89
|
||||
// // 响应: B5 F0 0A [状态2字节][就绪标志][频率设置2字节][幅度错误标志][欠量程错误标志][过量程错误标志][M7标记][低频标记] CRC
|
||||
// // 响应格式:
|
||||
// // [0-1]: 状态寄存器
|
||||
// // [2]: 数据就绪标志
|
||||
// // [3-4]: 实际频率分频器设置 (0x2000=较低频率)
|
||||
// // [5]: 幅度错误标志 (0xAE=有错误, 0x00=无)
|
||||
// // [6]: 欠量程错误标志 (0x01=有, 0x00=无)
|
||||
// // [7]: 过量程错误标志 (0x02=有, 0x00=无)
|
||||
// // [8]: 0x77 - M7命令标记
|
||||
// // [9]: 0x20 - 低频标记
|
||||
// // 重置传感器
|
||||
// ldc1612_reset_sensor();
|
||||
// delay_ms(100);
|
||||
|
||||
// // 使用保守的配置尝试启动线圈
|
||||
// // 1. 设置最高驱动电流
|
||||
// uint8_t drive_data[2] = {0xF8, 0x00}; // 最高电流
|
||||
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SET_DRIVER_CURRENT_REG, drive_data);
|
||||
// delay_ms(10);
|
||||
|
||||
// // 2. 设置较低的频率分频器(适合更大电感值)
|
||||
// uint8_t freq_low_data[2] = {0x20, 0x00}; // 更低频率
|
||||
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SET_FREQ_REG_START + CHANNEL_0, freq_low_data);
|
||||
// delay_ms(10);
|
||||
|
||||
// // 3. 设置更长的LC稳定时间
|
||||
// uint8_t lc_stable_data[2] = {0x08, 0x00}; // 更长稳定时间
|
||||
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SET_LC_STABILIZE_REG_START + CHANNEL_0, lc_stable_data);
|
||||
// delay_ms(10);
|
||||
|
||||
// // 4. 设置更长的转换时间
|
||||
// uint8_t conv_time_data[2] = {0x04, 0x00}; // 更长转换时间
|
||||
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SET_CONVERSION_TIME_REG_START + CHANNEL_0, conv_time_data);
|
||||
// delay_ms(10);
|
||||
|
||||
// // 5. 设置转换偏移
|
||||
// uint8_t conv_offset_data[2] = {0x00, 0x00};
|
||||
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SET_CONVERSION_OFFSET_REG_START + CHANNEL_0, conv_offset_data);
|
||||
// delay_ms(10);
|
||||
|
||||
// // 6. 配置错误寄存器 - 降低错误敏感度
|
||||
// uint8_t error_config_data[2] = {0x00, 0x00}; // 允许所有错误
|
||||
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, ERROR_CONFIG_REG, error_config_data);
|
||||
// delay_ms(10);
|
||||
|
||||
// // 7. 配置MUX寄存器
|
||||
// // ldc1612_configure_mux_register(0, CHANNEL_0, LDC1612_MUX_RR_SEQUENCE_1, LDC1612_MUX_FILTER_1MHz);
|
||||
// delay_ms(10);
|
||||
|
||||
// // 8. 启动传感器
|
||||
// uint8_t sensor_start_data[2] = {0x16, 0x01}; // 活动模式
|
||||
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SENSOR_CONFIG_REG, sensor_start_data);
|
||||
// delay_ms(500); // 给予充分时间稳定
|
||||
|
||||
// // 检查结果
|
||||
// uint16_t status_m7 = ldc1612_get_sensor_status();
|
||||
// bool ready_m7 = ldc1612_is_data_ready(CHANNEL_0);
|
||||
|
||||
// // 读取实际配置的频率分频器确认
|
||||
// uint8_t freq_readback[2];
|
||||
// LDC1612_IIC_READ_16BITS(LDC1612_ADDR, SET_FREQ_REG_START + CHANNEL_0, freq_readback);
|
||||
// uint16_t freq_actual = (freq_readback[0] << 8) | freq_readback[1];
|
||||
|
||||
// uint8_t m7_info[10];
|
||||
// m7_info[0] = (uint8_t)(status_m7 >> 8);
|
||||
// m7_info[1] = (uint8_t)(status_m7 & 0xFF);
|
||||
// m7_info[2] = ready_m7 ? 0x01 : 0x00;
|
||||
// m7_info[3] = (uint8_t)(freq_actual >> 8); // 实际频率分频器
|
||||
// m7_info[4] = (uint8_t)(freq_actual & 0xFF);
|
||||
// m7_info[5] = (status_m7 & 0x0008) ? 0xAE : 0x00; // 幅度错误
|
||||
// m7_info[6] = (status_m7 & 0x0001) ? 0x01 : 0x00; // 欠量程错误
|
||||
// m7_info[7] = (status_m7 & 0x0002) ? 0x02 : 0x00; // 过量程错误
|
||||
// m7_info[8] = 0x77; // M7命令标记
|
||||
// m7_info[9] = 0x20; // 低频标记
|
||||
|
||||
// send_response(RESP_TYPE_OK, m7_info, sizeof(m7_info));
|
||||
// return;
|
||||
// case 8u: // M8命令 - 极端参数测试
|
||||
// // 命令: D5 03 02 4D 38 8A
|
||||
// // 响应: B5 F0 06 [状态2字节][就绪标志][幅度错误标志][M8标记][极端测试标记] CRC
|
||||
// // 响应格式:
|
||||
// // [0-1]: 传感器状态寄存器
|
||||
// // [2]: 数据就绪标志 (0x01=就绪, 0x00=未就绪)
|
||||
// // [3]: 幅度错误标志 (0xAE=仍有错误, 0x00=错误消失)
|
||||
// // [4]: 0x88 - M8命令标记
|
||||
// // [5]: 0xEE - 极端测试标记
|
||||
// {
|
||||
// // 重置传感器
|
||||
// ldc1612_reset_sensor();
|
||||
// delay_ms(100);
|
||||
|
||||
// // 极端配置1: 极低频率
|
||||
// uint8_t extreme_freq[2] = {0x40, 0x00};
|
||||
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SET_FREQ_REG_START + CHANNEL_0, extreme_freq);
|
||||
// delay_ms(10);
|
||||
|
||||
// // 极端配置2: 最大驱动电流
|
||||
// uint8_t max_drive[2] = {0xFF, 0x00};
|
||||
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SET_DRIVER_CURRENT_REG, max_drive);
|
||||
// delay_ms(10);
|
||||
|
||||
// // 极端配置3: 禁用错误检测
|
||||
// uint8_t no_errors[2] = {0x00, 0x00};
|
||||
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, ERROR_CONFIG_REG, no_errors);
|
||||
// delay_ms(10);
|
||||
|
||||
// // 启动传感器
|
||||
// uint8_t start_data[2] = {0x16, 0x01};
|
||||
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SENSOR_CONFIG_REG, start_data);
|
||||
// delay_ms(1000); // 等待1秒
|
||||
|
||||
// // 读取状态
|
||||
// uint16_t status_8 = ldc1612_get_sensor_status();
|
||||
// bool ready_8 = ldc1612_is_data_ready(CHANNEL_0);
|
||||
|
||||
// uint8_t m8_result[6];
|
||||
// m8_result[0] = (uint8_t)(status_8 >> 8);
|
||||
// m8_result[1] = (uint8_t)(status_8 & 0xFF);
|
||||
// m8_result[2] = ready_8 ? 0x01 : 0x00;
|
||||
// m8_result[3] = (status_8 & 0x0008) ? 0xAE : 0x00; // 幅度错误
|
||||
// m8_result[4] = 0x88; // M8标记
|
||||
// m8_result[5] = 0xEE; // 极端测试标记
|
||||
|
||||
// send_response(RESP_TYPE_OK, m8_result, sizeof(m8_result));
|
||||
// return;
|
||||
// }
|
||||
// case 9u: // M9命令 - 多频率特性测试
|
||||
// // 命令: D5 03 02 4D 39 8B
|
||||
// // 响应: B5 F0 08 [高频状态2字节][高频就绪标志][低频状态2字节][低频就绪标志][M9标记][多频测试标记] CRC
|
||||
// // 响应格式:
|
||||
// // [0-1]: 高频测试状态
|
||||
// // [2]: 高频就绪标志 (0x01=就绪, 0x00=未就绪)
|
||||
// // [3-4]: 低频测试状态
|
||||
// // [5]: 低频就绪标志 (0x01=就绪, 0x00=未就绪)
|
||||
// // [6]: 0x99 - M9命令标记
|
||||
// // [7]: 0xAA - 多频测试标记
|
||||
// {
|
||||
// // 测试1: 高频配置
|
||||
// ldc1612_reset_sensor();
|
||||
// delay_ms(50);
|
||||
|
||||
// uint8_t high_freq[2] = {0x04, 0x00}; // 高频
|
||||
// uint8_t low_drive[2] = {0x80, 0x00}; // 低电流
|
||||
|
||||
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SET_FREQ_REG_START + CHANNEL_0, high_freq);
|
||||
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SET_DRIVER_CURRENT_REG, low_drive);
|
||||
// delay_ms(10);
|
||||
|
||||
// // 启动高频测试
|
||||
// uint8_t start_hf[2] = {0x16, 0x01};
|
||||
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SENSOR_CONFIG_REG, start_hf);
|
||||
// delay_ms(200);
|
||||
|
||||
// uint16_t hf_status = ldc1612_get_sensor_status();
|
||||
// bool hf_ready = ldc1612_is_data_ready(CHANNEL_0);
|
||||
|
||||
// // 测试2: 低频配置
|
||||
// uint8_t sleep_mode[2] = {0x20, 0x01};
|
||||
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SENSOR_CONFIG_REG, sleep_mode);
|
||||
// delay_ms(50);
|
||||
|
||||
// uint8_t low_freq[2] = {0x20, 0x00}; // 低频
|
||||
// uint8_t high_drive[2] = {0xC0, 0x00}; // 高电流
|
||||
|
||||
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SET_FREQ_REG_START + CHANNEL_0, low_freq);
|
||||
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SET_DRIVER_CURRENT_REG, high_drive);
|
||||
// delay_ms(10);
|
||||
|
||||
// // 启动低频测试
|
||||
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SENSOR_CONFIG_REG, start_hf);
|
||||
// delay_ms(200);
|
||||
|
||||
// uint16_t lf_status = ldc1612_get_sensor_status();
|
||||
// bool lf_ready = ldc1612_is_data_ready(CHANNEL_0);
|
||||
|
||||
// uint8_t m9_result[8];
|
||||
// m9_result[0] = (uint8_t)(hf_status >> 8); // 高频状态
|
||||
// m9_result[1] = (uint8_t)(hf_status & 0xFF);
|
||||
// m9_result[2] = hf_ready ? 0x01 : 0x00; // 高频就绪
|
||||
// m9_result[3] = (uint8_t)(lf_status >> 8); // 低频状态
|
||||
// m9_result[4] = (uint8_t)(lf_status & 0xFF);
|
||||
// m9_result[5] = lf_ready ? 0x01 : 0x00; // 低频就绪
|
||||
// m9_result[6] = 0x99; // M9标记
|
||||
// m9_result[7] = 0xAA; // 多频测试标记
|
||||
|
||||
// send_response(RESP_TYPE_OK, m9_result, sizeof(m9_result));
|
||||
// return;
|
||||
// }
|
||||
// case 201u: // M201命令
|
||||
// send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok));
|
||||
// return;
|
||||
|
534
Src/i2c.c
534
Src/i2c.c
@@ -156,7 +156,6 @@ i2c_result_t i2c_bus_reset(void) {
|
||||
return I2C_RECOVERY_OK;
|
||||
}
|
||||
|
||||
#ifdef DEBUG_VERBOSE
|
||||
/**
|
||||
* @brief 扫描I2C总线,查找连接的设备
|
||||
*
|
||||
@@ -263,364 +262,391 @@ void i2c_scan(void) {
|
||||
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TC) == RESET) {}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
i2c_result_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[2]) {
|
||||
uint8_t state = I2C_STATE_START;
|
||||
i2c_state_t state = I2C_STATE_START;
|
||||
uint16_t timeout = 0;
|
||||
uint8_t i2c_timeout_flag = 0;
|
||||
uint8_t retry_count = 0;
|
||||
|
||||
/* parameter validation */
|
||||
if (data == NULL || slave_addr > 0x7F) {
|
||||
return I2C_RESULT_INVALID_PARAM;
|
||||
}
|
||||
|
||||
/* enable acknowledge */
|
||||
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
||||
|
||||
while (!(i2c_timeout_flag)) {
|
||||
while (retry_count < I2C_MAX_RETRY) {
|
||||
switch (state) {
|
||||
case I2C_STATE_START:
|
||||
/* i2c master sends start signal only when the bus is idle */
|
||||
timeout = 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) {
|
||||
i2c_start_on_bus(I2C0);
|
||||
timeout = 0;
|
||||
state = I2C_STATE_SEND_ADDRESS;
|
||||
} else {
|
||||
timeout = 0;
|
||||
state = I2C_STATE_START;
|
||||
#ifdef DEBUG_VERBOES
|
||||
printf("i2c bus is busy in WRITE BYTE!\n");
|
||||
#endif
|
||||
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:
|
||||
/* i2c master sends START signal successfully */
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) && (timeout < I2C_TIME_OUT)) {
|
||||
/* 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) {
|
||||
i2c_master_addressing(I2C0, slave_addr << 1, I2C_TRANSMITTER);
|
||||
timeout = 0;
|
||||
state = I2C_STATE_CLEAR_ADDRESS;
|
||||
} else {
|
||||
timeout = 0;
|
||||
state = I2C_STATE_START;
|
||||
#ifdef DEBUG_VERBOES
|
||||
printf("i2c master sends start signal timeout in WRITE BYTE!\n");
|
||||
#endif
|
||||
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:
|
||||
/* address flag set means i2c slave sends ACK */
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) && (timeout < I2C_TIME_OUT)) {
|
||||
/* 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) {
|
||||
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_DATA;
|
||||
timeout =0;
|
||||
state = I2C_STATE_TRANSMIT_REG;
|
||||
break;
|
||||
} else {
|
||||
timeout = 0;
|
||||
state = I2C_STATE_START;
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
||||
timeout =0;
|
||||
#ifdef DEBUG_VERBOES
|
||||
printf("i2c master clears address flag timeout in WRITE BYTE!\n");
|
||||
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) {
|
||||
/* send IIC register address */
|
||||
i2c_data_transmit(I2C0, reg_addr);
|
||||
timeout = 0;
|
||||
} else {
|
||||
timeout = 0;
|
||||
state = I2C_STATE_START;
|
||||
#ifdef DEBUG_VERBOES
|
||||
printf("i2c master sends data timeout in WRITE BYTE!\n");
|
||||
#endif
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
/* wait until BTC bit is set */
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
|
||||
/* send register MSB value */
|
||||
i2c_data_transmit(I2C0, data[0]);
|
||||
timeout = 0;
|
||||
|
||||
/* 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) {
|
||||
/* send register MSB value */
|
||||
i2c_data_transmit(I2C0, data[0]);
|
||||
timeout = 0;
|
||||
} else {
|
||||
timeout = 0;
|
||||
state = I2C_STATE_START;
|
||||
#ifdef DEBUG_VERBOES
|
||||
printf("i2c master sends MSB data timeout in WRITE BYTE!\n");
|
||||
#endif
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
/* wait until BTC bit is set */
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout < I2C_TIME_OUT) {
|
||||
/* send register LSB value */
|
||||
i2c_data_transmit(I2C0, data[1]);
|
||||
timeout = 0;
|
||||
state = I2C_STATE_STOP;
|
||||
} else {
|
||||
timeout = 0;
|
||||
state = I2C_STATE_START;
|
||||
#ifdef DEBUG_VERBOES
|
||||
printf("i2c master sends LSB data timeout in WRITE BYTE!\n");
|
||||
#endif
|
||||
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;
|
||||
}
|
||||
|
||||
/* wait until BTC bit is set */
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
|
||||
/* send register LSB value */
|
||||
i2c_data_transmit(I2C0, data[1]);
|
||||
timeout = 0;
|
||||
|
||||
/* wait until BTC bit is set */
|
||||
while (!i2c_flag_get(I2C0, I2C_FLAG_BTC) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout < I2C_TIME_OUT) {
|
||||
state = I2C_STATE_STOP;
|
||||
timeout = 0;
|
||||
} else {
|
||||
timeout = 0;
|
||||
state = I2C_STATE_START;
|
||||
#ifdef DEBUG_VERBOES
|
||||
printf("i2c master sends data timeout in WRITE BYTE!\n");
|
||||
#endif
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
state = I2C_STATE_STOP;
|
||||
break;
|
||||
|
||||
case I2C_STATE_STOP:
|
||||
/* send a stop condition to I2C bus */
|
||||
i2c_stop_on_bus(I2C0);
|
||||
/* i2c master sends STOP signal successfully */
|
||||
|
||||
timeout = 0;
|
||||
while ((I2C_CTL0(I2C0) & I2C_CTL0_STOP) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout < I2C_TIME_OUT) {
|
||||
timeout = 0;
|
||||
state = I2C_STATE_END;
|
||||
i2c_timeout_flag = I2C_OK;
|
||||
} else {
|
||||
timeout = 0;
|
||||
state = I2C_STATE_START;
|
||||
#ifdef DEBUG_VERBOES
|
||||
printf("i2c master sends stop signal timeout in WRITE BYTE!\n");
|
||||
#endif
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
/* i2c master sends STOP signal successfully */
|
||||
/* 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++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
return I2C_RESULT_ERROR;
|
||||
}
|
||||
|
||||
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_VERBOES
|
||||
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;
|
||||
i2c_timeout_flag = I2C_OK;
|
||||
timeout = 0;
|
||||
#ifdef DEBUG_VERBOES
|
||||
printf("i2c master sends start signal in WRITE BYTE.\n");
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
}
|
||||
return I2C_RESULT_SUCCESS;
|
||||
return I2C_RESULT_TIMEOUT;
|
||||
}
|
||||
|
||||
i2c_result_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data) {
|
||||
i2c_state_t state = I2C_STATE_START;
|
||||
uint16_t timeout = 0;
|
||||
uint8_t retry_count = 0;
|
||||
bool write_phase = true;
|
||||
|
||||
// 参数检查:防止空指针和非法地址
|
||||
if (data == NULL || slave_addr > 0x7F) {
|
||||
return I2C_RESULT_INVALID_PARAM;
|
||||
}
|
||||
|
||||
i2c_state_t state = I2C_STATE_START;
|
||||
bool read_cycle = false;
|
||||
bool i2c_timeout_flag = false;
|
||||
uint16_t timeout = 0;
|
||||
uint8_t number_of_byte = 2;
|
||||
|
||||
/* enable acknowledge */
|
||||
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
||||
|
||||
while (!(i2c_timeout_flag)) {
|
||||
while (retry_count < (uint8_t)I2C_MAX_RETRY) {
|
||||
switch (state) {
|
||||
case I2C_STATE_START:
|
||||
if (RESET == read_cycle) {
|
||||
/* i2c master sends start signal only when the bus is idle */
|
||||
while (i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout < I2C_TIME_OUT) {
|
||||
/* whether to send ACK or not for the next byte */
|
||||
i2c_ackpos_config(I2C0, I2C_ACKPOS_NEXT);
|
||||
} else {
|
||||
// i2c_bus_reset();
|
||||
timeout = 0;
|
||||
state = I2C_STATE_START;
|
||||
#ifdef DEBUG_VERBOES
|
||||
printf("i2c bus is busy in READ!\n");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
/* send the start signal */
|
||||
i2c_start_on_bus(I2C0);
|
||||
timeout = 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:
|
||||
/* i2c master sends START signal successfully */
|
||||
/* 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) {
|
||||
if (RESET == read_cycle) {
|
||||
i2c_master_addressing(I2C0, slave_addr << 1, I2C_TRANSMITTER);
|
||||
state = I2C_STATE_CLEAR_ADDRESS;
|
||||
} else {
|
||||
i2c_master_addressing(I2C0, slave_addr << 1, I2C_RECEIVER);
|
||||
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
||||
state = I2C_STATE_CLEAR_ADDRESS;
|
||||
}
|
||||
timeout = 0;
|
||||
} else {
|
||||
timeout = 0;
|
||||
state = I2C_STATE_START;
|
||||
read_cycle = RESET;
|
||||
#ifdef DEBUG_VERBOES
|
||||
printf("i2c master sends start signal timeout in READ!\n");
|
||||
#endif
|
||||
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:
|
||||
/* address flag set means i2c slave sends ACK */
|
||||
/* wait for address to be acknowledged */
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout < I2C_TIME_OUT) {
|
||||
/* Check for NACK before clearing address flag */
|
||||
if (i2c_flag_get(I2C0, I2C_FLAG_AERR)) {
|
||||
/* NACK received - slave did not acknowledge address */
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
||||
i2c_stop_on_bus(I2C0);
|
||||
#ifdef DEBUG_VERBOES
|
||||
printf("i2c NACK received for address 0x%02X in read!\n", slave_addr);
|
||||
#endif
|
||||
return I2C_RESULT_NACK;
|
||||
}
|
||||
|
||||
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);
|
||||
if ((SET == read_cycle) && (1 == number_of_byte)) {
|
||||
/* send a stop condition to I2C bus */
|
||||
i2c_stop_on_bus(I2C0);
|
||||
}
|
||||
timeout = 0;
|
||||
state = I2C_STATE_TRANSMIT_DATA;
|
||||
} else {
|
||||
timeout = 0;
|
||||
state = I2C_STATE_START;
|
||||
read_cycle = RESET;
|
||||
#ifdef DEBUG_VERBOES
|
||||
printf("i2c master clears address flag timeout in READ!\n");
|
||||
#endif
|
||||
/* READ phase for 2 bytes: set POS=NEXT and disable ACK BEFORE clearing ADDR */
|
||||
i2c_ackpos_config(I2C0, I2C_ACKPOS_NEXT);
|
||||
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
||||
|
||||
/* now clear address flag to release SCL and enter data phase */
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||
|
||||
state = I2C_STATE_RECEIVE_DATA;
|
||||
}
|
||||
|
||||
timeout = 0;
|
||||
break;
|
||||
|
||||
case I2C_STATE_TRANSMIT_DATA:
|
||||
if (RESET == read_cycle) {
|
||||
/* 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) {
|
||||
/* send the EEPROM's internal address to write to : only one byte address */
|
||||
i2c_data_transmit(I2C0, reg_addr);
|
||||
timeout = 0;
|
||||
} else {
|
||||
timeout = 0;
|
||||
state = I2C_STATE_START;
|
||||
read_cycle = RESET;
|
||||
#ifdef DEBUG_VERBOES
|
||||
printf("i2c master wait data buffer is empty timeout in READ!\n");
|
||||
#endif
|
||||
}
|
||||
/* wait until BTC bit is set */
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout < I2C_TIME_OUT) {
|
||||
timeout = 0;
|
||||
state = I2C_STATE_START;
|
||||
read_cycle = SET;
|
||||
} else {
|
||||
timeout = 0;
|
||||
state = I2C_STATE_START;
|
||||
read_cycle = RESET;
|
||||
#ifdef DEBUG_VERBOES
|
||||
printf("i2c master sends register address timeout in READ!\n");
|
||||
#endif
|
||||
}
|
||||
} else {
|
||||
while (number_of_byte) {
|
||||
timeout++;
|
||||
if (2 == number_of_byte) {
|
||||
/* wait until BTC bit is set */
|
||||
while (!i2c_flag_get(I2C0, I2C_FLAG_BTC));
|
||||
/* send a stop condition to I2C bus */
|
||||
i2c_stop_on_bus(I2C0);
|
||||
}
|
||||
/* wait until RBNE bit is set */
|
||||
if (i2c_flag_get(I2C0, I2C_FLAG_RBNE)) {
|
||||
/* read a byte from the EEPROM */
|
||||
*data = i2c_data_receive(I2C0);
|
||||
/* point to the next location where the byte read will be saved */
|
||||
data++;
|
||||
/* decrement the read bytes counter */
|
||||
number_of_byte--;
|
||||
timeout = 0;
|
||||
}
|
||||
if (timeout > I2C_TIME_OUT) {
|
||||
timeout = 0;
|
||||
state = I2C_STATE_START;
|
||||
read_cycle = 0;
|
||||
#ifdef DEBUG_VERBOES
|
||||
printf("i2c master sends data timeout in READ!\n");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
timeout = 0;
|
||||
state = I2C_STATE_STOP;
|
||||
/* 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 BTC: Bit 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 (R/W bit is set by library) */
|
||||
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:
|
||||
/* Wait for BTC (both bytes received) */
|
||||
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;
|
||||
break;
|
||||
|
||||
case I2C_STATE_STOP:
|
||||
/* i2c master sends STOP signal successfully */
|
||||
/* wait for stop condition to complete */
|
||||
while ((I2C_CTL0(I2C0) & I2C_CTL0_STOP) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout < I2C_TIME_OUT) {
|
||||
timeout = 0;
|
||||
state = I2C_STATE_END;
|
||||
i2c_timeout_flag = I2C_OK;
|
||||
} else {
|
||||
timeout = 0;
|
||||
state = I2C_STATE_START;
|
||||
read_cycle = 0;
|
||||
#ifdef DEBUG_VERBOES
|
||||
printf("i2c master sends stop signal timeout in READ!\n");
|
||||
#endif
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
/* i2c master sends STOP signal successfully */
|
||||
/* success */
|
||||
return I2C_RESULT_SUCCESS;
|
||||
|
||||
case I2C_STATE_ERROR:
|
||||
/* send stop condition to release bus */
|
||||
i2c_stop_on_bus(I2C0);
|
||||
|
||||
retry_count++;
|
||||
if (retry_count >= I2C_MAX_RETRY) {
|
||||
#ifdef DEBUG_VERBOES
|
||||
printf("IIC read failed after %d retries\n", I2C_RETRY_MAX);
|
||||
#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;
|
||||
read_cycle = 0;
|
||||
i2c_timeout_flag = I2C_OK;
|
||||
timeout = 0;
|
||||
#ifdef DEBUG_VERBOES
|
||||
printf("i2c master sends start signal in READ.\n");
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
}
|
||||
return I2C_RESULT_SUCCESS;
|
||||
return I2C_RESULT_TIMEOUT;
|
||||
}
|
||||
|
||||
#ifdef DEBUG_VERBOSE
|
||||
|
295
Src/ldc1612.c
295
Src/ldc1612.c
@@ -1,295 +0,0 @@
|
||||
//
|
||||
// Created by dell on 24-12-3.
|
||||
//
|
||||
|
||||
#include "ldc1612.h"
|
||||
|
||||
/** @brief set conversion interval time.
|
||||
@param channel LDC1612 has total two channels.
|
||||
@param result The value to be set.
|
||||
* */
|
||||
void ldc1612_set_conversion_time(uint8_t channel, uint16_t result) {
|
||||
uint8_t data[2] = {0};
|
||||
data[0] = (result >> 8) & 0xFF;
|
||||
data[1] = result & 0xFF;
|
||||
|
||||
#ifdef SOFTWARE_IIC
|
||||
soft_i2c_write_16bits(LDC1612_ADDR, SET_CONVERSION_TIME_REG_START + channel, data);
|
||||
#else
|
||||
i2c_write_16bits(LDC1612_ADDR, SET_CONVERSION_TIME_REG_START + channel, data);
|
||||
#endif
|
||||
}
|
||||
|
||||
/** @brief set conversion offset.
|
||||
@param channel LDC1612 has total two channels.
|
||||
@param result The value to be set.
|
||||
* */
|
||||
void ldc1612_set_conversion_offset(uint8_t channel, uint16_t result) {
|
||||
uint8_t data[2] = {0};
|
||||
data[0] = (result >> 8) & 0xFF;
|
||||
data[1] = result & 0xFF;
|
||||
#ifdef SOFTWARE_IIC
|
||||
soft_i2c_write_16bits(LDC1612_ADDR, SET_CONVERSION_OFFSET_REG_START + channel, data);
|
||||
#else
|
||||
i2c_write_16bits(LDC1612_ADDR, SET_CONVERSION_OFFSET_REG_START + channel, data);
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
/** @brief Before conversion,wait LC sensor stabilize for a short time.
|
||||
@param channel LDC1612 has total two channels.
|
||||
@param result The value to be set.
|
||||
* */
|
||||
void ldc1612_set_LC_stabilize_time(uint8_t channel, uint16_t result) {
|
||||
uint8_t data[2] = {0};
|
||||
data[0] = (result >> 8) & 0xFF;
|
||||
data[1] = result & 0xFF;
|
||||
#ifdef SOFTWARE_IIC
|
||||
soft_i2c_write_16bits(LDC1612_ADDR, SET_LC_STABILIZE_REG_START + channel, data);
|
||||
#else
|
||||
i2c_write_16bits(LDC1612_ADDR, SET_LC_STABILIZE_REG_START + channel, data);
|
||||
#endif
|
||||
}
|
||||
|
||||
/** @brief set input frequency divide and fref divide.
|
||||
@param channel LDC1612 has total two channels.
|
||||
@param FIN_DIV FIN input divide
|
||||
@param FREF_DIV fref,reference frequency of sensor.
|
||||
* */
|
||||
void ldc1612_set_freq_divide(uint8_t channel) {
|
||||
uint16_t value;
|
||||
uint16_t fin_div, freq_div;
|
||||
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;
|
||||
} else {
|
||||
freq_div = 4;
|
||||
}
|
||||
|
||||
value = fin_div << 12;
|
||||
value |= freq_div;
|
||||
// printf("\tvalue: 0x%x\r\n", value);
|
||||
|
||||
uint8_t data[2] = {0};
|
||||
data[0] = (value >> 8) & 0xFF;
|
||||
data[1] = value & 0xFF;
|
||||
// printf("\tFIN_DIV: %d, FREF_DIV: %d\r\n", fin_div, freq_div);
|
||||
#ifdef SOFTWARE_IIC
|
||||
soft_i2c_write_16bits(LDC1612_ADDR, SET_FREQ_REG_START + channel, data);
|
||||
#else
|
||||
i2c_write_16bits(LDC1612_ADDR, SET_FREQ_REG_START + channel, data);
|
||||
#endif
|
||||
}
|
||||
|
||||
/** @brief Error output config.
|
||||
@param result The value to be set.
|
||||
* */
|
||||
void ldc1612_set_error_config(uint16_t value) {
|
||||
uint8_t data[2] = {0};
|
||||
data[0] = (value >> 8) & 0xFF;
|
||||
data[1] = value & 0xFF;
|
||||
|
||||
#ifdef SOFTWARE_IIC
|
||||
soft_i2c_write_16bits(LDC1612_ADDR, ERROR_CONFIG_REG, data);
|
||||
#else
|
||||
i2c_write_16bits(LDC1612_ADDR, ERROR_CONFIG_REG, data);
|
||||
#endif
|
||||
}
|
||||
|
||||
/** @brief mux config.
|
||||
@param result The value to be set.
|
||||
* */
|
||||
void ldc1612_set_mux_config(uint16_t value) {
|
||||
uint8_t data[2] = {0};
|
||||
data[0] = (value >> 8) & 0xFF;
|
||||
data[1] = value & 0xFF;
|
||||
|
||||
#ifdef SOFTWARE_IIC
|
||||
soft_i2c_write_16bits(LDC1612_ADDR, MUL_CONFIG_REG, data);
|
||||
#else
|
||||
i2c_write_16bits(LDC1612_ADDR, MUL_CONFIG_REG, data);
|
||||
#endif
|
||||
}
|
||||
|
||||
/** @brief reset sensor.
|
||||
|
||||
* */
|
||||
void ldc1612_reset_sensor(void) {
|
||||
uint8_t data[2] = {0};
|
||||
data[0] = 0x80;
|
||||
data[1] = 0x00;
|
||||
|
||||
#ifdef SOFTWARE_IIC
|
||||
soft_i2c_write_16bits(LDC1612_ADDR, SENSOR_RESET_REG, data);
|
||||
#else
|
||||
i2c_write_16bits(LDC1612_ADDR, SENSOR_RESET_REG, data);
|
||||
#endif
|
||||
}
|
||||
|
||||
/** @brief set drive current of sensor.
|
||||
@param result The value to be set.
|
||||
* */
|
||||
void ldc1612_set_drive_current(uint8_t channel, uint16_t value) {
|
||||
uint8_t data[2] = {0};
|
||||
data[0] = (value >> 8) & 0xFF;
|
||||
data[1] = value & 0xFF;
|
||||
|
||||
#ifdef SOFTWARE_IIC
|
||||
soft_i2c_write_16bits(LDC1612_ADDR, SET_DRIVER_CURRENT_REG + channel, data);
|
||||
#else
|
||||
i2c_write_16bits(LDC1612_ADDR, SET_DRIVER_CURRENT_REG + channel, data);
|
||||
#endif
|
||||
}
|
||||
|
||||
/** @brief Main config part of sensor.Contains select channel、start conversion、sleep mode、sensor activation mode、INT pin disable ..
|
||||
@param result The value to be set.
|
||||
* */
|
||||
void ldc1612_set_sensor_config(uint16_t value) {
|
||||
uint8_t data[2] = {0};
|
||||
data[0] = (value >> 8) & 0xFF;
|
||||
data[1] = value & 0xFF;
|
||||
|
||||
#ifdef SOFTWARE_IIC
|
||||
soft_i2c_write_16bits(LDC1612_ADDR, SENSOR_CONFIG_REG, data);
|
||||
#else
|
||||
i2c_write_16bits(LDC1612_ADDR, SENSOR_CONFIG_REG, data);
|
||||
#endif
|
||||
}
|
||||
|
||||
void ldc1612_single_ch0_config(void) {
|
||||
ldc1612_set_freq_divide(CHANNEL_0); //0x14 --0x1002
|
||||
|
||||
ldc1612_set_LC_stabilize_time(CHANNEL_0, LC_STABILIZE_TIME_CH0); //0x10 --0x001E
|
||||
|
||||
ldc1612_set_conversion_time(CHANNEL_0, LDC1612_CONVERSION_TIME_CH0); //0x08 --0x0546
|
||||
|
||||
ldc1612_set_error_config(LDC1612_ERROR_CONFIG); //0x19 --0x0000)
|
||||
|
||||
ldc1612_set_drive_current(CHANNEL_0, LDC1612_DRIVE_CURRENT); //0x1E --0x9000
|
||||
|
||||
ldc1612_set_mux_config(LDC1612_MUX_CONFIG); //0x1B --0x020C
|
||||
|
||||
ldc1612_set_sensor_config(LDC1612_SENSOR_CONFIG); //0x1A --0x1601
|
||||
}
|
||||
|
||||
void ldc1612_iic_get_sensor_infomation(void) {
|
||||
uint8_t data[2] = {0};
|
||||
|
||||
#ifdef SOFTWARE_IIC
|
||||
soft_i2c_read_16bits(LDC1612_ADDR, READ_MANUFACTURER_ID, data);
|
||||
#else
|
||||
i2c_read_16bits(LDC1612_ADDR, READ_MANUFACTURER_ID, data);
|
||||
#endif
|
||||
printf("\tManufacturer: 0x%x", (data[0] << 8) | data[1]);
|
||||
|
||||
#ifdef SOFTWARE_IIC
|
||||
soft_i2c_read_16bits(LDC1612_ADDR, READ_DEVICE_ID, data);
|
||||
#else
|
||||
i2c_read_16bits(LDC1612_ADDR, READ_DEVICE_ID, data);
|
||||
#endif
|
||||
printf("\tDevice: 0x%x", (data[0] << 8) | data[1]);
|
||||
}
|
||||
|
||||
uint16_t ldc1612_get_manufacturer_id(void) {
|
||||
uint8_t data[2] = {0};
|
||||
|
||||
#ifdef SOFTWARE_IIC
|
||||
soft_i2c_read_16bits(LDC1612_ADDR, READ_MANUFACTURER_ID, data);
|
||||
#else
|
||||
i2c_read_16bits(LDC1612_ADDR, READ_MANUFACTURER_ID, data);
|
||||
#endif
|
||||
return (data[0] << 8) | data[1];
|
||||
}
|
||||
|
||||
uint16_t ldc1612_get_deveice_id(void) {
|
||||
uint8_t data[2] = {0};
|
||||
|
||||
#ifdef SOFTWARE_IIC
|
||||
soft_i2c_read_16bits(LDC1612_ADDR, READ_DEVICE_ID, data);
|
||||
#else
|
||||
i2c_read_16bits(LDC1612_ADDR, READ_DEVICE_ID, data);
|
||||
#endif
|
||||
return (data[0] << 8) | data[1];
|
||||
}
|
||||
|
||||
/** @brief read the raw channel result from register.
|
||||
@param channel LDC1612 has total two channels.
|
||||
@param result raw data
|
||||
* */
|
||||
uint32_t ldc1612_get_raw_channel_result(uint8_t channel) {
|
||||
uint32_t raw_value = 0;
|
||||
uint8_t value[2] = {0};
|
||||
|
||||
#ifdef SOFTWARE_IIC
|
||||
soft_i2c_read_16bits(LDC1612_ADDR, CONVERTION_RESULT_REG_START + channel, value);
|
||||
#else
|
||||
i2c_read_16bits(LDC1612_ADDR, CONVERTION_RESULT_REG_START + channel, value);
|
||||
#endif
|
||||
raw_value |= (uint32_t) ((value[0] << 8) | value[1]) << 16;
|
||||
|
||||
#ifdef SOFTWARE_IIC
|
||||
soft_i2c_read_16bits(LDC1612_ADDR, CONVERTION_RESULT_REG_START + channel + 1, value);
|
||||
#else
|
||||
i2c_read_16bits(LDC1612_ADDR, CONVERTION_RESULT_REG_START + channel + 1, value);
|
||||
#endif
|
||||
raw_value |= (uint32_t) ((value[0] << 8) | value[1]);
|
||||
return ldc1612_parse_raw_result(raw_value);
|
||||
}
|
||||
|
||||
/** @brief parse the data which read from data register.
|
||||
@param channel LDC1612 has total two channels.
|
||||
@param raw_result the raw data which read from data register,it contains error codes and sensor value;
|
||||
* */
|
||||
uint32_t ldc1612_parse_raw_result(uint32_t raw_result) {
|
||||
uint32_t calibration_value = 0;
|
||||
uint8_t error_code = 0;
|
||||
|
||||
calibration_value = raw_result & 0x0FFFFFFF;
|
||||
if (0xFFFFFFF == calibration_value) {
|
||||
return 0xF0000000;
|
||||
// ERR_NC-No coil detected!!!
|
||||
}
|
||||
|
||||
error_code = raw_result >> 24;
|
||||
|
||||
if (error_code & 0x80) {
|
||||
return 0x80000000;
|
||||
// ERR_UR-Under range error!!!
|
||||
}
|
||||
if (error_code & 0x40) {
|
||||
return 0x40000000;
|
||||
// ERR_OR-Over range error!!!
|
||||
}
|
||||
if (error_code & 0x20) {
|
||||
return 0x20000000;
|
||||
// ERR_WD-Watch dog timeout error!!!
|
||||
}
|
||||
if (error_code & 0x10) {
|
||||
return 0x10000000;
|
||||
// ERR_AE-error!!!
|
||||
}
|
||||
|
||||
return raw_result;
|
||||
}
|
||||
|
||||
void ldc1612_drvie_current_detect(uint8_t channel) {
|
||||
uint8_t data[2] = {0};
|
||||
uint16_t init_value = 0 , drive_current = 0;
|
||||
|
||||
ldc1612_set_sensor_config(LDC1612_SLEEP_MODE);
|
||||
ldc1612_set_freq_divide(channel);
|
||||
soft_i2c_read_16bits(LDC1612_ADDR, SENSOR_CONFIG_REG, data);
|
||||
ldc1612_set_sensor_config(LDC1612_SLEEP_MODE);
|
||||
ldc1612_set_sensor_config(LDC1612_SENSOR_CONFIG); //0x1A --0x1601
|
||||
delay_ms(10);
|
||||
soft_i2c_read_16bits(LDC1612_ADDR, SET_DRIVER_CURRENT_REG, data);
|
||||
|
||||
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);
|
||||
}
|
57
Src/main.c
57
Src/main.c
@@ -40,9 +40,6 @@ OF SUCH DAMAGE.
|
||||
#include <stdio.h>
|
||||
#include "i2c.h"
|
||||
#include "board_config.h"
|
||||
#include "ldc1612.h"
|
||||
|
||||
bool g_status_switch = false;
|
||||
|
||||
/*!
|
||||
\brief main function
|
||||
@@ -81,61 +78,9 @@ int main(void)
|
||||
i2c_bus_reset();
|
||||
#endif
|
||||
|
||||
// i2c_scan();
|
||||
|
||||
uint8_t ldc_data[2] = {0};
|
||||
|
||||
i2c_result_t i2c_result = i2c_read_16bits(LDC1612_ADDR, READ_MANUFACTURER_ID, ldc_data);
|
||||
|
||||
// const char* i2c_string = i2c_get_status_string(i2c_result);
|
||||
|
||||
// const char* msg1 = "I2C Status: ";
|
||||
// for (uint8_t i = 0; msg1[i] != '\0'; i++) {
|
||||
// while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
|
||||
// usart_data_transmit(RS485_PHY, msg1[i]);
|
||||
// }
|
||||
|
||||
// // 发送i2c_string内容
|
||||
// for (uint8_t i = 0; i2c_string[i] != '\0'; i++) {
|
||||
// while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
|
||||
// usart_data_transmit(RS485_PHY, i2c_string[i]);
|
||||
// }
|
||||
|
||||
// // 发送换行符
|
||||
// const char* newline1 = "\r\n";
|
||||
// for (uint8_t i = 0; newline1[i] != '\0'; i++) {
|
||||
// while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
|
||||
// usart_data_transmit(RS485_PHY, newline1[i]);
|
||||
// }
|
||||
|
||||
// 第二句:发送 "LDC1612 Manufacturer ID: 0x" + 十六进制数值 + "\r\n"
|
||||
const char* msg2 = "LDC1612 Manufacturer ID: 0x";
|
||||
for (uint8_t i = 0; msg2[i] != '\0'; i++) {
|
||||
while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
|
||||
usart_data_transmit(RS485_PHY, msg2[i]);
|
||||
}
|
||||
|
||||
// 发送十六进制数值
|
||||
uint16_t manufacturer_id = (ldc_data[0] << 8) | ldc_data[1];
|
||||
uint8_t hex_chars[] = "0123456789ABCDEF";
|
||||
for (int8_t i = 3; i >= 0; i--) {
|
||||
uint8_t nibble = (manufacturer_id >> (i * 4)) & 0x0F;
|
||||
while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
|
||||
usart_data_transmit(RS485_PHY, hex_chars[nibble]);
|
||||
}
|
||||
|
||||
// 发送换行符
|
||||
const char* newline2 = "\r\n";
|
||||
for (uint8_t i = 0; newline2[i] != '\0'; i++) {
|
||||
while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
|
||||
usart_data_transmit(RS485_PHY, newline2[i]);
|
||||
}
|
||||
|
||||
// 等待所有数据发送完成
|
||||
while (usart_flag_get(RS485_PHY, USART_FLAG_TC) == RESET) {}
|
||||
|
||||
while(1){
|
||||
command_process();
|
||||
delay_ms(100);
|
||||
delay_ms(10);
|
||||
}
|
||||
}
|
||||
|
@@ -1,224 +0,0 @@
|
||||
//
|
||||
// Sensor Usage Example
|
||||
// 传感器使用示例代码
|
||||
//
|
||||
|
||||
#include "ldc1612.h"
|
||||
// #include "tmp112.h"
|
||||
#include "i2c.h"
|
||||
|
||||
/*!
|
||||
\brief 传感器初始化示例
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void sensors_init_example(void) {
|
||||
ldc1612_status_t ldc_status;
|
||||
// tmp112a_status_t tmp_status;
|
||||
|
||||
/* 初始化I2C总线 */
|
||||
i2c_status_t i2c_status = i2c_config();
|
||||
if (i2c_status != I2C_STATUS_SUCCESS) {
|
||||
// 使用串口发送错误信息
|
||||
const char* error_msg = "I2C init failed\r\n";
|
||||
for (uint8_t i = 0; error_msg[i] != '\0'; i++) {
|
||||
while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
|
||||
usart_data_transmit(RS485_PHY, error_msg[i]);
|
||||
}
|
||||
while (usart_flag_get(RS485_PHY, USART_FLAG_TC) == RESET) {}
|
||||
return;
|
||||
}
|
||||
|
||||
/* 扫描I2C总线 */
|
||||
// i2c_scan();
|
||||
|
||||
/* 初始化LDC1612 */
|
||||
ldc_status = ldc1612_init();
|
||||
if (ldc_status == LDC1612_STATUS_SUCCESS) {
|
||||
const char* msg = "LDC1612 init success\r\n";
|
||||
for (uint8_t i = 0; msg[i] != '\0'; i++) {
|
||||
while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
|
||||
usart_data_transmit(RS485_PHY, msg[i]);
|
||||
}
|
||||
while (usart_flag_get(RS485_PHY, USART_FLAG_TC) == RESET) {}
|
||||
|
||||
/* 配置通道0 */
|
||||
ldc_status = ldc1612_config_single_channel(LDC1612_CHANNEL_0);
|
||||
if (ldc_status != LDC1612_STATUS_SUCCESS) {
|
||||
const char* error = "LDC1612 config failed\r\n";
|
||||
for (uint8_t i = 0; error[i] != '\0'; i++) {
|
||||
while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
|
||||
usart_data_transmit(RS485_PHY, error[i]);
|
||||
}
|
||||
while (usart_flag_get(RS485_PHY, USART_FLAG_TC) == RESET) {}
|
||||
}
|
||||
} else {
|
||||
const char* error = "LDC1612 init failed: ";
|
||||
for (uint8_t i = 0; error[i] != '\0'; i++) {
|
||||
while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
|
||||
usart_data_transmit(RS485_PHY, error[i]);
|
||||
}
|
||||
const char* status_str = ldc1612_get_status_string(ldc_status);
|
||||
for (uint8_t i = 0; status_str[i] != '\0'; i++) {
|
||||
while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
|
||||
usart_data_transmit(RS485_PHY, status_str[i]);
|
||||
}
|
||||
const char* newline = "\r\n";
|
||||
for (uint8_t i = 0; newline[i] != '\0'; i++) {
|
||||
while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
|
||||
usart_data_transmit(RS485_PHY, newline[i]);
|
||||
}
|
||||
while (usart_flag_get(RS485_PHY, USART_FLAG_TC) == RESET) {}
|
||||
}
|
||||
|
||||
/* 初始化TMP112A */
|
||||
// tmp_status = tmp112a_init();
|
||||
// if (tmp_status == TMP112A_STATUS_SUCCESS) {
|
||||
// const char* msg = "TMP112A init success\r\n";
|
||||
// for (uint8_t i = 0; msg[i] != '\0'; i++) {
|
||||
// while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
|
||||
// usart_data_transmit(RS485_PHY, msg[i]);
|
||||
// }
|
||||
// while (usart_flag_get(RS485_PHY, USART_FLAG_TC) == RESET) {}
|
||||
|
||||
// /* 设置温度阈值 */
|
||||
// tmp_status = tmp112a_set_thresholds(-10.0f, 50.0f);
|
||||
// if (tmp_status != TMP112A_STATUS_SUCCESS) {
|
||||
// const char* error = "TMP112A threshold config failed\r\n";
|
||||
// for (uint8_t i = 0; error[i] != '\0'; i++) {
|
||||
// while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
|
||||
// usart_data_transmit(RS485_PHY, error[i]);
|
||||
// }
|
||||
// while (usart_flag_get(RS485_PHY, USART_FLAG_TC) == RESET) {}
|
||||
// }
|
||||
// } else {
|
||||
// const char* error = "TMP112A init failed: ";
|
||||
// for (uint8_t i = 0; error[i] != '\0'; i++) {
|
||||
// while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
|
||||
// usart_data_transmit(RS485_PHY, error[i]);
|
||||
// }
|
||||
// const char* status_str = tmp112a_get_status_string(tmp_status);
|
||||
// for (uint8_t i = 0; status_str[i] != '\0'; i++) {
|
||||
// while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
|
||||
// usart_data_transmit(RS485_PHY, status_str[i]);
|
||||
// }
|
||||
// const char* newline = "\r\n";
|
||||
// for (uint8_t i = 0; newline[i] != '\0'; i++) {
|
||||
// while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
|
||||
// usart_data_transmit(RS485_PHY, newline[i]);
|
||||
// }
|
||||
// while (usart_flag_get(RS485_PHY, USART_FLAG_TC) == RESET) {}
|
||||
// }
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief 传感器读取示例
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void sensors_read_example(void) {
|
||||
ldc1612_result_t ldc_result;
|
||||
// tmp112a_result_t tmp_result;
|
||||
ldc1612_status_t ldc_status;
|
||||
// tmp112a_status_t tmp_status;
|
||||
|
||||
/* 读取LDC1612数据 */
|
||||
ldc_status = ldc1612_read_channel(LDC1612_CHANNEL_0, &ldc_result);
|
||||
if (ldc_status == LDC1612_STATUS_SUCCESS) {
|
||||
if (!ldc_result.error_flag) {
|
||||
const char* msg = "LDC1612 Data: 0x";
|
||||
for (uint8_t i = 0; msg[i] != '\0'; i++) {
|
||||
while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
|
||||
usart_data_transmit(RS485_PHY, msg[i]);
|
||||
}
|
||||
|
||||
/* 发送32位十六进制数据 */
|
||||
uint8_t hex_chars[] = "0123456789ABCDEF";
|
||||
for (int8_t i = 7; i >= 0; i--) {
|
||||
uint8_t nibble = (ldc_result.frequency >> (i * 4)) & 0x0F;
|
||||
while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
|
||||
usart_data_transmit(RS485_PHY, hex_chars[nibble]);
|
||||
}
|
||||
|
||||
const char* newline = "\r\n";
|
||||
for (uint8_t i = 0; newline[i] != '\0'; i++) {
|
||||
while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
|
||||
usart_data_transmit(RS485_PHY, newline[i]);
|
||||
}
|
||||
while (usart_flag_get(RS485_PHY, USART_FLAG_TC) == RESET) {}
|
||||
} else {
|
||||
const char* error = "LDC1612 Error Code: 0x";
|
||||
for (uint8_t i = 0; error[i] != '\0'; i++) {
|
||||
while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
|
||||
usart_data_transmit(RS485_PHY, error[i]);
|
||||
}
|
||||
|
||||
uint8_t hex_chars[] = "0123456789ABCDEF";
|
||||
while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
|
||||
usart_data_transmit(RS485_PHY, hex_chars[(ldc_result.error_code >> 4) & 0x0F]);
|
||||
while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
|
||||
usart_data_transmit(RS485_PHY, hex_chars[ldc_result.error_code & 0x0F]);
|
||||
|
||||
const char* newline = "\r\n";
|
||||
for (uint8_t i = 0; newline[i] != '\0'; i++) {
|
||||
while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
|
||||
usart_data_transmit(RS485_PHY, newline[i]);
|
||||
}
|
||||
while (usart_flag_get(RS485_PHY, USART_FLAG_TC) == RESET) {}
|
||||
}
|
||||
}
|
||||
|
||||
/* 读取TMP112A数据 */
|
||||
// tmp_status = tmp112a_read_temperature(&tmp_result);
|
||||
// if (tmp_status == TMP112A_STATUS_SUCCESS) {
|
||||
// const char* msg = "Temperature: ";
|
||||
// for (uint8_t i = 0; msg[i] != '\0'; i++) {
|
||||
// while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
|
||||
// usart_data_transmit(RS485_PHY, msg[i]);
|
||||
// }
|
||||
|
||||
// /* 简单的温度显示(整数部分) */
|
||||
// int16_t temp_int = (int16_t)tmp_result.temperature_c;
|
||||
// if (temp_int < 0) {
|
||||
// while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
|
||||
// usart_data_transmit(RS485_PHY, '-');
|
||||
// temp_int = -temp_int;
|
||||
// }
|
||||
|
||||
// if (temp_int >= 100) {
|
||||
// while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
|
||||
// usart_data_transmit(RS485_PHY, '0' + (temp_int / 100));
|
||||
// temp_int %= 100;
|
||||
// }
|
||||
// if (temp_int >= 10) {
|
||||
// while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
|
||||
// usart_data_transmit(RS485_PHY, '0' + (temp_int / 10));
|
||||
// temp_int %= 10;
|
||||
// }
|
||||
// while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
|
||||
// usart_data_transmit(RS485_PHY, '0' + temp_int);
|
||||
|
||||
// const char* unit = " C";
|
||||
// for (uint8_t i = 0; unit[i] != '\0'; i++) {
|
||||
// while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
|
||||
// usart_data_transmit(RS485_PHY, unit[i]);
|
||||
// }
|
||||
|
||||
// if (tmp_result.alert_flag) {
|
||||
// const char* alert = " [ALERT]";
|
||||
// for (uint8_t i = 0; alert[i] != '\0'; i++) {
|
||||
// while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
|
||||
// usart_data_transmit(RS485_PHY, alert[i]);
|
||||
// }
|
||||
// }
|
||||
|
||||
// const char* newline = "\r\n";
|
||||
// for (uint8_t i = 0; newline[i] != '\0'; i++) {
|
||||
// while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
|
||||
// usart_data_transmit(RS485_PHY, newline[i]);
|
||||
// }
|
||||
// while (usart_flag_get(RS485_PHY, USART_FLAG_TC) == RESET) {}
|
||||
// }
|
||||
}
|
323
Src/tmp112.c
323
Src/tmp112.c
@@ -1,323 +0,0 @@
|
||||
//
|
||||
// Created by dell on 24-12-20.
|
||||
// TMP112A Temperature Sensor Driver Implementation
|
||||
//
|
||||
|
||||
#include "tmp112.h"
|
||||
|
||||
/* Private function prototypes */
|
||||
static i2c_status_t tmp112a_write_register(uint8_t reg_addr, uint16_t value);
|
||||
static i2c_status_t tmp112a_read_register(uint8_t reg_addr, uint16_t *value);
|
||||
static float tmp112a_raw_to_celsius(uint16_t raw_data);
|
||||
static uint16_t tmp112a_celsius_to_raw(float temperature);
|
||||
|
||||
/*!
|
||||
\brief 初始化TMP112A传感器
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval tmp112a_status_t
|
||||
*/
|
||||
tmp112a_status_t tmp112a_init(void) {
|
||||
i2c_status_t i2c_status;
|
||||
|
||||
/* 配置传感器为默认设置 */
|
||||
i2c_status = tmp112a_config(TMP112A_CONFIG_DEFAULT);
|
||||
if (i2c_status != I2C_STATUS_SUCCESS) {
|
||||
return TMP112A_STATUS_ERROR;
|
||||
}
|
||||
|
||||
/* 等待配置生效 */
|
||||
delay_ms(1);
|
||||
|
||||
return TMP112A_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief 配置TMP112A传感器
|
||||
\param[in] config: 配置值
|
||||
\param[out] none
|
||||
\retval tmp112a_status_t
|
||||
*/
|
||||
tmp112a_status_t tmp112a_config(uint16_t config) {
|
||||
i2c_status_t status = tmp112a_write_register(TMP112A_CONFIG_REG, config);
|
||||
return (status == I2C_STATUS_SUCCESS) ? TMP112A_STATUS_SUCCESS : TMP112A_STATUS_ERROR;
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief 读取温度
|
||||
\param[in] none
|
||||
\param[out] result: 结果结构体指针
|
||||
\retval tmp112a_status_t
|
||||
*/
|
||||
tmp112a_status_t tmp112a_read_temperature(tmp112a_result_t *result) {
|
||||
uint16_t raw_data;
|
||||
i2c_status_t status;
|
||||
|
||||
if (result == NULL) {
|
||||
return TMP112A_STATUS_INVALID_PARAM;
|
||||
}
|
||||
|
||||
/* 读取温度寄存器 */
|
||||
status = tmp112a_read_register(TMP112A_TEMP_REG, &raw_data);
|
||||
if (status != I2C_STATUS_SUCCESS) {
|
||||
return TMP112A_STATUS_ERROR;
|
||||
}
|
||||
|
||||
/* 解析温度数据 */
|
||||
result->raw_data = raw_data;
|
||||
result->temperature_c = tmp112a_raw_to_celsius(raw_data);
|
||||
result->temperature_f = result->temperature_c * 9.0f / 5.0f + 32.0f;
|
||||
|
||||
/* 检查温度范围 */
|
||||
if (result->temperature_c < TMP112A_TEMP_MIN || result->temperature_c > TMP112A_TEMP_MAX) {
|
||||
return TMP112A_STATUS_OUT_OF_RANGE;
|
||||
}
|
||||
|
||||
/* 检查报警标志 */
|
||||
uint16_t config_reg;
|
||||
status = tmp112a_read_register(TMP112A_CONFIG_REG, &config_reg);
|
||||
if (status == I2C_STATUS_SUCCESS) {
|
||||
result->alert_flag = (config_reg & TMP112A_CONFIG_AL) ? true : false;
|
||||
} else {
|
||||
result->alert_flag = false;
|
||||
}
|
||||
|
||||
return TMP112A_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief 设置温度阈值
|
||||
\param[in] low_temp: 低温阈值 (°C)
|
||||
\param[in] high_temp: 高温阈值 (°C)
|
||||
\param[out] none
|
||||
\retval tmp112a_status_t
|
||||
*/
|
||||
tmp112a_status_t tmp112a_set_thresholds(float low_temp, float high_temp) {
|
||||
uint16_t low_raw, high_raw;
|
||||
i2c_status_t status;
|
||||
|
||||
/* 参数验证 */
|
||||
if (low_temp < TMP112A_TEMP_MIN || low_temp > TMP112A_TEMP_MAX ||
|
||||
high_temp < TMP112A_TEMP_MIN || high_temp > TMP112A_TEMP_MAX ||
|
||||
low_temp >= high_temp) {
|
||||
return TMP112A_STATUS_INVALID_PARAM;
|
||||
}
|
||||
|
||||
/* 转换温度为原始值 */
|
||||
low_raw = tmp112a_celsius_to_raw(low_temp);
|
||||
high_raw = tmp112a_celsius_to_raw(high_temp);
|
||||
|
||||
/* 写入低温阈值 */
|
||||
status = tmp112a_write_register(TMP112A_TLOW_REG, low_raw);
|
||||
if (status != I2C_STATUS_SUCCESS) {
|
||||
return TMP112A_STATUS_ERROR;
|
||||
}
|
||||
|
||||
/* 写入高温阈值 */
|
||||
status = tmp112a_write_register(TMP112A_THIGH_REG, high_raw);
|
||||
if (status != I2C_STATUS_SUCCESS) {
|
||||
return TMP112A_STATUS_ERROR;
|
||||
}
|
||||
|
||||
return TMP112A_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief 进入关机模式
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval tmp112a_status_t
|
||||
*/
|
||||
tmp112a_status_t tmp112a_shutdown(void) {
|
||||
uint16_t config_reg;
|
||||
i2c_status_t status;
|
||||
|
||||
/* 读取当前配置 */
|
||||
status = tmp112a_read_register(TMP112A_CONFIG_REG, &config_reg);
|
||||
if (status != I2C_STATUS_SUCCESS) {
|
||||
return TMP112A_STATUS_ERROR;
|
||||
}
|
||||
|
||||
/* 设置关机位 */
|
||||
config_reg |= TMP112A_CONFIG_SD;
|
||||
|
||||
/* 写回配置 */
|
||||
status = tmp112a_write_register(TMP112A_CONFIG_REG, config_reg);
|
||||
return (status == I2C_STATUS_SUCCESS) ? TMP112A_STATUS_SUCCESS : TMP112A_STATUS_ERROR;
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief 退出关机模式
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval tmp112a_status_t
|
||||
*/
|
||||
tmp112a_status_t tmp112a_wakeup(void) {
|
||||
uint16_t config_reg;
|
||||
i2c_status_t status;
|
||||
|
||||
/* 读取当前配置 */
|
||||
status = tmp112a_read_register(TMP112A_CONFIG_REG, &config_reg);
|
||||
if (status != I2C_STATUS_SUCCESS) {
|
||||
return TMP112A_STATUS_ERROR;
|
||||
}
|
||||
|
||||
/* 清除关机位 */
|
||||
config_reg &= ~TMP112A_CONFIG_SD;
|
||||
|
||||
/* 写回配置 */
|
||||
status = tmp112a_write_register(TMP112A_CONFIG_REG, config_reg);
|
||||
if (status != I2C_STATUS_SUCCESS) {
|
||||
return TMP112A_STATUS_ERROR;
|
||||
}
|
||||
|
||||
/* 等待传感器启动 */
|
||||
delay_ms(1);
|
||||
|
||||
return TMP112A_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief 单次转换
|
||||
\param[in] none
|
||||
\param[out] result: 结果结构体指针
|
||||
\retval tmp112a_status_t
|
||||
*/
|
||||
tmp112a_status_t tmp112a_one_shot(tmp112a_result_t *result) {
|
||||
uint16_t config_reg;
|
||||
i2c_status_t status;
|
||||
uint8_t timeout = 100; // 100ms超时
|
||||
|
||||
if (result == NULL) {
|
||||
return TMP112A_STATUS_INVALID_PARAM;
|
||||
}
|
||||
|
||||
/* 读取当前配置 */
|
||||
status = tmp112a_read_register(TMP112A_CONFIG_REG, &config_reg);
|
||||
if (status != I2C_STATUS_SUCCESS) {
|
||||
return TMP112A_STATUS_ERROR;
|
||||
}
|
||||
|
||||
/* 启动单次转换 */
|
||||
config_reg |= TMP112A_CONFIG_OS;
|
||||
status = tmp112a_write_register(TMP112A_CONFIG_REG, config_reg);
|
||||
if (status != I2C_STATUS_SUCCESS) {
|
||||
return TMP112A_STATUS_ERROR;
|
||||
}
|
||||
|
||||
/* 等待转换完成 */
|
||||
do {
|
||||
delay_ms(1);
|
||||
status = tmp112a_read_register(TMP112A_CONFIG_REG, &config_reg);
|
||||
if (status != I2C_STATUS_SUCCESS) {
|
||||
return TMP112A_STATUS_ERROR;
|
||||
}
|
||||
timeout--;
|
||||
} while ((config_reg & TMP112A_CONFIG_OS) && timeout > 0);
|
||||
|
||||
if (timeout == 0) {
|
||||
return TMP112A_STATUS_TIMEOUT;
|
||||
}
|
||||
|
||||
/* 读取转换结果 */
|
||||
return tmp112a_read_temperature(result);
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief 获取状态字符串
|
||||
\param[in] status: 状态码
|
||||
\param[out] none
|
||||
\retval const char* 状态字符串
|
||||
*/
|
||||
const char* tmp112a_get_status_string(tmp112a_status_t status) {
|
||||
switch (status) {
|
||||
case TMP112A_STATUS_SUCCESS:
|
||||
return "SUCCESS";
|
||||
case TMP112A_STATUS_ERROR:
|
||||
return "ERROR";
|
||||
case TMP112A_STATUS_TIMEOUT:
|
||||
return "TIMEOUT";
|
||||
case TMP112A_STATUS_INVALID_PARAM:
|
||||
return "INVALID_PARAM";
|
||||
case TMP112A_STATUS_OUT_OF_RANGE:
|
||||
return "OUT_OF_RANGE";
|
||||
default:
|
||||
return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
|
||||
/* Private Functions Implementation */
|
||||
|
||||
/*!
|
||||
\brief 写入寄存器
|
||||
\param[in] reg_addr: 寄存器地址
|
||||
\param[in] value: 写入值
|
||||
\param[out] none
|
||||
\retval i2c_status_t
|
||||
*/
|
||||
static i2c_status_t tmp112a_write_register(uint8_t reg_addr, uint16_t value) {
|
||||
uint8_t data[2];
|
||||
data[0] = (value >> 8) & 0xFF;
|
||||
data[1] = value & 0xFF;
|
||||
|
||||
return i2c_write_16bits(TMP112A_ADDR, reg_addr, data);
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief 读取寄存器
|
||||
\param[in] reg_addr: 寄存器地址
|
||||
\param[out] value: 读取值指针
|
||||
\retval i2c_status_t
|
||||
*/
|
||||
static i2c_status_t tmp112a_read_register(uint8_t reg_addr, uint16_t *value) {
|
||||
uint8_t data[2];
|
||||
i2c_status_t status;
|
||||
|
||||
if (value == NULL) {
|
||||
return I2C_STATUS_INVALID_PARAM;
|
||||
}
|
||||
|
||||
status = i2c_read_16bits(TMP112A_ADDR, reg_addr, data);
|
||||
if (status == I2C_STATUS_SUCCESS) {
|
||||
*value = ((uint16_t)data[0] << 8) | data[1];
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief 将原始数据转换为摄氏度
|
||||
\param[in] raw_data: 原始数据
|
||||
\param[out] none
|
||||
\retval float 温度值(°C)
|
||||
*/
|
||||
static float tmp112a_raw_to_celsius(uint16_t raw_data) {
|
||||
int16_t temp_raw;
|
||||
|
||||
/* TMP112A使用12位分辨率,数据在高12位 */
|
||||
temp_raw = (int16_t)(raw_data >> 4);
|
||||
|
||||
/* 处理负数 */
|
||||
if (temp_raw & 0x800) {
|
||||
temp_raw |= 0xF000; // 符号扩展
|
||||
}
|
||||
|
||||
/* 转换为摄氏度 */
|
||||
return (float)temp_raw * TMP112A_TEMP_RESOLUTION;
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief 将摄氏度转换为原始数据
|
||||
\param[in] temperature: 温度值(°C)
|
||||
\param[out] none
|
||||
\retval uint16_t 原始数据
|
||||
*/
|
||||
static uint16_t tmp112a_celsius_to_raw(float temperature) {
|
||||
int16_t temp_raw;
|
||||
|
||||
/* 转换为原始值 */
|
||||
temp_raw = (int16_t)(temperature / TMP112A_TEMP_RESOLUTION);
|
||||
|
||||
/* 移位到高12位 */
|
||||
return (uint16_t)(temp_raw << 4);
|
||||
}
|
@@ -7,8 +7,8 @@ set(VERSION "V${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
|
||||
string(TIMESTAMP BUILD_DATE "%Y-%m-%d")
|
||||
|
||||
# 编译条件(如IIC类型等)
|
||||
set(IIC_TYPE "AutoDetectDriveCurrent")
|
||||
# set(IIC_TYPE "HW-IIC")
|
||||
# set(IIC_TYPE "AutoDetectDriveCurrent")
|
||||
set(IIC_TYPE "HW-IIC")
|
||||
|
||||
# 其它自定义宏
|
||||
add_definitions(-DIIC_TYPE=${IIC_TYPE})
|
||||
|
978
i2c_wait.c
978
i2c_wait.c
@@ -1,978 +0,0 @@
|
||||
//
|
||||
// Created by dell on 24-12-20.
|
||||
// Improved I2C driver with better state machine and error handling
|
||||
//
|
||||
|
||||
#include "i2c.h"
|
||||
|
||||
/* Private variables */
|
||||
static uint8_t i2c_retry_count = 0;
|
||||
|
||||
/*!
|
||||
\brief write 16-bit data to I2C device with improved state machine
|
||||
\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
|
||||
*/
|
||||
i2c_status_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, const uint8_t data[2]) {
|
||||
i2c_state_t state = I2C_STATE_START;
|
||||
uint16_t timeout = 0;
|
||||
uint8_t data_index = 0;
|
||||
uint8_t retry_count = 0;
|
||||
|
||||
/* Parameter validation */
|
||||
if (data == NULL || slave_addr > 0x7F) {
|
||||
return I2C_STATUS_INVALID_PARAM;
|
||||
}
|
||||
|
||||
/* Enable acknowledge */
|
||||
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
||||
|
||||
while (retry_count < I2C_MAX_RETRY) {
|
||||
switch (state) {
|
||||
case I2C_STATE_START:
|
||||
timeout = 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 with write bit */
|
||||
i2c_master_addressing(I2C0, (slave_addr << 1), I2C_TRANSMITTER);
|
||||
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;
|
||||
}
|
||||
|
||||
/* Clear address flag */
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||
state = I2C_STATE_TRANSMIT_REG;
|
||||
timeout = 0;
|
||||
break;
|
||||
|
||||
case I2C_STATE_TRANSMIT_REG:
|
||||
/* 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_TRANSMIT_DATA;
|
||||
timeout = 0;
|
||||
data_index = 0;
|
||||
break;
|
||||
|
||||
case I2C_STATE_TRANSMIT_DATA:
|
||||
/* 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;
|
||||
}
|
||||
|
||||
/* Send data bytes */
|
||||
if (data_index < 2) {
|
||||
i2c_data_transmit(I2C0, data[data_index]);
|
||||
data_index++;
|
||||
timeout = 0;
|
||||
/* Stay in this state until all data is sent */
|
||||
} else {
|
||||
/* All data sent, proceed to stop */
|
||||
state = I2C_STATE_STOP;
|
||||
timeout = 0;
|
||||
}
|
||||
break;
|
||||
|
||||
case I2C_STATE_STOP:
|
||||
/* Send stop condition */
|
||||
i2c_stop_on_bus(I2C0);
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
/* Success */
|
||||
return I2C_STATUS_SUCCESS;
|
||||
|
||||
case I2C_STATE_ERROR:
|
||||
/* Send stop condition to release bus */
|
||||
i2c_stop_on_bus(I2C0);
|
||||
|
||||
/* Increment retry counter */
|
||||
retry_count++;
|
||||
if (retry_count >= I2C_MAX_RETRY) {
|
||||
#ifdef DEBUG_VERBOSE
|
||||
// printf("I2C write failed after %d retries\r\n", I2C_MAX_RETRY);
|
||||
const char* msg5_prefix = "I2C write failed after ";
|
||||
for (uint8_t i = 0; msg5_prefix[i] != '\0'; i++) {
|
||||
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TBE) == RESET) {}
|
||||
usart_data_transmit(I2C_DEBUG_UART, msg5_prefix[i]);
|
||||
}
|
||||
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TBE) == RESET) {}
|
||||
usart_data_transmit(I2C_DEBUG_UART, '0' + I2C_MAX_RETRY);
|
||||
const char* msg5_suffix = " retries\r\n";
|
||||
for (uint8_t i = 0; msg5_suffix[i] != '\0'; i++) {
|
||||
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TBE) == RESET) {}
|
||||
usart_data_transmit(I2C_DEBUG_UART, msg5_suffix[i]);
|
||||
}
|
||||
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TC) == RESET) {}
|
||||
#endif
|
||||
return I2C_STATUS_TIMEOUT;
|
||||
}
|
||||
|
||||
/* Reset state machine for retry */
|
||||
state = I2C_STATE_START;
|
||||
timeout = 0;
|
||||
data_index = 0;
|
||||
|
||||
/* Small delay before retry */
|
||||
delay_10us(10);
|
||||
break;
|
||||
|
||||
default:
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return I2C_STATUS_TIMEOUT;
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief read 16-bit data from I2C device with improved state machine
|
||||
\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
|
||||
*/
|
||||
i2c_status_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data) {
|
||||
i2c_state_t state = I2C_STATE_START;
|
||||
uint16_t timeout = 0;
|
||||
uint8_t data_index = 0;
|
||||
uint8_t retry_count = 0;
|
||||
bool write_phase = true; /* First phase: write register address */
|
||||
|
||||
/* Parameter validation */
|
||||
if (data == NULL || slave_addr > 0x7F) {
|
||||
return I2C_STATUS_INVALID_PARAM;
|
||||
}
|
||||
|
||||
/* Enable acknowledge */
|
||||
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
||||
|
||||
while (retry_count < I2C_MAX_RETRY) {
|
||||
switch (state) {
|
||||
case I2C_STATE_START:
|
||||
timeout = 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;
|
||||
}
|
||||
|
||||
/* Configure ACK position for 2-byte read */
|
||||
if (!write_phase) {
|
||||
i2c_ackpos_config(I2C0, I2C_ACKPOS_NEXT);
|
||||
}
|
||||
|
||||
/* 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);
|
||||
/* Disable ACK for last byte */
|
||||
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
/* Clear address flag */
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||
|
||||
if (write_phase) {
|
||||
state = I2C_STATE_TRANSMIT_REG;
|
||||
} else {
|
||||
/* For single byte read, send stop after clearing address */
|
||||
if (data_index == 1) {
|
||||
i2c_stop_on_bus(I2C0);
|
||||
}
|
||||
state = I2C_STATE_RECEIVE_DATA;
|
||||
data_index = 0;
|
||||
}
|
||||
timeout = 0;
|
||||
break;
|
||||
|
||||
case I2C_STATE_TRANSMIT_REG:
|
||||
/* 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;
|
||||
}
|
||||
|
||||
/* Switch to read phase */
|
||||
write_phase = false;
|
||||
state = I2C_STATE_START;
|
||||
timeout = 0;
|
||||
break;
|
||||
|
||||
case I2C_STATE_RECEIVE_DATA:
|
||||
if (data_index < 2) {
|
||||
if (data_index == 1) {
|
||||
/* Wait for BTC before sending stop for last byte */
|
||||
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 condition before reading last byte */
|
||||
i2c_stop_on_bus(I2C0);
|
||||
}
|
||||
|
||||
/* Wait for 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;
|
||||
}
|
||||
|
||||
/* Read data byte */
|
||||
data[data_index] = i2c_data_receive(I2C0);
|
||||
data_index++;
|
||||
timeout = 0;
|
||||
|
||||
if (data_index >= 2) {
|
||||
state = I2C_STATE_STOP;
|
||||
}
|
||||
} else {
|
||||
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;
|
||||
}
|
||||
|
||||
/* Success */
|
||||
return I2C_STATUS_SUCCESS;
|
||||
|
||||
case I2C_STATE_ERROR:
|
||||
/* Send stop condition to release bus */
|
||||
i2c_stop_on_bus(I2C0);
|
||||
|
||||
/* Increment retry counter */
|
||||
retry_count++;
|
||||
if (retry_count >= I2C_MAX_RETRY) {
|
||||
#ifdef DEBUG_VERBOSE
|
||||
// printf("I2C read failed after %d retries\r\n", I2C_MAX_RETRY);
|
||||
const char* msg6_prefix = "I2C read failed after ";
|
||||
for (uint8_t i = 0; msg6_prefix[i] != '\0'; i++) {
|
||||
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TBE) == RESET) {}
|
||||
usart_data_transmit(I2C_DEBUG_UART, msg6_prefix[i]);
|
||||
}
|
||||
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TBE) == RESET) {}
|
||||
usart_data_transmit(I2C_DEBUG_UART, '0' + I2C_MAX_RETRY);
|
||||
const char* msg6_suffix = " retries\r\n";
|
||||
for (uint8_t i = 0; msg6_suffix[i] != '\0'; i++) {
|
||||
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TBE) == RESET) {}
|
||||
usart_data_transmit(I2C_DEBUG_UART, msg6_suffix[i]);
|
||||
}
|
||||
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TC) == RESET) {}
|
||||
#endif
|
||||
return I2C_STATUS_TIMEOUT;
|
||||
}
|
||||
|
||||
/* Reset state machine for retry */
|
||||
state = I2C_STATE_START;
|
||||
write_phase = true;
|
||||
timeout = 0;
|
||||
data_index = 0;
|
||||
|
||||
/* Small delay before retry */
|
||||
delay_10us(10);
|
||||
break;
|
||||
|
||||
default:
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return I2C_STATUS_TIMEOUT;
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief get status string for debugging
|
||||
\param[in] status: i2c_status_t value
|
||||
\param[out] none
|
||||
\retval const char* status string
|
||||
*/
|
||||
const char* i2c_get_status_string(i2c_status_t status) {
|
||||
switch (status) {
|
||||
case I2C_STATUS_SUCCESS:
|
||||
return "SUCCESS";
|
||||
case I2C_STATUS_TIMEOUT:
|
||||
return "TIMEOUT";
|
||||
case I2C_STATUS_NACK:
|
||||
return "NACK";
|
||||
case I2C_STATUS_BUS_BUSY:
|
||||
return "BUS_BUSY";
|
||||
case I2C_STATUS_ERROR:
|
||||
return "ERROR";
|
||||
case I2C_STATUS_INVALID_PARAM:
|
||||
return "INVALID_PARAM";
|
||||
default:
|
||||
return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Created by dell on 24-12-3.
|
||||
// LDC1612 Inductive Sensor Driver Implementation
|
||||
//
|
||||
|
||||
#include "ldc1612.h"
|
||||
|
||||
/* Private function prototypes */
|
||||
static i2c_status_t ldc1612_write_register(uint8_t reg_addr, uint16_t value);
|
||||
static i2c_status_t ldc1612_read_register(uint8_t reg_addr, uint16_t *value);
|
||||
static uint16_t ldc1612_calculate_clock_dividers(uint8_t channel);
|
||||
static uint32_t ldc1612_parse_raw_result(uint32_t raw_result);
|
||||
|
||||
/*!
|
||||
\brief 初始化LDC1612传感器
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval ldc1612_status_t
|
||||
*/
|
||||
ldc1612_status_t ldc1612_init(void) {
|
||||
i2c_status_t i2c_status;
|
||||
uint16_t device_id, manufacturer_id;
|
||||
|
||||
/* 复位传感器 */
|
||||
i2c_status = ldc1612_reset();
|
||||
if (i2c_status != I2C_STATUS_SUCCESS) {
|
||||
return LDC1612_STATUS_ERROR;
|
||||
}
|
||||
|
||||
/* 等待复位完成 */
|
||||
delay_ms(10);
|
||||
|
||||
/* 验证设备ID */
|
||||
device_id = ldc1612_get_device_id();
|
||||
manufacturer_id = ldc1612_get_manufacturer_id();
|
||||
|
||||
if (device_id != 0x3055 || manufacturer_id != 0x5449) {
|
||||
return LDC1612_STATUS_ERROR;
|
||||
}
|
||||
|
||||
return LDC1612_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief 复位LDC1612传感器
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval ldc1612_status_t
|
||||
*/
|
||||
ldc1612_status_t ldc1612_reset(void) {
|
||||
i2c_status_t status = ldc1612_write_register(LDC1612_RESET_DEV, LDC1612_RESET_VALUE);
|
||||
return (status == I2C_STATUS_SUCCESS) ? LDC1612_STATUS_SUCCESS : LDC1612_STATUS_ERROR;
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief 配置单通道模式
|
||||
\param[in] channel: 通道号 (0或1)
|
||||
\param[out] none
|
||||
\retval ldc1612_status_t
|
||||
*/
|
||||
ldc1612_status_t ldc1612_config_single_channel(uint8_t channel) {
|
||||
i2c_status_t status;
|
||||
uint16_t clock_dividers;
|
||||
|
||||
if (channel > 1) {
|
||||
return LDC1612_STATUS_INVALID_PARAM;
|
||||
}
|
||||
|
||||
/* 进入休眠模式进行配置 */
|
||||
status = ldc1612_write_register(LDC1612_CONFIG, LDC1612_SENSOR_CONFIG_SLEEP);
|
||||
if (status != I2C_STATUS_SUCCESS) return LDC1612_STATUS_ERROR;
|
||||
|
||||
/* 计算并设置时钟分频 */
|
||||
clock_dividers = ldc1612_calculate_clock_dividers(channel);
|
||||
status = ldc1612_write_register(LDC1612_CLOCK_DIVIDERS_CH0 + channel, clock_dividers);
|
||||
if (status != I2C_STATUS_SUCCESS) return LDC1612_STATUS_ERROR;
|
||||
|
||||
/* 设置稳定时间 */
|
||||
status = ldc1612_write_register(LDC1612_SETTLECOUNT_CH0 + channel, LDC1612_SETTLECOUNT_CH0_DEFAULT);
|
||||
if (status != I2C_STATUS_SUCCESS) return LDC1612_STATUS_ERROR;
|
||||
|
||||
/* 设置转换时间 */
|
||||
status = ldc1612_write_register(LDC1612_RCOUNT_CH0 + channel, LDC1612_CONVERSION_TIME_CH0);
|
||||
if (status != I2C_STATUS_SUCCESS) return LDC1612_STATUS_ERROR;
|
||||
|
||||
/* 设置错误配置 */
|
||||
status = ldc1612_write_register(LDC1612_ERROR_CONFIG, LDC1612_ERROR_CONFIG_DEFAULT);
|
||||
if (status != I2C_STATUS_SUCCESS) return LDC1612_STATUS_ERROR;
|
||||
|
||||
/* 设置驱动电流 */
|
||||
status = ldc1612_write_register(LDC1612_DRIVE_CURRENT_CH0 + channel, LDC1612_DRIVE_CURRENT_DEFAULT);
|
||||
if (status != I2C_STATUS_SUCCESS) return LDC1612_STATUS_ERROR;
|
||||
|
||||
/* 设置MUX配置 */
|
||||
status = ldc1612_write_register(LDC1612_MUX_CONFIG, LDC1612_MUX_CONFIG_DEFAULT);
|
||||
if (status != I2C_STATUS_SUCCESS) return LDC1612_STATUS_ERROR;
|
||||
|
||||
/* 退出休眠模式,开始转换 */
|
||||
status = ldc1612_write_register(LDC1612_CONFIG, LDC1612_SENSOR_CONFIG_ACTIVE);
|
||||
if (status != I2C_STATUS_SUCCESS) return LDC1612_STATUS_ERROR;
|
||||
|
||||
return LDC1612_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief 读取制造商ID
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval uint16_t 制造商ID
|
||||
*/
|
||||
uint16_t ldc1612_get_manufacturer_id(void) {
|
||||
uint16_t id = 0;
|
||||
ldc1612_read_register(LDC1612_MANUFACTURER_ID, &id);
|
||||
return id;
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief 读取设备ID
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval uint16_t 设备ID
|
||||
*/
|
||||
uint16_t ldc1612_get_device_id(void) {
|
||||
uint16_t id = 0;
|
||||
ldc1612_read_register(LDC1612_DEVICE_ID, &id);
|
||||
return id;
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief 读取通道原始数据
|
||||
\param[in] channel: 通道号
|
||||
\param[out] result: 结果结构体指针
|
||||
\retval ldc1612_status_t
|
||||
*/
|
||||
ldc1612_status_t ldc1612_read_channel(uint8_t channel, ldc1612_result_t *result) {
|
||||
uint16_t msb, lsb;
|
||||
uint32_t raw_data;
|
||||
i2c_status_t status;
|
||||
|
||||
if (channel > 1 || result == NULL) {
|
||||
return LDC1612_STATUS_INVALID_PARAM;
|
||||
}
|
||||
|
||||
/* 读取MSB */
|
||||
status = ldc1612_read_register(LDC1612_DATA_CH0_MSB + (channel * 2), &msb);
|
||||
if (status != I2C_STATUS_SUCCESS) return LDC1612_STATUS_ERROR;
|
||||
|
||||
/* 读取LSB */
|
||||
status = ldc1612_read_register(LDC1612_DATA_CH0_LSB + (channel * 2), &lsb);
|
||||
if (status != I2C_STATUS_SUCCESS) return LDC1612_STATUS_ERROR;
|
||||
|
||||
/* 组合32位数据 */
|
||||
raw_data = ((uint32_t)msb << 16) | lsb;
|
||||
|
||||
/* 解析结果 */
|
||||
result->raw_data = raw_data;
|
||||
result->frequency = ldc1612_parse_raw_result(raw_data);
|
||||
|
||||
/* 检查错误 */
|
||||
if (result->frequency >= 0x10000000) {
|
||||
result->error_flag = true;
|
||||
result->error_code = (result->frequency >> 24) & 0xFF;
|
||||
return LDC1612_STATUS_ERROR;
|
||||
} else {
|
||||
result->error_flag = false;
|
||||
result->error_code = 0;
|
||||
}
|
||||
|
||||
return LDC1612_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
/*!
|
||||
\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) {
|
||||
if (channel > 1) {
|
||||
return LDC1612_STATUS_INVALID_PARAM;
|
||||
}
|
||||
|
||||
i2c_status_t status = ldc1612_write_register(LDC1612_DRIVE_CURRENT_CH0 + channel, current);
|
||||
return (status == I2C_STATUS_SUCCESS) ? LDC1612_STATUS_SUCCESS : LDC1612_STATUS_ERROR;
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief 自动检测驱动电流
|
||||
\param[in] channel: 通道号
|
||||
\param[out] none
|
||||
\retval ldc1612_status_t
|
||||
*/
|
||||
ldc1612_status_t ldc1612_auto_detect_drive_current(uint8_t channel) {
|
||||
uint16_t config_value, drive_current_reg;
|
||||
uint16_t init_value, drive_current;
|
||||
i2c_status_t status;
|
||||
|
||||
if (channel > 1) {
|
||||
return LDC1612_STATUS_INVALID_PARAM;
|
||||
}
|
||||
|
||||
/* 进入休眠模式 */
|
||||
status = ldc1612_write_register(LDC1612_CONFIG, LDC1612_SENSOR_CONFIG_SLEEP);
|
||||
if (status != I2C_STATUS_SUCCESS) return LDC1612_STATUS_ERROR;
|
||||
|
||||
/* 设置时钟分频 */
|
||||
uint16_t clock_dividers = ldc1612_calculate_clock_dividers(channel);
|
||||
status = ldc1612_write_register(LDC1612_CLOCK_DIVIDERS_CH0 + channel, clock_dividers);
|
||||
if (status != I2C_STATUS_SUCCESS) return LDC1612_STATUS_ERROR;
|
||||
|
||||
/* 读取当前配置并禁用Rp覆盖 */
|
||||
status = ldc1612_read_register(LDC1612_CONFIG, &config_value);
|
||||
if (status != I2C_STATUS_SUCCESS) return LDC1612_STATUS_ERROR;
|
||||
|
||||
config_value &= ~(1 << 12); // 禁用RP_OVERRIDE_EN
|
||||
status = ldc1612_write_register(LDC1612_CONFIG, config_value);
|
||||
if (status != I2C_STATUS_SUCCESS) return LDC1612_STATUS_ERROR;
|
||||
|
||||
/* 启动测量 */
|
||||
status = ldc1612_write_register(LDC1612_CONFIG, LDC1612_SENSOR_CONFIG_ACTIVE);
|
||||
if (status != I2C_STATUS_SUCCESS) return LDC1612_STATUS_ERROR;
|
||||
|
||||
/* 等待至少一次转换完成 */
|
||||
delay_ms(10);
|
||||
|
||||
/* 读取初始驱动电流值 */
|
||||
status = ldc1612_read_register(LDC1612_DRIVE_CURRENT_CH0 + channel, &drive_current_reg);
|
||||
if (status != I2C_STATUS_SUCCESS) return LDC1612_STATUS_ERROR;
|
||||
|
||||
init_value = (drive_current_reg >> 6) & 0x1F;
|
||||
drive_current = (init_value << 11) | 0x0000;
|
||||
|
||||
/* 写入检测到的驱动电流 */
|
||||
status = ldc1612_write_register(LDC1612_DRIVE_CURRENT_CH0 + channel, drive_current);
|
||||
if (status != I2C_STATUS_SUCCESS) return LDC1612_STATUS_ERROR;
|
||||
|
||||
return LDC1612_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief 获取状态字符串
|
||||
\param[in] status: 状态码
|
||||
\param[out] none
|
||||
\retval const char* 状态字符串
|
||||
*/
|
||||
const char* ldc1612_get_status_string(ldc1612_status_t status) {
|
||||
switch (status) {
|
||||
case LDC1612_STATUS_SUCCESS:
|
||||
return "SUCCESS";
|
||||
case LDC1612_STATUS_ERROR:
|
||||
return "ERROR";
|
||||
case LDC1612_STATUS_TIMEOUT:
|
||||
return "TIMEOUT";
|
||||
case LDC1612_STATUS_INVALID_PARAM:
|
||||
return "INVALID_PARAM";
|
||||
case LDC1612_STATUS_NO_COIL:
|
||||
return "NO_COIL";
|
||||
case LDC1612_STATUS_UNDER_RANGE:
|
||||
return "UNDER_RANGE";
|
||||
case LDC1612_STATUS_OVER_RANGE:
|
||||
return "OVER_RANGE";
|
||||
default:
|
||||
return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
|
||||
/* Private Functions Implementation */
|
||||
|
||||
/*!
|
||||
\brief 写入寄存器
|
||||
\param[in] reg_addr: 寄存器地址
|
||||
\param[in] value: 写入值
|
||||
\param[out] none
|
||||
\retval i2c_status_t
|
||||
*/
|
||||
static i2c_status_t ldc1612_write_register(uint8_t reg_addr, uint16_t value) {
|
||||
uint8_t data[2];
|
||||
data[0] = (value >> 8) & 0xFF;
|
||||
data[1] = value & 0xFF;
|
||||
|
||||
return i2c_write_16bits(LDC1612_ADDR, reg_addr, data);
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief 读取寄存器
|
||||
\param[in] reg_addr: 寄存器地址
|
||||
\param[out] value: 读取值指针
|
||||
\retval i2c_status_t
|
||||
*/
|
||||
static i2c_status_t ldc1612_read_register(uint8_t reg_addr, uint16_t *value) {
|
||||
uint8_t data[2];
|
||||
i2c_status_t status;
|
||||
|
||||
if (value == NULL) {
|
||||
return I2C_STATUS_INVALID_PARAM;
|
||||
}
|
||||
|
||||
status = i2c_read_16bits(LDC1612_ADDR, reg_addr, data);
|
||||
if (status == I2C_STATUS_SUCCESS) {
|
||||
*value = ((uint16_t)data[0] << 8) | data[1];
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief 计算时钟分频值
|
||||
\param[in] channel: 通道号
|
||||
\param[out] none
|
||||
\retval uint16_t 分频值
|
||||
*/
|
||||
static uint16_t ldc1612_calculate_clock_dividers(uint8_t channel) {
|
||||
uint16_t fin_div, fref_div;
|
||||
float sensor_freq;
|
||||
|
||||
/* 计算传感器频率 (MHz) */
|
||||
sensor_freq = 1.0f / (2.0f * 3.14159f * sqrtf(LDC1612_COIL_L_UH * LDC1612_COIL_C_PF * 1e-18f)) * 1e-6f;
|
||||
|
||||
/* 计算FIN分频 */
|
||||
fin_div = (uint16_t)(sensor_freq / 8.75f + 1);
|
||||
|
||||
/* 计算FREF分频 */
|
||||
if (fin_div * 4 < 40) {
|
||||
fref_div = 2;
|
||||
} else {
|
||||
fref_div = 4;
|
||||
}
|
||||
|
||||
return (fin_div << 12) | fref_div;
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief 解析原始结果
|
||||
\param[in] raw_result: 原始数据
|
||||
\param[out] none
|
||||
\retval uint32_t 解析后的数据
|
||||
*/
|
||||
static uint32_t ldc1612_parse_raw_result(uint32_t raw_result) {
|
||||
uint32_t calibration_value;
|
||||
uint8_t error_code;
|
||||
|
||||
calibration_value = raw_result & 0x0FFFFFFF;
|
||||
|
||||
/* 检查无线圈错误 */
|
||||
if (calibration_value == 0x0FFFFFFF) {
|
||||
return LDC1612_ERROR_NO_COIL;
|
||||
}
|
||||
|
||||
error_code = (raw_result >> 24) & 0xFF;
|
||||
|
||||
/* 检查各种错误 */
|
||||
if (error_code & 0x80) {
|
||||
return LDC1612_ERROR_UNDER_RANGE;
|
||||
}
|
||||
if (error_code & 0x40) {
|
||||
return LDC1612_ERROR_OVER_RANGE;
|
||||
}
|
||||
if (error_code & 0x20) {
|
||||
return LDC1612_ERROR_WATCHDOG;
|
||||
}
|
||||
if (error_code & 0x10) {
|
||||
return LDC1612_ERROR_AMPLITUDE;
|
||||
}
|
||||
|
||||
return calibration_value;
|
||||
}
|
||||
|
||||
|
||||
// ldc1612.h
|
||||
//
|
||||
// Created by dell on 24-12-3.
|
||||
// LDC1612 Inductive Sensor Driver Header
|
||||
//
|
||||
|
||||
#ifndef LDC1612_H
|
||||
#define LDC1612_H
|
||||
|
||||
#include "gd32e23x_it.h"
|
||||
#include "gd32e23x.h"
|
||||
#include "systick.h"
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include "board_config.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
|
||||
|
||||
/* Channel Definitions */
|
||||
/******************************************************************************/
|
||||
#define LDC1612_CHANNEL_0 0
|
||||
#define LDC1612_CHANNEL_1 1
|
||||
|
||||
/* 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 // 复位值
|
||||
|
||||
/* 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)
|
||||
|
||||
/* 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;
|
||||
|
||||
/******************************************************************************/
|
||||
/* Function Declarations */
|
||||
|
||||
/*!
|
||||
\brief 初始化LDC1612传感器
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval ldc1612_status_t
|
||||
*/
|
||||
ldc1612_status_t ldc1612_init(void);
|
||||
|
||||
/*!
|
||||
\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);
|
||||
|
||||
/*!
|
||||
\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);
|
||||
|
||||
/*!
|
||||
\brief 读取通道原始数据
|
||||
\param[in] channel: 通道号
|
||||
\param[out] result: 结果结构体指针
|
||||
\retval ldc1612_status_t
|
||||
*/
|
||||
ldc1612_status_t ldc1612_read_channel(uint8_t channel, ldc1612_result_t *result);
|
||||
|
||||
/*!
|
||||
\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);
|
||||
|
||||
/*!
|
||||
\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);
|
||||
|
||||
#endif //LDC1612_H
|
Reference in New Issue
Block a user