/**
* @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 *************************************/
// #define __DEBUG_MOTOR_STEP_ATY
// #define S_PARAM_C 0.5f // 0-1
// #define Motor_StartLine_2
#define Motor_StartLine_T
// #define Motor_StartLine_S
// #define Motor_Channel PWM_T2C2
// 3 max, if need more, add codes at every function
#define MOTOR_COUNT 2
// pin defination must include all motors
// diag pin is selectable
#define MOTOR_EN_PORT_0 0
#define MOTOR_EN_PIN_0 P37
#define MOTOR_DIR_PORT_0 0
#define MOTOR_DIR_PIN_0 P21
#define MOTOR_STEP_PORT_0 0
#define MOTOR_STEP_PIN_0 P23
#define MOTOR_DIAG_PORT_0 0
#define MOTOR_DIAG_PIN_0 0
#define MOTOR_PWM_Channel_0 PWM_CHANNEL_8P
#define MOTOR_EN_PORT_1 0
#define MOTOR_EN_PIN_1 P36
#define MOTOR_DIR_PORT_1 0
#define MOTOR_DIR_PIN_1 P20
#define MOTOR_STEP_PORT_1 0
#define MOTOR_STEP_PIN_1 P22
#define MOTOR_DIAG_PORT_1 0
#define MOTOR_DIAG_PIN_1 0
#define MOTOR_PWM_Channel_1 PWM_CHANNEL_7P
#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)
#define MOTOR_DIR_IN 1
#define MOTOR_DIR_OUT 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);
#endif /* __MOTOR_STEP_ATY_H */
/******************************** End Of File *********************************/