You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
	
	
		
			298 lines
		
	
	
		
			14 KiB
		
	
	
	
		
			C
		
	
			
		
		
	
	
			298 lines
		
	
	
		
			14 KiB
		
	
	
	
		
			C
		
	
| /*****************************************************************************
 | |
|  * 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>© 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"
 | |
| 
 | |
| /* Define --------------------------------------------------------------------*/
 | |
| #define DAU_ADJ_POINT_SUM 10
 | |
| 
 | |
| #define DAU_PKT_LEN 1536                        // DAU 发包最大数据长度.
 | |
| #define DAU_PKT_PRPS_LEN 640
 | |
| #define DAU_PKT_PRPS_DATA_LEN 1280
 | |
| 
 | |
| #define DAU_REG_ADDR_GSCR       0x0000          // 全局寄存器 - 软件特征寄存器.
 | |
| #define DAU_REG_ADDR_GSVR       0x0001          // 全局寄存器 - 软件版本寄存器.
 | |
| #define DAU_REG_ADDR_GCR        0x0002          // 全局寄存器 - 控制寄存器.
 | |
| #define DAU_REG_ADDR_GSR        0x0003          // 全局寄存器 - 状态寄存器.
 | |
| #define DAU_REG_ADDR_GPFPCCR    0x0004          // 全局寄存器 - 工频周期时钟计数寄存器.
 | |
| #define DAU_REG_ADDR_GPFPCR     0x0006          // 全局寄存器 - 工频周期计数寄存器.
 | |
| #define DAU_REG_ADDR_GNSCR      0x0008          // 全局寄存器 - 纳秒级时钟计数寄存器.
 | |
| #define DAU_REG_PORT_ADDR_MSRR  0x9000          // 端口状态寄存器 - 通道周波的原始采样值的均方根.
 | |
| #define DAU_REG_PORT_ADDR_AVR   0x9008          // 端口状态寄存器 - 通道周波校准后的数值.
 | |
| 
 | |
| #define DAU_GSVR_SECONDARY_Pos  (0)
 | |
| #define DAU_GSVR_SECONDARY_Msk  (0xFF << DAU_GSVR_SECONDARY_Pos)    /*!< 0x00FF */
 | |
| #define DAU_GSVR_PRIMARY_Pos    (8)
 | |
| #define DAU_GSVR_PRIMARY_Msk    (0xFF << DAU_GSVR_PRIMARY_Pos)      /*!< 0xFF00 */
 | |
| 
 | |
| #define DAU_GSR_SYNC_Pos        (0)
 | |
| #define DAU_GSR_SYNC_Msk        (0x1 << DAU_GSR_SYNC_Pos)           /*!< 0x0001 */
 | |
| #define DAU_GSR_MODE_Pos        (1)
 | |
| #define DAU_GSR_MODE_Msk        (0x1 << DAU_GSR_MODE_Pos)           /*!< 0x0002 */
 | |
| 
 | |
| #define DAU_GCR_MODE_Pos        (0)
 | |
| #define DAU_GCR_MODE_Msk        (0x3 << DAU_GCR_MODE_Pos)           /*!< 0x0003 */
 | |
| 
 | |
| #define DAU_REG_PORT_BASE       0x1000          // 端口寄存器基地址.
 | |
| #define DAU_REG_PORT_OFFSET     0x1000          // 每个端口寄存器偏移地址.
 | |
| 
 | |
| #define DAU_CR_SR_Pos           (0)
 | |
| #define DAU_CR_SR_Msk           (0xFF << DAU_CR_SR_Pos)             /*!< 0x00FF */
 | |
| #define DAU_CR_PT_Pos           (12)
 | |
| #define DAU_CR_PT_Msk           (0xF << DAU_CR_PT_Pos)              /*!< 0xF000 */
 | |
| 
 | |
| #define DAU_FTR_FT_Pos          (0)
 | |
| #define DAU_FTR_FT_Msk          (0xF << DAU_FTR_FT_Pos)             /*!< 0x000F */
 | |
