// 同步模块公共头文件
// 杭州秒寻科技有限公司
// 周涛
// 2023-11-18

#ifndef SYNCPUBLIC_H
#define SYNCPUBLIC_H

// 模块管理结构
typedef struct
{
    uint8 byTxRxFlag;     // 模块收发标识，0：rx，1：tx
    uint8 byTxCplt;       // 发送完成，1:完成，0：未完成
    uint8 byRxCplt;       // 接收完成，1:完成，0：未完成
    uint8 bySendAlarm;    // 0:无告警，1:告警
    uint8 byReceiveAlarm; // 0:无告警，1:告警
} SYNC_RXTX_STATUS_MANAGE_T;

// 配置载波频率
typedef struct
{
    unsigned int FREQUENCRY;  // target frequencry, unit:Hz
    unsigned int DEVIATION;   // target deviation,  unit:Hz
    unsigned int SYMBOL_RATE; // target symbol rate,unit:bps
    unsigned char XTAL_PPM;   // 晶振频偏
} FREQ_CFG;

#define SYNC_BURST_WRITE_FIFO 0x7A // 连续读写fifo的寄存器入口地址
#define SYNC_BURST_WRITE_REG 0x7B  // 连续读写寄存器的入口地址

#define SYNC_CHIP_ID 0x2310C0 // 芯片ID
#define SYNC_PAGE0_DATA_LEN 128
#define SYNC_PAGE1_DATA_LEN 128
#define SYNC_PAGE2_DATA_LEN 128

// 切换状态命令
#define SYNC_STATUS_GO_SLEEP 0x01
#define SYNC_STATUS_GO_READY 0x02
#define SYNC_STATUS_GO_TX 0x04
#define SYNC_STATUS_GO_RX 0x08
#define SYNC_STATUS_GO_TFS 0x10
#define SYNC_STATUS_GO_RFS 0x20

// 模块状态
#define SYNC_STATUS_IDLE 0x00
#define SYNC_STATUS_SLEEP 0x81
#define SYNC_STATUS_READY 0x82
#define SYNC_STATUS_RFS 0x84
#define SYNC_STATUS_TFS 0x88
#define SYNC_STATUS_RX 0x90
#define SYNC_STATUS_TX 0xA0

// 模块频率
#define SYNC_LOW_FREQUENCY 112500000
#define SYNC_HIGH_FREQUENCY 960000000

#define RADIO_TX_POWER_OFFSET_LABEL 10 // 发射功率偏移

