2025-09-23 00:27:51 +08:00
|
|
|
#include "adc.h"
|
2025-11-10 02:08:03 +08:00
|
|
|
#include "Can.h"
|
2026-01-04 22:26:22 +08:00
|
|
|
#include "FOC.h"
|
2025-11-23 18:31:47 +08:00
|
|
|
#include "Filter.h"
|
2026-01-04 22:26:22 +08:00
|
|
|
#include "config.h"
|
2026-05-16 13:09:57 +08:00
|
|
|
#include "initial.h"
|
2025-10-19 23:20:28 +08:00
|
|
|
#include "math.h"
|
2026-01-04 22:26:22 +08:00
|
|
|
#include "systick.h"
|
2026-02-08 18:53:55 +08:00
|
|
|
#include <cstdint>
|
2025-10-30 20:20:14 +08:00
|
|
|
#include <cstring>
|
2025-11-01 20:29:11 +08:00
|
|
|
#include <gd32c10x_dma.h>
|
2025-09-23 00:27:51 +08:00
|
|
|
|
|
|
|
|
constexpr float ADC_SCALE = 3.3f / 4096.0f;
|
|
|
|
|
constexpr float TEMP_FACTOR = 1000.0f / 4.3f;
|
|
|
|
|
constexpr float VCC_FACTOR = 11.0f;
|
2025-10-19 23:20:28 +08:00
|
|
|
constexpr float CURRENT_FACTOR =
|
|
|
|
|
1.0f / 50.0f * 100.0f; // 50倍放大,100倍电流采样电阻, 单位mA
|
2025-11-10 02:08:03 +08:00
|
|
|
extern CAN_transmiter CAN0_transmiter;
|
2026-03-09 01:48:12 +08:00
|
|
|
extern FOC_Controller FOC;
|
2026-01-04 22:26:22 +08:00
|
|
|
sampler sampler; // 采样器初始化
|
2026-05-16 13:09:57 +08:00
|
|
|
u16 adc_value[3] = {0}; // regular DMA: MCU温度 / DRV温度 / VBUS
|
|
|
|
|
static volatile u16 adc_current_value[3] = {0}; // injected 同步采样: U / V / W
|
|
|
|
|
static volatile u8 adc_current_frame_ready = 0;
|
|
|
|
|
|
|
|
|
|
static inline float current_adc_to_ma(u16 raw_adc) {
|
|
|
|
|
return (raw_adc * ADC_SCALE + CURRENT_OFFSET) * CURRENT_FACTOR;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-01 20:29:11 +08:00
|
|
|
/*!
|
|
|
|
|
\brief configure the DMA peripheral
|
|
|
|
|
\param[in] none
|
|
|
|
|
\param[out] none
|
|
|
|
|
\retval none
|
|
|
|
|
*/
|
|
|
|
|
void dma_config(void) {
|
|
|
|
|
/* enable DMA0 clock */
|
|
|
|
|
rcu_periph_clock_enable(RCU_DMA0);
|
|
|
|
|
/* ADC_DMA_channel configuration */
|
|
|
|
|
dma_parameter_struct dma_data_parameter;
|
2025-10-02 02:52:07 +08:00
|
|
|
|
2025-11-01 20:29:11 +08:00
|
|
|
/* ADC DMA_channel configuration */
|
|
|
|
|
dma_deinit(DMA0, DMA_CH0);
|
|
|
|
|
|
|
|
|
|
/* initialize DMA single data mode */
|
|
|
|
|
dma_data_parameter.periph_addr = (uint32_t)(&ADC_RDATA(ADC0));
|
|
|
|
|
dma_data_parameter.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
|
|
|
|
|
dma_data_parameter.memory_addr = (uint32_t)(adc_value);
|
|
|
|
|
dma_data_parameter.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
|
|
|
|
|
dma_data_parameter.periph_width = DMA_PERIPHERAL_WIDTH_16BIT;
|
|
|
|
|
dma_data_parameter.memory_width = DMA_MEMORY_WIDTH_16BIT;
|
|
|
|
|
dma_data_parameter.direction = DMA_PERIPHERAL_TO_MEMORY;
|
2026-05-16 13:09:57 +08:00
|
|
|
dma_data_parameter.number = 3;
|
2025-11-01 20:29:11 +08:00
|
|
|
dma_data_parameter.priority = DMA_PRIORITY_HIGH;
|
|
|
|
|
dma_init(DMA0, DMA_CH0, &dma_data_parameter);
|
|
|
|
|
|
|
|
|
|
dma_circulation_enable(DMA0, DMA_CH0);
|
|
|
|
|
|
|
|
|
|
/* enable DMA channel */
|
|
|
|
|
dma_channel_enable(DMA0, DMA_CH0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
\brief configure the ADC peripheral
|
|
|
|
|
\param[in] none
|
|
|
|
|
\param[out] none
|
|
|
|
|
\retval none
|
|
|
|
|
*/
|
|
|
|
|
void adc_config(void) {
|
|
|
|
|
/* enable ADC clock */
|
|
|
|
|
rcu_periph_clock_enable(RCU_ADC0);
|
|
|
|
|
/* config ADC clock */
|
|
|
|
|
rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV4);
|
|
|
|
|
|
|
|
|
|
/* 复位ADC */
|
|
|
|
|
adc_deinit(ADC0);
|
|
|
|
|
/* ADC模式配置 */
|
|
|
|
|
adc_mode_config(ADC_MODE_FREE);
|
2026-05-16 13:09:57 +08:00
|
|
|
/* regular 组保留连续扫描,用于温度/VBUS 后台采样 */
|
2025-11-01 20:29:11 +08:00
|
|
|
adc_special_function_config(ADC0, ADC_CONTINUOUS_MODE, ENABLE);
|
|
|
|
|
adc_special_function_config(ADC0, ADC_SCAN_MODE, ENABLE);
|
2026-05-16 13:09:57 +08:00
|
|
|
/* injected 组用于 PWM 同步电流采样,关闭自动串联,独立由 TIMER1 触发 */
|
|
|
|
|
adc_special_function_config(ADC0, ADC_INSERTED_CHANNEL_AUTO, DISABLE);
|
2025-11-01 20:29:11 +08:00
|
|
|
/* ADC数据右对齐配置 */
|
|
|
|
|
adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT);
|
|
|
|
|
/* 使能ADC温度传感器和内部参考电压 */
|
|
|
|
|
adc_tempsensor_vrefint_enable();
|
|
|
|
|
|
|
|
|
|
/* 配置ADC通道长度 */
|
2026-05-16 13:09:57 +08:00
|
|
|
adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL, 3);
|
2025-11-01 20:29:11 +08:00
|
|
|
/* 配置ADC规则通道 */
|
|
|
|
|
adc_regular_channel_config(ADC0, 0, ADC_CHANNEL_16,
|
2026-03-19 23:49:08 +08:00
|
|
|
ADC_SAMPLETIME_41POINT5);
|
2025-11-22 02:21:33 +08:00
|
|
|
adc_regular_channel_config(ADC0, 1, ADC_CHANNEL_9, ADC_SAMPLETIME_28POINT5);
|
2026-05-16 13:09:57 +08:00
|
|
|
adc_regular_channel_config(ADC0, 2, ADC_CHANNEL_6, ADC_SAMPLETIME_28POINT5);
|
2025-11-01 20:29:11 +08:00
|
|
|
|
2026-05-16 13:09:57 +08:00
|
|
|
/* regular 组继续软件触发连续转换:本组只服务低速量 */
|
2025-11-01 20:29:11 +08:00
|
|
|
adc_external_trigger_source_config(ADC0, ADC_REGULAR_CHANNEL,
|
|
|
|
|
ADC0_1_EXTTRIG_REGULAR_NONE);
|
|
|
|
|
adc_external_trigger_config(ADC0, ADC_REGULAR_CHANNEL, ENABLE);
|
|
|
|
|
|
2026-05-16 13:09:57 +08:00
|
|
|
/* injected 组改为 TIMER1 CH3 导出的 TRGO 触发,实现三相同一采样时刻锁存 */
|
|
|
|
|
adc_channel_length_config(ADC0, ADC_INSERTED_CHANNEL, 4);
|
|
|
|
|
adc_inserted_channel_config(ADC0, 0, ADC_CHANNEL_3, ADC_SAMPLETIME_55POINT5);
|
|
|
|
|
adc_inserted_channel_config(ADC0, 1, ADC_CHANNEL_3, ADC_SAMPLETIME_55POINT5);
|
|
|
|
|
adc_inserted_channel_config(ADC0, 2, ADC_CHANNEL_4, ADC_SAMPLETIME_55POINT5);
|
|
|
|
|
adc_inserted_channel_config(ADC0, 3, ADC_CHANNEL_5, ADC_SAMPLETIME_55POINT5);
|
|
|
|
|
adc_external_trigger_source_config(ADC0, ADC_INSERTED_CHANNEL,
|
|
|
|
|
ADC0_1_EXTTRIG_INSERTED_T1_TRGO);
|
|
|
|
|
adc_external_trigger_config(ADC0, ADC_INSERTED_CHANNEL, ENABLE);
|
|
|
|
|
|
2025-11-01 20:29:11 +08:00
|
|
|
/* 使能ADC接口 */
|
|
|
|
|
adc_enable(ADC0);
|
|
|
|
|
/* ADC校准 */
|
|
|
|
|
adc_calibration_enable(ADC0);
|
|
|
|
|
/* 使能ADC DMA功能 */
|
|
|
|
|
adc_dma_mode_enable(ADC0);
|
2026-05-16 13:09:57 +08:00
|
|
|
/* injected 完成中断负责搬运完整三相样本 */
|
|
|
|
|
adc_interrupt_enable(ADC0, ADC_INT_EOIC);
|
|
|
|
|
/* 先启动 regular 组连续后台采样 */
|
2025-11-01 20:29:11 +08:00
|
|
|
adc_software_trigger_enable(ADC0, ADC_REGULAR_CHANNEL);
|
|
|
|
|
}
|
2025-10-19 23:20:28 +08:00
|
|
|
/**
|
|
|
|
|
* @brief 采样类初始化
|
|
|
|
|
*/
|
2025-12-08 02:48:59 +08:00
|
|
|
u8 sampler::init() {
|
2025-11-01 20:29:11 +08:00
|
|
|
dma_config(); // DMA采样ADC初始化
|
|
|
|
|
adc_config(); // ADC初始化
|
2026-05-16 13:09:57 +08:00
|
|
|
current_sample_head = 0;
|
|
|
|
|
adc_current_frame_ready = 0;
|
2025-12-08 02:48:59 +08:00
|
|
|
delay_1ms(1);
|
2026-05-16 13:09:57 +08:00
|
|
|
|
|
|
|
|
const float current_init = current_adc_to_ma(
|
|
|
|
|
static_cast<u16>((-CURRENT_OFFSET / ADC_SCALE) + 0.5f));
|
2025-11-01 23:41:32 +08:00
|
|
|
// 初始采样
|
2026-05-16 13:09:57 +08:00
|
|
|
for (int i = 0; i < ADC_FILTER_SIZE; i++) {
|
2025-10-19 23:20:28 +08:00
|
|
|
mcu_temp_samples[i] =
|
|
|
|
|
(1.43f - adc_value[0] * ADC_SCALE) * TEMP_FACTOR + 35.0f;
|
2025-10-30 20:20:14 +08:00
|
|
|
|
2026-05-16 13:09:57 +08:00
|
|
|
currU_samples[i] = current_init;
|
|
|
|
|
currV_samples[i] = current_init;
|
|
|
|
|
currW_samples[i] = current_init;
|
2025-10-30 20:20:14 +08:00
|
|
|
|
2026-05-16 13:09:57 +08:00
|
|
|
vbus_samples[i] = adc_value[2] * ADC_SCALE * VCC_FACTOR;
|
2025-09-23 00:27:51 +08:00
|
|
|
|
2025-10-19 23:20:28 +08:00
|
|
|
float R_now = (float)adc_value[1] / (4096 - (float)adc_value[1]) * 4700;
|
|
|
|
|
drv_temp_samples[i] =
|
|
|
|
|
1.0f / (0.00335f + 0.00025f * log(R_now / 10000.0f)) - 273.15f;
|
2025-12-08 02:48:59 +08:00
|
|
|
delay_1ms(1);
|
2025-10-19 23:20:28 +08:00
|
|
|
}
|
2025-12-08 02:48:59 +08:00
|
|
|
return 0;
|
2025-10-19 23:20:28 +08:00
|
|
|
}
|
2025-09-23 00:27:51 +08:00
|
|
|
/**
|
|
|
|
|
* @brief 获取MCU温度
|
|
|
|
|
* @return float
|
|
|
|
|
* 温度值,单位为摄氏度,范围应在-40到145之间,如果读取失败则返回0.0f
|
|
|
|
|
*/
|
2025-10-19 23:20:28 +08:00
|
|
|
float sampler::get_mcu_temp() {
|
2026-05-16 13:09:57 +08:00
|
|
|
tempora_sample = medium_filter(mcu_temp_samples, ADC_FILTER_SIZE);
|
2025-10-19 23:20:28 +08:00
|
|
|
// 检查温度读数是否在有效范围内
|
2025-11-23 18:31:47 +08:00
|
|
|
if (tempora_sample < MIN_TEMP or tempora_sample > MAX_TEMP) {
|
2025-10-19 23:20:28 +08:00
|
|
|
return ERROR_ADC_VAL;
|
2025-09-23 00:27:51 +08:00
|
|
|
}
|
2025-10-19 23:20:28 +08:00
|
|
|
return tempora_sample;
|
2025-10-30 20:20:14 +08:00
|
|
|
}
|
2025-09-23 00:27:51 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 获取电机驱动温度
|
|
|
|
|
* @return float 温度值,单位为摄氏度
|
|
|
|
|
*
|
|
|
|
|
* 通过热敏电阻分压电路计算温度值,使用Steinhart-Hart方程进行温度转换。
|
|
|
|
|
* 如果计算结果超出有效范围(-40到255摄氏度),则重新采样计算。
|
|
|
|
|
*/
|
2025-10-19 23:20:28 +08:00
|
|
|
float sampler::get_drv_temp() {
|
2026-05-16 13:09:57 +08:00
|
|
|
tempora_sample = medium_filter(drv_temp_samples, ADC_FILTER_SIZE);
|
2025-10-19 23:20:28 +08:00
|
|
|
// 检查温度读数是否在有效范围内
|
|
|
|
|
if (tempora_sample < MIN_TEMP || tempora_sample > MAX_TEMP) {
|
|
|
|
|
return ERROR_ADC_VAL;
|
2025-09-23 00:27:51 +08:00
|
|
|
}
|
2025-10-19 23:20:28 +08:00
|
|
|
return tempora_sample;
|
2025-09-23 00:27:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 获取电流值
|
|
|
|
|
* @param type 电流类型,'U'表示U相电流,'V'表示V相电流,'W'表示W相电流
|
|
|
|
|
* @return float 电流值,单位为安培
|
|
|
|
|
*
|
|
|
|
|
* 通过ADC采样计算各相电流值
|
|
|
|
|
*/
|
2025-10-19 23:20:28 +08:00
|
|
|
float sampler::get_current(char type) {
|
2026-05-16 13:09:57 +08:00
|
|
|
u8 idx = (current_sample_head == 0U) ? (ADC_FILTER_SIZE - 1U)
|
|
|
|
|
: (current_sample_head - 1U);
|
2025-09-23 00:27:51 +08:00
|
|
|
switch (type) {
|
|
|
|
|
case 'U': {
|
2026-02-08 18:53:55 +08:00
|
|
|
tempora_sample = currU_samples[idx];
|
2025-09-23 00:27:51 +08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 'V': {
|
2026-02-08 18:53:55 +08:00
|
|
|
tempora_sample = currV_samples[idx];
|
2025-09-23 00:27:51 +08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 'W': {
|
2026-02-08 18:53:55 +08:00
|
|
|
tempora_sample = currW_samples[idx];
|
2025-09-23 00:27:51 +08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default: {
|
|
|
|
|
return ERROR_ADC_VAL;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-11-23 18:31:47 +08:00
|
|
|
if (tempora_sample < MIN_CURRENT or tempora_sample > MAX_CURRENT) {
|
2025-09-23 00:27:51 +08:00
|
|
|
return ERROR_ADC_VAL;
|
|
|
|
|
}
|
2025-12-05 00:46:19 +08:00
|
|
|
|
2025-10-19 23:20:28 +08:00
|
|
|
return tempora_sample;
|
2025-09-23 00:27:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 获取供电电压
|
|
|
|
|
* @return float 电压值,单位为伏特
|
|
|
|
|
*
|
|
|
|
|
* 通过ADC采样计算供电电压值,使用迭代方式确保获取有效的电压读数。
|
|
|
|
|
* 如果计算结果超出有效范围(-40到255伏特),则重新采样计算。
|
|
|
|
|
*/
|
2025-10-19 23:20:28 +08:00
|
|
|
float sampler::get_vbus_voltage() {
|
2026-05-16 13:09:57 +08:00
|
|
|
tempora_sample = medium_filter(vbus_samples, ADC_FILTER_SIZE);
|
2025-10-19 23:20:28 +08:00
|
|
|
// 检查电压读数是否在有效范围内
|
2026-05-16 13:09:57 +08:00
|
|
|
const u8 mid = ADC_FILTER_SIZE / 2;
|
|
|
|
|
if (vbus_samples[mid] > MAX_VCC or vbus_samples[mid] < MIN_VCC) {
|
2025-10-19 23:20:28 +08:00
|
|
|
return ERROR_ADC_VAL;
|
2025-09-23 00:27:51 +08:00
|
|
|
}
|
2025-10-30 20:20:14 +08:00
|
|
|
return tempora_sample;
|
2025-09-23 00:27:51 +08:00
|
|
|
}
|
2025-10-19 23:20:28 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 更新MCU温度
|
|
|
|
|
*
|
|
|
|
|
* 通过ADC采样计算MCU温度值,使用Steinhart-Hart方程进行温度转换。
|
|
|
|
|
* 如果计算结果超出有效范围(-40到255摄氏度),则重新采样计算。
|
|
|
|
|
*/
|
|
|
|
|
void sampler::update_mcu_temp() {
|
2025-10-31 01:17:52 +08:00
|
|
|
memmove(mcu_temp_samples, mcu_temp_samples + 1,
|
2026-05-16 13:09:57 +08:00
|
|
|
(ADC_FILTER_SIZE - 1) * sizeof(float));
|
|
|
|
|
mcu_temp_samples[ADC_FILTER_SIZE - 1] =
|
2025-10-30 20:20:14 +08:00
|
|
|
(1.43f - adc_value[0] * ADC_SCALE) * TEMP_FACTOR + 35.0f;
|
2025-10-19 23:20:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 更新MCU温度
|
|
|
|
|
*/
|
|
|
|
|
void sampler::update_drv_temp() {
|
2025-10-31 01:17:52 +08:00
|
|
|
memmove(drv_temp_samples, drv_temp_samples + 1,
|
2026-05-16 13:09:57 +08:00
|
|
|
(ADC_FILTER_SIZE - 1) * sizeof(float));
|
2025-10-19 23:20:28 +08:00
|
|
|
float R_now = (float)adc_value[1] / (4096 - (float)adc_value[1]) * 4700;
|
2026-05-16 13:09:57 +08:00
|
|
|
drv_temp_samples[ADC_FILTER_SIZE - 1] =
|
2025-10-30 20:20:14 +08:00
|
|
|
1.0f / (0.00335f + 0.00025f * log(R_now / 10000.0f)) - 273.15f;
|
2025-10-19 23:20:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 更新相电流
|
|
|
|
|
*/
|
|
|
|
|
void sampler::update_current() {
|
2026-05-16 13:09:57 +08:00
|
|
|
if (adc_current_frame_ready == 0U) {
|
|
|
|
|
return;
|
2025-11-23 18:31:47 +08:00
|
|
|
}
|
2026-05-16 13:09:57 +08:00
|
|
|
|
|
|
|
|
const u8 idx = current_sample_head;
|
|
|
|
|
currU_samples[idx] = current_adc_to_ma(adc_current_value[0]);
|
|
|
|
|
currV_samples[idx] = current_adc_to_ma(adc_current_value[1]);
|
|
|
|
|
currW_samples[idx] = current_adc_to_ma(adc_current_value[2]);
|
|
|
|
|
|
|
|
|
|
current_sample_head++;
|
|
|
|
|
if (current_sample_head >= ADC_FILTER_SIZE) {
|
|
|
|
|
current_sample_head = 0;
|
|
|
|
|
}
|
|
|
|
|
adc_current_frame_ready = 0U;
|
2025-10-19 23:20:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 更新供电电压
|
|
|
|
|
*/
|
|
|
|
|
void sampler::update_vbus_voltage() {
|
2026-05-16 13:09:57 +08:00
|
|
|
memmove(vbus_samples, vbus_samples + 1, (ADC_FILTER_SIZE - 1) * sizeof(float));
|
|
|
|
|
vbus_samples[ADC_FILTER_SIZE - 1] = adc_value[2] * ADC_SCALE * VCC_FACTOR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern "C" void ADC0_1_IRQHandler(void) {
|
|
|
|
|
if (adc_interrupt_flag_get(ADC0, ADC_INT_FLAG_EOIC) != RESET) {
|
|
|
|
|
/* 丢弃 rank0 dummy,真实样本使用 rank1/2/3,对应 U/V/W 同一 PWM 时刻 */
|
|
|
|
|
(void)adc_inserted_data_read(ADC0, ADC_INSERTED_CHANNEL_0);
|
|
|
|
|
adc_current_value[0] = adc_inserted_data_read(ADC0, ADC_INSERTED_CHANNEL_1);
|
|
|
|
|
adc_current_value[1] = adc_inserted_data_read(ADC0, ADC_INSERTED_CHANNEL_2);
|
|
|
|
|
adc_current_value[2] = adc_inserted_data_read(ADC0, ADC_INSERTED_CHANNEL_3);
|
|
|
|
|
adc_current_frame_ready = 1U;
|
|
|
|
|
adc_interrupt_flag_clear(ADC0, ADC_INT_FLAG_EOIC);
|
|
|
|
|
|
|
|
|
|
extern TaskScheduler scheduler;
|
|
|
|
|
scheduler.taskFlags |= (1u << TASK_CURRENT_UPDATE);
|
|
|
|
|
}
|
|
|
|
|
}
|