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 <noreply@anthropic.com>
This commit is contained in:
Niklas SchoeningandClaude Opus 5.5 committed 2026-10-16 21:13:37 +02:00
1 parent 26fe4cc0cf
commit 46398252ad
4 files changed
+23 -9

No files matched your search

+2 -1
View File
@@ -41,7 +41,8 @@ extern "C" {
/* Exported constants --------------------------------------------------------*/ /* Exported constants --------------------------------------------------------*/
/* USER CODE BEGIN EC */ /* 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 */ /* USER CODE END EC */
/* Exported macro ------------------------------------------------------------*/ /* Exported macro ------------------------------------------------------------*/
@@ -13,8 +13,8 @@ static int CapabilityFunc( int argc, char** argv, void* ctx )
(void)argv; (void)argv;
(void)ctx; (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", 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 1, // has spindle
0, // has spindle status 1, // has spindle status
0, // has stepper 0, // has stepper
0, // has stepper move relative 0, // has stepper move relative
0, // has stepper move speed 0, // has stepper move speed
+18 -4
View File
@@ -1,4 +1,5 @@
#include "spindleLogic.h" #include "spindleLogic.h"
#include "main.h"
#include "FreeRTOS.h" #include "FreeRTOS.h"
#include <stddef.h> #include <stddef.h>
@@ -16,9 +17,13 @@ static void SPINDLE_SetDutyCycle(SpindleHandle_t h, void* context, int backwards
{ {
(void)h; (void)h;
(void)context; (void)context;
(void)backwards; if (backwards) {
(void)dutyCycle; __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_4, 0);
// TODO: set PWM duty cycle and direction __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)h;
(void)context; (void)context;
(void)ena;
// TODO: enable / disable PWM output and half bridges // 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.enaPWM = SPINDLE_EnaPWM;
s.context = NULL; 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 // the library copies the params, therefore a local struct is fine here
return SPINDLE_CreateInstance( 4*configMINIMAL_STACK_SIZE, configMAX_PRIORITIES - 3, console, &s ); return SPINDLE_CreateInstance( 4*configMINIMAL_STACK_SIZE, configMAX_PRIORITIES - 3, console, &s );
} }
+1 -2
View File
@@ -107,7 +107,7 @@ static void playgroundTask(void* args)
{ {
(void)args; (void)args;
printf("Hnot ello World\r\n"); printf("Hello World\r\n");
while(1) { while(1) {
vTaskDelay(pdMS_TO_TICKS(1000)); vTaskDelay(pdMS_TO_TICKS(1000));
@@ -248,7 +248,6 @@ int main(void)
console = init_console_custom(); console = init_console_custom();
spindle = init_spindle_custom(console, 9000, -9000, 1600); spindle = init_spindle_custom(console, 9000, -9000, 1600);
// start the scheduler and setup free rtos execution context // start the scheduler and setup free rtos execution context
vTaskStartScheduler(); vTaskStartScheduler();