| 
 | |
| /* 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);
 | |
| 
 | |
| /* 消息队列类型. */
 | |
| typedef enum
 | |
| {
 | |
|     DAU_SEND_TYPE_REG_RD = 1,                   // 读取寄存器.
 | |
|     DAU_SEND_TYPE_REG_WR,                       // 写入寄存器.
 | |
|     DAU_SEND_TYPE_UPDATE,                       // FPGA 升级.
 | |
|     DAU_SEND_TYPE_COUNT
 | |
| } DAU_SEND_TYPE_E;
 | |
| 
 | |
| /* 共有命令字. */
 | |
| typedef enum
 | |
| {
 | |
|     DAU_PKT_CMD_RD = 0x01,
 | |
|     DAU_PKT_CMD_WR = 0x02,
 | |
|     DAU_PKT_CMD_SAV = 0x03,
 | |
|     DAU_PKT_CMD_UPD = 0x04,
 | |
|     DAU_PKT_CMD_PRPS = 0x81,
 | |
| } DAU_PKT_CMD_E;
 | |
| 
 | |
| /* 共有命令字. */
 | |
| typedef enum
 | |
| {
 | |
|     DAU_UPD_PROCESS = 0,
 | |
|     DAU_UPD_OK = 1,
 | |
| } DAU_UPD_ERR_E;
 | |
| 
 | |
| /* 和 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 power_fre_cnt;
 | |
|     int16_t data[DAU_PKT_PRPS_LEN];
 | |
| } dau_pkt_prps_t;
 | |
| 
 | |
| /* DAU 全局寄存器. */
 | |
| typedef struct
 | |
| {
 | |
|     uint16_t GSCR;                              // 全局软件特征寄存器 (Global Softwave Characteristic Register).
 | |
|     uint16_t GSVR;                              // 全局软件版本寄存器 (Global Softwave Version Register).
 | |
|     uint16_t GCR;                               // 全局控制寄存器 (Global Control Register).
 | |
|     uint16_t GSR;                               // 全局状态寄存器 (Global Status Register).
 | |
|     uint32_t GPFPCCR;                           // 全局工频周期时钟计数寄存器 (Global Power Frequency Period Clock Count Register).
 | |
|     uint32_t GPFPCR;                            // 全局工频周期计数寄存器 (Global Power Frequency Period Count Register).
 | |
|     uint32_t GNSCR;                             // 全局纳秒级时钟计数寄存器 (Global ns Count Register).                           
 | |
| } dau_reg_t;
 | |
| 
 | |
| /* DAU 通道寄存器. */
 | |
| typedef struct
 | |
| {
 | |
|     uint16_t CR;                                // 控制寄存器 (Control Register).
 | |
|     uint16_t NR;                                // 底噪寄存器 (Noise Register).
 | |
|     uint16_t ASPR[DAU_ADJ_POINT_SUM];           // 校准分段点寄存器 (Adjust Segmentation Register).
 | |
|     uint16_t AFAR[DAU_ADJ_POINT_SUM];           // 校准系数 a 寄存器 (Adjust Factor A Register).
 | |
|     uint16_t AFBR[DAU_ADJ_POINT_SUM];           // 校准系数 b 寄存器 (Adjust Factor B Register).
 | |
|     uint16_t FTR;                               // 滤波类型寄存器 (Filtering type Register).
 | |
|     uint16_t WTDR;                              // 波形触发延时间隔寄存器 (Waveform Trigger Delay Register).
 | |
|     uint16_t WTTR;                              // 波形触发阈值寄存器 (Waveform Trigger Threshold Register).
 | |
|     uint16_t WTPSR;                             // 波形触发记录采样点寄存器 (Waveform Trigger Ponit Sum Register).
 | |
|     uint16_t WTBPR;                             // 波形触发前记录采样点寄存器 (Waveform Trigger Befor Ponit Register).
 | |
| } dau_port_reg_t;
 | |
| 
 | |
