/**
* @file MOTOR_STEP_ATY.h
*
* @param Project DEVICE_GENERAL_ATY_LIB
*
* @author ATY
*
* @copyright
* - Copyright 2017 - 2023 MZ-ATY
* - This code follows:
* - MZ-ATY Various Contents Joint Statement -
*
* https://mengze.top/MZ-ATY_VCJS
* - CC 4.0 BY-NC-SA -
*
* https://creativecommons.org/licenses/by-nc-sa/4.0/
* - Your use will be deemed to have accepted the terms of this statement.
*
* @brief Familiar functions of SteppingMotor control
*
* @version
* - 1_01_221231 > ATY
* -# Preliminary version, first Release
* - 1_02_230408 > ATY
* -# Add State Machine and so
* - 1_03_230426 > ATY
* -# Change name "SteppingMotor_ATY" to "MOTOR_STEP_ATY"
********************************************************************************
*/
#ifndef __MOTOR_STEP_ATY_H
#define __MOTOR_STEP_ATY_H
#include "INCLUDE_ATY.h"
#include "HW_GPIO_ATY.h"
#include "HW_PWM_ATY.h"
/******************************* For user *************************************/
#include "main.h"
// #define __DEBUG_MOTOR_STEP_ATY
/* set program for motor soft start */
// #define S_PARAM_C 0.5f // 0-1
// #define Motor_StartLine_2
// #define Motor_StartLine_T
// #define Motor_StartLine_S
/* 3 max, if need more, add codes at every function */
// #define MOTOR_COUNT 2
/* pin defination must include all motors,
diag pin is selectable, do not define if not use */
// #define MOTOR_EN_PORT_0 TMC1_EN_GPIO_Port
// #define MOTOR_EN_PIN_0 TMC1_EN_Pin
// #define MOTOR_DIR_PORT_0 TMC1_DIR_GPIO_Port
// #define MOTOR_DIR_PIN_0 TMC1_DIR_Pin
// #define MOTOR_STEP_PORT_0 TMC1_STEP_GPIO_Port
// #define MOTOR_STEP_PIN_0 TMC1_STEP_Pin
// // #define MOTOR_DIAG_PORT_0 0
// // #define MOTOR_DIAG_PIN_0 0
// #define MOTOR_PWM_Channel_0 PWM_T3C2
// #define MOTOR_EN_PORT_1 TMC1_EN_GPIO_Port
// #define MOTOR_EN_PIN_1 TMC1_EN_Pin
// #define MOTOR_DIR_PORT_1 TMC1_DIR_GPIO_Port
// #define MOTOR_DIR_PIN_1 TMC1_DIR_Pin
// #define MOTOR_STEP_PORT_1 TMC1_STEP_GPIO_Port
// #define MOTOR_STEP_PIN_1 TMC1_STEP_Pin
// // #define MOTOR_DIAG_PORT_1 0
// // #define MOTOR_DIAG_PIN_1 0
// #define MOTOR_PWM_Channel_1 PWM_T1C1
/* set step pin vol level when motor is not enable */
// #define STATIC_STEP_LEVEL 0
/******************************************************************************/
typedef struct{
uint8_t motorEn;
uint8_t motorDir;
uint8_t motorStepLevel;
uint8_t motorDiag;
uint32_t motorSoftSpeed;
uint32_t motorSpeed;
uint32_t motorSpeedCounter;
uint32_t motorSoftSteps;
uint32_t motorSteps;
uint32_t motorStepCounter;
uint32_t motorStopCounter; // delay for stall detection or driver error(TMC2209)
uint8_t motorSetState; // 0: stop, 1: start hold, 2: start soft, 3: run speed, 4: change dir, 5: stop state, soft set 0 after use, 10: scram motor
float speedIncreaseStep;
float S_paramA;
#ifdef S_PARAM_C
float S_paramC;
#else
float S_paramC;
#endif
}motorStep_t;
extern motorStep_t motorStep_t_Group[MOTOR_COUNT];
#define MOTOR_PARAM_SET(dir, softSpeed, speed, softSteps, steps, setState, channel) \
do \
{ \
motorStep_t_Group[channel].motorDir = (uint8_t)dir; \
motorStep_t_Group[channel].motorSoftSpeed = (uint32_t)softSpeed; \
motorStep_t_Group[channel].motorSpeed = (uint32_t)speed; \
motorStep_t_Group[channel].motorSoftSteps = (uint32_t)softSteps; \
motorStep_t_Group[channel].motorSteps = (uint32_t)steps; \
motorStep_t_Group[channel].motorSetState = (uint8_t)setState; \
} while(0)
#define MOTOR_PARAM_GET(dir, softSpeed, speed, softSteps, steps, setState, channel) \
do \
{ \
dir = motorStep_t_Group[(uint8_t)channel].motorDir; \
softSpeed = motorStep_t_Group[(uint8_t)channel].motorSoftSpeed; \
speed = motorStep_t_Group[(uint8_t)channel].motorSpeed; \
softSteps = motorStep_t_Group[(uint8_t)channel].motorSoftSteps; \
steps = motorStep_t_Group[(uint8_t)channel].motorSteps; \
setState = motorStep_t_Group[(uint8_t)channel].motorSetState; \
} while(0)
void MotorSetAble(uint8_t enable, uint8_t channel);
void MotorSetDirection(uint8_t dir, uint8_t channel);
void MotorSetStepLevel(uint8_t level, uint8_t channel);
uint8_t MotorGetDiagLevel(uint8_t channel);
void MotorStart(uint8_t dir, uint32_t speed, uint8_t channel);
void MotorStartSpeed(uint32_t speed, uint8_t channel);
void MotorStop(uint8_t channel);
void MotorStateMachine_Step(void);
void MotorStateMachine_Step_PWM(void);
#define MOTOR_DIR_IN 1
#define MOTOR_DIR_OUT 0
#define MOTOR_SET_REVERSE 4
#define MOTOR_SET_START_DIRECTLY 3
#define MOTOR_SET_START_SOFT 2
#define MOTOR_SET_STOP 0
#define MOTOR_SET_SCRAM 10
#define MOTOR_SET_FINISHED 5
#define MOTOR_SET_RUNNING 1
#define PWM_T01C1 0x11
#define PWM_T01C2 0x12
#define PWM_T01C3 0x13
#define PWM_T01C4 0x14
#define PWM_T02C1 0x21
#define PWM_T02C2 0x22
#define PWM_T02C3 0x23
#define PWM_T02C4 0x24
#define PWM_T03C1 0x31
#define PWM_T03C2 0x32
#define PWM_T03C3 0x33
#define PWM_T03C4 0x34
#define PWM_T04C1 0x41
#define PWM_T04C2 0x42
#define PWM_T04C3 0x43
#define PWM_T04C4 0x44
#define PWM_T05C1 0x51
#define PWM_T05C2 0x52
#define PWM_T05C3 0x53
#define PWM_T05C4 0x54
#define PWM_T06C1 0x61
#define PWM_T06C2 0x62
#define PWM_T06C3 0x63
#define PWM_T06C4 0x64
#define PWM_T07C1 0x71
#define PWM_T07C2 0x72
#define PWM_T07C3 0x73
#define PWM_T07C4 0x74
#define PWM_T08C1 0x81
#define PWM_T08C2 0x82
#define PWM_T08C3 0x83
#define PWM_T08C4 0x84
#define PWM_T09C1 0x91
#define PWM_T09C2 0x92
#define PWM_T09C3 0x93
#define PWM_T09C4 0x94
#define PWM_T10C1 0xA1
#define PWM_T10C2 0xA2
#define PWM_T10C3 0xA3
#define PWM_T10C4 0xA4
#define PWM_T11C1 0xB1
#define PWM_T11C2 0xB2
#define PWM_T11C3 0xB3
#define PWM_T11C4 0xB4
#define PWM_T12C1 0xC1
#define PWM_T12C2 0xC2
#define PWM_T12C3 0xC3
#define PWM_T12C4 0xC4
#define PWM_T13C1 0xD1
#define PWM_T13C2 0xD2
#define PWM_T13C3 0xD3
#define PWM_T13C4 0xD4
#define PWM_T14C1 0xE1
#define PWM_T14C2 0xE2
#define PWM_T14C3 0xE3
#define PWM_T14C4 0xE4
#define PWM_T15C1 0xF1
#define PWM_T15C2 0xF2
#define PWM_T15C3 0xF3
#define PWM_T15C4 0xF4
#endif /* __MOTOR_STEP_ATY_H */
/******************************** End Of File *********************************/