/*
 * @Date: 2025-06-26 16:30:18
 * @Author: mypx
 * @LastEditors: mypx mypx_coder@163.com
 * @LastEditTime: 2025-07-22 10:18:43
 * @FilePath: h0c_comm.h
 * @Description: 
 * Copyright (c) 2025 by mypx, All Rights Reserved.
 */
#ifndef __H0C_COMM_H__
#define __H0C_COMM_H__
#include "sv_common.h"
/* SV630P Servo Driver H0C Group Communication Parameter Register Address Macros */

/* H0C.00: Station address */
#define REG_H0C_00_AXIS_ADDR 0x0C00
#define H0C_00_MIN           0x01
#define H0C_00_MAX           0xF7
/* H0C.02: Communication baud rate */
#define REG_H0C_02_BAUD_RATE 0x0C02
typedef enum
{
    SV630P_BAUD_2400   = 0, /**0:2400bps */
    SV630P_BAUD_4800   = 1, /**1:4800bps */
    SV630P_BAUD_9600   = 2, /**2:9600bps */
    SV630P_BAUD_19200  = 3, /**3:19200bps */
    SV630P_BAUD_38400  = 4, /**4:38400bps */
    SV630P_BAUD_57600  = 5, /**5:57600bps */
    SV630P_BAUD_115200 = 6  /**6:115200bps */
} SV630P_BaudRate;

/* H0C.03: Data format */
#define REG_H0C_03_DATA_FORMAT 0x0C03
typedef enum
{
    SV630P_MODBUS_NONE_2STOP = 0, /**< 0:No parity, 2 stop bits */
    SV630P_MODBUS_EVEN_1STOP = 1, /**< 1:Even parity, 1 stop bit */
    SV630P_MODBUS_ODD_1STOP  = 2, /**< 2:Odd parity, 1 stop bit */
    SV630P_MODBUS_NONE_1STOP = 3  /**< 3:No parity, 1 stop bit */
} SV630P_ModbusFormat;

/* H0C.08: CANopen communication baud rate */
#define REG_H0C_08_CAN_RATE 0x0C08
#define H0C_08_MIN          0x0000
#define H0C_08_MAX          8

/* H0C.09: Communication VDI enable (200C-09h)
 * Enable virtual DI function, 0=disable, 1=enable
 * Address: 0x0C09
 * Range: 0~1 */
#define REG_H0C_09_VDI_ENABLE 0x0C09

typedef enum
{
    VDI_DISABLE = 0,
    VDI_ENABLE  = 1
} VDI_StatusTypeDef;


/* H0C.10: Default VDI value after power-on (200C-0Bh)
 * Initial VDI level configuration when servo is powered on, bit0 for VDI1, bit1 for VDI2...
 * Address: 0x0C10
 * Range: 0~0xFFFF */
#define REG_H0C_10_VDI_DEFAULT 0x0C10
#define H0C_10_MIN             0x0000
#define H0C_10_MAX             0xFFFF

/* H0C.11: Communication VDO enable (200C-0Ch)
 * Enable virtual DO function, 0=disable, 1=enable
 * Address: 0x0C11
 * Range: 0~1 */
#define REG_H0C_11_VDO_ENABLE 0x0C11
#define H0C_11_MIN            0x0000
#define H0C_11_MAX            0x0001

/* H0C.12: Default output level when VDO function select is 0 (200C-0Dh)
 * Default output level when VDO function is not enabled
 * Address: 0x0C12
 * Range: 0~0xFFFF */
#define REG_H0C_12_VDO_DEFAULT_LEVEL 0x0C12
#define H0C_12_MIN                   0x0000
#define H0C_12_MAX                   0xFFFF

/* H0C.13: Whether to update communication function code value to EEPROM (200C-0Eh)
 * 0=not update, 1=update
 * Address: 0x0C13
 * Range: 0~1 */
#define REG_H0C_13_FUNC_CODE_EEPROM 0x0C13
#define H0C_13_MIN                  0x0000
#define H0C_13_MAX                  0x0001

/* H0C.14: Modbus error code (200C-0Fh)
 * Display Modbus communication error code
 * Address: 0x0C14
 * Range: 0~4 (0x0000~0x0004) */