| /* DAU 通道状态寄存器. */
 | |
| typedef struct
 | |
| {
 | |
|     uint16_t MSRR[PD_DAU_PORT_SUM];             // 通道周波的原始采样值的均方根 (Mean Square Root Register).
 | |
|     uint16_t AVR[PD_DAU_PORT_SUM];              // 通道周波校准后的数值 (Adjust Value Register).
 | |
| } dau_port_state_reg_t;
 | |
| 
 | |
| /* DAU 通道寄存器. */
 | |
| typedef struct
 | |
| {
 | |
|     uint32_t prps_cnt;                          // prps 工频周期计数.
 | |
|     uint32_t prps_index;                        // prps 计数.
 | |
|     int16_t prps[PD_PRPS_NUM];                  // 1s prps 原始数据.
 | |
|     int16_t noise_level[PD_POWER_FRE];          // prps 计算使用的每个工频周期的噪声等级.
 | |
|     uint8_t is_complete;                        // prps 数据是否完整.
 | |
| } dau_port_state_t;
 | |
| 
 | |
| /* DAU 通道数据处理函数. */
 | |
| typedef struct
 | |
| {
 | |
|     dau_prps_default_f *data_default;           // PRPS buffer 还原默认值.
 | |
|     dau_prps_process_f *denoise;                // PRPS 自动 / 手动降噪.
 | |
|     dau_prps_process_f *denoise_r;              // PRPS 关联降噪.
 | |
|     dau_prps_process_f *denoise_state;          // PRPS 降噪数据统计.
 | |
|     dau_prps_process_f *denoise_real;           // 实时数据自动 / 手动降噪.
 | |
|     dau_prps_process_f *denoise_real_r;         // 实时数据关联降噪.
 | |
|     dau_prps_process_f *denoise_real_state;     // PRPS 降噪数据统计.
 | |
|     dau_prps_process_f *event;                  // 事件计算.
 | |
|     dau_trend_process_f *trend;                 // 趋势数据.
 | |
| } dau_port_func_t;
 | |
| 
 | |
| /* DAU 板卡全局结构 */
 | |
| typedef struct{
 | |
|     uint8_t unit;                                   // FPGA 板卡号.
 | |
|     uint8_t port_num;                               // FPGA 板卡端口数量.
 | |
|     uint16_t ip_port;                               // 端口号.
 | |
|     int32_t ip_addr;                                // IP 地址.
 | |
|     uint16_t msg_send_type;                         // 发包线程需要处理的消息队列类型.
 | |
|     uint16_t send_type;                             // 当前发送的数据类型 DAU_SEND_TYPE_XXX.
 | |
|     uint8_t is_valid;                               // 用于配置保存.
 | |
|     uint8_t is_connect;                             // 记录 DAU 是否通讯正常.
 | |
|     dau_port_state_t port_state[PD_DAU_PORT_SUM];   // 端口状态.
 | |
|     dau_port_func_t *port_func[PD_DAU_PORT_SUM];     // 端口数据处理函数. 
 | |
|     pthread_mutex_t mutex;                          // 收发线程同步使用.
 | |
|     dau_reg_t reg;                                  // DAU 全局寄存器软标.
 | |
|     dau_reg_t reg_dau;                              // DAU 全局寄存器硬表.
 | |
|     dau_port_reg_t port_reg[PD_DAU_PORT_SUM];       // 通道寄存器软表.
 | |
|     dau_port_reg_t port_reg_dau[PD_DAU_PORT_SUM];   // 通道寄存器硬表.
 | |
|     dau_port_state_reg_t port_state_reg;            // 通道状态寄存器.
 | |
| } dau_t;
 | |
| 
 | |
| /* DAU 全局结构 */
 | |
