################################################################################
# Copyright © 2019 TRINAMIC Motion Control GmbH & Co. KG
# (now owned by Analog Devices Inc.),
#
# Copyright © 2023 Analog Devices Inc. All Rights Reserved.
# This software is proprietary to Analog Devices, Inc. and its licensors.
################################################################################

"""
Dump all register values of the TMC4331 IC.

The connection to a Landungsbrücke is established over USB. TMCL commands are used for communicating with the IC.
"""
import pytrinamic
from pytrinamic.connections import ConnectionManager
from pytrinamic.evalboards import TMC4361_eval

pytrinamic.show_info()

my_interface = ConnectionManager().connect()
print(my_interface)

eval_board = TMC4361_eval(my_interface)
mc = eval_board.ics[0]
print("Motion controller info: " + str(mc.get_info()))
print("Register dump for " + str(mc.get_name()) + ":")

print("GENERAL_CONF:                           0x{0:08X}".format(eval_board.read_register(mc.REG.GENERAL_CONF)))
print("REFERENCE_CONF:                         0x{0:08X}".format(eval_board.read_register(mc.REG.REFERENCE_CONF)))
print("START_CONF:                             0x{0:08X}".format(eval_board.read_register(mc.REG.START_CONF)))
print("INPUT_FILT_CONF:                        0x{0:08X}".format(eval_board.read_register(mc.REG.INPUT_FILT_CONF)))
print("ENC_IN_CONF:                            0x{0:08X}".format(eval_board.read_register(mc.REG.ENC_IN_CONF)))
print("ENC_IN_DATA:                            0x{0:08X}".format(eval_board.read_register(mc.REG.ENC_IN_DATA)))
print("STEP_CONF:                              0x{0:08X}".format(eval_board.read_register(mc.REG.STEP_CONF)))
print("SPI_STATUS_SELECTION:                   0x{0:08X}".format(eval_board.read_register(mc.REG.SPI_STATUS_SELECTION)))
print("EVENT_CLEAR_CONF:                       0x{0:08X}".format(eval_board.read_register(mc.REG.EVENT_CLEAR_CONF)))
print("INTR_CONF:                              0x{0:08X}".format(eval_board.read_register(mc.REG.INTR_CONF)))
print("EVENTS:                                 0x{0:08X}".format(eval_board.read_register(mc.REG.EVENTS)))
print("STATUS:                                 0x{0:08X}".format(eval_board.read_register(mc.REG.STATUS)))
print("STP_LENGTH_ADD___DIR_SETUP_TIME:        0x{0:08X}".format(eval_board.read_register(mc.REG.STP_LENGTH_ADD___DIR_SETUP_TIME)))
print("START_OUT_ADD:                          0x{0:08X}".format(eval_board.read_register(mc.REG.START_OUT_ADD)))
print("GEAR_RATIO:                             0x{0:08X}".format(eval_board.read_register(mc.REG.GEAR_RATIO)))
print("START_DELAY:                            0x{0:08X}".format(eval_board.read_register(mc.REG.START_DELAY)))
print("CLK_GATING_DELAY:                       0x{0:08X}".format(eval_board.read_register(mc.REG.CLK_GATING_DELAY)))
print("STDBY_DELAY:                            0x{0:08X}".format(eval_board.read_register(mc.REG.STDBY_DELAY)))
print("CL_ANGLES:                              0x{0:08X}".format(eval_board.read_register(mc.REG.CL_ANGLES)))
print("HOME_SAFETY_MARGIN:                     0x{0:08X}".format(eval_board.read_register(mc.REG.HOME_SAFETY_MARGIN)))
print("RAMPMODE:                               0x{0:08X}".format(eval_board.read_register(mc.REG.RAMPMODE)))
print("XACTUAL:                                0x{0:08X}".format(eval_board.read_register(mc.REG.XACTUAL)))
print("VACTUAL:                                0x{0:08X}".format(eval_board.read_register(mc.REG.VACTUAL)))
print("AACTUAL:                                0x{0:08X}".format(eval_board.read_register(mc.REG.AACTUAL)))
print("VMAX:                                   0x{0:08X}".format(eval_board.read_register(mc.REG.VMAX)))
print("VSTART:                                 0x{0:08X}".format(eval_board.read_register(mc.REG.VSTART)))
print("VSTOP:                                  0x{0:08X}".format(eval_board.read_register(mc.REG.VSTOP)))
print("VBREAK:                                 0x{0:08X}".format(eval_board.read_register(mc.REG.VBREAK)))
print("AMAX:                                   0x{0:08X}".format(eval_board.read_register(mc.REG.AMAX)))
print("DMAX:                                   0x{0:08X}".format(eval_board.read_register(mc.REG.DMAX)))
print("ASTART:                                 0x{0:08X}".format(eval_board.read_register(mc.REG.ASTART)))
print("DFINAL:                                 0x{0:08X}".format(eval_board.read_register(mc.REG.DFINAL)))
print("DSTOP:                                  0x{0:08X}".format(eval_board.read_register(mc.REG.DSTOP)))
print("BOW1:                                   0x{0:08X}".format(eval_board.read_register(mc.REG.BOW1)))
print("BOW2:                                   0x{0:08X}".format(eval_board.read_register(mc.REG.BOW2)))
print("BOW3:                                   0x{0:08X}".format(eval_board.read_register(mc.REG.BOW3)))
print("BOW4:                                   0x{0:08X}".format(eval_board.read_register(mc.REG.BOW4)))
print("CLK_FREQ:                               0x{0:08X}".format(eval_board.read_register(mc.REG.CLK_FREQ)))
print("POS_COMP:                               0x{0:08X}".format(eval_board.read_register(mc.REG.POS_COMP)))
print("VIRT_STOP_LEFT:                         0x{0:08X}".format(eval_board.read_register(mc.REG.VIRT_STOP_LEFT)))
print("VIRT_STOP_RIGHT:                        0x{0:08X}".format(eval_board.read_register(mc.REG.VIRT_STOP_RIGHT)))
print("X_HOME:                                 0x{0:08X}".format(eval_board.read_register(mc.REG.X_HOME)))
print("X_LATCH___REV_CNT___X_RANGE:            0x{0:08X}".format(eval_board.read_register(mc.REG.X_LATCH___REV_CNT___X_RANGE)))
print("XTARGET:                                0x{0:08X}".format(eval_board.read_register(mc.REG.XTARGET)))
print("X_PIPE0:                                0x{0:08X}".format(eval_board.read_register(mc.REG.X_PIPE0)))
print("X_PIPE1:                                0x{0:08X}".format(eval_board.read_register(mc.REG.X_PIPE1)))
print("X_PIPE2:                                0x{0:08X}".format(eval_board.read_register(mc.REG.X_PIPE2)))
print("X_PIPE3:                                0x{0:08X}".format(eval_board.read_register(mc.REG.X_PIPE3)))
print("X_PIPE4:                                0x{0:08X}".format(eval_board.read_register(mc.REG.X_PIPE4)))
print("X_PIPE5:                                0x{0:08X}".format(eval_board.read_register(mc.REG.X_PIPE5)))
print("X_PIPE6:                                0x{0:08X}".format(eval_board.read_register(mc.REG.X_PIPE6)))
print("X_PIPE7:                                0x{0:08X}".format(eval_board.read_register(mc.REG.X_PIPE7)))
print("SH_REG0:                                0x{0:08X}".format(eval_board.read_register(mc.REG.SH_REG0)))
print("SH_REG1:                                0x{0:08X}".format(eval_board.read_register(mc.REG.SH_REG1)))
print("SH_REG2:                                0x{0:08X}".format(eval_board.read_register(mc.REG.SH_REG2)))
print("SH_REG3:                                0x{0:08X}".format(eval_board.read_register(mc.REG.SH_REG3)))
print("SH_REG4:                                0x{0:08X}".format(eval_board.read_register(mc.REG.SH_REG4)))
print("SH_REG5:                                0x{0:08X}".format(eval_board.read_register(mc.REG.SH_REG5)))
print("SH_REG6:                                0x{0:08X}".format(eval_board.read_register(mc.REG.SH_REG6)))
print("SH_REG7:                                0x{0:08X}".format(eval_board.read_register(mc.REG.SH_REG7)))
print("SH_REG8:                                0x{0:08X}".format(eval_board.read_register(mc.REG.SH_REG8)))
print("SH_REG9:                                0x{0:08X}".format(eval_board.read_register(mc.REG.SH_REG9)))
print("SH_REG10:                               0x{0:08X}".format(eval_board.read_register(mc.REG.SH_REG10)))
print("SH_REG11:                               0x{0:08X}".format(eval_board.read_register(mc.REG.SH_REG11)))
print("SH_REG12:                               0x{0:08X}".format(eval_board.read_register(mc.REG.SH_REG12)))
print("SH_REG13:                               0x{0:08X}".format(eval_board.read_register(mc.REG.SH_REG13)))
print("ENC_POS:                                0x{0:08X}".format(eval_board.read_register(mc.REG.ENC_POS)))
print("ENC_LATCH___ENC_RESET_VAL:              0x{0:08X}".format(eval_board.read_register(mc.REG.ENC_LATCH___ENC_RESET_VAL)))
print("ENC_POS_DEV___CL_TR_TOLERANCE:          0x{0:08X}".format(eval_board.read_register(mc.REG.ENC_POS_DEV___CL_TR_TOLERANCE)))
print("ENC_POS_DEV_TOL:                        0x{0:08X}".format(eval_board.read_register(mc.REG.ENC_POS_DEV_TOL)))
print("ENC_IN_RES___ENC_CONST:                 0x{0:08X}".format(eval_board.read_register(mc.REG.ENC_IN_RES___ENC_CONST)))
print("ENC_OUT_RES:                            0x{0:08X}".format(eval_board.read_register(mc.REG.ENC_OUT_RES)))
print("SER_CLK_IN_HIGH_LOW:                    0x{0:08X}".format(eval_board.read_register(mc.REG.SER_CLK_IN_HIGH_LOW)))
print("SSI_IN_CLK_DELAY___SSI_IN_WTIME:        0x{0:08X}".format(eval_board.read_register(mc.REG.SSI_IN_CLK_DELAY___SSI_IN_WTIME)))
print("SER_PTIME:                              0x{0:08X}".format(eval_board.read_register(mc.REG.SER_PTIME)))
print("CL_OFFSET:                              0x{0:08X}".format(eval_board.read_register(mc.REG.CL_OFFSET)))
print("PID_VEL___PID_P___CL_VMAX_CALC_P:       0x{0:08X}".format(eval_board.read_register(mc.REG.PID_VEL___PID_P___CL_VMAX_CALC_P)))
print("PID_ISUM_RD___PID_I___CL_VMAX_CALC_I:   0x{0:08X}".format(eval_board.read_register(mc.REG.PID_ISUM_RD___PID_I___CL_VMAX_CALC_I)))
print("PID_D___CL_DELTA_P:                     0x{0:08X}".format(eval_board.read_register(mc.REG.PID_D___CL_DELTA_P)))
print("PID_E___PID_I_CLIP___PID_D_CLKDIV:      0x{0:08X}".format(eval_board.read_register(mc.REG.PID_E___PID_I_CLIP___PID_D_CLKDIV)))
print("PID_DV_CLIP:                            0x{0:08X}".format(eval_board.read_register(mc.REG.PID_DV_CLIP)))
print("PID_TOLERANCE___CL_TOLERANCE:           0x{0:08X}".format(eval_board.read_register(mc.REG.PID_TOLERANCE___CL_TOLERANCE)))
print("V_ENC:                                  0x{0:08X}".format(eval_board.read_register(mc.REG.V_ENC)))
print("V_ENC_MEAN:                             0x{0:08X}".format(eval_board.read_register(mc.REG.V_ENC_MEAN)))
print("ADDR_TO_ENC:                            0x{0:08X}".format(eval_board.read_register(mc.REG.ADDR_TO_ENC)))
print("DATA_TO_ENC:                            0x{0:08X}".format(eval_board.read_register(mc.REG.DATA_TO_ENC)))
print("MSLUT__:                                0x{0:08X}".format(eval_board.read_register(mc.REG.MSLUT__)))
print("MSLUTSEL:                               0x{0:08X}".format(eval_board.read_register(mc.REG.MSLUTSEL)))
print("MSCNT:                                  0x{0:08X}".format(eval_board.read_register(mc.REG.MSCNT)))
print("ENC_COMP____:                           0x{0:08X}".format(eval_board.read_register(mc.REG.ENC_COMP____)))
print("VERSION_NO:                             0x{0:08X}".format(eval_board.read_register(mc.REG.VERSION_NO)))

my_interface.close()

print("\nReady.")
