4 Commits

Author SHA1 Message Date
233576fd9a test 2025-08-21 17:12:04 +08:00
b78c1e416a detect MCU flash size to config diff usart 2025-08-21 00:39:30 +08:00
b6485c5c39 add tmp112a code 2025-08-18 00:18:21 +08:00
443e9bf8f2 fix H-file 2025-08-17 18:01:01 +08:00
16 changed files with 442 additions and 109 deletions

View File

@@ -31,8 +31,8 @@ set(TARGET_SRC
Src/command.c
Src/i2c.c
Src/ldc1612.c
# Src/tmp112.c
# Src/sensor_example.c
Src/tmp112.c
Src/board_config.c
)
# 设置输出目录

View File

@@ -1,6 +1,10 @@
#ifndef BOARD_CONFIG_H
#define BOARD_CONFIG_H
#define GD32E23XF4 0x10
#define GD32E23XF6 0x20
#define GD32E23XF8 0x40
/* >>>>>>>>>>>>>>>>>>>>[RS485 PHY DEFINE]<<<<<<<<<<<<<<<<<<<< */
// #define RS485_MAX13487 // RS485 PHY : MAX13487 (AutoDir)
@@ -16,6 +20,28 @@
// #define DEBUG_VERBOSE // Debug Assertions Status : Debug Verbose Information
#undef DEBUG_VERBOSE // Debug Assertions Status : No Debug Verbose Information
/* >>>>>>>>>>>>>>>>>>>>[EDDY DRIVE CURRENT DETECTION]<<<<<<<<<<<<<<<<<<<< */
// #define EDDY_DRIVE_CURRENT_DETECTION // Eddy Drive Current Detection : Enable
#undef EDDY_DRIVE_CURRENT_DETECTION // Eddy Drive Current Detection : Disable
/******************************************************************************/
/* Dynamic USART Configuration Structure */
typedef struct {
uint32_t rcu_usart;
uint32_t usart_periph;
IRQn_Type irq_type;
void (*irq_handler)(void); // 函数指针:指向中断处理函数
} usart_config_t;
extern usart_config_t g_usart_config;
extern uint8_t g_mcu_flash_size;
/* USART中断处理函数声明 */
void usart0_irq_handler(void);
void usart1_irq_handler(void);
/******************************************************************************/
#define RCU_GPIO_I2C RCU_GPIOF
@@ -30,22 +56,25 @@
/******************************************************************************/
#define LED_RCU RCU_GPIOA
#define LED_PORT GPIOA
#define LED_PIN GPIO_PIN_7
#define LED_RCU RCU_GPIOA
/******************************************************************************/
#define RS485_RCU RCU_USART0
#define RS485_RCU (g_usart_config.rcu_usart)
#define RS485_PHY (g_usart_config.usart_periph)
#define RS485_IRQ (g_usart_config.irq_type)
#define RS485_GPIO_RCU RCU_GPIOA
#define RS485_GPIO_PORT GPIOA
#define RS485_EN_PIN GPIO_PIN_1
#define RS485_TX_PIN GPIO_PIN_2
#define RS485_RX_PIN GPIO_PIN_3
#define RS485_PHY USART0
#define RS485_BAUDRATE 115200U
#define RS485_EN_PIN GPIO_PIN_1
#define RS485_IRQ USART0_IRQn
/******************************************************************************/
void mcu_detect_and_config(void);
uint8_t get_flash_size(void);
#endif //BOARD_CONFIG_H

View File

