diff --git a/stm32-stepper-root/stepper_h7/CM7/Core/Inc/Stepper/stepper.h b/stm32-stepper-root/stepper_h7/CM7/Core/Inc/Stepper/stepper.h index bb1ed07..18fce1a 100644 --- a/stm32-stepper-root/stepper_h7/CM7/Core/Inc/Stepper/stepper.h +++ b/stm32-stepper-root/stepper_h7/CM7/Core/Inc/Stepper/stepper.h @@ -7,6 +7,8 @@ #include "LibL6474.h" #include +int StepperConsoleFunction( int argc, char** argv, void* ctx ); + static void* StepLibraryMalloc( unsigned int size ); static void StepLibraryFree( const void* const ptr ); diff --git a/stm32-stepper-root/stepper_h7/CM7/Core/Src/Stepper/stepper.c b/stm32-stepper-root/stepper_h7/CM7/Core/Src/Stepper/stepper.c index 1cf38f1..e224646 100644 --- a/stm32-stepper-root/stepper_h7/CM7/Core/Src/Stepper/stepper.c +++ b/stm32-stepper-root/stepper_h7/CM7/Core/Src/Stepper/stepper.c @@ -1,5 +1,52 @@ #include "Stepper/stepper.h" #include "LibL6474.h" +#include "main.h" + +int StepperConsoleFunction( int argc, char** argv, void* ctx ) +// -------------------------------------------------------------------------------------------------------------------- +{ + //possible commands are + //(stepper) step 100 + (void)argc; + (void)argv; + (void)ctx; + + L6474_Handle_t h = (L6474_Handle_t)ctx; + + // first decode the subcommand and all arguments + if ( argc == 0 ) + { + printf("invalid number of arguments\r\nFAIL"); + return -1; + } + else if ( strcmp(argv[0], "step") == 0 ) + { + + if ( argc < 2 ) + { + printf("missing step value for step command\r\nFAIL"); + return -1; + } + L6474_StepIncremental(h, atoi(argv[1])); + printf("%d steps", (int)atoi(argv[1])); + + } + else if ( strcmp(argv[0], "reset") == 0 ) + { + init_stepper(NULL, NULL, NULL); + } + else + { + printf("passed invalid sub command\r\nFAIL"); + return -1; + } + + + + // now back to console + return 0; +} + static void* StepLibraryMalloc( unsigned int size ){ @@ -14,7 +61,7 @@ static int StepDriverSpiTransfer( void* pIO, char* pRX, const char* pTX, unsigne for ( unsigned int i = 0; i < length; i++ ) { HAL_GPIO_WritePin(STEP_SPI_CS_GPIO_Port, STEP_SPI_CS_Pin, GPIO_PIN_RESET); - HAL_SPI_TransmitReceive(&hspi1, pTX, pRX, 1, -1); + HAL_SPI_TransmitReceive(&hspi1, (uint8_t*)&pTX[i], (uint8_t*)&pRX[i], 1, HAL_MAX_DELAY); HAL_GPIO_WritePin(STEP_SPI_CS_GPIO_Port, STEP_SPI_CS_Pin, GPIO_PIN_SET); } return 0; @@ -29,9 +76,30 @@ void StepDriverReset(void* pGPO, const int ena){ } void StepLibraryDelay(unsigned int ms){ - vTaskDelay(pdMS_TO_TICKS(ms)); + HAL_Delay(ms); } +// 1) Funktion schreiben (oben in stepper.c, vor init_stepper) +static int StepLibraryStep( void* pPWM, int dir, unsigned int numPulses ){ + (void)pPWM; + + if(dir == 1){ + HAL_GPIO_WritePin(STEP_DIR_GPIO_Port, STEP_DIR_Pin, GPIO_PIN_SET); + }else{ + HAL_GPIO_WritePin(STEP_DIR_GPIO_Port, STEP_DIR_Pin, GPIO_PIN_RESET); + } + + for(unsigned int i = 0; i < numPulses; i++){ + HAL_GPIO_WritePin(STEP_PULSE_GPIO_Port, STEP_PULSE_Pin, GPIO_PIN_SET); + HAL_Delay(1); + HAL_GPIO_WritePin(STEP_PULSE_GPIO_Port, STEP_PULSE_Pin, GPIO_PIN_RESET); + HAL_Delay(1); + } + return 0; +} + + + L6474_Handle_t init_stepper(void* pIO, void* pGPO, void* pPWM){ @@ -40,23 +108,26 @@ L6474_Handle_t init_stepper(void* pIO, void* pGPO, void* pPWM){ (void)pGPO; (void)pPWM; - L6474x_Platform_t p; + + + + static L6474x_Platform_t p; p.malloc = StepLibraryMalloc; p.free = StepLibraryFree; p.transfer = StepDriverSpiTransfer; p.reset = StepDriverReset; p.sleep = StepLibraryDelay; + p.step = StepLibraryStep; + //p.stepAsync = StepTimerAsync; //p.cancelStep = StepTimerCancelAsync; L6474_Handle_t s = L6474_CreateInstance(&p, pIO, pGPO, pPWM); L6474_BaseParameter_t b; - b.stepMode = smMICRO4; // Mikroschritt 1/4 - b.OcdTh = ocdth1125mA; // Überstromschwelle 1125 mA - b.TimeOnMin = 21; // µs (Datenblatt-Standard) - b.TimeOffMin = 21; // µs (Datenblatt-Standard) - b.TorqueVal = 700; // mA, bewusst unter OcdTh - b.TFast = 0x19; // Standardwert (Fast-Decay) + L6474_SetBaseParameter(&b); // sinnvolle Standardwerte + b.stepMode = smMICRO4; + b.OcdTh = ocdth1125mA; + L6474_EncodePhaseCurrentParameter(&b, 700.0f); // 700 mA richtig umrechnen int result = 0; @@ -71,7 +142,7 @@ L6474_Handle_t init_stepper(void* pIO, void* pGPO, void* pPWM){ // and then we step a bit if ( result == 0 ) { - result |= L6474_StepIncremental(s, 100000 ); + result |= L6474_StepIncremental(s, 10000 ); } else { diff --git a/stm32-stepper-root/stepper_h7/CM7/Core/Src/main.c b/stm32-stepper-root/stepper_h7/CM7/Core/Src/main.c index 536c44b..94d6875 100644 --- a/stm32-stepper-root/stepper_h7/CM7/Core/Src/main.c +++ b/stm32-stepper-root/stepper_h7/CM7/Core/Src/main.c @@ -60,7 +60,6 @@ SPI_HandleTypeDef hspi1; TIM_HandleTypeDef htim2; UART_HandleTypeDef huart3; - /* USER CODE BEGIN PV */ /* USER CODE END PV */ @@ -315,11 +314,15 @@ int main(void) CONSOLE_RegisterCommand(c, "fabi", "prints a hallo fabi", fabi, NULL); + CONSOLE_RegisterCommand(c, "stepper", "stepper command", + StepperConsoleFunction, NULL); + //Spindle init_spindle(c, 9000, -9000, 1600); + //Stepepr - init_stepper(NULL, NULL, NULL); +