From 46398252ad0698d9aeccac3f9f96652189ad6b98 Mon Sep 17 00:00:00 2001 From: Niklas Schoening Date: Fri, 16 Oct 2026 21:13:37 +0200 Subject: [PATCH] Implement spindle PWM and H-bridge control Fill in the LibSpindle platform callbacks so the spindle commands (spindle start/stop/status) drive the BTS7960 H-bridge. - spindleLogic.c: SPINDLE_SetDutyCycle sets the TIM2 compare values, CH4 (SPINDLE_PWM_R) for forward and CH3 (SPINDLE_PWM_L) for backward, the inactive channel is set to 0 first. SPINDLE_EnaPWM switches both half bridges via SPINDLE_ENA_L/R. Both PWM channels are started in init_spindle_custom before the spindle instance is created. - main.h: export htim2 so the spindle module can access the timer. - consoleLogic.c: report CAP_HAS_SPINDLE and CAP_HAS_SPINDLE_STATUS in the capability command. - main.c: fix typo in the playground task output. Co-Authored-By: Claude Opus 5.5 --- stepper_h7/CM7/Core/Inc/main.h | 3 ++- .../CM7/Core/Src/Console/consoleLogic.c | 4 ++-- .../CM7/Core/Src/Spindle/spindleLogic.c | 22 +++++++++++++++---- stepper_h7/CM7/Core/Src/main.c | 3 +-- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/stepper_h7/CM7/Core/Inc/main.h b/stepper_h7/CM7/Core/Inc/main.h index 5656925..af1a234 100644 --- a/stepper_h7/CM7/Core/Inc/main.h +++ b/stepper_h7/CM7/Core/Inc/main.h @@ -41,7 +41,8 @@ extern "C" { /* Exported constants --------------------------------------------------------*/ /* USER CODE BEGIN EC */ - +// timer handle defined in main.c, used for the spindle PWM (CH3 = SPINDLE_PWM_L, CH4 = SPINDLE_PWM_R) +extern TIM_HandleTypeDef htim2; /* USER CODE END EC */ /* Exported macro ------------------------------------------------------------*/ diff --git a/stepper_h7/CM7/Core/Src/Console/consoleLogic.c b/stepper_h7/CM7/Core/Src/Console/consoleLogic.c index cdf98dc..e3832d8 100644 --- a/stepper_h7/CM7/Core/Src/Console/consoleLogic.c +++ b/stepper_h7/CM7/Core/Src/Console/consoleLogic.c @@ -13,8 +13,8 @@ static int CapabilityFunc( int argc, char** argv, void* ctx ) (void)argv; (void)ctx; printf("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\r\nOK", - 0, // has spindle - 0, // has spindle status + 1, // has spindle + 1, // has spindle status 0, // has stepper 0, // has stepper move relative 0, // has stepper move speed diff --git a/stepper_h7/CM7/Core/Src/Spindle/spindleLogic.c b/stepper_h7/CM7/Core/Src/Spindle/spindleLogic.c index 7b28cbd..72e9d6c 100644 --- a/stepper_h7/CM7/Core/Src/Spindle/spindleLogic.c +++ b/stepper_h7/CM7/Core/Src/Spindle/spindleLogic.c @@ -1,4 +1,5 @@ #include "spindleLogic.h" +#include "main.h" #include "FreeRTOS.h" #include @@ -16,9 +17,13 @@ static void SPINDLE_SetDutyCycle(SpindleHandle_t h, void* context, int backwards { (void)h; (void)context; - (void)backwards; - (void)dutyCycle; - // TODO: set PWM duty cycle and direction + if (backwards) { + __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_4, 0); + __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_3, dutyCycle * 10000); + } else { + __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_3, 0); + __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_4, dutyCycle * 10000); + } } // -------------------------------------------------------------------------------------------------------------------- @@ -28,8 +33,14 @@ static void SPINDLE_EnaPWM(SpindleHandle_t h, void* context, int ena) { (void)h; (void)context; - (void)ena; // TODO: enable / disable PWM output and half bridges + if(ena) { + HAL_GPIO_WritePin(SPINDLE_ENA_L_GPIO_Port, SPINDLE_ENA_L_Pin, SET); + HAL_GPIO_WritePin(SPINDLE_ENA_R_GPIO_Port, SPINDLE_ENA_R_Pin, SET); + } else { + HAL_GPIO_WritePin(SPINDLE_ENA_L_GPIO_Port, SPINDLE_ENA_L_Pin, RESET); + HAL_GPIO_WritePin(SPINDLE_ENA_R_GPIO_Port, SPINDLE_ENA_R_Pin, RESET); + } } // -------------------------------------------------------------------------------------------------------------------- @@ -45,6 +56,9 @@ SpindleHandle_t init_spindle_custom(ConsoleHandle_t console, float maxRPM, float s.enaPWM = SPINDLE_EnaPWM; s.context = NULL; + HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_3); + HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_4); + // the library copies the params, therefore a local struct is fine here return SPINDLE_CreateInstance( 4*configMINIMAL_STACK_SIZE, configMAX_PRIORITIES - 3, console, &s ); } diff --git a/stepper_h7/CM7/Core/Src/main.c b/stepper_h7/CM7/Core/Src/main.c index b7f9cba..749dbf5 100644 --- a/stepper_h7/CM7/Core/Src/main.c +++ b/stepper_h7/CM7/Core/Src/main.c @@ -107,7 +107,7 @@ static void playgroundTask(void* args) { (void)args; - printf("Hnot ello World\r\n"); + printf("Hello World\r\n"); while(1) { vTaskDelay(pdMS_TO_TICKS(1000)); @@ -248,7 +248,6 @@ int main(void) console = init_console_custom(); spindle = init_spindle_custom(console, 9000, -9000, 1600); - // start the scheduler and setup free rtos execution context vTaskStartScheduler();