From 8064eae0d963073fd8c44c8cdfc39171673a2663 Mon Sep 17 00:00:00 2001 From: xushenghao Date: Tue, 31 Dec 2024 09:20:29 +0800 Subject: [PATCH] =?UTF-8?q?tmc2240=E5=8A=A0=E4=B8=8A=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 10 ++--- driver/tmc2240.c | 90 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 6 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index bb879da..4699573 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -38,10 +38,7 @@ "C_Cpp_Runner.compilerArgs": [], "C_Cpp_Runner.linkerArgs": [], "C_Cpp_Runner.includePaths": [], - "C_Cpp_Runner.includeSearch": [ - "*", - "**/*" - ], + "C_Cpp_Runner.includeSearch": ["*", "**/*"], "C_Cpp_Runner.excludeSearch": [ "**/build", "**/build/**", @@ -55,5 +52,6 @@ "C_Cpp_Runner.useLeakSanitizer": false, "C_Cpp_Runner.showCompilationTime": false, "C_Cpp_Runner.useLinkTimeOptimization": false, - "C_Cpp_Runner.msvcSecureNoWarnings": false -} \ No newline at end of file + "C_Cpp_Runner.msvcSecureNoWarnings": false, + "C_Cpp.errorSquiggles": "disabled" +} diff --git a/driver/tmc2240.c b/driver/tmc2240.c index d4e2296..bbc8550 100644 --- a/driver/tmc2240.c +++ b/driver/tmc2240.c @@ -43,6 +43,15 @@ static void tmc2240_read(tmc2240_index_e index, uint8_t *wdata, uint8_t *rdata) tmc->spi->gpios.cs->set(*tmc->spi->gpios.cs); } +/** + * @brief 向TMC2240寄存器写入数据 + * + * 向指定TMC2240的指定寄存器写入32位数据。 + * + * @param index TMC2240索引 + * @param reg 寄存器地址 + * @param data 要写入的数据 + */ static void tmc2240_reg_write(tmc2240_index_e index, uint8_t reg, uint32_t data) { DBG_ASSERT(index < TMC2240_MAX __DBG_LINE); @@ -59,6 +68,16 @@ static void tmc2240_reg_write(tmc2240_index_e index, uint8_t reg, uint32_t data) tmc2240_write(index, wdata); } +/** + * @brief 从TMC2240寄存器中读取数据 + * + * 从指定的TMC2240驱动器的指定寄存器中读取数据。 + * + * @param index TMC2240驱动器的索引 + * @param reg 要读取的寄存器地址 + * + * @return 读取到的32位数据 + */ static uint32_t tmc2240_reg_read(tmc2240_index_e index, uint8_t reg) { DBG_ASSERT(index < TMC2240_MAX __DBG_LINE); @@ -110,6 +129,13 @@ static void tmc2240_config_write(tmc2240_index_e index) tmc2240_reg_write(index, TMC2240_GSTAT, tmc->config.gstat.data); } +/** + * @brief 更新TMC2240步进电机的步进角度和每圈脉冲数 + * + * 根据TMC2240的配置更新步进电机的步进角度和每圈脉冲数。 + * + * @param index TMC2240步进电机的索引 + */ static void _tmc2240_motor_update(tmc2240_index_e index) { DBG_ASSERT(index < TMC2240_MAX __DBG_LINE); @@ -152,6 +178,14 @@ static void _tmc2240_motor_update(tmc2240_index_e index) tmc->motor.circle_pulse = 360 / tmc->motor.step_angle; } +/** + * @brief 启用或禁用TMC2240电机驱动器 + * + * 根据传入的索引和启用标志启用或禁用TMC2240电机驱动器。 + * + * @param index TMC2240驱动器的索引 + * @param enable 启用标志,TRUE表示启用,FALSE表示禁用 + */ static void _tmc2240_enable(tmc2240_index_e index, BOOL enable) { DBG_ASSERT(index < TMC2240_MAX __DBG_LINE); @@ -182,6 +216,14 @@ static void _tmc2240_enable(tmc2240_index_e index, BOOL enable) } } +/** + * @brief 设置TMC2240驱动器的方向 + * + * 该函数用于设置TMC2240驱动器的方向。 + * + * @param index TMC2240驱动器的索引 + * @param dir 方向设置,TMC2240_FORWARD表示正向,TMC2240_BACKWARD表示反向 + */ static void _tmc2240_direction(tmc2240_index_e index, tmc2240_direction_e dir) { DBG_ASSERT(index < TMC2240_MAX __DBG_LINE); @@ -198,6 +240,17 @@ static void _tmc2240_direction(tmc2240_index_e index, tmc2240_direction_e dir) } } +/** + * @brief 初始化TMC2240电机驱动器 + * + * 初始化TMC2240电机驱动器,配置相关硬件资源,包括SPI通信、定时器以及GPIO引脚。 + * + * @param index TMC2240的索引号 + * @param SPIx SPI外设指针 + * @param timer 定时器外设指针 + * @param time_ch 定时器通道号 + * @param gpios SPI通信相关的GPIO引脚组 + */ void tmc2240_init(tmc2240_index_e index, SPI_TypeDef *SPIx, TIM_TypeDef *timer, uint32_t time_ch, spi_gpio_group_t *gpios) { DBG_ASSERT(index < TMC2240_MAX __DBG_LINE); @@ -230,6 +283,14 @@ tmc2240_t *tmc2240_get(tmc2240_index_e index) return &_tmc2240[index]; } +/** + * @brief 设置TMC2240的占空比 + * + * 根据给定的索引和百分比设置TMC2240的占空比。 + * + * @param index TMC2240的索引 + * @param percent 占空比的百分比,范围为-100到100 + */ void tmc2240_percent(tmc2240_index_e index, float32 percent) { DBG_ASSERT(index < TMC2240_MAX __DBG_LINE); @@ -239,6 +300,13 @@ void tmc2240_percent(tmc2240_index_e index, float32 percent) PWM_SET_DUTY(tmc->timer, tmc->time_ch, ABS(percent)); } +/** + * @brief 读取TMC2240的配置参数 + * + * 从TMC2240电机驱动器的寄存器中读取多个配置参数,并将其存储在相应的结构体中。 + * + * @param index TMC2240电机驱动器的索引 + */ void tmc2240_config_read(tmc2240_index_e index) { DBG_ASSERT(index < TMC2240_MAX __DBG_LINE); @@ -256,6 +324,13 @@ void tmc2240_config_read(tmc2240_index_e index) tmc->data.tmc2240_temperature = (float32)(tmc->data.tmc2240_adc_temp - 2038) / 7.7; } +/** + * @brief 测试TMC2240电机驱动器 + * + * 该函数用于测试TMC2240电机驱动器,并设置相关参数。 + * + * @param index TMC2240电机驱动器的索引 + */ void tmc2240_test(tmc2240_index_e index) { DBG_ASSERT(index < TMC2240_MAX __DBG_LINE); @@ -288,6 +363,14 @@ void tmc2240_test(tmc2240_index_e index) } } +/** + * @brief 设置TMC2240电机的角度 + * + * 设置指定TMC2240电机的目标角度。 + * + * @param index 电机索引 + * @param angle 目标角度,正值表示正向旋转,负值表示反向旋转 + */ void tmc2240_motor_set_angle(tmc2240_index_e index, int32_t angle) { DBG_ASSERT(index < TMC2240_MAX __DBG_LINE); @@ -334,6 +417,13 @@ void tmc2240_motor_set_angle(tmc2240_index_e index, int32_t angle) _tmc2240_enable(index, tmc->params.enable); } +/** + * @brief 更新TMC2240步进电机的状态 + * + * 该函数用于更新TMC2240步进电机的状态,包括脉冲计数、步距计数和绝对位置等。 + * + * @param index TMC2240步进电机的索引 + */ void tmc2240_motor_update(tmc2240_index_e index) { DBG_ASSERT(index < TMC2240_MAX __DBG_LINE);