步进电机转动完成

This commit is contained in:
许晟昊 2024-12-24 12:59:11 +08:00
parent 76736247f5
commit 08e310c72a
8 changed files with 1468 additions and 1309 deletions

View File

@ -257,6 +257,10 @@ void TIM3_IRQHandler(void)
/* USER CODE END TIM3_IRQn 0 */
/* USER CODE BEGIN TIM3_IRQn 1 */
if (IS_TIM_IT_FLAG(TIM3))
{
TIM_IRQ_HANDLER(TIM3);
}
/* USER CODE END TIM3_IRQn 1 */
}

View File

@ -148,7 +148,24 @@
<Name></Name>
</SetRegEntry>
</TargetDriverDllRegistry>
<Breakpoint/>
<Breakpoint>
<Bp>
<Number>0</Number>
<Type>0</Type>
<LineNumber>262</LineNumber>
<EnabledFlag>1</EnabledFlag>
<Address>134224768</Address>
<ByteObject>0</ByteObject>
<HtxType>0</HtxType>
<ManyObjects>0</ManyObjects>
<SizeOfObject>0</SizeOfObject>
<BreakByAccess>0</BreakByAccess>
<BreakIfRCount>1</BreakIfRCount>
<Filename>../Core/Src/stm32f1xx_it.c</Filename>
<ExecCommand></ExecCommand>
<Expression>\\motor\../Core/Src/stm32f1xx_it.c\262</Expression>
</Bp>
</Breakpoint>
<WatchWindow1>
<Ww>
<count>0</count>
@ -158,7 +175,7 @@
<Ww>
<count>1</count>
<WinNumber>1</WinNumber>
<ItemText>tmc</ItemText>
<ItemText>tmc,0x0A</ItemText>
</Ww>
<Ww>
<count>2</count>
@ -185,6 +202,11 @@
<WinNumber>1</WinNumber>
<ItemText>vel,0x0A</ItemText>
</Ww>
<Ww>
<count>7</count>
<WinNumber>1</WinNumber>
<ItemText>0x03E8,0x0A</ItemText>
</Ww>
</WatchWindow1>
<MemoryWindow1>
<Mm>

File diff suppressed because it is too large Load Diff

View File

@ -13,6 +13,7 @@ static uint8_t business_inspection(struct flow *fl)
FL_HEAD(fl);
for (;;)
{
tmc2240_process(TMC2240_1);
FL_LOCK_DELAY(fl, FL_CLOCK_SEC);
}
FL_TAIL(fl);
@ -33,7 +34,7 @@ static uint8_t idle_inspection(struct flow *fl)
for (;;)
{
GPIO_TOGGLE(LED_BLUE_GPIO_Port, LED_BLUE_Pin);
tmc2240_test(TMC2240_1);
tmc2240_config_read(TMC2240_1);
ssd1306_f6x8_string(0, 2, " tick:");
ssd1306_f6x8_number(40, 2, tick++, 0);
ssd1306_f6x8_string(0, 4, " cfg:");

View File

@ -42,7 +42,6 @@ void board_init(void)
ssd1306_init();
{
GPIO_RESET(TMC2240_EN_GPIO_Port, TMC2240_EN_Pin);
spi_gpio_group_t gpios;
gpios.cs = gpio_create(TMC2240_CS_GPIO_Port, TMC2240_CS_Pin);
gpios.mosi = gpio_create(TMC2240_MOSI_GPIO_Port, TMC2240_MOSI_Pin);
@ -50,6 +49,9 @@ void board_init(void)
gpios.miso = gpio_create(TMC2240_MISO_GPIO_Port, TMC2240_MISO_Pin);
gpios.rst = gpio_create(NULL, 0);
gpios.rdy = gpio_create(NULL, 0);
tmc2240_init(TMC2240_1, SPI1, &gpios);
tmc2240_init(TMC2240_1, SPI1, TIM3, LL_TIM_CHANNEL_CH3, &gpios);
tmc2240_get(TMC2240_1)->en = gpio_create(TMC2240_EN_GPIO_Port, TMC2240_EN_Pin);
tmc2240_get(TMC2240_1)->dir = gpio_create(TMC2240_DIR_GPIO_Port, TMC2240_DIR_Pin);
tmc2240_get(TMC2240_1)->params.enable = TRUE;
}
}

