#include "TYW_stdint.h"

#ifndef SGMCD1020Q_H
#define SGMCD1020Q_H

#include "Simulated_SPI_1_Master.h"
#include "Dio.h"

/* SGMCD1020Q 寄存器地址定义 */
#define SGMCD_REG_SPI_CHECK          0x00
#define SGMCD_REG_DEVICE_CONFIG      0x02
#define SGMCD_REG_TRISTATE_SP        0x04
#define SGMCD_REG_TRISTATE_SG        0x06
#define SGMCD_REG_WETTING_CURRENT_SP 0x08
#define SGMCD_REG_WETTING_CURRENT_SG0 0x0A
#define SGMCD_REG_WETTING_CURRENT_SG1 0x0C
#define SGMCD_REG_CONT_WETTING_SP    0x16
#define SGMCD_REG_CONT_WETTING_SG    0x18
#define SGMCD_REG_INT_ENABLE_SP      0x1A
#define SGMCD_REG_INT_ENABLE_SG      0x1C
#define SGMCD_REG_LPM_CONFIG         0x1E
#define SGMCD_REG_WAKEUP_ENABLE_SP   0x20
#define SGMCD_REG_WAKEUP_ENABLE_SG   0x22
#define SGMCD_REG_COMPARATOR_ONLY_SP 0x24
#define SGMCD_REG_COMPARATOR_ONLY_SG 0x26
#define SGMCD_REG_LPM_THRESHOLD_SP   0x28
#define SGMCD_REG_LPM_THRESHOLD_SG   0x2A
#define SGMCD_REG_POLLING_CURRENT_SP 0x2C
#define SGMCD_REG_POLLING_CURRENT_SG 0x2E
#define SGMCD_REG_ENTER_LPM          0x39
#define SGMCD_REG_AMUX_CONTROL       0x3A
#define SGMCD_REG_READ_SWITCH_STATUS 0x3E
#define SGMCD_REG_FAULT_STATUS       0x42
#define SGMCD_REG_INTERRUPT_REQUEST  0x47
#define SGMCD_REG_RESET              0x49

/* 湿润电流级别定义 */
#define WETTING_CURRENT_2MA   0x00
#define WETTING_CURRENT_8MA   0x02
#define WETTING_CURRENT_12MA  0x04
#define WETTING_CURRENT_16MA  0x06

/* 设备选择 */
typedef enum {
    SGMCD_DEVICE_1 = 0,
    SGMCD_DEVICE_2,
    SGMCD_DEVICE_3
} SGMCD_DeviceType;


/* 开关状态联合体定义 */
typedef union {
    uint32_t raw_value;  /* 原始32位数据 */
    
    struct {
        /* 位域定义 - 与SGMCD1020Q寄存器布局完全对应 */
        uint32_t sg0    : 1;  /* SG0开关状态 */
        uint32_t sg1    : 1;  /* SG1开关状态 */
        uint32_t sg2    : 1;  /* SG2开关状态 */
        uint32_t sg3    : 1;  /* SG3开关状态 */
        uint32_t sg4    : 1;  /* SG4开关状态 */
        uint32_t sg5    : 1;  /* SG5开关状态 */
        uint32_t sg6    : 1;  /* SG6开关状态 */
        uint32_t sg7    : 1;  /* SG7开关状态 */
        uint32_t sg8    : 1;  /* SG8开关状态 */
        uint32_t sg9    : 1;  /* SG9开关状态 */
        uint32_t sg10   : 1;  /* SG10开关状态 */
        uint32_t sg11   : 1;  /* SG11开关状态 */
        uint32_t sg12   : 1;  /* SG12开关状态 */
        uint32_t sg13   : 1;  /* SG13开关状态 */
        
        uint32_t sp0    : 1;  /* SP0开关状态 */
        uint32_t sp1    : 1;  /* SP1开关状态 */
        uint32_t sp2    : 1;  /* SP2开关状态 */
        uint32_t sp3    : 1;  /* SP3开关状态 */
        uint32_t sp4    : 1;  /* SP4开关状态 */
        uint32_t sp5    : 1;  /* SP5开关状态 */
        uint32_t sp6    : 1;  /* SP6开关状态 */
        uint32_t sp7    : 1;  /* SP7开关状态 */
        
        uint32_t intflg : 1;  /* 中断标志位 */
        uint32_t fault  : 1;  /* 故障状态位 */
        uint32_t type   : 1;  /* 类型位（读操作为0） */
        uint32_t addr   : 7;  /* 地址位（0x3E） */
    } bits;    
} SGMCD1020Q_SwitchStatus_t;

extern volatile SGMCD1020Q_SwitchStatus_t SGMCD1020Q_1;
extern volatile SGMCD1020Q_SwitchStatus_t SGMCD1020Q_2;
extern volatile SGMCD1020Q_SwitchStatus_t SGMCD1020Q_3;

/* 开关状态枚举定义 */
typedef enum {
    SWITCH_OPEN = 0,    /* 开关断开 */
    SWITCH_CLOSED = 1   /* 开关闭合 */
} SwitchState_t;

/* 通道类型枚举 */
typedef enum {
    CHANNEL_SG = 0,     /* 开关对地通道 */
    CHANNEL_SP = 1      /* 可编程开关通道 */
} ChannelType_t;

/* 函数声明 */
void SGMCD1020Q_Init(void);
void SGMCD1020Q_SelectDevice(SGMCD_DeviceType device);
uint32_t SGMCD1020Q_SPI_Transfer(uint32_t data);
uint32_t SGMCD1020Q_ReadRegister(uint8_t regAddr);
void SGMCD1020Q_WriteRegister(uint8_t regAddr, uint32_t data);
uint32_t SGMCD1020Q_ReadSwitchStatus(void);
void SGMCD1020Q_SetWettingCurrent(uint8_t channel, uint8_t currentLevel);
void SGMCD1020Q_EnableInterrupt(uint8_t channel, uint8_t enable);
void SGMCD1020Q_MainFunction(void);
void SGMCD1020Q_EnterLowPowerMode(void);
void SGMCD1020Q_Reset(void);
uint32_t SGMCD1020Q_ReadFaultStatus(void);

#endif /* SGMCD1020Q_H */

