/*****************************************************************************
 * file    include/pd_dau.h 
 * author  YuLiang
 * version 1.0.0
 * date    03-Feb-2023
 * brief   This file provides all the headers of the dau functions.
 ******************************************************************************
 * Attention
 *
 * <h2><center>&copy; COPYRIGHT(c) 2021 LandPower</center></h2>
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *   1. Redistributions of source code must retain the above copyright notice,
 *      this list of conditions and the following disclaimer.
 *   2. Redistributions in binary form must reproduce the above copyright notice,
 *      this list of conditions and the following disclaimer in the documentation
 *      and/or other materials provided with the distribution.
 *   3. Neither the name of LandPower nor the names of its contributors may be used to 
 *      endorse or promote products derived from this software without specific
 *      prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 ******************************************************************************/

#ifndef __PD_DAU_H__
#define __PD_DAU_H__

#ifdef CFG_DEV_TYPE_LAND_PD
/* Includes ------------------------------------------------------------------*/
#include "pd_main.h"
#include <sys/ioctl.h>

/* Define --------------------------------------------------------------------*/

#define DAU_MEM_ORIGINAL_ADDR   0x840000000        
#define DAU_MEM_ORIGINAL_LEN    0x10000000

#define DAU_MEM_REG_ADDR        0x80010000
#define DAU_MEM_REG_LEN         0x10000

#define ALL_ADDITIONAL_LEN      16

#define DEFAULT_SAMPLE_FREQ_MHZ     5000          // 默认采样频率 5000MHz
#define DEFAULT_SAMPLE_NUM          10240         // 默认采样点数 10240
#define DEFAULT_INTVEAL_TIME_US     2000          // 默认中断传输间隔时间 2000us 
#define DEFAULT_TRIG_THRESHOLD      20            // 默认触发阈值 20
#define DEFAULT_TRIG_GAP            100           // 默认触发间隔 100us
#define DEFAULT_TRIG_LOCATION       3             // 默认触发位置 3
#define DEFAULT_FACTOR_MV           2800          // 默认系数 1250
#define DEFAULT_PT_B_SYNC_MODE      0             // 默认PT和B码同步模式bit0-1 0-外部, 1-内部
#define DEFAULT_IGIRB_MODE          0             // 默认IRIG-B模式 0-南瑞, 1-正常
#define DEFAULT_PT_INTFREQ          20000000      // PT内同步周期时间ns
#define DEFAULT_HEARTBEAT_INTVEAL   20            // 默认心跳间隔 20s
#define DEFAULT_TREND_PERIOD        900           // 默认趋势周期 900s
#define DEFAULT_CH_TYPE             1             // 1-uhf
#define DEFAULT_CH_UNIT             2             // 1-dbm 2-mv

#define DEFAULT_SEND_WAVE_POINTS    10000         // 默认发送波形点数 10000
#define DEFAULT_PORT_ENBALE         0x03          // 0-7bit使能标志对应1-8端口

#define DAU_PKT_LEN 1536                          // DAU 发包最大数据长度.
#define DAU_PKT_PRPS_LEN 640
#define DAU_PKT_PRPS_DATA_LEN 1280

#define IOCTL_MAGIC		                 'm'
#define IOCTL_IRQ_WAIT                   _IO(IOCTL_MAGIC, 0x01)
#define IOCTL_IRQ_WAKE                   _IO(IOCTL_MAGIC, 0x02)
#define IOCTL_IRQ_CLEAR                  _IO(IOCTL_MAGIC, 0x03)
#define IOCTL_IRQ_WAIT_TIMEOUT           _IO(IOCTL_MAGIC, 0x04)


/* Exported types ------------------------------------------------------------*/
typedef void dau_prps_default_f(int16_t*);
typedef void dau_prps_process_f(uint8_t, uint8_t);
typedef void dau_trend_process_f(uint8_t, uint8_t, uint16_t);

/* 和 DAU 通讯使用的报文结构体. */
typedef struct
{
    uint8_t slave_id;
    uint8_t func;
    uint16_t addr;
    uint16_t len;
    uint16_t data[DAU_PKT_LEN];
} dau_pkt_t;