@@ -17,7 +17,8 @@
*/
/** @brief 传感器周期上报使能标志 */
extern volatile bool g_sensor_report_enabled;
extern volatile bool g_eddy_current_sensor_report_enabled;
extern volatile bool g_temperature_sensor_report_enabled;
/**
* @section Command_Protocol 协议格式
@@ -62,23 +63,42 @@ extern volatile bool g_sensor_report_enabled;
*/
/**
* @brief 获取传感器周期上报使能状态。
* @brief 获取电涡流传感器周期上报使能状态。
* @return bool 上报状态。
* @retval true 传感器周期上报已启用。
* @retval false 传感器周期上报已禁用。
* @ingroup Command
*/
bool get_sensor_report_enabled(void);
bool get_eddy_sensor_report_enabled(void);
/**
* @brief 设置传感器周期上报使能状态。
* @brief 设置电涡流传感器周期上报使能状态。
* @param enabled 上报使能标志。
* @arg true 启用传感器周期上报。
* @arg false 禁用传感器周期上报。
* @note 推荐通过此函数修改状态,便于后续功能扩展。
* @ingroup Command
*/
void set_sensor_report_status(bool enabled);
void set_eddy_sensor_report_status(bool enabled);
/**
* @brief 获取温度传感器周期上报使能状态。
* @return bool 上报状态。
* @retval true 传感器周期上报已启用。
* @retval false 传感器周期上报已禁用。
* @ingroup Command
*/
bool get_temp_sensor_report_enabled(void);
/**
* @brief 设置温度传感器周期上报使能状态。
* @param enabled 上报使能标志。
* @arg true 启用传感器周期上报。
* @arg false 禁用传感器周期上报。
* @note 推荐通过此函数修改状态,便于后续功能扩展。
* @ingroup Command
*/
void set_temp_sensor_report_status(bool enabled);
/**
* @brief 处理串口环形缓冲区中的命令数据。
@@ -103,4 +123,6 @@ void handle_command(const uint8_t *cmd, uint8_t len);
void eddy_current_report(void);
void temperature_raw_value_report(void);
#endif // COMMAND_H

View File

@@ -14,7 +14,6 @@
#include <stdlib.h>
#include <math.h>
#include "board_config.h"
#include "soft_i2c.h"
#include "i2c.h"
/***************************************************************************/
@@ -32,60 +31,153 @@
/***************************************************************************/
#define LDC1612_ADDR 0x2B
#define LDC1612_ADDR (0x2B)
/*Register Rddr*/
/***************************************************************************/
/************************Register Addr***************************************/
#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_SETTLECOUNT_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 MUX_CONFIG_REG 0X1B
#define SENSOR_RESET_REG 0X1C
#define SET_DRIVER_CURRENT_REG 0X1E
#define READ_MANUFACTURER_ID 0X7E
#define READ_DEVICE_ID 0X7F
/******************************************************************************/
/**********************Sensor Channel****************************************/
#define CHANNEL_0 0
#define CHANNEL_1 1
/*************************MUX_CONFIG********************************************/
#define LDC1612_MUX_CONFIG 0x0200
/*************************MUX_CONFIG********************************************
* 0x0209 AutoScanEN: 0 / RR_SEQ: 00 / RESERVED: 0 0010 0000 1 / Deglitch: 001( 1MHz)
* 0x020C AutoScanEN: 0 / RR_SEQ: 00 / RESERVED: 0 0010 0000 1 / Deglitch: 100(3.3MHz)
* 0x020D AutoScanEN: 0 / RR_SEQ: 00 / RESERVED: 0 0010 0000 1 / Deglitch: 100( 10MHz)
* 0x020F AutoScanEN: 0 / RR_SEQ: 00 / RESERVED: 0 0010 0000 1 / Deglitch: 100( 33MHz)
*/
#define LDC1612_MUX_CONFIG 0x020C
/***********************SENSOR_CONFIG********************************************/
/***********************SENSOR_CONFIG********************************************
* 0x1601 Active CH0: 00 / SLEEP: 0 / OVERDRIVE: 1 / LowPowerMode: 0 / AutoAmpDis 1 / CLK(ext): 1 / RESERVED: 0 / INTB_Dis : 0 / HighCurrent: 0 / RESERVED: 00 0001
* 0x1201 Active CH0: 00 / SLEEP: 0 / OVERDRIVE: 1 / LowPowerMode: 0 / AutoAmpDis 0 / CLK(ext): 1 / RESERVED: 0 / INTB_Dis : 0 / HighCurrent: 0 / RESERVED: 00 0001
* 0x1641 Active CH0: 00 / SLEEP: 0 / OVERDRIVE: 1 / LowPowerMode: 0 / AutoAmpDis 1 / CLK(ext): 1 / RESERVED: 0 / INTB_Dis : 0 / HighCurrent: 1 / RESERVED: 00 0001
* 0x1241 Active CH0: 00 / SLEEP: 0 / OVERDRIVE: 1 / LowPowerMode: 0 / AutoAmpDis 0 / CLK(ext): 1 / RESERVED: 0 / INTB_Dis : 0 / HighCurrent: 1 / RESERVED: 00 0001
*/
#define LDC1612_SENSOR_CONFIG_CH0 0x1601 //
#ifdef EDDY_DRIVE_CURRENT_DETECTION
#define LDC1612_SENSOR_CONFIG_CH0 0x1241
#else
#define LDC1612_SENSOR_CONFIG_CH0 0x1641
#endif
#define LDC1612_SLEEP_MODE 0x2801
/****************************CONVERSION_TIME************************************/
#define LDC1612_CONVERSION_TIME_CH0 0x1000 // 0x1000=4096个时钟周期
#define LC_STABILIZE_TIME_CH0 0x0020 // 0x0020=32个时钟周期
/****************************CONVERSION_TIME************************************
* Freq_ref = 40MHz / CHx_FREF_DIVIDER
* ******RCOUNT_CHx*******
* Reference Count Conversion Interval Time
* 0x0005 ~ 0xFFFF
* default: 0x0080
* RCOUNT_CHx * 16 / Freq_ref = Conversion Interval Time
*
* ******SETTLECOUNT_CHx*******
* Conversion Settling Time
* 0x0000 ~ 0xFFFF
* default: 0x0000
* SETTLECOUNT_CHx * 16 / Freq_ref = Conversion Settling Time
* 0x1000 4096*16个时钟周期
* 0x0100 256*16个时钟周期
* 0x0000/0x0001 32*16个时钟周期
*
* ******RCOUNT_CHx*******
*/
#define LDC1612_RCOUNT_TIME_CH0 0x1000 // 0x1000=4096个时钟周期
#define LDC1612_SETTLECOUNT_CH0 0x0100
/**************************DRIVE_CURRENT****************************************/
#define LDC1612_DRIVE_CURRENT 0x9000 //A000
/**************************DRIVE_CURRENT****************************************
* 0xA000 CH_IDRIVE: 1010 0 / CH_INIT_IDRIVE: 000 00 / RESERVED: 00 0000
* 0x9000 CH_IDRIVE: 1001 0 / CH_INIT_IDRIVE: 000 00 / RESERVED: 00 0000
* CH_INIT_IDRIVE will update when every conversion systick ==>AutoAmpDis is 0
* CH_INIT_IDRIVE will store init drive current calculated ==> AutoAmpDis is 1
*/
#define LDC1612_DRIVE_CURRENT 0x9000
/**************************SENSOR_CONFIG***************************************/
#define LDC1612_SLEEP_MODE 0x2801
/**************************OTHER_CONFIG*****************************************/
/**************************ERROR_CONFIG****************************************
* [15] Under-Range ERR to OUT (DATA_CHx.CHx_ERR_UR)
* [14] Over-Range ERR to OUT (DATA_CHx.CHx_ERR_OR)
* [13] Watchdog-Timeout ERR to OUT (DATA_CHx.CHx_ERR_WD)
* [12] Amplitude-High-Error ERR to OUT (DATA_CHx.CHx_ERR_AE)
* [11] Amplitude-Low-Error ERR to OUT (DATA_CHx.CHx_ERR_AE)
* [10] RESERVED
* [ 9] RESERVED
* [ 8] RESERVED
* [ 7] Under-Range ERR to INTB (STATUS.ERR_UR)
* [ 6] Over-Range ERR to INTB (STATUS.ERR_OR)
* [ 5] Watchdog-Timeout ERR to INTB (STATUS.ERR_WD)
* [ 4] Amplitude-High-Error ERR to INTB (STATUS.ERR_AHE)
* [ 3] Amplitude-Low-Error ERR to INTB (STATUS.ERR_ALE)
* [ 2] Zero_Count_Error ERR to INTB (STATUS.ERR_ZC)
* [ 1] RESERVED
* [ 0] Data_Ready_Flag to INTB (STATUS.DRDY)
*
* 0x0000 No ERR to OUT or INTB
*/
#define LDC1612_ERROR_CONFIG 0x0000
/**************************STATUS****************************************
* [15]
* [14] Error Channel 0b00: CH0 / 0b01: CH1 / 0b10: CH2 / 0b11: CH3
* [13] Conversion Under-Range Error 0b0: No / 0b1: Yes
* [12] Conversion Over-Range Error 0b0: No / 0b1: Yes
* [11] Watchdog Timeout Error 0b0: No / 0b1: Yes
* [10] Amplitude High Error 0b0: No / 0b1: Yes
* [ 9] Amplitude Low Error 0b0: No / 0b1: Yes
* [ 8] Zero Count Error 0b0: No / 0b1: Yes
* [ 7] RESERVED
* [ 6] Ddata Ready Flag 0b0: No new results / 0b1: New results available
* [ 5] RESERVED
* [ 4] RESERVED
* [ 3] CH0 Unread Conversion Result 0b0: No / 0b1: Yes(DATA_CH0)
* [ 2] CH1 Unread Conversion Result 0b0: No / 0b1: Yes(DATA_CH1)
* [ 1] CH2 Unread Conversion Result 0b0: No / 0b1: Yes(DATA_CH2)
* [ 0] CH3 Unread Conversion Result 0b0: No / 0b1: Yes(DATA_CH3)
*
* 0x0000 No ERR to OUT or INTB
*/
/*****************CONVERSION_OFFSET_CONFIG****************************************/
#define SET_CONVERSION_OFFSET_CH0 0x0000
/***********************RESET DEVICE********************************************
0x8000 RESET_DEV: 1 / RESERVED: 000 0000 0000 0000
*/
#define LDC1612_RESET_DEV 0x8000 //[15:0] 0b1000 0000 0000 0000
/***********************IDs****************************************************/
#define LDC1612_MANUFACTURER_ID 0x5449
#define LDC1612_DEVICE_ID 0x3055
/******************************************************************************/
#define COIL_RP_KOM 7.2
#define COIL_L_UH 33
#define COIL_L_UH 11.22
#define COIL_C_PF 150
#define COIL_Q_FACTOR 35.97
#define COIL_FREQ_HZ 2262000
#define COIL_Q_FACTOR 31.09
#define COIL_FREQ_HZ 5323770
/******************************************************************************/
@@ -112,7 +204,7 @@ 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);
uint16_t ldc1612_get_sensor_status(void);

