#pragma once

// =================================================================================================
// General Registers (p.23)
// =================================================================================================
#define REG_ADDR_GENERAL_GCONF        0x00
#define REG_ADDR_GENERAL_GSTAT        0x01
#define REG_ADDR_GENERAL_IFCNT        0x02
#define REG_ADDR_GENERAL_NODECONF     0x03
#define REG_ADDR_GENERAL_OTP_PROG     0x04
#define REG_ADDR_GENERAL_OTP_READ     0x05
#define REG_ADDR_GENERAL_IOIN         0x06
#define REG_ADDR_GENERAL_FACTORY_CONF 0x07

/// @brief Global configuration flags
struct REG_GCONF {
    /**
     * (Reset default=1)
     * 0: Use internal reference derived from 5VOUT
     * 1: Use voltage supplied to VREF as current reference
     */
    unsigned int I_scale_analog: 1;

    /**
     * (Reset default: OTP)
     * 0: Operation with external sense resistors
     * 1: Internal sense resistors. Use current supplied into VREF as reference for internal sense resistor. VREF pin internally is driven to GND in this mode.
     */
    unsigned int internal_Rsense: 1;

    /**
     * (Reset default: OTP)
     * 0: StealthChop PWM mode enabled (depending on velocity thresholds). Initially switch from off to on state while in stand still, only.
     * 1: SpreadCycle mode enabled A high level on the pin SPREAD inverts this flag to switch between both chopper modes.
     */
    unsigned int en_SpreadCycle: 1;

    /**
     * 1: Inverse motor direction
     */
    unsigned int shaft: 1;

    /**
     * 0: INDEX shows the first microstep position of sequencer
     * 1: INDEX pin outputs overtemperature prewarning flag (otpw) instead
     */
    unsigned int index_otpw: 1;

    /**
     * 0: INDEX output as selected by index_otpw
     * 1: INDEX output shows step pulses from internal pulse generator (toggle upon each step)
     */
    unsigned int index_step: 1;

    /**
     * 0: PDN_UART controls standstill current reduction
     * 1: PDN_UART input function disabled. Set this bit, when using the UART interface!
     */
    unsigned int pdn_disable: 1;

    /**
     * 0: Microstep resolution selected by pins MS1, MS2
     * 1: Microstep resolution selected by MRES register
     */
    unsigned int mstep_reg_select: 1;

    /**
     * 0: No filtering of STEP pulses
     * 1: Software pulse generator optimization enabled when fullstep frequency > 750Hz (roughly). TSTEP shows filtered step time values when active
     */
    unsigned int multistep_filt: 1;

    /**
     * 0: Normal operation
     * 1: Enable analog test output on pin ENN (pull down resistor off), ENN treated as enabled. IHOLD[1..0] selects the function of DCO: 0…2: T120, DAC, VDDH
     */
    unsigned int test_mode: 1;
    
    unsigned int :22;
};

/// @brief Global status flags
struct REG_GSTAT {
    /**
     * 1: Indicates that the IC has been reset since the last read access to GSTAT. All registers have been cleared to reset values.
     */
    unsigned int reset: 1;

    /**
     * 1: Indicates, that the driver has been shut down due to overtemperature or short circuit detection since the last read access. Read DRV_STATUS for details. The flag can only be cleared when all error conditions are cleared
     */
    unsigned int drv_err: 1;
    
    /**
     * Indicates an undervoltage on the charge pump. The driver is disabled in this case. This flag is not latched and thus does not need to be cleared
     */
    unsigned int uv_cp: 1;
    
    unsigned int :29;
};

/// @brief Interface transmission counter
struct REG_IFCNT {
    /**
     * Interface transmission counter. This register becomes incremented with each successful UART interface write access. Read out to check the serial transmission for lost data. Read accesses do not change the content. The counter wraps around from 255 to 0.
     */
    unsigned int mstep: 8;

    unsigned int :24;
};

/// @brief Node configuration
struct REG_NODECONF {
    unsigned int :8;

    /**
     * SENDDELAY for read access (time until reply is sent):
     * 0, 1: 8 bit times (Attention: Don’t use in multi-node)
     * 2, 3: 3*8 bit times
     * 4, 5: 5*8 bit times
     * 6, 7: 7*8 bit times
     * 8, 9: 9*8 bit times
     * 10, 11: 11*8 bit times
     * 12, 13: 13*8 bit times
     * 14, 15: 15*8 bit times
     */
    unsigned int senddelay: 4;