View File

@ -80,14 +80,17 @@ static uint32_t tmc2240_reg_read(tmc2240_index_e index, uint8_t reg)
*
* @param index TMC2240步进电机驱动器的索引
*/
static void tmc2240_config(tmc2240_index_e index)
static void tmc2240_config_write(tmc2240_index_e index)
{
DBG_ASSERT(index < TMC2240_MAX __DBG_LINE);
tmc2240_t *tmc = &_tmc2240[index];
DBG_ASSERT(tmc != NULL __DBG_LINE);
tmc->config.gconf.data = 0x00000000;
tmc->config.chopconf.data = 0x00410153;
tmc->config.chopconf.bits.mres = TMC2240_MRES_8;
tmc->config.drvconf.data = 0x00000021;
tmc->config.global_scaler.data = 0x00000000;
tmc->config.ihold_irun.data = 0x00071f1f;
@ -102,17 +105,63 @@ static void tmc2240_config(tmc2240_index_e index)
tmc2240_reg_write(index, TMC2240_GSTAT, tmc->config.gstat.data);
}
void tmc2240_init(tmc2240_index_e index, SPI_TypeDef *SPIx, spi_gpio_group_t *gpios)
static void _tmc2240_enable(tmc2240_index_e index, BOOL enable)
{
DBG_ASSERT(index < TMC2240_MAX __DBG_LINE);
tmc2240_t *tmc = &_tmc2240[index];
DBG_ASSERT(tmc != NULL __DBG_LINE);
if (enable == TRUE)
{
tmc->en->reset(*tmc->en);
PWM_START(tmc->timer, tmc->time_ch);
}
else
{
tmc->en->set(*tmc->en);
PWM_STOP(tmc->timer, tmc->time_ch);
}
}
static void _tmc2240_direction(tmc2240_index_e index, tmc2240_direction_e dir)
{
DBG_ASSERT(index < TMC2240_MAX __DBG_LINE);
tmc2240_t *tmc = &_tmc2240[index];
DBG_ASSERT(tmc != NULL __DBG_LINE);
if (dir == TMC2240_FORWARD)
{
tmc->dir->reset(*tmc->dir);
}
else
{
tmc->dir->set(*tmc->dir);
}
}
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);
tmc2240_t *tmc = &_tmc2240[index];
tmc->timer = timer;
tmc->time_ch = time_ch;
tmc->spi = spi_create(SPI_TYPE_NORMAL, *gpios, 0);
DBG_ASSERT(tmc->spi != NULL __DBG_LINE);
osel_memset((uint8_t *)&tmc->config, 0, sizeof(tmc2240_config_t));
osel_memset((uint8_t *)&tmc->read_config, 0, sizeof(tmc2240_config_t));
tmc->spi->interface.hardware_enable(tmc->spi, SPIx);
tmc2240_config(index);
{
tmc->default_tm.sysclk = SystemCoreClock / 1000;
tmc->default_tm.psc = PWM_GET_PSC(tmc->timer);
tmc->default_tm.arr = PWM_GET_ARR(tmc->timer);
tmc->default_tm.freq = PWM_GET_FREQ(tmc->timer);
}
tmc->params.percent = TMC2240_PWM_DUTY_DEFAULT;
tmc->params.arr = 0;
tmc->params.freq = 0;
tmc->params.enable = FALSE;
tmc->params.direction = TMC2240_FORWARD;
tmc2240_config_write(index);
}
tmc2240_t *tmc2240_get(tmc2240_index_e index)
@ -121,14 +170,16 @@ tmc2240_t *tmc2240_get(tmc2240_index_e index)
return &_tmc2240[index];
}
void tmc2240_velocity(tmc2240_index_e index, int32_t vel)
void tmc2240_percent(tmc2240_index_e index, float32 percent)
{
DBG_ASSERT(index < TMC2240_MAX __DBG_LINE);
tmc2240_t *tmc = &_tmc2240[index];
DBG_ASSERT(tmc != NULL __DBG_LINE);
PWM_SET_DUTY(tmc->timer, tmc->time_ch, ABS(percent));
}
void tmc2240_test(tmc2240_index_e index)
void tmc2240_config_read(tmc2240_index_e index)
{
DBG_ASSERT(index < TMC2240_MAX __DBG_LINE);
tmc2240_t *tmc = &_tmc2240[index];
@ -141,6 +192,28 @@ void tmc2240_test(tmc2240_index_e index)
tmc->read_config.ihold_irun.data = tmc2240_reg_read(index, TMC2240_IHOLD_IRUN);
tmc->read_config.pwmconf.data = tmc2240_reg_read(index, TMC2240_PWMCONF);
tmc->read_config.gstat.data = tmc2240_reg_read(index, TMC2240_GSTAT);
tmc->adc_temp = tmc2240_reg_read(index, TMC2240_ADC_TEMP);
tmc->temperature = (float32)(tmc->adc_temp - 2038) / 7.7;
tmc->data.tmc2240_adc_temp = tmc2240_reg_read(index, TMC2240_ADC_TEMP);
tmc->data.tmc2240_temperature = (float32)(tmc->data.tmc2240_adc_temp - 2038) / 7.7;
}
void tmc2240_process(tmc2240_index_e index)
{
DBG_ASSERT(index < TMC2240_MAX __DBG_LINE);
tmc2240_t *tmc = &_tmc2240[index];
DBG_ASSERT(tmc != NULL __DBG_LINE);
_tmc2240_enable(index, tmc->params.enable);
_tmc2240_direction(index, tmc->params.direction);
if (PWM_GET_ARR(tmc->timer) != tmc->params.arr)
{
if (tmc->params.arr == 0)
{
tmc->params.arr = tmc->default_tm.arr;
}
PWM_SET_ARR(tmc->timer, tmc->params.arr);
tmc->params.freq = PWM_GET_FREQ(tmc->timer);
}
tmc2240_percent(index, tmc->params.percent);
}