| typedef struct{
 | |
|     int fd;                                     // DAU 通讯使用的 socket.
 | |
|     int32_t recv_fifo_id;                       // 数据从接收线程发送到数据处理线程的 fifo.
 | |
|     uint32_t real_idx;
 | |
|     uint32_t trend_idx;
 | |
|     uint32_t event_index[PD_DAU_SUM][PD_DAU_PORT_SUM];          // 每个端口的事件索引.
 | |
|     uint32_t trend_event_index[PD_DAU_SUM][PD_DAU_PORT_SUM];    // 每个端口的趋势事件索引.
 | |
|     char recv_buf[DAU_PKT_LEN];                 // DAU 收包 buffer.
 | |
|     int8_t is_update;                           // 是否开始升级.
 | |
|     int8_t update_ret;                          // 升级结果, 0 - 进行中, 1 - 成功, <0 - 失败.
 | |
|     uint16_t update_idx;                        // 升级报文索引.
 | |
|     uint32_t recv_cnt;
 | |
|     uint32_t recv_cnt_old;
 | |
|     uint32_t recv_err_cnt;
 | |
|     uint32_t recv_err_cnt_old;
 | |
|     uint32_t data_err_cnt;
 | |
|     uint32_t data_err_cnt_old;
 | |
|     uint32_t send_err_cnt;
 | |
|     uint32_t send_err_cnt_old;
 | |
| } dau_ctrl_t;
 | |
| 
 | |
| /* DAU 发送报文数据. */
 | |
| typedef struct _dau_send_msg_data_t
 | |
| {
 | |
|     uint16_t send_type;                         // 发送的数据类型.
 | |
|     uint16_t send_len;                          // 发送的数据长度.
 | |
|     void *data;                                 // 数据的内存地址.
 | |
| } dau_send_msg_data_t;
 | |
| 
 | |
| /* DAU 消息队列要求的结构体样式. */
 | |
| typedef struct _dau_send_msg_t
 | |
| {
 | |
|     long type;                                  // 消息类型, 用于收报文过滤.
 | |
|     dau_send_msg_data_t data;                   // 消息数据.
 | |
| } dau_send_msg_t;
 | |
| 
 | |
| /* DAU 接收报文数据. */
 | |
| typedef struct _dau_recv_msg_data_t
 | |
| {
 | |
|     uint32_t ip_addr;                           // 收包数据的源 IP 地址.
 | |
|     uint32_t recv_len;                          // 收包数据长度.
 | |
|     void *data;                                 // 数据的内存地址.
 | |
| } dau_recv_msg_data_t;
 | |
| 
 | |
| /* DAU 消息队列要求的结构体样式. */
 | |
| typedef struct _dau_recv_msg_t
 | |
| {
 | |
|     long type;                                  // 消息类型, 用于收报文过滤.
 | |
|     dau_recv_msg_data_t data;                   // 消息数据.
 | |
| } dau_recv_msg_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) << 12)
 | |
| 
 | |
| /* 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_config_set(uint8_t unit, uint8_t port);
 | |
| extern int32_t dau_port_type_set(uint8_t unit, uint8_t port, uint8_t type);
 | |
| extern int32_t dau_handle_init(void);
 | |
| extern uint8_t dau_power_frequency_state_get(void);
 | |
| extern float dau_power_frequency_get(void);
 | |
| extern uint8_t dau_connect_get(void);
 | |
| extern void* dau_port_state_get(void *arg);
 | |
| extern int32_t dau_reg_global_write(uint8_t unit);
 | |
| extern int32_t dau_reg_port_write(uint8_t unit, uint8_t port);
 | |
| extern int32_t dau_param_save(uint8_t unit);
 | |
| extern void dau_shutdown(void);
 | |
| extern int32_t dau_start(void);
 | |
| extern int32_t dau_update(void);
 | |
| extern void dau_show(uint8_t unit);
 | |
| extern void dau_show_adj(uint8_t unit);
 | |
| extern void dau_show_reg(uint8_t unit);
 | |
| extern void dau_show_reg_port(uint8_t unit, uint8_t port);
 | |
| #endif
 | |
| #endif
 | |
| /************************ (C) COPYRIGHT LandPower ***** END OF FILE ****/
 |