    unsigned int :16;
};

/// @brief OTP programming
struct REG_OPT_PROG {
    /**
     * Selection of OTP bit to be programmed to the selected byte location (n=0..7: programs bit n to a logic 1)
     */
    unsigned int otpbit: 3;

    /**
     * Selection of OTP programming location (0, 1 or 2)
     */
    unsigned int optbyte: 1;

    /**
     * Set to 0xbd to enable programming. A programming time of minimum 10ms per bit is recommended (check by reading OTP_READ).
     */
    unsigned int optmagic: 8;

    unsigned int :16;
};

/// @brief OTP read (Access to OTP memory result and update)
struct OTP_READ {
    /// @brief OTP0 byte 0 read data
    unsigned int opt0: 8;

    /// @brief OTP1 byte 1 read data
    unsigned int opt1: 8;

    /// @brief OTP2 byte 2 read data
    unsigned int opt2: 8;

    unsigned int :8;
};


/// @brief Inputs (Reads the state of all input pins available)
struct REG_IOIN {
    unsigned int enn: 1;
    unsigned int :1;
    unsigned int ms1: 1;
    unsigned int ms2: 1;
    unsigned int diag: 1;
    unsigned int :1;
    unsigned int pdn_uart: 1;
    unsigned int step: 1;
    unsigned int spread_en: 1;
    unsigned int dir: 1;

    /**
     * @brief VERSION: 0x21=first version of the IC Identical numbers mean full digital compatibility.
     */
    unsigned int version: 8;
};

struct REG_FACTORY_CONF {
    /**
     * Lowest to highest clock frequency. Check at charge pump output. The frequency span is not guaranteed, but it is tested, that tuning to 12MHz internal clock is possible. The devices come preset to 12MHz clock frequency by OTP programming.
     */
    unsigned int fclktrim: 5;

    /**
     * %00: OT=143°C, OTPW=120°C
     * %01: OT=150°C, OTPW=120°C
     * %10: OT=150°C, OTPW=143°C
     * %11: OT=157°C, OTPW=143°C
     */
    unsigned int ottrim: 2;
    
    unsigned int :25;
};

// =================================================================================================
// Velocity Dependent Control (p.28)
// =================================================================================================
#define REG_ADDR_VELOCITY_IHOLD_IRUN   0x10
#define REG_ADDR_VELOCITY_TPOWERDOWN   0x11
#define REG_ADDR_VELOCITY_TSTEP        0x12
#define REG_ADDR_VELOCITY_TPWMTHRS     0x13
#define REG_ADDR_VELOCITY_VACTUAL      0x22

/// @brief Driver current control
struct REG_IHOLD_IRUN {
    /**
     * (Reset default: OTP)
     * IHOLD (Reset default: OTP)
     * Standstill current (0=1/32 … 31=32/32)
     * In combination with StealthChop mode, setting
     * IHOLD=0 allows to choose freewheeling or coil short circuit (passive braking) for motor stand still.
     */
    unsigned int ihold: 5;

    /**
     * (Reset default=31)
     * Motor run current (0=1/32 … 31=32/32)
     * 
     * Tint: Choose sense resistors in a way, that normal IRUN is 16 to 31 for best microstep performance.
     */
    unsigned int irun: 5;
    
    /**
     * (Reset default: OTP)
     * Controls the number of clock cycles for motor power down after standstill is detected (stst=1) and TPOWERDOWN has expired. The smooth transition avoids a motor jerk upon power down.
     * 0: instant power down
     * 1..15: Delay per current reduction step in multiple of 2^18 clocks
     */
    unsigned int iholddelay: 4;
    unsigned int :18;
};


struct REG_TPOWERDOWN {
    /**
     * (Reset default=20)
     * Sets the delay time from stand still (stst) detection to motor current power down. Time range is about 0 to 5.6 seconds.
     * 0…((2^8)-1) * 2^18 tCLK
     * Attention: A minimum setting of 2 is required to allow automatic tuning of StealthChop PWM_OFFS_AUTO.
     */
    unsigned int tpow: 8;

    unsigned int :24;
};

/// @brief Time between two 1/256 microsteps
struct REG_TSTEP {
    /**
     * Actual measured time between two 1/256 microsteps derived from the step input frequency in units of 1/fCLK. Measured value is (2^20)-1 in case of overflow or stand still. TSTEP always relates to 1/256 step, independent of the actual MRES. The TSTEP related threshold uses a hysteresis of 1/16 of the compare value to compensate for jitter in the clock or the step frequency: (Txxx*15/16)-1 is the lower compare value for each TSTEP based comparison.
     * This means, that the lower switching velocity equals the calculated setting, but the upper switching velocity is higher as defined by the hysteresis setting.
     * 
     */
    unsigned int tstep: 20;