View File

@@ -19,7 +19,7 @@
/******************************************************************************/
/* TMP112A I2C Address */
#define TMP112A_ADDR (0x48) // 7-bit address (ADD0=GND)
#define TMP112A_ADDR (0x49) // 7-bit address (ADD0=GND)
/* Register Addresses */
/******************************************************************************/
@@ -59,7 +59,7 @@
/* Default Configuration */
/******************************************************************************/
#define TMP112A_CONFIG_DEFAULT (TMP112A_RESOLUTION_12BIT | TMP112A_RATE_4HZ)
#define TMP112A_CONFIG_DEFAULT (TMP112A_RESOLUTION_12BIT | TMP112A_RATE_8HZ)
/* Temperature Conversion Constants */
/******************************************************************************/
@@ -111,6 +111,8 @@ tmp112a_status_t tmp112a_config(uint16_t config);
*/
tmp112a_status_t tmp112a_read_temperature(tmp112a_result_t *result);
void tmp112a_get_raw_temperature_value(uint8_t *value);
/*!
\brief 设置温度阈值
\param[in] low_temp: 低温阈值 (°C)

52
Src/board_config.c Normal file
View File

@@ -0,0 +1,52 @@
#include "gd32e23x.h"
#include "board_config.h"
#include "systick.h"
/******************************************************************************/
#define FLASH_SIZE_ADDR (*(const uint8_t *)0x1FFFF7E0) // Flash base address
/******************************************************************************/
/* 前向声明中断处理函数 */
void usart0_irq_handler(void);
void usart1_irq_handler(void);
usart_config_t g_usart_config = {
.rcu_usart = RCU_USART1,
.usart_periph = USART1,
.irq_type = USART1_IRQn,
.irq_handler = usart1_irq_handler // 初始化函数指针
};
uint8_t g_mcu_flash_size = 0;
void mcu_detect_and_config(void) {
g_mcu_flash_size = FLASH_SIZE_ADDR;
switch (g_mcu_flash_size) {
case GD32E23XF4:
g_usart_config.rcu_usart = RCU_USART0;
g_usart_config.usart_periph = USART0;
g_usart_config.irq_type = USART0_IRQn;
g_usart_config.irq_handler = usart0_irq_handler; // 指向USART0处理函数
break;
case GD32E23XF6:
g_usart_config.rcu_usart = RCU_USART1;
g_usart_config.usart_periph = USART1;
g_usart_config.irq_type = USART1_IRQn;
g_usart_config.irq_handler = usart1_irq_handler; // 指向USART1处理函数
break;
default: // Default to GD32E23XF8
g_usart_config.rcu_usart = RCU_USART1;
g_usart_config.usart_periph = USART1;
g_usart_config.irq_type = USART1_IRQn;
g_usart_config.irq_handler = usart1_irq_handler; // 指向USART1处理函数
break;
}
}
uint8_t get_flash_size(void) {
return g_mcu_flash_size;
}