// --------------------------- CUS PAGE1 defines -----------------------------------------
#define CMT2310A_CMT_REG_00 0x00     // export by RFPDK
#define CMT2310A_CMT_REG_01 0x01     // export by RFPDK
#define CMT2310A_CMT_REG_02 0x02     // export by RFPDK
#define CMT2310A_CMT_REG_03 0x03     // export by RFPDK
#define CMT2310A_CMT_REG_04 0x04     // export by RFPDK
#define CMT2310A_CMT_REG_05 0x05     // export by RFPDK
#define CMT2310A_CMT_REG_06 0x06     // export by RFPDK
#define CMT2310A_CMT_REG_07 0x07     // export by RFPDK
#define CMT2310A_CMT_REG_08 0x08     // export by RFPDK
#define CMT2310A_CMT_REG_09 0x09     // export by RFPDK
#define CMT2310A_CMT_REG_10 0x0A     // export by RFPDK
#define CMT2310A_CMT_REG_11 0x0B     // export by RFPDK
#define CMT2310A_CMT_REG_12 0x0C     // export by RFPDK
#define CMT2310A_CMT_REG_13 0x0D     // export by RFPDK
#define CMT2310A_CMT_REG_14 0x0E     // export by RFPDK
#define CMT2310A_CMT_REG_15 0x0F     // export by RFPDK
#define CMT2310A_TX_FREQ_REG_00 0x10 // export by RFPDK
#define CMT2310A_TX_FREQ_REG_01 0x11 // export by RFPDK
#define CMT2310A_TX_FREQ_REG_02 0x12 // export by RFPDK
#define CMT2310A_TX_FREQ_REG_03 0x13 // export by RFPDK
#define CMT2310A_TX_MODE_REG_00 0x14 // export by RFPDK
#define CMT2310A_TX_DR_REG_00 0x15   // export by RFPDK
#define CMT2310A_TX_DR_REG_01 0x16   // export by RFPDK
#define CMT2310A_TX_DR_REG_02 0x17   // export by RFPDK
#define CMT2310A_TX_DR_REG_03 0x18   // export by RFPDK
#define CMT2310A_TX_DR_REG_04 0x19   // export by RFPDK
#define CMT2310A_TX_DR_REG_05 0x1A   // export by RFPDK
#define CMT2310A_TX_DEV_REG_00 0x1B  // export by RFPDK
#define CMT2310A_TX_DEV_REG_01 0x1C  // export by RFPDK
#define CMT2310A_TX_DEV_REG_02 0x1D  // export by RFPDK
#define CMT2310A_TX_PWR_REG_00 0x1E  // export by RFPDK
#define CMT2310A_TX_PWR_REG_01 0x1F  // export by RFPDK
#define CMT2310A_TX_PWR_REG_02 0x20  // export by RFPDK
#define CMT2310A_TX_PWR_REG_03 0x21  // export by RFPDK
#define CMT2310A_TX_PWR_REG_04 0x22  // export by RFPDK
#define CMT2310A_TX_PWR_REG_05 0x23  // export by RFPDK
#define CMT2310A_TX_PWR_REG_06 0x24  // export by RFPDK
#define CMT2310A_TX_MISC_REG_00 0x25 // export by RFPDK
#define CMT2310A_TX_MISC_REG_01 0x26 // export by RFPDK
#define CMT2310A_TX_MISC_REG_02 0x27 // export by RFPDK
#define CMT2310A_TX_RESEV_00 0x28    // export by RFPDK
#define CMT2310A_TX_RESEV_01 0x29    // export by RFPDK
#define CMT2310A_TX_RESEV_02 0x2A    // export by RFPDK
#define CMT2310A_TX_RESEV_03 0x2B    // export by RFPDK
#define CMT2310A_TX_RESEV_04 0x2C    // export by RFPDK
#define CMT2310A_TX_RESEV_05 0x2D    // export by RFPDK
#define CMT2310A_TX_RESEV_06 0x2E    // export by RFPDK
#define CMT2310A_TX_RESEV_07 0x2F    // export by RFPDK
#define CMT2310A_RX_FREQ_REG_00 0x30 // export by RFPDK
#define CMT2310A_RX_FREQ_REG_01 0x31 // export by RFPDK
#define CMT2310A_RX_FREQ_REG_02 0x32 // export by RFPDK
#define CMT2310A_RX_FREQ_REG_03 0x33 // export by RFPDK
#define CMT2310A_RX_FREQ_REG_04 0x34 // export by RFPDK
#define CMT2310A_RX_IRF_REG_00 0x35  // export by RFPDK
#define CMT2310A_RX_IRF_REG_01 0x36  // export by RFPDK
#define CMT2310A_RX_IRF_REG_02 0x37  // export by RFPDK
#define CMT2310A_RX_IRF_REG_03 0x38  // export by RFPDK
#define CMT2310A_RX_PWR_REG_00 0x39  // export by RFPDK
#define CMT2310A_RX_PWR_REG_01 0x3A  // export by RFPDK
#define CMT2310A_RX_PWR_REG_02 0x3B  // export by RFPDK
#define CMT2310A_RX_DR_REG_00 0x3C   // export by RFPDK
#define CMT2310A_RX_DR_REG_01 0x3D   // export by RFPDK
#define CMT2310A_RX_DR_REG_02 0x3E   // export by RFPDK
#define CMT2310A_RX_DR_REG_03 0x3F   // export by RFPDK
#define CMT2310A_RX_DR_REG_04 0x40   // export by RFPDK
#define CMT2310A_RX_DR_REG_05 0x41   // export by RFPDK
#define CMT2310A_RX_DR_REG_06 0x42   // export by RFPDK
#define CMT2310A_RX_DR_REG_07 0x43   // export by RFPDK
#define CMT2310A_RX_CDR_REG_00 0x44  // export by RFPDK
#define CMT2310A_RX_CDR_REG_01 0x45  // export by RFPDK
#define CMT2310A_RX_CDR_REG_02 0x46  // export by RFPDK
#define CMT2310A_RX_CDR_REG_03 0x47  // export by RFPDK
#define CMT2310A_RX_CHNL_REG_00 0x48 // export by RFPDK
#define CMT2310A_RX_CHNL_REG_01 0x49 // export by RFPDK
#define CMT2310A_RX_CHNL_REG_02 0x4A // export by RFPDK
#define CMT2310A_RX_AGC_REG_00 0x4B  // export by RFPDK
#define CMT2310A_RX_AGC_REG_01 0x4C  // export by RFPDK
#define CMT2310A_RX_AGC_REG_02 0x4D  // export by RFPDK
#define CMT2310A_RX_AGC_REG_03 0x4E  // export by RFPDK
#define CMT2310A_RX_AGC_REG_04 0x4F  // export by RFPDK
#define CMT2310A_RX_AGC_REG_05 0x50  // export by RFPDK
#define CMT2310A_RX_AGC_REG_06 0x51  // export by RFPDK
#define CMT2310A_RX_2FSK_REG_00 0x52 // export by RFPDK
#define CMT2310A_RX_2FSK_REG_01 0x53 // export by RFPDK
#define CMT2310A_RX_2FSK_REG_02 0x54 // export by RFPDK
#define CMT2310A_RX_2FSK_REG_03 0x55 // export by RFPDK
#define CMT2310A_RX_AFC_REG_00 0x56  // export by RFPDK
#define CMT2310A_RX_AFC_REG_01 0x57  // export by RFPDK
#define CMT2310A_RX_AFC_REG_02 0x58  // export by RFPDK
#define CMT2310A_RX_AFC_REG_03 0x59  // export by RFPDK
#define CMT2310A_RX_4FSK_REG_00 0x5A // export by RFPDK
#define CMT2310A_RX_4FSK_REG_01 0x5B // export by RFPDK
#define CMT2310A_RX_4FSK_REG_02 0x5C // export by RFPDK
#define CMT2310A_RX_4FSK_REG_03 0x5D // export by RFPDK
#define CMT2310A_RX_OOK_REG_00 0x5E  // export by RFPDK
#define CMT2310A_RX_OOK_REG_01 0x5F  // export by RFPDK
#define CMT2310A_RX_OOK_REG_02 0x60  // export by RFPDK
#define CMT2310A_RX_OOK_REG_03 0x61  // export by RFPDK