/* DAU 全局寄存器. */
typedef struct
{
    uint32_t ver_num;                           // 固件
    uint32_t b_time_year;                       // 年
    uint32_t b_time_day;                        // 日
    uint32_t b_time_hour;                       // 时
    uint32_t b_time_min;                        // 分
    uint32_t b_time_sec;                        // 秒
    uint32_t b_time_epoch_sec;                  // 世纪秒
    uint32_t pt_cycle;                          // PT测量周期时间ns
    uint32_t pt_selfsync_cycle;                 // PT自同步周期ns
    uint32_t pt_B_sync_mode;                    // pt和B码模式 bit0-pt bit1-B码
    uint32_t reserve2[6];
    uint32_t sample_interrupt_intveal_us;       // 采样间隔时间us
    uint32_t sample_nums;                       // 采样数据长度
    uint32_t reserve3[1];
    uint32_t trig_threshold;                    // 触发阈值
    uint32_t trig_location;                     // 触发位置 0-15
    uint32_t trig_gap;                          // 触发传输间隔 0-66535 
    uint32_t mv_factor;                         // 电压因子, 单位mV
    uint32_t irigB_polarity;                    // IRIG-B极性, 0-南瑞, 1-正常
    uint32_t ps_epoch_sec;                      // ps设置世纪秒
    uint32_t reserve4[7];
    uint32_t port_enable;                       // 通道使能[0-7bit]
    uint32_t chip_dna[3];                       // 芯片序列号
    uint32_t ch_type;                           // 通道类型, 1：UHF
    uint32_t unit;                              // 单位
    uint32_t reserve5[26];                      // 保留
} dau_reg_global_t;

/* DAU 通道寄存器. */
typedef struct
{
    uint32_t data_start_addr;                   // 数据起始地址
    uint32_t signle_wave_length;                // 单个波形数据长度
    uint32_t data_valid;                        // 数据是否有效，0没有更新，1有更新数据
    uint32_t wave_nums;                         // 波形数量
    uint32_t total_pulse_nums;                  // 总脉冲个数
    int32_t  ground_noise;                      // 底噪
    int32_t  ch_send_num;                       // 通道发送编号
    uint32_t reserve1[9];                       // 保留
} dau_reg_port_t;

/* 时间特征数据结构. */
typedef struct
{
    uint16_t pt_frame_num;              // PT 帧号
    uint16_t year;                      // 年
    uint16_t day;                       // 日
    uint16_t hour;                      // 时
    uint16_t minute;                    // 分
    uint16_t sec;                       // 秒
    uint32_t nano_sec;                  // 纳秒
    uint16_t phase;                     // 相位
    int16_t noise;                      // 底噪
    uint32_t epoch_sec;                 // 世纪秒
    uint16_t reserve[3];                // 保留
    int16_t max;                        // 最大值
} wave_additional_data_t;

/* DAU 全局寄存器. */
typedef struct
{
    dau_reg_global_t reg_global;                // 全局寄存器
    dau_reg_port_t reg_port[PD_DAU_PORT_SUM];   // 端口寄存器
} dau_reg_t;


/* DAU 板卡全局结构 */
typedef struct{
    uint8_t unit;                               // FPGA 板卡号.
    uint8_t port_num;                           // FPGA 板卡端口数量.
    uint8_t is_valid;
    uint8_t is_connect; 

    dau_reg_t *reg;                             // DAU 寄存器映射地址.
    char *adc_data;                         // DAU ADC数据映射地址.
} dau_t;

/* DAU 全局结构 */
typedef struct{
    int fd_adc_int;                             // adc数据中断
    int fd_reg_mmu;                             // MMU 寄存器映射文件描述符
    int fd_data_mmu;                            // MMU 波形数据映射文件描述符
    pthread_mutex_t mutex;                      // 趋势处理线程同步信号量 
    uint32_t dau_sum;
    uint32_t recv_cnt;
    uint32_t recv_cnt_old;
} dau_ctrl_t;

/* Exported macro ------------------------------------------------------------*/
#define SET_BIT(REG, BIT)     ((REG) |= (BIT))
#define CLEAR_BIT(REG, BIT)   ((REG) &= ~(BIT))
#define READ_BIT(REG, BIT)    ((REG) & (BIT))
#define WRITE_REG(REG, VAL)   ((REG) = (VAL))
#define MODIFY_REG(REG, CLEARMASK, SETMASK)  WRITE_REG((REG), (((REG) & (~(CLEARMASK))) | (SETMASK)))

#define DAU_REG_PORT_ADDR_GET(port)  ((port + 1) << 8)

/* Extern global variables ---------------------------------------------------*/
extern dau_t *dau[PD_DAU_SUM];
extern dau_ctrl_t dau_ctrl;

/* Extern functions ----------------------------------------------------------*/
extern int32_t dau_port_to_vport(uint8_t unit, uint8_t port);
extern int32_t dau_vport_to_port(uint8_t vport, uint8_t *unit, uint8_t *port);
// extern int32_t dau_port_type_set(uint8_t unit, uint8_t port);
extern int32_t dau_handle_init(void);
extern int32_t dau_handle_init_after(void);
extern uint8_t dau_connect_get(void);
extern int32_t dau_is_valid(dau_t *dau_node);
extern void dau_show(uint8_t unit);
extern void dau_show_value(uint8_t unit);
extern wave_additional_data_t* _dau_get_wave_addtion_data(char *frame, uint32_t frame_len);
extern void _dau_set_time(uint32_t tval);
#endif
#endif
/************************ (C) COPYRIGHT LandPower ***** END OF FILE ****/