    unsigned int :12;
};

/// @brief  Upper velocity for StealthChop voltage PWM mode
struct REG_TPWMTHRS {
    /**
     * Sets the upper velocity for StealthChop voltage PWM mode.
     * TSTEP ≥ TPWMTHRS
     * - StealthChop PWM mode is enabled, if configured
     * When the velocity exceeds the limit set by TPWMTHRS, the driver switches to SpreadCycle.
     * 0: Disabled
     */
    unsigned int tpwmthrs: 20;
    
    unsigned int :12;
};

struct REG_VACTUAL {
    /**
     * ACTUAL allows moving the motor by UART control.
     * It gives the motor velocity in +-(2^23)-1 [μsteps / t]
     * 0: Normal operation. Driver reacts to STEP input.
     * !=0: Motor moves with the velocity given by VACTUAL. Step pulses can be monitored via INDEX output. The motor direction is controlled by the sign of VACTUAL.
     */
    int vactual: 24;

    unsigned int :8;
};

// =================================================================================================
// StallGuard (p.29)
// =================================================================================================

#define REG_ADDR_STALLGUARD_TCOOLTHRS    0x14
#define REG_ADDR_STALLGUARD_SGTHRS       0x40
#define REG_ADDR_STALLGUARD_SG_RESULT    0x41
#define REG_ADDR_STALLGUARD_COOLCONF     0x42

struct REG_TCOOLTHRS {
    /**
     * This is the lower threshold velocity for switching on smart energy CoolStep and StallGuard to DIAG output. (unsigned) Set this parameter to disable CoolStep at low speeds, where it cannot work reliably. The stall output signal become enabled when exceeding this velocity. It becomes disabled again once the velocity falls below this threshold.
     * TCOOLTHRS ≥ TSTEP > TPWMTHRS
     * - CoolStep is enabled, if configured (only with StealthChop)
     * - Stall output signal on pin DIAG is enabled
     */
    unsigned int tcoolthrs: 20;

    unsigned int :12;
};

struct REG_SGTHRS {
    /**
     * Detection threshold for stall. The StallGuard value SG_RESULT becomes compared to the double of this threshold. A stall is signaled with
     * SG_RESULT ≤ SGTHRS*2
     */
    unsigned int sgthrs: 8;

    unsigned int :24;
};

struct REG_SG_RESULT {
    /**
     * StallGuard result. SG_RESULT becomes updated with each fullstep, independent of TCOOLTHRS and SGTHRS. A higher value signals a lower motor load and more torque headroom. Intended for StealthChop mode, only. Bits 9 and 0 will always show 0. Scaling to 10 bit is for compatibility to StallGuard2
     */
    unsigned int sg_result: 10;

    unsigned int :22;
};

struct REG_COOLCONF {
    /**
     * @brief CoolStep configuration
     */
    unsigned int coolconf: 16;
    
    unsigned int :16;
};

// =================================================================================================
// Sequencer Registers (p.30)
// =================================================================================================q

#define REG_ADDR_SEQUENCER_MSCNT        0x6A
#define REG_ADDR_SEQUENCER_MSCURACT     0x6B

/// @brief Microstep counter
struct REG_MSCNT {
    /**
     * Microstep counter. Indicates actual position in the microstep table for CUR_A. CUR_B uses an offset of 256 into the table. Reading out MSCNT allows determination of the motor position within the electrical wave.
     */
    unsigned int mscnt: 10;

    unsigned int :22;
};

/// @brief Microstep current (actual)
struct REG_MSCURACT {
    /**
     * Actual microstep current for motor phase B (sine wave) as read from the internal sine wave table (not scaled by current setting)
     */
    int cur_a: 9;
    
    /**
     * Actual microstep current for motor phase A (co-sine wave) as read from the internal sine wave table (not scaled by current setting)
     */
    int cur_b: 9;

    unsigned int :14;
};

// =================================================================================================
// Chopper Registers (p.32)
// =================================================================================================