View File

@ -12,6 +12,8 @@
#include "main.h"
#include "spis.h"
#define TMC2240_PWM_DUTY_DEFAULT 50 // PWM默认占空比
/*
0x00 = 0x00002108 ;; writing GCONF @ address 0=0x00 with 0x00002108=8456=0.0
0x03 = 0x00000000 ;; writing SLAVECONF @ address 1=0x03 with 0x00000000=0=0.0
@ -103,6 +105,25 @@ typedef enum
TMC2240_MAX,
} tmc2240_index_e;
typedef enum
{
TMC2240_FORWARD, // 正转
TMC2240_BACKWARD, // 反转
} tmc2240_direction_e;
typedef enum
{
TMC2240_MRES_256,
TMC2240_MRES_128,
TMC2240_MRES_64,
TMC2240_MRES_32,
TMC2240_MRES_16,
TMC2240_MRES_8,
TMC2240_MRES_4,
TMC2240_MRES_2,
TMC2240_MRES_1, // FULL STEP
} tmc2240_mres_e;
// 0x00 GCONF
typedef union
{
@ -188,17 +209,6 @@ typedef union
} bits;
} gstat_u;
// 0x04 IOIN
typedef union
{
uint32_t data;
struct
{
uint32_t step : 1;
uint32_t dir : 1;
} bits;
} ioin_u;
// 0x0A DRVCONF
typedef union
{
@ -237,49 +247,6 @@ typedef union
} bits;
} ihold_irun_u;
// 0x11 TPOWERDOWN
typedef union
{
uint32_t data;
struct
{
/**
* TPOWERDOW将电机停顿后的延迟时间(STST)
04
2
SteaIhChop2脉宽调制自动关闭
=10
0...((2^8)-1)x 2^18 tCLK
*/
uint32_t tpow : 8; // 0-9
uint32_t reserved1 : 24;
} bits;
} tpowerdown_u;
// 0x13 TPWMTHRS
typedef union
{
uint32_t data;
struct
{
uint32_t tpwmthrs : 20; // 0-19
uint32_t reserved1 : 12; // 20-31
} bits;
} tpwmthrs_u;
// 0x2D DIRECT_MODE
typedef union
{
uint32_t data;
struct
{
uint32_t direct_coil_a : 9; // 0-8
uint32_t reserved1 : 7; // 9-15
uint32_t direct_coil_b : 9; // 16-24
uint32_t reserved2 : 7; // 25-31
} bits;
} direct_mode_u;
// 0x6C CHOPCONF
typedef union
{
@ -336,29 +303,54 @@ typedef struct
{
gconf_u gconf; // 0x00 GCONF
gstat_u gstat; // 0x01 GSTAT
ioin_u ioin; // 0x04 IOIN
drvconf_u drvconf; // 0x0A DRVCONF
global_scaler_u global_scaler; // 0x0B GLOBAL_SCALER
ihold_irun_u ihold_irun; // 0x10 IHOLD_IRUN
tpowerdown_u tpowerdown; // 0x11 TPOWERDOWN
direct_mode_u direct_mode; // 0x2D DIRECT_MODE
chopconf_u chopconf; // 0x6C CHOPCONF
pwmconf_u pwmconf; // 0x70 PWMCONF
} tmc2240_config_t;
typedef struct
{
gpio_t *en; ///< EN_PIN
gpio_t *dir; ///< DIR_PIN
TIM_TypeDef *timer;
uint32_t time_ch;
spi_t *spi;
tmc2240_config_t config;
tmc2240_config_t read_config;
uint16_t adc_temp;
float32 temperature;
uint32_t step;
__IO uint32_t step_count;
// PRIVATE
struct
{
uint32_t sysclk;
uint32_t psc; // 预分频系数
uint16_t arr; // 自动重装值 auto reload value
uint32_t freq;
} default_tm;
struct
{
BOOL enable; // 使能
tmc2240_direction_e direction;
float32 percent;
uint16_t arr;
uint32_t freq;
} params;
struct
{
uint16_t tmc2240_adc_temp;
float32 tmc2240_temperature;
} data;
} tmc2240_t;
void tmc2240_init(tmc2240_index_e index, SPI_TypeDef *SPIx, spi_gpio_group_t *gpios);
void tmc2240_init(tmc2240_index_e index, SPI_TypeDef *SPIx, TIM_TypeDef *timer, uint32_t time_ch, spi_gpio_group_t *gpios);
tmc2240_t *tmc2240_get(tmc2240_index_e index);
void tmc2240_velocity(tmc2240_index_e index, int32_t vel);
void tmc2240_test(tmc2240_index_e index);
void tmc2240_process(tmc2240_index_e index);
void tmc2240_config_read(tmc2240_index_e index);
#endif // __TMC2240_H

View File

@ -40,7 +40,9 @@
} while (__LINE__ == -1)
#define PWM_GET_ARR(TIMx) LL_TIM_GetAutoReload(TIMx)
#define PWM_SET_ARR(TIMx, ARR) LL_TIM_SetAutoReload(TIMx, ARR - 1)
#define PWM_GET_PSC(TIMx) LL_TIM_GetPrescaler(TIMx)
#define PWM_SET_PSC(TIMx, PSC) LL_TIM_SetPrescaler(TIMx, PSC - 1)
/**
* @brief Sets the PWM frequency