#define CMT2310A_RX_RSSI_REG_00 0x62 // rf_rssi_reg 0, 	coll_handle[1:0] + coll_step_sel[1:0] + rssi_update_sel[1:0] + nc + coll_det_en

#define CMT2310A_COLL_STEP_SEL_MASK (3 << 4)
#define CMT2310A_COLL_STEP_SEL_6dB (0 << 4)  // collision select 6dB
#define CMT2310A_COLL_STEP_SEL_10dB (1 << 4) // collision select 10dB
#define CMT2310A_COLL_STEP_SEL_16dB (2 << 4) // collision select 16dB
#define CMT2310A_COLL_STEP_SEL_20dB (3 << 4) // collision select 20dB

#define CMT2310A_RSSI_UPDATE_SEL_MASK (3 << 2)
#define CMT2310A_RSSI_UPDATE_ALWAYS (0 << 2)   // rssi update always on
#define CMT2310A_RSSI_UPDATE_PREAM_OK (1 << 2) // rssi	update by preamble detected
#define CMT2310A_RSSI_UPDATE_SYNC_OK (2 << 2)  // rssi update by syncword detected
#define CMT2310A_RSSI_UPDATE_PKT_DONE (3 << 2) // rssi update by packet done

#define CMT2310A_COLL_DET_EN (1 << 0) // 0=disable,  	1=enable collision detect

#define CMT2310A_RX_RSSI_REG_01 0x63 // rf_rssi_reg 1, 	rssi_absolute_th[7:0]
#define CMT2310A_RSSI_ABS_TH_REG 0x63

#define CMT2310A_RX_DOUT_REG_00 0x64 // rf_dout_reg, 	nc + nc + nc + dout_adjust_sel[2:0] + dout_adjust_mode + dout_adjust_en
#define CMT2310A_DOUT_ADJUST_SEL_MASK (7 << 2)
#define CMT2310A_DOUT_ADJUST_3_33 (0 << 2)
#define CMT2310A_DOUT_ADJUST_6_66 (1 << 2)
#define CMT2310A_DOUT_ADJUST_9_99 (2 << 2)
#define CMT2310A_DOUT_ADJUST_13_32 (3 << 2)
#define CMT2310A_DOUT_ADJUST_16_65 (4 << 2)
#define CMT2310A_DOUT_ADJUST_19_98 (5 << 2)
#define CMT2310A_DOUT_ADJUST_23_21 (6 << 2)
#define CMT2310A_DOUT_ADJUST_26_64 (7 << 2)

#define CMT2310A_DOUT_ADJUST_MODE (1 << 1) // 0=+1 duty, 	1=-1 duty
#define CMT2310A_DOUT_ADJUST_EN (1 << 0)   // 0=disable, 	1=enable dout adjust

#define CMT2310A_RX_ANTD_REG_00 0x67 // rf_ant_reg		nc + nc + nc + nc + ant_wait_pmb[1:0] + ant_sw_dis + ant_div_en
#define CMT2310A_ANT_WAIT_PMB_MASK (3 << 2)
#define CMT2310A_ANT_WAIT_1_5_UNIT (0 << 2) // wait preamble size *1.5, when antenna calibrate
#define CMT2310A_ANT_WAIT_2_UNIT (1 << 2)   // wait preamble size *2, when antenna calibrate
#define CMT2310A_ANT_WAIT_2_5_UNIT (2 << 2) // wait preamble size *2.5, when antenna calibrate
#define CMT2310A_ANT_WAIT_3_UNIT (3 << 2)   // wait preamble size *3, when antenna calibrate

#define CMT2310A_ANT_SW_DIS (1 << 1) // 0=enable, 	1=bypass antenna switch
#define CMT2310A_ANT_DIV_EN (1 << 0) // 0=disable, 	1=enable antenna diversity

#endif