View File

@@ -18,6 +18,7 @@
#include "board_config.h"
#include "gd32e23x_usart.h"
#include "ldc1612.h"
#include "tmp112.h"
/* ============================================================================
* 协议格式说明
@@ -78,7 +79,8 @@
* ============================================================================ */
/** @brief 传感器周期上报使能标志 */
volatile bool g_sensor_report_enabled = false;
volatile bool g_eddy_current_sensor_report_enabled = false;
volatile bool g_temperature_sensor_report_enabled = false;
/** @name 预设响应数据
* @{ */
@@ -91,25 +93,47 @@ static const uint8_t s_report_status_err[] = { 'e','r','r' }; /**< 错误响应
* ============================================================================ */
/**
* @brief 查询是否启用周期性传感器上报。
* @brief 查询电涡流传感器是否启用周期性传感器上报。
* @return true 表示启用false 表示禁用。
* @ingroup Command
*/
bool get_sensor_report_enabled(void)
bool get_eddy_sensor_report_enabled(void)
{
return g_sensor_report_enabled;
return g_eddy_current_sensor_report_enabled;
}
/**
* @brief 设置是否启用周期性传感器上报标志。
* @brief 设置电涡流传感器是否启用周期性传感器上报标志。
* @details 本模块内部保存的布尔状态,供其他逻辑决定是否进行周期性数据上报;
* 推荐通过本函数修改而非直接访问全局/静态变量,以便后续扩展(如加锁/回调)。
* @param status true 启用周期上报false 禁用。
* @ingroup Command
*/
void set_sensor_report_status(bool status)
void set_eddy_sensor_report_status(bool status)
{
g_sensor_report_enabled = status;
g_eddy_current_sensor_report_enabled = status;
}
/**
* @brief 查询温度传感器是否启用周期性传感器上报。
* @return true 表示启用false 表示禁用。
* @ingroup Command
*/
bool get_temp_sensor_report_enabled(void)
{
return g_temperature_sensor_report_enabled;
}
/**
* @brief 设置温度传感器是否启用周期性传感器上报标志。
* @details 本模块内部保存的布尔状态,供其他逻辑决定是否进行周期性数据上报;
* 推荐通过本函数修改而非直接访问全局/静态变量,以便后续扩展(如加锁/回调)。
* @param status true 启用周期上报false 禁用。
* @ingroup Command
*/
void set_temp_sensor_report_status(bool status)
{
g_temperature_sensor_report_enabled = status;
}
/**
@@ -277,10 +301,19 @@ void handle_command(const uint8_t *frame, uint8_t len) {
// 仅基础命令,如 M1, M2, M3
switch (base_cmd) {
case 1u: // M1: enable sensor report
set_sensor_report_status(true);
set_eddy_sensor_report_status(true);
return;
case 2u: // M2: disable sensor report
set_sensor_report_status(false);
set_eddy_sensor_report_status(false);
return;
case 3u:
set_temp_sensor_report_status(true);
return;
case 4u:
set_temp_sensor_report_status(false);
return;
// case 201u: // M201命令
@@ -423,7 +456,7 @@ void command_process(void) {
}
void eddy_current_report(void) {
// if (!g_sensor_report_enabled) return;
// if (!g_eddy_current_sensor_report_enabled) return;
uint32_t raw_result = ldc1612_get_raw_channel_result(CHANNEL_0);
uint8_t sensor_data[4];
@@ -434,3 +467,21 @@ void eddy_current_report(void) {
send_response(RESP_TYPE_OK, sensor_data, sizeof(sensor_data));
}
void temperature_raw_value_report(void) {
// if (!g_temperature_sensor_report_enabled) return;
uint8_t raw_result[4];
uint8_t value[2] = {0};
uint32_t raw_value = 0;
// i2c_read_16bits(TMP112A_ADDR, TMP112A_TEMP_REG, value);
tmp112a_get_raw_temperature_value(value);
raw_value = (uint32_t)((uint16_t) (value[0] << 4) | (value[1]>>4)) * 625;
raw_result[0] = (uint8_t)(raw_value >> 24);
raw_result[1] = (uint8_t)(raw_value >> 16);
raw_result[2] = (uint8_t)(raw_value >> 8);
raw_result[3] = (uint8_t)(raw_value & 0xFF);
send_response(RESP_TYPE_OK, raw_result, sizeof(raw_result));
}

View File

@@ -37,6 +37,7 @@ OF SUCH DAMAGE.
#include "uart.h"
#include "uart_ring_buffer.h"
#include "led.h"
#include "board_config.h"
/*!
\brief this function handles NMI exception
@@ -102,8 +103,15 @@ void SysTick_Handler(void) {
}
void USART0_IRQHandler(void) {
if (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE)) {
uint8_t data = usart_data_receive(USART0);
(void)uart_ring_buffer_put(data); // 缓冲满时丢弃,返回值可用于统计
// 检查当前配置是否使用USART0并且函数指针不为空
if(g_usart_config.usart_periph == USART0 && g_usart_config.irq_handler != 0) {
g_usart_config.irq_handler(); // 通过函数指针调用对应的处理函数
}
}
void USART1_IRQHandler(void) {
// 检查当前配置是否使用USART1并且函数指针不为空
if(g_usart_config.usart_periph == USART1 && g_usart_config.irq_handler != 0) {
g_usart_config.irq_handler(); // 通过函数指针调用对应的处理函数
}
}

View File

@@ -137,10 +137,10 @@ ldc1612_status_t ldc1612_config_single_channel(uint8_t channel) {
delay_ms(5);
/* Step 3: 配置LC稳定时间 - 影响测量精度 */
ldc1612_write_register(SET_LC_STABILIZE_REG_START + channel, LC_STABILIZE_TIME_CH0);
ldc1612_write_register(SET_SETTLECOUNT_REG_START + channel, LDC1612_SETTLECOUNT_CH0);
/* Step 4: 配置转换时间 - 影响测量速度和精度 */
ldc1612_write_register(SET_CONVERSION_TIME_REG_START + channel, LDC1612_CONVERSION_TIME_CH0);
ldc1612_write_register(SET_CONVERSION_TIME_REG_START + channel, LDC1612_RCOUNT_TIME_CH0);
/* Step 5: 配置转换偏移 */
ldc1612_write_register(SET_CONVERSION_OFFSET_REG_START + channel, SET_CONVERSION_OFFSET_CH0);
@@ -150,7 +150,7 @@ ldc1612_status_t ldc1612_config_single_channel(uint8_t channel) {
/* Step 7: 配置多路复用器 - 设置通道选择和滤波 */
// ldc1612_configure_mux_register(LDC1612_MUX_AUTOSCAN_DISABLE, LDC1612_MUX_RR_SEQUENCE_0, LDC1612_MUX_FILTER_ALL_LOW, LDC1612_MUX_FILTER_NONE);
ldc1612_write_register(MUL_CONFIG_REG, LDC1612_MUX_CONFIG);
ldc1612_write_register(MUX_CONFIG_REG, LDC1612_MUX_CONFIG);
/* Step 8: 配置错误输出 */
ldc1612_write_register(ERROR_CONFIG_REG, LDC1612_ERROR_CONFIG);
@@ -174,43 +174,53 @@ uint32_t ldc1612_get_raw_channel_result(uint8_t channel) {
uint8_t value[2] = {0};
/* Read MSW */
LDC1612_IIC_READ_16BITS(LDC1612_ADDR, CONVERTION_RESULT_REG_START + channel, value);
LDC1612_IIC_READ_16BITS(LDC1612_ADDR, CONVERTION_RESULT_REG_START + (channel * 2), value);
raw_value |= (uint32_t)(((uint16_t)value[0] << 8) | value[1]) << 16;
/* Read LSW */
LDC1612_IIC_READ_16BITS(LDC1612_ADDR, CONVERTION_RESULT_REG_START + channel + 1, value);
LDC1612_IIC_READ_16BITS(LDC1612_ADDR, CONVERTION_RESULT_REG_START + 1 + (channel * 2), value);
raw_value |= (uint32_t)(((uint16_t)value[0] << 8) | value[1]);
uint32_t calibration_value = raw_value & 0x0FFFFFFF;
if (calibration_value == 0x0FFFFFFF) {
return 0xF0000000; /* No coil */
}
uint8_t error_code = (uint8_t)(raw_value >> 24);
if (error_code & 0x80) return 0x80000000; /* Under range */
if (error_code & 0x40) return 0x40000000; /* Over range */
if (error_code & 0x20) return 0x20000000; /* Watchdog */
if (error_code & 0x10) return 0x10000000; /* Amplitude error */
if (LDC1612_ERROR_CONFIG & 0xF800) {
uint8_t error_code = (uint8_t)(raw_value >> 24);
if (error_code & 0x80) return 0x80000000; /* Under range */
if (error_code & 0x40) return 0x40000000; /* Over range */
if (error_code & 0x20) return 0x20000000; /* Watchdog */
if (error_code & 0x10) return 0x10000000; /* Amplitude error */
}
return raw_value;
}
// void ldc1612_drvie_current_detect(uint8_t channel) {
// uint8_t data[2] = {0};
// uint16_t init_value = 0 , drive_current = 0;
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_configure_frequency(channel);
// LDC1612_IIC_READ_16BITS(LDC1612_ADDR, SENSOR_CONFIG_REG, data);
// ldc1612_set_sensor_config(LDC1612_SLEEP_MODE);
// ldc1612_set_sensor_config(LDC1612_SENSOR_CONFIG_CH0); //0x1A --0x1601
// delay_ms(10);
// LDC1612_IIC_READ_16BITS(LDC1612_ADDR, SET_DRIVER_CURRENT_REG, data);
ldc1612_write_register(SENSOR_CONFIG_REG, LDC1612_SLEEP_MODE);
delay_ms(10);
// 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);
// }
uint16_t freq_divider = ldc1612_calculate_freq_divider(channel);
ldc1612_write_register(SET_FREQ_REG_START + channel, freq_divider);
delay_ms(5);
LDC1612_IIC_READ_16BITS(LDC1612_ADDR, SENSOR_CONFIG_REG, data);
// ldc1612_set_sensor_config(LDC1612_SLEEP_MODE);
ldc1612_write_register(SENSOR_CONFIG_REG, LDC1612_SLEEP_MODE);
delay_ms(10);
ldc1612_write_register(SENSOR_CONFIG_REG, LDC1612_SENSOR_CONFIG_CH0);
delay_ms(10);
LDC1612_IIC_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);
}
/** @brief Get sensor status register
@return Status register value

View File

@@ -41,6 +41,7 @@ OF SUCH DAMAGE.
#include "i2c.h"
#include "board_config.h"
#include "ldc1612.h"
#include "tmp112.h"
/*!
\brief main function
@@ -50,12 +51,16 @@ OF SUCH DAMAGE.
*/
int main(void)
{
led_init();
mcu_detect_and_config();
setbuf(stdout, NULL);
systick_config();
rs485_init();
led_init();
// led_init();
printf("Flash size: %d Kbytes\n", get_flash_size());
#ifdef DEBUG_VERBOSE
char hello_world[] = {"Hello World!\r\n"};
@@ -80,10 +85,31 @@ int main(void)
ldc1612_init();
ldc1612_config_single_channel(CHANNEL_0);
tmp112a_init();
#ifdef EDDY_DRIVE_CURRENT_DETECTION
ldc1612_drvie_current_detect(CHANNEL_0);
#endif
while(1){
#ifndef EDDY_DRIVE_CURRENT_DETECTION
command_process();
delay_ms(10);
if (g_sensor_report_enabled)
if (g_eddy_current_sensor_report_enabled)
eddy_current_report();
if (g_temperature_sensor_report_enabled)
temperature_raw_value_report();
#else
ldc1612_drvie_current_detect(CHANNEL_0);
delay_ms(1000);
#endif
}
}

