2026-05-23 22:46:32 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @file device/Timer.cpp
|
|
|
|
|
|
* @brief 底层设备驱动与外设封装源文件
|
|
|
|
|
|
* @note 本文件仅补充 Doxygen 文档注释,不改变代码逻辑。
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2025-09-23 00:27:51 +08:00
|
|
|
|
#include "Timer.h"
|
2025-11-22 23:59:43 +08:00
|
|
|
|
#include "Can.h"
|
2026-01-04 22:26:22 +08:00
|
|
|
|
#include "FOC.h"
|
2025-10-19 23:20:28 +08:00
|
|
|
|
#include "Timer.h"
|
2026-09-01 02:46:25 +08:00
|
|
|
|
#include "config.h"
|
2025-09-23 00:27:51 +08:00
|
|
|
|
#include "gd32c10x.h"
|
|
|
|
|
|
#include "gd32c10x_gpio.h"
|
2025-10-29 21:22:10 +08:00
|
|
|
|
#include "gd32c10x_misc.h"
|
2025-10-16 21:43:34 +08:00
|
|
|
|
#include "gd32c10x_rcu.h"
|
2025-09-23 00:27:51 +08:00
|
|
|
|
#include "gd32c10x_timer.h"
|
2025-11-13 02:04:58 +08:00
|
|
|
|
#include "initial.h"
|
2025-09-23 00:27:51 +08:00
|
|
|
|
|
2026-05-24 01:21:41 +08:00
|
|
|
|
volatile u32 total_us100_counter = 0; // TIMER2 ISR 内部 100us 调度索引
|
2026-03-09 01:48:12 +08:00
|
|
|
|
extern FOC_Controller FOC;
|
2025-11-13 02:04:58 +08:00
|
|
|
|
extern TaskScheduler scheduler;
|
2025-11-22 23:59:43 +08:00
|
|
|
|
extern CAN_transmiter CAN0_transmiter;
|
2025-10-16 21:43:34 +08:00
|
|
|
|
// 添加系统运行时间计数器
|
2025-12-16 22:39:39 +08:00
|
|
|
|
extern u32 cnt_test;
|
2025-10-16 21:43:34 +08:00
|
|
|
|
|
2026-03-09 01:35:26 +08:00
|
|
|
|
// 任务调度查找表 - 每 100us 执行的任务 (1ms 周期内)
|
2026-09-01 02:46:25 +08:00
|
|
|
|
// MCU/DRV 温度保持 1kHz:热模型兼作过流保护,温度积累按 1ms 步进,
|
|
|
|
|
|
// 每毫秒都要拿到新温度采样。
|
2026-03-09 01:35:26 +08:00
|
|
|
|
static const u32 task_schedule_100us[10] = {
|
|
|
|
|
|
(1u << TASK_MCU_TEMP_UPDATE), // 0: MCU 温度更新
|
|
|
|
|
|
(1u << TASK_DRV_TEMP_UPDATE), // 1: 驱动温度更新
|
|
|
|
|
|
(1u << TASK_VBUS_UPDATE), // 2: 电压更新
|
2026-09-01 02:46:25 +08:00
|
|
|
|
0, 0, 0, 0, 0, 0, // 3~8: 空闲
|
2026-03-09 01:35:26 +08:00
|
|
|
|
(1u << TASK_CAN_TRANSMIT) // 9: CAN 发送
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 任务调度查找表 - 每 1ms 执行的任务 (10ms 周期内)
|
|
|
|
|
|
static const u32 task_schedule_1ms[10] = {
|
|
|
|
|
|
(1u << TASK_THERMAL_CONTROL), // 0: 热量控制
|
|
|
|
|
|
(1u << TASK_MAGNETIC_CHECK), // 1: 磁感应强度检测
|
|
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0 // 空闲
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 任务调度查找表 - 每 10ms 执行的任务 (100ms 周期内)
|
|
|
|
|
|
static const u32 task_schedule_10ms[10] = {
|
|
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // 当前无任务,预留扩展
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 任务调度查找表 - 每 100ms 执行的任务 (1s 周期内)
|
|
|
|
|
|
static const u32 task_schedule_100ms[10] = {
|
|
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // 当前无任务,预留扩展
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-09-23 00:27:51 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 设置引脚中断优先级
|
|
|
|
|
|
* @return void
|
|
|
|
|
|
*
|
2026-03-09 01:35:26 +08:00
|
|
|
|
* 设置引脚中断优先级
|
2025-09-23 00:27:51 +08:00
|
|
|
|
*/
|
|
|
|
|
|
void nvic_config(void) {
|
2026-03-09 01:35:26 +08:00
|
|
|
|
/* 设置 NVIC 优先级分组,1 位抢占优先级,3 位响应优先级 */
|
2025-10-02 02:52:07 +08:00
|
|
|
|
nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
|
2026-03-09 01:35:26 +08:00
|
|
|
|
/* 使能 TIMER2 中断,设置抢占优先级为 1,子优先级为 1 */
|
2025-11-23 18:31:47 +08:00
|
|
|
|
nvic_irq_enable(TIMER2_IRQn, 0, 1);
|
2026-05-16 13:09:57 +08:00
|
|
|
|
nvic_irq_enable(ADC0_1_IRQn, 0, 0);
|
2025-12-16 22:39:39 +08:00
|
|
|
|
nvic_irq_enable(TIMER1_IRQn, 0, 2);
|
2025-09-23 00:27:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-03-09 01:35:26 +08:00
|
|
|
|
* @brief 配置定时器 2-任务管理
|
2025-09-23 00:27:51 +08:00
|
|
|
|
* @return void
|
|
|
|
|
|
*
|
2026-03-09 01:35:26 +08:00
|
|
|
|
* 配置定时器 2,用于定时执行 FOC 控制、采样、CAN 发送等任务
|
2025-09-23 00:27:51 +08:00
|
|
|
|
*/
|
2025-10-16 21:43:34 +08:00
|
|
|
|
void TIMER2_config(void) {
|
2025-09-23 00:27:51 +08:00
|
|
|
|
/* ----------------------------------------------------------------------------
|
2025-10-16 21:43:34 +08:00
|
|
|
|
TIMER2 Configuration:
|
|
|
|
|
|
TIMER2CLK = SystemCoreClock/120 = 1MHz, the period is 100us(100/1000000 =
|
2025-10-14 00:49:58 +08:00
|
|
|
|
100us).
|
2025-09-23 00:27:51 +08:00
|
|
|
|
----------------------------------------------------------------------------
|
|
|
|
|
|
*/
|
2025-10-02 02:52:07 +08:00
|
|
|
|
timer_parameter_struct timer_struct;
|
2025-09-23 00:27:51 +08:00
|
|
|
|
|
2026-03-09 01:35:26 +08:00
|
|
|
|
/* 使能 TIMER2 时钟 */
|
2025-10-16 21:43:34 +08:00
|
|
|
|
rcu_periph_clock_enable(RCU_TIMER2);
|
2025-09-23 00:27:51 +08:00
|
|
|
|
|
2026-03-09 01:35:26 +08:00
|
|
|
|
/* 复位 TIMER2 外设到默认状态 */
|
2025-10-16 21:43:34 +08:00
|
|
|
|
timer_deinit(TIMER2);
|
2026-03-09 01:35:26 +08:00
|
|
|
|
/* 初始化 TIMER 初始化参数结构体 */
|
2025-10-02 02:52:07 +08:00
|
|
|
|
timer_struct_para_init(&timer_struct);
|
2026-03-09 01:35:26 +08:00
|
|
|
|
/* TIMER2 配置参数设置 */
|
2025-10-02 02:52:07 +08:00
|
|
|
|
timer_struct.prescaler =
|
2026-03-09 01:35:26 +08:00
|
|
|
|
TIMER2_PRESCALER; /* 预分频值:120 分频 (从 0 开始计数) */
|
2025-10-02 02:52:07 +08:00
|
|
|
|
timer_struct.alignedmode = TIMER_COUNTER_EDGE; /* 边沿对齐模式 */
|
|
|
|
|
|
timer_struct.counterdirection = TIMER_COUNTER_UP; /* 向上计数模式 */
|
|
|
|
|
|
timer_struct.period =
|
2026-03-09 01:35:26 +08:00
|
|
|
|
TIMER2_PERIOD; /* 自动重装载值:100 次计数 (从 0 开始计数) */
|
|
|
|
|
|
timer_struct.clockdivision = TIMER_CKDIV_DIV1; /* 时钟分频值:1 分频 */
|
2025-10-16 21:43:34 +08:00
|
|
|
|
timer_init(TIMER2, &timer_struct);
|
2025-09-23 00:27:51 +08:00
|
|
|
|
|
|
|
|
|
|
/* 清除更新中断标志位 */
|
2025-10-16 21:43:34 +08:00
|
|
|
|
timer_interrupt_flag_clear(TIMER2, TIMER_INT_FLAG_UP);
|
2025-09-23 00:27:51 +08:00
|
|
|
|
/* 使能更新中断 */
|
2025-10-16 21:43:34 +08:00
|
|
|
|
timer_interrupt_enable(TIMER2, TIMER_INT_UP);
|
2025-09-23 00:27:51 +08:00
|
|
|
|
|
2026-03-09 01:35:26 +08:00
|
|
|
|
/* 使能 TIMER2 */
|
2025-10-16 21:43:34 +08:00
|
|
|
|
timer_enable(TIMER2);
|
2025-09-23 00:27:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-03-09 01:35:26 +08:00
|
|
|
|
* @brief 配置 TIMER2-任务管理中断
|
2025-09-23 00:27:51 +08:00
|
|
|
|
* @return void
|
|
|
|
|
|
*/
|
|
|
|
|
|
void User_timer_init(void) {
|
2026-03-09 01:35:26 +08:00
|
|
|
|
/* 配置 TIMER2 */
|
2025-10-16 21:43:34 +08:00
|
|
|
|
TIMER2_config();
|
2026-03-09 01:35:26 +08:00
|
|
|
|
/* 配置 TIMER2 中断 */
|
2025-10-02 02:52:07 +08:00
|
|
|
|
nvic_config();
|
2025-09-23 00:27:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-03-09 01:35:26 +08:00
|
|
|
|
* @brief TIMER2-任务管理中断处理函数 (优化版)
|
2025-09-23 00:27:51 +08:00
|
|
|
|
* @return void
|
2026-03-09 01:35:26 +08:00
|
|
|
|
* @note 使用查找表替代多层 switch-case,减少代码复杂度和执行时间
|
|
|
|
|
|
* 中断周期:50us
|
|
|
|
|
|
* - 每 50us: 电机控制任务 (角度、速度更新)
|
|
|
|
|
|
* - 每 100us: 传感器采样、CAN 通信
|
|
|
|
|
|
* - 每 1ms: 热量控制、磁检
|
|
|
|
|
|
* - 每 10ms/100ms/1s: 预留扩展任务
|
2025-09-23 00:27:51 +08:00
|
|
|
|
*/
|
2025-10-16 21:43:34 +08:00
|
|
|
|
void TIMER2_IRQHandler(void) {
|
2026-03-09 01:35:26 +08:00
|
|
|
|
// 快速路径:检查并清除中断标志
|
|
|
|
|
|
if (timer_interrupt_flag_get(TIMER2, TIMER_INT_UP) == RESET) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
timer_interrupt_flag_clear(TIMER2, TIMER_INT_UP);
|
2025-11-13 02:04:58 +08:00
|
|
|
|
|
2026-03-09 01:48:12 +08:00
|
|
|
|
// 固定任务:每 50us 执行电机控制
|
|
|
|
|
|
scheduler.taskFlags |= (1u << TASK_ANGLE_UPDATE) |
|
|
|
|
|
|
(1u << TASK_VELOCITY_UPDATE) |
|
|
|
|
|
|
(1u << TASK_MOTOR_CONTROL);
|
2025-11-23 18:31:47 +08:00
|
|
|
|
|
2026-03-09 01:48:12 +08:00
|
|
|
|
// 每 100us 执行一次的任务(使用位运算替代布尔变量)
|
|
|
|
|
|
static u32 call_counter = 0;
|
|
|
|
|
|
call_counter++;
|
|
|
|
|
|
|
|
|
|
|
|
if ((call_counter & 1u) == 0u) { // 偶数次调用(每 100us)
|
|
|
|
|
|
// 使用查找表设置 1ms 周期内的任务
|
|
|
|
|
|
scheduler.taskFlags |= task_schedule_100us[total_us100_counter];
|
|
|
|
|
|
|
|
|
|
|
|
total_us100_counter++;
|
|
|
|
|
|
if (total_us100_counter >= 10u) {
|
|
|
|
|
|
total_us100_counter = 0;
|
|
|
|
|
|
|
|
|
|
|
|
// 每 1ms 执行的任务
|
|
|
|
|
|
static u32 ms1_counter = 0;
|
|
|
|
|
|
ms1_counter++;
|
|
|
|
|
|
if (ms1_counter >= 10u) {
|
|
|
|
|
|
ms1_counter = 0;
|
|
|
|
|
|
|
|
|
|
|
|
// 每 10ms 执行的任务
|
|
|
|
|
|
static u32 ms10_counter = 0;
|
|
|
|
|
|
ms10_counter++;
|
|
|
|
|
|
if (ms10_counter >= 10u) {
|
|
|
|
|
|
ms10_counter = 0;
|
|
|
|
|
|
|
|
|
|
|
|
// 每 100ms 执行的任务
|
|
|
|
|
|
static u32 ms100_counter = 0;
|
|
|
|
|
|
ms100_counter++;
|
|
|
|
|
|
if (ms100_counter >= 10u) {
|
|
|
|
|
|
ms100_counter = 0;
|
|
|
|
|
|
// 每 1s 任务预留位置
|
|
|
|
|
|
}
|
|
|
|
|
|
scheduler.taskFlags |= task_schedule_100ms[ms100_counter];
|
|
|
|
|
|
}
|
|
|
|
|
|
scheduler.taskFlags |= task_schedule_10ms[ms10_counter];
|
|
|
|
|
|
}
|
|
|
|
|
|
scheduler.taskFlags |= task_schedule_1ms[ms1_counter];
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-23 00:27:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-03-09 01:35:26 +08:00
|
|
|
|
* @brief TIMER1-PWM GPIO 配置函数
|
2025-09-23 00:27:51 +08:00
|
|
|
|
*/
|
2025-10-16 21:43:34 +08:00
|
|
|
|
void timer1_gpio_config(void) {
|
2025-09-23 00:27:51 +08:00
|
|
|
|
// 时钟使能
|
|
|
|
|
|
rcu_periph_clock_enable(RCU_GPIOA);
|
2025-10-16 21:43:34 +08:00
|
|
|
|
rcu_periph_clock_enable(RCU_GPIOB);
|
2025-09-23 00:27:51 +08:00
|
|
|
|
rcu_periph_clock_enable(RCU_AF);
|
2025-10-16 21:43:34 +08:00
|
|
|
|
rcu_periph_clock_enable(RCU_TIMER1);
|
2025-09-23 00:27:51 +08:00
|
|
|
|
|
2026-03-09 01:35:26 +08:00
|
|
|
|
// TIMER1 GPIO 初始化
|
2025-09-23 00:27:51 +08:00
|
|
|
|
gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_0);
|
|
|
|
|
|
gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
|
|
|
|
|
|
gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_2);
|
|
|
|
|
|
|
2026-09-01 02:46:25 +08:00
|
|
|
|
#if GATE_DRIVER_DRV8323
|
|
|
|
|
|
/* 新板:PB12(DRVSLP=ENABLE+INLx) 由 gate_driver 统一管理——
|
|
|
|
|
|
* gate_driver_preinit() 已置低保持芯片睡眠,gate_driver_bringup()
|
|
|
|
|
|
* 在 SPI 配置完成后才拉高。此处严禁提前置高(SPI 未配置前芯片
|
|
|
|
|
|
* 带默认 6x PWM 模式醒来,INLx=1 时上管永远无法导通)。
|
|
|
|
|
|
* PB0 板上接地未用,不配置。 */
|
|
|
|
|
|
#else
|
2025-10-19 23:20:28 +08:00
|
|
|
|
gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ,
|
|
|
|
|
|
GPIO_PIN_0 | GPIO_PIN_12);
|
2025-10-16 21:43:34 +08:00
|
|
|
|
gpio_bit_set(GPIOB, GPIO_PIN_0);
|
|
|
|
|
|
gpio_bit_set(GPIOB, GPIO_PIN_12);
|
2026-09-01 02:46:25 +08:00
|
|
|
|
#endif
|
2025-10-16 21:43:34 +08:00
|
|
|
|
|
2026-03-09 01:35:26 +08:00
|
|
|
|
/* 配置 TIMER1 完全重映射*/
|
2025-10-16 21:43:34 +08:00
|
|
|
|
gpio_pin_remap_config(GPIO_TIMER1_FULL_REMAP, DISABLE);
|
2025-09-23 00:27:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-01-04 22:26:22 +08:00
|
|
|
|
* @brief TIMER1-PWM 配置函数
|
2025-09-23 00:27:51 +08:00
|
|
|
|
*/
|
2025-10-16 21:43:34 +08:00
|
|
|
|
void timer1_pwm_config(void) {
|
2025-10-19 23:20:28 +08:00
|
|
|
|
timer_parameter_struct timer_initpara;
|
|
|
|
|
|
timer_oc_parameter_struct timer_ocinitpara;
|
2025-09-23 00:27:51 +08:00
|
|
|
|
|
2026-03-09 01:35:26 +08:00
|
|
|
|
// 使能 TIMER1 时钟
|
2025-10-19 23:20:28 +08:00
|
|
|
|
rcu_periph_clock_enable(RCU_TIMER1);
|
2025-09-23 00:27:51 +08:00
|
|
|
|
|
2026-03-09 01:35:26 +08:00
|
|
|
|
// 复位 TIMER1
|
2025-10-19 23:20:28 +08:00
|
|
|
|
timer_deinit(TIMER1);
|
2025-09-23 00:27:51 +08:00
|
|
|
|
|
2026-03-09 01:35:26 +08:00
|
|
|
|
// 配置 TIMER1 基本参数
|
2025-10-19 23:20:28 +08:00
|
|
|
|
timer_struct_para_init(&timer_initpara);
|
|
|
|
|
|
timer_initpara.prescaler = TIMER1_PRESCALER; // 预分频器
|
2026-05-16 13:09:57 +08:00
|
|
|
|
timer_initpara.alignedmode = TIMER_COUNTER_CENTER_BOTH; // 中心对齐,给采样留对称平台窗
|
|
|
|
|
|
timer_initpara.counterdirection = TIMER_COUNTER_UP;
|
2025-10-19 23:20:28 +08:00
|
|
|
|
timer_initpara.period = TIMER1_PERIOD; // 计数周期
|
|
|
|
|
|
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
|
|
|
|
|
|
timer_init(TIMER1, &timer_initpara);
|
2025-09-23 00:27:51 +08:00
|
|
|
|
|
2025-10-19 23:20:28 +08:00
|
|
|
|
// 配置通道输出参数
|
|
|
|
|
|
timer_channel_output_struct_para_init(&timer_ocinitpara);
|
|
|
|
|
|
timer_ocinitpara.outputstate = TIMER_CCX_ENABLE;
|
|
|
|
|
|
timer_ocinitpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
|
2025-09-23 00:27:51 +08:00
|
|
|
|
|
2026-03-09 01:35:26 +08:00
|
|
|
|
// 配置三个通道为 PWM 模式
|
2025-10-19 23:20:28 +08:00
|
|
|
|
timer_channel_output_config(TIMER1, TIMER_CH_0, &timer_ocinitpara);
|
|
|
|
|
|
timer_channel_output_config(TIMER1, TIMER_CH_1, &timer_ocinitpara);
|
|
|
|
|
|
timer_channel_output_config(TIMER1, TIMER_CH_2, &timer_ocinitpara);
|
2025-09-23 00:27:51 +08:00
|
|
|
|
|
2026-05-16 13:09:57 +08:00
|
|
|
|
// 配置第四个通道 (CH3) 为固定采样点,只用于导出 ADC 触发
|
2025-11-22 02:21:33 +08:00
|
|
|
|
timer_ocinitpara.outputstate =
|
2026-05-16 13:09:57 +08:00
|
|
|
|
TIMER_CCX_DISABLE; // 不输出 PWM 波形,仅用于比较事件
|
2025-11-22 02:21:33 +08:00
|
|
|
|
timer_ocinitpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
|
|
|
|
|
|
timer_channel_output_config(TIMER1, TIMER_CH_3, &timer_ocinitpara);
|
|
|
|
|
|
|
2026-03-09 01:35:26 +08:00
|
|
|
|
// 设置 PWM 模式和占空比
|
2025-10-19 23:20:28 +08:00
|
|
|
|
timer_channel_output_mode_config(TIMER1, TIMER_CH_0, TIMER_OC_MODE_PWM0);
|
|
|
|
|
|
timer_channel_output_mode_config(TIMER1, TIMER_CH_1, TIMER_OC_MODE_PWM0);
|
|
|
|
|
|
timer_channel_output_mode_config(TIMER1, TIMER_CH_2, TIMER_OC_MODE_PWM0);
|
2025-11-22 02:21:33 +08:00
|
|
|
|
timer_channel_output_mode_config(TIMER1, TIMER_CH_3,
|
2026-03-09 01:35:26 +08:00
|
|
|
|
TIMER_OC_MODE_PWM0); // CH3 也设为 PWM 模式
|
2025-09-23 00:27:51 +08:00
|
|
|
|
|
2026-05-16 13:09:57 +08:00
|
|
|
|
/* 将 CH3 预装载比较点作为 TRGO 导出,供 ADC injected 组同步采样 */
|
|
|
|
|
|
timer_master_output_trigger_source_select(TIMER1, TIMER_TRI_OUT_SRC_O3CPRE);
|
|
|
|
|
|
|
2025-10-19 23:20:28 +08:00
|
|
|
|
timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_0,
|
2026-01-04 22:26:22 +08:00
|
|
|
|
TIMER1_DEFAULT_PULSE);
|
2025-10-19 23:20:28 +08:00
|
|
|
|
timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_1,
|
2026-01-04 22:26:22 +08:00
|
|
|
|
TIMER1_DEFAULT_PULSE);
|
2025-10-19 23:20:28 +08:00
|
|
|
|
timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_2,
|
2026-01-04 22:26:22 +08:00
|
|
|
|
TIMER1_DEFAULT_PULSE);
|
2026-05-16 13:09:57 +08:00
|
|
|
|
// 设置 CH3 为固定 ADC 采样点,避免与三相 PWM duty 耦合
|
2025-11-22 02:21:33 +08:00
|
|
|
|
timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_3,
|
|
|
|
|
|
TIMER1_CH3_COMPARE_VALUE);
|
2025-09-23 00:27:51 +08:00
|
|
|
|
|
2025-10-19 23:20:28 +08:00
|
|
|
|
// 使能主输出和自动重载
|
|
|
|
|
|
timer_primary_output_config(TIMER1, ENABLE);
|
|
|
|
|
|
timer_auto_reload_shadow_enable(TIMER1);
|
|
|
|
|
|
|
2026-03-09 01:35:26 +08:00
|
|
|
|
// 启动 TIMER1
|
2025-10-19 23:20:28 +08:00
|
|
|
|
timer_enable(TIMER1);
|
2025-09-23 00:27:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-16 13:09:57 +08:00
|
|
|
|
// CH3 现在仅作为 TRGO 采样点保留;采样完成由 ADC injected 中断接管
|
2025-11-22 02:21:33 +08:00
|
|
|
|
/**
|
2026-03-09 01:35:26 +08:00
|
|
|
|
* @brief TIMER1 中断处理函数
|
2025-11-22 02:21:33 +08:00
|
|
|
|
* @return void
|
|
|
|
|
|
*/
|
2025-12-16 22:39:39 +08:00
|
|
|
|
void TIMER1_IRQHandler(void) {
|
2025-11-22 02:21:33 +08:00
|
|
|
|
if (timer_interrupt_flag_get(TIMER1, TIMER_INT_FLAG_CH3) != RESET) {
|
|
|
|
|
|
timer_interrupt_flag_clear(TIMER1, TIMER_INT_FLAG_CH3);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-23 00:27:51 +08:00
|
|
|
|
/*!
|
|
|
|
|
|
\brief set PWM
|
|
|
|
|
|
\param[in] channel: TIMER_CH_0/TIMER_CH_1/TIMER_CH_2/TIMER_CH_3 (valid
|
|
|
|
|
|
values: 0, 1, 2, 3)
|
|
|
|
|
|
\param[in] pulse: PWM pulse value
|
|
|
|
|
|
\param[out] none
|
|
|
|
|
|
\retval none
|
|
|
|
|
|
\note The channel parameter must be between 0 and 3 inclusive.
|
|
|
|
|
|
*/
|
2025-10-16 21:43:34 +08:00
|
|
|
|
void Set_Pwm(u16 channel, float duty_cycle) {
|
|
|
|
|
|
float pulse = duty_cycle * TIMER1_PERIOD;
|
2025-10-19 23:20:28 +08:00
|
|
|
|
|
2026-09-01 02:46:25 +08:00
|
|
|
|
#if GATE_DRIVER_DRV8323
|
|
|
|
|
|
/* 新板通道重映射:逻辑相 A→CH2、B→CH1、C→CH0(硬件 INHA/B/C
|
|
|
|
|
|
* 分别接 PWM1/2/3 = PA2/PA1/PA0),与 Set_3Pwm 保持一致 */
|
|
|
|
|
|
if (channel == 0) {
|
|
|
|
|
|
channel = 2;
|
|
|
|
|
|
} else if (channel == 2) {
|
|
|
|
|
|
channel = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif
|
2025-09-23 00:27:51 +08:00
|
|
|
|
if (channel <= 3) {
|
2025-10-16 21:43:34 +08:00
|
|
|
|
timer_channel_output_pulse_value_config(TIMER1, channel, pulse);
|
2025-09-23 00:27:51 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
} // Ensure channel is within valid range
|
|
|
|
|
|
}
|
2025-10-16 21:43:34 +08:00
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
|
\brief set PWM
|
|
|
|
|
|
\param[in] channel: TIMER_CH_0/TIMER_CH_1/TIMER_CH_2/TIMER_CH_3 (valid
|
|
|
|
|
|
values: 0, 1, 2, 3)
|
|
|
|
|
|
\param[in] pulse: PWM pulse value
|
|
|
|
|
|
\param[out] none
|
|
|
|
|
|
\retval none
|
|
|
|
|
|
\note The channel parameter must be between 0 and 3 inclusive.
|
|
|
|
|
|
*/
|
2025-10-19 23:20:28 +08:00
|
|
|
|
void Set_3Pwm(float duty_cycle0, float duty_cycle1, float duty_cycle2) {
|
2026-09-01 02:46:25 +08:00
|
|
|
|
#if GATE_DRIVER_DRV8323
|
|
|
|
|
|
/* 新板硬件映射:CH0(PA0)→PWM3→INHC,CH1(PA1)→PWM2→INHB,
|
|
|
|
|
|
* CH2(PA2)→PWM1→INHA。入参按 (A,B,C) 相序书写,此处把 A/C 换到
|
|
|
|
|
|
* 物理通道,修正新板与老板相反的 INHx 接线。 */
|
|
|
|
|
|
TIMER_CH0CV(TIMER1) = (uint32_t)(duty_cycle2 * TIMER1_PERIOD);
|
|
|
|
|
|
TIMER_CH1CV(TIMER1) = (uint32_t)(duty_cycle1 * TIMER1_PERIOD);
|
|
|
|
|
|
TIMER_CH2CV(TIMER1) = (uint32_t)(duty_cycle0 * TIMER1_PERIOD);
|
|
|
|
|
|
#else
|
|
|
|
|
|
// 直接写寄存器(老板:CH0/1/2 = IN1/2/3 = A/B/C 相)
|
2025-11-22 23:59:43 +08:00
|
|
|
|
TIMER_CH0CV(TIMER1) = (uint32_t)(duty_cycle0 * TIMER1_PERIOD);
|
|
|
|
|
|
TIMER_CH1CV(TIMER1) = (uint32_t)(duty_cycle1 * TIMER1_PERIOD);
|
|
|
|
|
|
TIMER_CH2CV(TIMER1) = (uint32_t)(duty_cycle2 * TIMER1_PERIOD);
|
2026-09-01 02:46:25 +08:00
|
|
|
|
#endif
|
2026-05-16 13:09:57 +08:00
|
|
|
|
}
|