2026-05-23 22:46:32 +08:00
|
|
|
/**
|
|
|
|
|
* @file device/motor.cpp
|
|
|
|
|
* @brief 底层设备驱动与外设封装源文件
|
|
|
|
|
* @note 本文件仅补充 Doxygen 文档注释,不改变代码逻辑。
|
|
|
|
|
*/
|
|
|
|
|
|
2025-12-08 02:48:59 +08:00
|
|
|
#include "motor.h"
|
|
|
|
|
#include "Timer.h"
|
|
|
|
|
#include "gd32c10x_eval.h"
|
2026-01-24 01:50:39 +08:00
|
|
|
#include "gd32c10x_gpio.h"
|
2025-12-08 02:48:59 +08:00
|
|
|
#include <cstdio>
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief driver输出初始化
|
|
|
|
|
*/
|
|
|
|
|
void motor_init(void) {
|
|
|
|
|
timer1_gpio_config();
|
|
|
|
|
timer1_pwm_config();
|
|
|
|
|
// 使能GPIOB时钟
|
|
|
|
|
rcu_periph_clock_enable(RCU_GPIOB);
|
|
|
|
|
// 配置PB2为输入引脚,用于读取电机状态
|
|
|
|
|
gpio_init(GPIOB, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_2);
|
|
|
|
|
/* 配置GPIOB的PB14为推挽输出 SPICS */
|
|
|
|
|
gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ,
|
|
|
|
|
GPIO_PIN_14 | GPIO_PIN_12);
|
2026-01-24 01:50:39 +08:00
|
|
|
|
|
|
|
|
// 使能GPIOC时钟,用于电机驱动使能引脚
|
|
|
|
|
rcu_periph_clock_enable(RCU_MOTOR_EN_GPIO);
|
|
|
|
|
// 配置PC13为推挽输出,用于控制电机驱动器使能
|
|
|
|
|
gpio_init(MOTOR_EN_GPIO_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, MOTOR_EN_GPIO_PIN);
|
2025-12-08 02:48:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 电机复位
|
|
|
|
|
*/
|
|
|
|
|
void motor_reset(void) {
|
|
|
|
|
motor_sleep();
|
2026-05-24 01:21:41 +08:00
|
|
|
#if FOC_DEBUG
|
2025-12-08 02:48:59 +08:00
|
|
|
printf("motor reseting...\r\n");
|
2026-05-24 01:21:41 +08:00
|
|
|
#endif
|
2025-12-08 02:48:59 +08:00
|
|
|
motor_resume();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 电机休眠
|
|
|
|
|
*/
|
2026-01-24 01:50:39 +08:00
|
|
|
void motor_sleep(void) {
|
|
|
|
|
gpio_bit_reset(GPIOB, GPIO_PIN_12);
|
|
|
|
|
// 同时禁用电机驱动器
|
|
|
|
|
gpio_bit_reset(MOTOR_EN_GPIO_PORT, MOTOR_EN_GPIO_PIN);
|
|
|
|
|
}
|
2025-12-08 02:48:59 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 电机启动
|
|
|
|
|
*/
|
2026-01-24 01:50:39 +08:00
|
|
|
void motor_resume(void) {
|
|
|
|
|
gpio_bit_set(GPIOB, GPIO_PIN_12);
|
|
|
|
|
// 同时启用电机驱动器
|
|
|
|
|
gpio_bit_set(MOTOR_EN_GPIO_PORT, MOTOR_EN_GPIO_PIN);
|
|
|
|
|
}
|