View File

@@ -16,6 +16,7 @@
#include <unistd.h>
#include <sys/wait.h>
#include "gd32e23x_usart.h"
#include "board_config.h"
#undef errno
extern int errno;
@@ -164,7 +165,7 @@ int _execve(char *name, char **argv, char **env)
// USART0 printf重定向实现
int __io_putchar(int ch) {
// 等待发送缓冲区空
while (usart_flag_get(USART0, USART_FLAG_TBE) == RESET) {}
usart_data_transmit(USART0, (uint8_t)ch);
while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
usart_data_transmit(RS485_PHY, (uint8_t)ch);
return ch;
}

View File

@@ -6,8 +6,8 @@
#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 i2c_result_t tmp112a_write_register(uint8_t reg_addr, uint16_t value);
static i2c_result_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);
@@ -18,11 +18,11 @@ static uint16_t tmp112a_celsius_to_raw(float temperature);
\retval tmp112a_status_t
*/
tmp112a_status_t tmp112a_init(void) {
i2c_status_t i2c_status;
i2c_result_t i2c_status;
/* 配置传感器为默认设置 */
i2c_status = tmp112a_config(TMP112A_CONFIG_DEFAULT);
if (i2c_status != I2C_STATUS_SUCCESS) {
if (i2c_status != I2C_RESULT_SUCCESS) {
return TMP112A_STATUS_ERROR;
}
@@ -39,8 +39,8 @@ tmp112a_status_t tmp112a_init(void) {
\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;
i2c_result_t status = tmp112a_write_register(TMP112A_CONFIG_REG, config);
return (status == I2C_RESULT_SUCCESS) ? TMP112A_STATUS_SUCCESS : TMP112A_STATUS_ERROR;
}
/*!
@@ -51,7 +51,7 @@ tmp112a_status_t tmp112a_config(uint16_t config) {
*/
tmp112a_status_t tmp112a_read_temperature(tmp112a_result_t *result) {
uint16_t raw_data;
i2c_status_t status;
i2c_result_t status;
if (result == NULL) {
return TMP112A_STATUS_INVALID_PARAM;
@@ -59,7 +59,7 @@ tmp112a_status_t tmp112a_read_temperature(tmp112a_result_t *result) {
/* 读取温度寄存器 */
status = tmp112a_read_register(TMP112A_TEMP_REG, &raw_data);
if (status != I2C_STATUS_SUCCESS) {
if (status != I2C_RESULT_SUCCESS) {
return TMP112A_STATUS_ERROR;
}
@@ -76,7 +76,7 @@ tmp112a_status_t tmp112a_read_temperature(tmp112a_result_t *result) {
/* 检查报警标志 */
uint16_t config_reg;
status = tmp112a_read_register(TMP112A_CONFIG_REG, &config_reg);
if (status == I2C_STATUS_SUCCESS) {
if (status == I2C_RESULT_SUCCESS) {
result->alert_flag = (config_reg & TMP112A_CONFIG_AL) ? true : false;
} else {
result->alert_flag = false;
@@ -85,6 +85,11 @@ tmp112a_status_t tmp112a_read_temperature(tmp112a_result_t *result) {
return TMP112A_STATUS_SUCCESS;
}
void tmp112a_get_raw_temperature_value(uint8_t *value) {
i2c_read_16bits(TMP112A_ADDR, TMP112A_TEMP_REG, value);
return;
}
/*!
\brief 设置温度阈值
\param[in] low_temp: 低温阈值 (°C)
@@ -94,7 +99,7 @@ tmp112a_status_t tmp112a_read_temperature(tmp112a_result_t *result) {
*/
tmp112a_status_t tmp112a_set_thresholds(float low_temp, float high_temp) {
uint16_t low_raw, high_raw;
i2c_status_t status;
i2c_result_t status;
/* 参数验证 */
if (low_temp < TMP112A_TEMP_MIN || low_temp > TMP112A_TEMP_MAX ||
@@ -109,13 +114,13 @@ tmp112a_status_t tmp112a_set_thresholds(float low_temp, float high_temp) {
/* 写入低温阈值 */
status = tmp112a_write_register(TMP112A_TLOW_REG, low_raw);
if (status != I2C_STATUS_SUCCESS) {
if (status != I2C_RESULT_SUCCESS) {
return TMP112A_STATUS_ERROR;
}
/* 写入高温阈值 */
status = tmp112a_write_register(TMP112A_THIGH_REG, high_raw);
if (status != I2C_STATUS_SUCCESS) {
if (status != I2C_RESULT_SUCCESS) {
return TMP112A_STATUS_ERROR;
}
@@ -130,11 +135,11 @@ tmp112a_status_t tmp112a_set_thresholds(float low_temp, float high_temp) {
*/
tmp112a_status_t tmp112a_shutdown(void) {
uint16_t config_reg;
i2c_status_t status;
i2c_result_t status;
/* 读取当前配置 */
status = tmp112a_read_register(TMP112A_CONFIG_REG, &config_reg);
if (status != I2C_STATUS_SUCCESS) {
if (status != I2C_RESULT_SUCCESS) {
return TMP112A_STATUS_ERROR;
}
@@ -143,7 +148,7 @@ tmp112a_status_t tmp112a_shutdown(void) {
/* 写回配置 */
status = tmp112a_write_register(TMP112A_CONFIG_REG, config_reg);
return (status == I2C_STATUS_SUCCESS) ? TMP112A_STATUS_SUCCESS : TMP112A_STATUS_ERROR;
return (status == I2C_RESULT_SUCCESS) ? TMP112A_STATUS_SUCCESS : TMP112A_STATUS_ERROR;
}
/*!
@@ -154,11 +159,11 @@ tmp112a_status_t tmp112a_shutdown(void) {
*/
tmp112a_status_t tmp112a_wakeup(void) {
uint16_t config_reg;
i2c_status_t status;
i2c_result_t status;
/* 读取当前配置 */
status = tmp112a_read_register(TMP112A_CONFIG_REG, &config_reg);
if (status != I2C_STATUS_SUCCESS) {
if (status != I2C_RESULT_SUCCESS) {
return TMP112A_STATUS_ERROR;
}
@@ -167,7 +172,7 @@ tmp112a_status_t tmp112a_wakeup(void) {
/* 写回配置 */
status = tmp112a_write_register(TMP112A_CONFIG_REG, config_reg);
if (status != I2C_STATUS_SUCCESS) {
if (status != I2C_RESULT_SUCCESS) {
return TMP112A_STATUS_ERROR;
}
@@ -185,7 +190,7 @@ tmp112a_status_t tmp112a_wakeup(void) {
*/
tmp112a_status_t tmp112a_one_shot(tmp112a_result_t *result) {
uint16_t config_reg;
i2c_status_t status;
i2c_result_t status;
uint8_t timeout = 100; // 100ms超时
if (result == NULL) {
@@ -194,14 +199,14 @@ tmp112a_status_t tmp112a_one_shot(tmp112a_result_t *result) {
/* 读取当前配置 */
status = tmp112a_read_register(TMP112A_CONFIG_REG, &config_reg);
if (status != I2C_STATUS_SUCCESS) {
if (status != I2C_RESULT_SUCCESS) {
return TMP112A_STATUS_ERROR;
}
/* 启动单次转换 */
config_reg |= TMP112A_CONFIG_OS;
status = tmp112a_write_register(TMP112A_CONFIG_REG, config_reg);
if (status != I2C_STATUS_SUCCESS) {
if (status != I2C_RESULT_SUCCESS) {
return TMP112A_STATUS_ERROR;
}
@@ -209,7 +214,7 @@ tmp112a_status_t tmp112a_one_shot(tmp112a_result_t *result) {
do {
delay_ms(1);
status = tmp112a_read_register(TMP112A_CONFIG_REG, &config_reg);
if (status != I2C_STATUS_SUCCESS) {
if (status != I2C_RESULT_SUCCESS) {
return TMP112A_STATUS_ERROR;
}
timeout--;
@@ -253,9 +258,9 @@ const char* tmp112a_get_status_string(tmp112a_status_t status) {
\param[in] reg_addr: 寄存器地址
\param[in] value: 写入值
\param[out] none
\retval i2c_status_t
\retval i2c_result_t
*/
static i2c_status_t tmp112a_write_register(uint8_t reg_addr, uint16_t value) {
static i2c_result_t tmp112a_write_register(uint8_t reg_addr, uint16_t value) {
uint8_t data[2];
data[0] = (value >> 8) & 0xFF;
data[1] = value & 0xFF;
@@ -267,18 +272,18 @@ static i2c_status_t tmp112a_write_register(uint8_t reg_addr, uint16_t value) {
\brief 读取寄存器
\param[in] reg_addr: 寄存器地址
\param[out] value: 读取值指针
\retval i2c_status_t
\retval i2c_result_t
*/
static i2c_status_t tmp112a_read_register(uint8_t reg_addr, uint16_t *value) {
static i2c_result_t tmp112a_read_register(uint8_t reg_addr, uint16_t *value) {
uint8_t data[2];
i2c_status_t status;
i2c_result_t status;
if (value == NULL) {
return I2C_STATUS_INVALID_PARAM;
return I2C_RESULT_INVALID_PARAM;
}
status = i2c_read_16bits(TMP112A_ADDR, reg_addr, data);
if (status == I2C_STATUS_SUCCESS) {
if (status == I2C_RESULT_SUCCESS) {
*value = ((uint16_t)data[0] << 8) | data[1];
}

View File

@@ -3,6 +3,7 @@
#include "gd32e23x_rcu.h"
#include "gd32e23x_gpio.h"
#include "board_config.h"
#include "uart_ring_buffer.h"
void rs485_init(void) {
@@ -37,7 +38,7 @@ void rs485_init(void) {
usart_enable(RS485_PHY);
nvic_irq_enable(USART0_IRQn, 0);
nvic_irq_enable(RS485_IRQ, 0);
usart_interrupt_enable(RS485_PHY, USART_INT_RBNE);
// usart_interrupt_enable(RS485_PHY, USART_INT_IDLE);
@@ -70,3 +71,37 @@ void rs485_init(void) {
#endif // RS485_MAX13487
}
/******************************************************************************/
/* 具体的中断处理函数实现 */
/******************************************************************************/
void usart0_irq_handler(void) {
// 处理USART0的接收中断
if(usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE)) {
uint8_t data = usart_data_receive(USART0);
// 使用原有的环形缓冲区处理逻辑
(void)uart_ring_buffer_put(data); // 缓冲满时丢弃,返回值可用于统计
}
// 处理USART0的空闲中断
if(usart_interrupt_flag_get(USART0, USART_INT_FLAG_IDLE)) {
usart_interrupt_flag_clear(USART0, USART_INT_FLAG_IDLE);
// 在这里添加空闲中断处理逻辑
}
}
void usart1_irq_handler(void) {
// 处理USART1的接收中断
if(usart_interrupt_flag_get(USART1, USART_INT_FLAG_RBNE)) {
uint8_t data = usart_data_receive(USART1);
// 使用原有的环形缓冲区处理逻辑
(void)uart_ring_buffer_put(data); // 缓冲满时丢弃,返回值可用于统计
}
// 处理USART1的空闲中断
if(usart_interrupt_flag_get(USART1, USART_INT_FLAG_IDLE)) {
usart_interrupt_flag_clear(USART1, USART_INT_FLAG_IDLE);
// 在这里添加空闲中断处理逻辑
}
}

Binary file not shown.