#define REG_H0C_14_MODBUS_ERROR_CODE 0x0C14
#define H0C_14_MIN                   0x0000
#define H0C_14_MAX                   0x0004

/* H0C.16: Whether to update CAN communication function code value to EEPROM (200C-10h)
 * 0=not update, 1=update
 * Address: 0x0C16
 * Range: 0~1 */
#define REG_H0C_16_CAN_EEPROM_UPDATE 0x0C16
#define H0C_16_MIN                   0x0000
#define H0C_16_MAX                   0x0001

/* H0C.25: Modbus command response delay (200C-19h)
 * Modbus response delay time setting (unit: 100μs)
 * Address: 0x0C25
 * Range: 0~20 (0~2000μs) */
#define REG_H0C_25_RESPONSE_DELAY 0x0C25
#define H0C_25_MIN                0x0000
#define H0C_25_MAX                0x0014

/* H0C.26: Modbus communication data byte order (200C-1Ah)
 * 0=high 16 bits first, 1=low 16 bits first
 * Address: 0x0C26
 * Range: 0~1 */
#define REG_H0C_26_DATA_BYTE_ORDER 0x0C26
#define H0C_26_MIN                 0x0000
#define H0C_26_MAX                 0x0001

/* H0C.30: Modbus error frame format selection (200C-1Eh)
 * 0=standard error frame, 1=extended error frame
 * Address: 0x0C30
 * Range: 0~1 */
#define REG_H0C_30_ERROR_FRAME_FORMAT 0x0C30
#define H0C_30_MIN                    0x0000
#define H0C_30_MAX                    0x0001

/* H0C.31: Modbus receive handling (200C-1Fh)
 * 0=standard handling, 1=fast handling
 * Address: 0x0C31
 * Range: 0~1 */
#define REG_H0C_31_RX_HANDLING 0x0C31
#define H0C_31_MIN             0x0000
#define H0C_31_MAX             0x0001

/**
 * @brief H0C.00 Read Drive Axis Address (16-bit)
 * @param value Pointer to store the axis address (1-247)
 * @return nmbs_error
 * @note The drive axis address must be consistent with the host configuration
 */
nmbs_error sv630p_read_axis_address(uint16_t *value);

/**
 * @brief H0C.00 Write Drive Axis Address (16-bit)
 * @param value Axis address to write (1-247)
 * @return nmbs_error
 * @note Change when stopped, takes effect immediately
 */
nmbs_error sv630p_write_axis_address(uint16_t value);

/**
 * @brief H0C.02 Read Serial Baud Rate Setting (16-bit)
 * @param value Pointer to store the baud rate setting (0-6)
 * @return nmbs_error
 * @note 0:2400bps, 1:4800bps, 2:9600bps, 3:19200bps, 4:38400bps, 5:57600bps, 6:115200bps
 */
nmbs_error sv630p_read_baud_rate(SV630P_BaudRate *baud_rate);

/**
 * @brief H0C.02 Write Serial Baud Rate Setting (16-bit)
 * @param value Baud rate setting to write (0-6)
 * @return nmbs_error
 * @note Communication will be interrupted after modification, serial port needs to be re-initialized
 */
nmbs_error sv630p_write_baud_rate(SV630P_BaudRate baud_rate);

/**
 * @brief H0C.03 Read Modbus Communication Data Format (16-bit)
 * @param value Pointer to store the data format (0-3)
 * @return nmbs_error
 * @note 0:No parity+2 stop bits, 1:Even parity+1 stop bit, 2:Odd parity+1 stop bit, 3:No parity+1 stop bit
 */
nmbs_error sv630p_read_modbus_data_format(SV630P_ModbusFormat *fmt);

/**
 * @brief H0C.03 Write Modbus Communication Data Format (16-bit)
 * @param value Data format to write (0-3)
 * @return nmbs_error
 * @note Must be consistent with the host computer, otherwise communication will fail. Takes effect immediately.
 */
nmbs_error sv630p_write_modbus_data_format(SV630P_ModbusFormat fmt);

#endif // __SV_COM_ARGS_REG_H__
