//========================================================================
// Skyline Robotics PowerStep stepper motor driver control software
//
// AUTHOR:  Gabor Nagy
// DATE:    2016-Jul-03 05:01:41
//
// Copyright (C) 1996-2026 By Gabor Nagy. All rights reserved.
//========================================================================
#ifndef SR_PowerStep_h
#define SR_PowerStep_h

#include <SR/Motor.h>

typedef struct  SrPowerStep_S  SrPowerStep;

#define SrMotorCURRENT_MULT   10000

#define SrStepperDriverCore\
	SrMotorDriverCore;\
	int      StepPulseHighMin, StepPulseLowMin;  /* Minimum time in microseconds for step pulse high and low states */\
	int      Current;                            /* Motor driver current in units of SrMotorCURRENT_MULT            */\
	void     (*SetCurrent)(SrStepperMotor* PMotor, const int PP);\
	pthread_t        PThread;\
	pthread_mutex_t  _mutex;\
	EBool            PThread_Created





#ifdef __BEAGLEBONE__
	#if __BEAGLEBONE__ == BeagleBoneWHITE
		#define SrPowerStepCoreBeagleBoneExt\
			char*    PWMMUX;
	#else
		#define SrPowerStepCoreBeagleBoneExt
	#endif


#define SrPowerStepCoreBeagleBone\
	int16_t  STEPbit;\
	int16_t  DIRbit;\
	int16_t  LIMITAbit;\
	int16_t  LIMITBbit;\
	char*    PWMDir;\
	EBool    PWMInitialized;\
	SrPowerStepCoreBeagleBoneExt
#else
	#define  SrPowerStepCoreBeagleBone
#endif



#define SrPowerStepCore\
	SrStepperDriverCore;\
	SrPowerStepCoreBeagleBone


struct SrPowerStep_S
{
	SrPowerStepCore;
};


typedef struct
{
	SrPowerStepCore;

#ifdef __BEAGLEBONE__
// GPIO addresses GPIO port * 32 + pin (e.g. GPIO1_6 = 32 * 1 + 6 = 38)
// -1 : not used / invalid
//
	int16_t  ENABLEbit;
	EBool    Enable1;          // If TRUE, ENABLEbit = 1 means power outputs are enabled. If FALSE, 0 means enabled.
#endif
}	SrPowerStep2_16, SrPowerStep2_32, SrPowerStep4_128;



typedef struct
{
	SrPowerStepCore;

#ifdef __BEAGLEBONE__
// GPIO addresses GPIO port * 32 + pin (e.g. GPIO1_6 = 32 * 1 + 6 = 38)
// -1 : not used / invalid
//
	int16_t  ENABLEbit;
	int16_t  RESETbit;
	int16_t  TQ1bit;
	int16_t  TQ2bit;
	int16_t  ENC_Abit, ENC_Bbit, ENC_Ibit;
#endif
}	SrPowerStep3_16;


extern const SrMotorDriverClass  SrPowerStep2_16_class[];    // Skyline Robotics PowerStep2.5-16, with Allegro A3979 chip (2.5A, max 1/16 micro-stepping)
extern const SrMotorDriverClass  SrPowerStep2_32_class[];    // Skyline Robotics PowerStep2-32, with TI 8825 chip (2.5A, max 1/32 micro-stepping)
extern const SrMotorDriverClass  SrPowerStep3_16_class[];    // Skyline Robotics PowerStep3-16, with Toshiba TB6560 chip (3A, max 1/16 micro-stepping)
extern const SrMotorDriverClass  SrPowerStep4_128_class[];   // Skyline Robotics PowerStep4-128, with ON Semiconductor LV8727 chip (4A, max 1/128 micro-stepping)

extern void             Sr_PowerStepTest(SrGPIO* PGPIO, const SrMotorDriverClass* PMotorDriverClass, const int PIndex);
extern SrStepperMotor*  Sr_PowerStepCreateAndConnectMotor(SrGPIO* PGPIO, const SrMotorDriverClass* PMotorDriverClass, const int PIndex, const char* PName);

#endif // SR_PowerStep_h