#define REG_ADDR_CHOPPER_CHOPCONF     0x6C
#define REG_ADDR_CHOPPER_DRV_STATUS   0x6F
#define REG_ADDR_CHOPPER_PWMCONF      0x70
#define REG_ADDR_CHOPPER_PWM_SCALE    0x71
#define REG_ADDR_CHOPPER_PWM_AUTO     0x72

struct REG_CHOPCONF {
    /**
     * @brief off time and driver enable
     * Off time setting controls duration of slow decay phase 
     * NCLK= 24 + 32*TOFF 
     * %0000: Driver disable, all bridges off 
     * %0001: 1 – use only with TBL ≥ 2 
     * %0010 ... %1111: 2 ... 15 
     * (Default: OTP, resp. 3 in StealthChop mode)
     */
    unsigned int toff: 4;

    /**
     * @brief hysteresis start value added to HEND
     * %000 ... %111:
     * Add 1, 2, ..., 8 to hysteresis low value HEND
     * (1/512 of this setting adds to current setting)
     * Attention: Effective HEND+HSTRT ≤ 16.
     * Hint: Hysteresis decrement is done each 16 clocks
     */
    unsigned int hstrt: 3;

    /**
     * @brief HEND hysteresis low value OFFSET sine wave offset
     * %0000 ... %1111:
     * Hysteresis is -3, -2, -1, 0, 1, ..., 12
     * (1/512 of this setting adds to current setting)
     * This is the hysteresis value which becomes used for the hysteresis chopper.
     * (Default: OTP, resp. 0 in StealthChop mode)
     */
    unsigned int hend: 4;

    unsigned int :1;

    /**
     * @brief TBL blank time select
     * %00 ... %11:
     * Set comparator blank time to 16, 24, 32 or 40 clocks
     * Hint: %00 or %01 is recommended for most applications
     * (Default: OTP)
     */
    unsigned int tbl: 2;

    /**
     * @brief sense resistor voltage based current scaling
     * 0: Low sensitivity, high sense resistor voltage
     * 1: High sensitivity, low sense resistor voltage
     */
    unsigned int vsense: 1;

    unsigned int :6;

    /**
     * @brief micro step resolution
     * %0000: Native 256 microstep setting.
     * %0001 ... %1000:
     * 128, 64, 32, 16, 8, 4, 2, FULLSTEP
     * Reduced microstep resolution.
     * The resolution gives the number of microstep entries per sine quarter wave.
     * When choosing a lower microstep resolution, the driver automatically uses microstep positions which result in a symmetrical wave.
     * Number of microsteps per step pulse = 2^MRES (Selection by pins unless disabled by GCONF. mstep_reg_select)
     */
    unsigned int mres: 4;

    /**
     * @brief interpolation to 256 microsteps
     * 1: The actual microstep resolution (MRES) becomes extrapolated to 256 microsteps for smoothest motor operation. (Default: 1)
     */
    unsigned int intpol: 1;

    /**
     * @brief enable double edge step pulses
     * 1: Enable step impulse at each step edge to reduce step frequency requirement. This mode is not compatible with the step filtering function (multistep_filt)
     */
    unsigned int dedge: 1;

    /**
     * @brief short to GND protection disable
     * 0: Short to GND protection is on 
     * 1: Short to GND protection is disabled
     */
    unsigned int diss2g: 1;

    /**
     * @brief Low side short protection disable
     * 0: Short protection low side is on 
     * 1: Short protection low side is disabled
     */
    unsigned int diss2vs: 1;
};

struct DRV_STATUS {
    unsigned int drv_status: 32;
};

struct PWMCONF {
    unsigned int pwmconf: 22;
    unsigned int :10;
};


/**
 * Results of StealthChop amplitude regulator. These values can be used to monitor automatic PWM amplitude scaling (255=max. voltage).
 */
struct PWM_SCALE {
    /**
     * Actual PWM duty cycle. This value is used for scaling the values CUR_A and CUR_B read from the sine wave table.
     */
    unsigned int pwm_scale_sum: 8;

    /**
     * 9 Bit signed offset added to the calculated PWM duty cycle. This is the result of the automatic amplitude regulation based on current measurement.
     */
    unsigned int pwm_scale_auto: 9;

    unsigned int :15;
};

/**
 * These automatically generated values can be read out in order to determine a default / power up setting for PWM_GRAD and PWM_OFS.
 */
struct PWM_AUTO {

    /**
     * Automatically determined offset value
     */
    unsigned int pwm_ofs_auto: 8;

    /**
     * Automatically determined gradient value
     */
    unsigned int pwm_grad_auto: 8;

    unsigned int :16;
};