diff --git a/.settings/com.st.stm32cube.ide.mcu.sfrview.prefs b/.settings/com.st.stm32cube.ide.mcu.sfrview.prefs new file mode 100644 index 0000000..98a69fc --- /dev/null +++ b/.settings/com.st.stm32cube.ide.mcu.sfrview.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +sfrviewstate={"fFavorites"\:{"fLists"\:{}},"fProperties"\:{"fNodeProperties"\:{}}} diff --git a/.settings/stm32cubeide.project.prefs b/.settings/stm32cubeide.project.prefs index 3070c7c..2ed4be6 100644 --- a/.settings/stm32cubeide.project.prefs +++ b/.settings/stm32cubeide.project.prefs @@ -1,5 +1,5 @@ 635E684B79701B039C64EA45C3F84D30=4E298DF08B22D87FABACC213C3DEB1B4 66BE74F758C12D739921AEA421D593D3=0 -8DF89ED150041C4CBC7CB9A9CAA90856=AC75FFA836ABF92D776AF65FD0504403 +8DF89ED150041C4CBC7CB9A9CAA90856=E8FBF268A8CF0E2CBEA3C6C3E8CF39D0 DC22A860405A8BF2F2C095E5B6529F12=E8FBF268A8CF0E2CBEA3C6C3E8CF39D0 eclipse.preferences.version=1 diff --git a/Core/Inc/stm32f4xx_it.h b/Core/Inc/stm32f4xx_it.h index 2dcec8c..73f7d3f 100644 --- a/Core/Inc/stm32f4xx_it.h +++ b/Core/Inc/stm32f4xx_it.h @@ -57,7 +57,10 @@ void CAN1_RX0_IRQHandler(void); void CAN1_RX1_IRQHandler(void); void CAN1_SCE_IRQHandler(void); void TIM1_TRG_COM_TIM11_IRQHandler(void); +void USART1_IRQHandler(void); void USART3_IRQHandler(void); +void TIM6_DAC_IRQHandler(void); +void TIM7_IRQHandler(void); /* USER CODE BEGIN EFP */ /* USER CODE END EFP */ diff --git a/Core/Src/stm32f4xx_it.c b/Core/Src/stm32f4xx_it.c index f61434f..e08b482 100644 --- a/Core/Src/stm32f4xx_it.c +++ b/Core/Src/stm32f4xx_it.c @@ -57,6 +57,9 @@ /* External variables --------------------------------------------------------*/ extern CAN_HandleTypeDef hcan1; extern TIM_HandleTypeDef htim1; +extern TIM_HandleTypeDef htim6; +extern TIM_HandleTypeDef htim7; +extern UART_HandleTypeDef huart1; extern UART_HandleTypeDef huart3; extern TIM_HandleTypeDef htim11; @@ -234,6 +237,20 @@ void TIM1_TRG_COM_TIM11_IRQHandler(void) } /** + * @brief This function handles USART1 global interrupt. + */ +void USART1_IRQHandler(void) +{ + /* USER CODE BEGIN USART1_IRQn 0 */ + + /* USER CODE END USART1_IRQn 0 */ + HAL_UART_IRQHandler(&huart1); + /* USER CODE BEGIN USART1_IRQn 1 */ + + /* USER CODE END USART1_IRQn 1 */ +} + +/** * @brief This function handles USART3 global interrupt. */ void USART3_IRQHandler(void) @@ -247,6 +264,34 @@ void USART3_IRQHandler(void) /* USER CODE END USART3_IRQn 1 */ } +/** + * @brief This function handles TIM6 global interrupt, DAC1 and DAC2 underrun error interrupts. + */ +void TIM6_DAC_IRQHandler(void) +{ + /* USER CODE BEGIN TIM6_DAC_IRQn 0 */ + + /* USER CODE END TIM6_DAC_IRQn 0 */ + HAL_TIM_IRQHandler(&htim6); + /* USER CODE BEGIN TIM6_DAC_IRQn 1 */ + + /* USER CODE END TIM6_DAC_IRQn 1 */ +} + +/** + * @brief This function handles TIM7 global interrupt. + */ +void TIM7_IRQHandler(void) +{ + /* USER CODE BEGIN TIM7_IRQn 0 */ + + /* USER CODE END TIM7_IRQn 0 */ + HAL_TIM_IRQHandler(&htim7); + /* USER CODE BEGIN TIM7_IRQn 1 */ + + /* USER CODE END TIM7_IRQn 1 */ +} + /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ diff --git a/Core/Src/tim.c b/Core/Src/tim.c index df9da97..f1529a1 100644 --- a/Core/Src/tim.c +++ b/Core/Src/tim.c @@ -213,6 +213,10 @@ void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle) /* USER CODE END TIM6_MspInit 0 */ /* TIM6 clock enable */ __HAL_RCC_TIM6_CLK_ENABLE(); + + /* TIM6 interrupt Init */ + HAL_NVIC_SetPriority(TIM6_DAC_IRQn, 5, 0); + HAL_NVIC_EnableIRQ(TIM6_DAC_IRQn); /* USER CODE BEGIN TIM6_MspInit 1 */ /* USER CODE END TIM6_MspInit 1 */ @@ -224,6 +228,10 @@ void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle) /* USER CODE END TIM7_MspInit 0 */ /* TIM7 clock enable */ __HAL_RCC_TIM7_CLK_ENABLE(); + + /* TIM7 interrupt Init */ + HAL_NVIC_SetPriority(TIM7_IRQn, 5, 0); + HAL_NVIC_EnableIRQ(TIM7_IRQn); /* USER CODE BEGIN TIM7_MspInit 1 */ /* USER CODE END TIM7_MspInit 1 */ @@ -265,6 +273,9 @@ void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle) /* USER CODE END TIM6_MspDeInit 0 */ /* Peripheral clock disable */ __HAL_RCC_TIM6_CLK_DISABLE(); + + /* TIM6 interrupt Deinit */ + HAL_NVIC_DisableIRQ(TIM6_DAC_IRQn); /* USER CODE BEGIN TIM6_MspDeInit 1 */ /* USER CODE END TIM6_MspDeInit 1 */ @@ -276,6 +287,9 @@ void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle) /* USER CODE END TIM7_MspDeInit 0 */ /* Peripheral clock disable */ __HAL_RCC_TIM7_CLK_DISABLE(); + + /* TIM7 interrupt Deinit */ + HAL_NVIC_DisableIRQ(TIM7_IRQn); /* USER CODE BEGIN TIM7_MspDeInit 1 */ /* USER CODE END TIM7_MspDeInit 1 */ diff --git a/Core/Src/usart.c b/Core/Src/usart.c index 09f8ead..5d90b3b 100644 --- a/Core/Src/usart.c +++ b/Core/Src/usart.c @@ -140,6 +140,9 @@ void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle) GPIO_InitStruct.Alternate = GPIO_AF7_USART1; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + /* USART1 interrupt Init */ + HAL_NVIC_SetPriority(USART1_IRQn, 5, 0); + HAL_NVIC_EnableIRQ(USART1_IRQn); /* USER CODE BEGIN USART1_MspInit 1 */ /* USER CODE END USART1_MspInit 1 */ @@ -214,6 +217,8 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle) */ HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10); + /* USART1 interrupt Deinit */ + HAL_NVIC_DisableIRQ(USART1_IRQn); /* USER CODE BEGIN USART1_MspDeInit 1 */ /* USER CODE END USART1_MspDeInit 1 */ diff --git a/sdk b/sdk index 80de216..5314f26 160000 --- a/sdk +++ b/sdk @@ -1 +1 @@ -Subproject commit 80de216afbb14467ae6d21a83bf935fe91b78d2b +Subproject commit 5314f26b61cf37283c9dfc9caf57b116a590829c diff --git a/tmc5160_motor_driver_stand_alone.ioc b/tmc5160_motor_driver_stand_alone.ioc index a4c6b3c..61efe7e 100644 --- a/tmc5160_motor_driver_stand_alone.ioc +++ b/tmc5160_motor_driver_stand_alone.ioc @@ -93,8 +93,11 @@ NVIC.SavedSvcallIrqHandlerGenerated=true NVIC.SavedSystickIrqHandlerGenerated=true NVIC.SysTick_IRQn=true\:15\:0\:false\:false\:false\:true\:false\:true\:false NVIC.TIM1_TRG_COM_TIM11_IRQn=true\:15\:0\:false\:false\:true\:false\:false\:true\:true +NVIC.TIM6_DAC_IRQn=true\:5\:0\:false\:false\:true\:true\:true\:true\:true +NVIC.TIM7_IRQn=true\:5\:0\:false\:false\:true\:true\:true\:true\:true NVIC.TimeBase=TIM1_TRG_COM_TIM11_IRQn NVIC.TimeBaseIP=TIM11 +NVIC.USART1_IRQn=true\:5\:0\:false\:false\:true\:true\:true\:true\:true NVIC.USART3_IRQn=true\:5\:0\:false\:false\:true\:true\:true\:true\:true NVIC.UsageFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false\:false PA10.Mode=Asynchronous diff --git a/usrc/main.cpp b/usrc/main.cpp index 0818921..a8cde2a 100644 --- a/usrc/main.cpp +++ b/usrc/main.cpp @@ -38,6 +38,8 @@ static chip_cfg_t chipcfg = { .huart = &DEBUG_UART, .debuglight = DEBUG_LIGHT_GPIO, }; +TMC5130 m_motor1; +static int32_t m_velocity; /******************************************************************************* * 代码 * @@ -56,7 +58,6 @@ void regfn() { osDelay(atoi(argv[1])); }); -#if 0 /******************************************************************************* * 步进电机 * *******************************************************************************/ @@ -76,52 +77,40 @@ void regfn() { */ cmdScheduler.registerCmd("step_motor_setvelocity", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) { CHECK_ARGC(2); - - StepMotor* motor = NULL; - GET_MOTOR(motor); - - motor->setVelocity(atoi(argv[2])); + int motorid = atoi(argv[1]); + m_velocity = atoi(argv[2]); }); cmdScheduler.registerCmd("step_motor_set_acc", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) { CHECK_ARGC(2); - - StepMotor* motor = NULL; - GET_MOTOR(motor); - - motor->setAcc(atoi(argv[2])); + m_motor1.setAcceleration(atoi(argv[2])); }); cmdScheduler.registerCmd("step_motor_set_dec", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) { CHECK_ARGC(2); - - StepMotor* motor = NULL; - GET_MOTOR(motor); - - motor->setDec(atoi(argv[2])); + m_motor1.setDeceleration(atoi(argv[2])); }); cmdScheduler.registerCmd("step_motor_moveto", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) { CHECK_ARGC(2); - - StepMotor* motor = NULL; - GET_MOTOR(motor); - - motor->moveTo(atoi(argv[2])); + ZLOGI(TAG, "step_motor_moveto %d %d", atoi(argv[2]), m_velocity); + m_motor1.moveTo(atoi(argv[2]), m_velocity); }); cmdScheduler.registerCmd("step_motor_moveby", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) { CHECK_ARGC(2); - - StepMotor* motor = NULL; - GET_MOTOR(motor); - - motor->moveBy(atoi(argv[2])); + ZLOGI(TAG, "step_motor_moveby %d %d", atoi(argv[2]), m_velocity); + m_motor1.moveBy(atoi(argv[2]), m_velocity); }); cmdScheduler.registerCmd("step_motor_rotate", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) { CHECK_ARGC(2); - - StepMotor* motor = NULL; - GET_MOTOR(motor); - - motor->rotate(atoi(argv[2])); + ZLOGI(TAG, "step_motor_rotate %d %d", atoi(argv[2]), m_velocity); + if (atoi(argv[2]) > 0) { + m_motor1.rotate(m_velocity); + } else if (atoi(argv[2]) == 0) { + m_motor1.stop(); + } else { + m_motor1.rotate(-m_velocity); + } }); +#if 0 + cmdScheduler.registerCmd("step_motor_movetozero", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) { CHECK_ARGC(1); @@ -173,8 +162,6 @@ void regfn() { #define MOTOR1_SPI_MODE_SELECT PB4 // TMC5130HImpl m_tmc5130motor; -TMC5130 m_motor1; - void Main::run() { /******************************************************************************* * 系统初始化 * @@ -197,9 +184,11 @@ void Main::run() { m_motor1.setIHOLD_IRUN(1, 20, 0); m_motor1.setMotorShaft(true); - m_motor1.setAcceleration(300000); - m_motor1.setDeceleration(300000); - + m_motor1.setIHOLD_IRUN(MOTOR_IHOLD, MOTOR_IRUN, 0); + + m_motor1.setAcceleration(CFG_ACC); + m_motor1.setDeceleration(CFG_DEC); + m_velocity = CFG_VELOCITY; } int i = 1; diff --git a/usrc/project_configs.h b/usrc/project_configs.h index 9535089..7d03b43 100644 --- a/usrc/project_configs.h +++ b/usrc/project_configs.h @@ -16,3 +16,18 @@ #define IFLYTOP_PREEMPTPRIORITY_DEFAULT 5 + +#define MOTOR_SHAFT (false) // 电机是否反向 +#define MOTOR_IRUN (8) +#define MOTOR_IHOLD (1) +#define CFG_ACC (300000) // 加速度 +#define CFG_DEC (300000) // 减速度 +#define CFG_VELOCITY (20000) // 位置模式时运行的速度 +#define CFG_ZERO_SHIFT (0) // 回零时的偏移量 +#define CFG_RUNHOME_VELOCITY (50000) // 回零速度 +#define CFG_RUNTOHOME_DEC (100000) // 回零减速度 +#define CFG_MIN_POS (INT32_MIN) // 最小位置 +#define CFG_MAX_POS (INT32_MAX) // 最大位置 +#define CFG_RUNTOHOME_MAX_DISTANCE (INT32_MAX) // 回零时移动的最大位移 +#define CFG_RUNTOHOME_LEAVE_ZERO_POINT_DISTANCE (256 * 200 * 5) // 回零时离开零点的距离 +#define CFG_RUNTOHOME_KEEP_MOVE_DISTANCE (256 * 25 * 1) // 回零时保持运动的距离 \ No newline at end of file