/**
 * \file    prl_tcpm.h
 * \brief   Protocol - USB Type-C Port Manager
 *
 * \par     Copyright (c) 2017 UWE Technologies Corp., Ltd. All Rights Reserved.
 * \author  James
 * \date    2017-02-03
 * \version 
 * 
 * \history 
 *  Date        Note
 *  2017-02-03  Original Creation
 * 
 */
 
#ifndef __PRL_TCPM_H_
#define __PRL_TCPM_H_


#include "types.h"
#include "usbpd_timer_defs.h"


#pragma pack(1)

typedef struct tcpm_alert_s
{
    u16_t cc_status:1;                  ///< CC Status, 0b: Cleared, 1b: CC Status changed
    u16_t pwr_status:1;                 ///< Power Status, 0b: Cleared, 1b: Power Status changed
    u16_t rx_msg_status:1;              ///< Receive SOP* Message Status
    u16_t rx_hard_reset:1;              ///< Received Hard Reset
    u16_t tx_msg_failed:1;              ///< Transmit SOP* Message Failed
    u16_t tx_msg_discarded:1;           ///< Transmit SOP* Message Discarded
    u16_t tx_msg_sent:1;                ///< Transmit SOP* Message Successful
    u16_t vbus_volt_alarm_hi:1;         ///< VBUS Voltage Alarm High

    u16_t vbus_volt_alarm_lo:1;         ///< VBUS Voltage Alarm Low
    u16_t fault:1;                      ///< Fault
    u16_t rx_buf_overflow:1;            ///< Rx Buffer Overflow
    u16_t vbus_snk_disconnect_det:1;    ///< VBUS Sink Disconnect Detected
    u16_t rx_cable_reset:1;             ///< Received Cable Reset
    u16_t reserved:2;                   ///< Reserved
    u16_t vd_alert:1;                   ///< Vendor Defined Alert
} tcpm_alert_t;


typedef struct prl_state_info_s
{
    u32_t rx_state:4; ///< receive state
    u32_t rx_sop_msg_id:3; ///< stored SOP message id 
    u32_t rx_sop_msg_id_stored:1; ///< stored SOP message id flag
    u32_t rx_sop1_msg_id:3; ///< stored SOP' message id 
    u32_t rx_sop1_msg_id_stored:1; ///< stored SOP' message id flag
    u32_t rx_sop2_msg_id:3; ///< stored SOP" message id 
    u32_t rx_sop2_msg_id_stored:1; ///< stored SOP" message id flag
    u32_t rx_sop1d_msg_id:3; ///< stored SOP' debug message id 
    u32_t rx_sop1d_msg_id_stored:1; ///< stored SOP' debug message id flag
    u32_t rx_sop2d_msg_id:3; ///< stored SOP" debug message id 
    u32_t rx_sop2d_msg_id_stored:1; ///< stored SOP" debug message id flag
    u32_t tx_state:4; ///< transmit state
    u32_t tx_sop_msg_id:3; ///< transmit SOP message id
    u32_t tx_msg_req_frm_pe:1; ///< transmit message request from PE

    u32_t tx_sop1_msg_id:3; ///< transmit SOP' message id 
    u32_t tx_sop2_msg_id:3; ///< transmit SOP" message id 
    u32_t tx_sop1d_msg_id:3; ///< transmit SOP' debug message id 
    u32_t tx_sop2d_msg_id:3; ///< transmit SOP" debug message id 
    u32_t tx_msg_in_prog:1;
    u32_t hr_state:4; ///< hard reset state
    u32_t hr_exit_from_hard_reset:1;
    u32_t hr_hard_reset_sent:1;
    u32_t hr_cbl_rst_ind:1;
    u32_t chunk_rx_state:4; ///< chunk rx state
    u32_t chunk_tx_state:4; ///< chunk tx state
    u32_t chunk_router_state:4;///< chunk router state

    u32_t prl_reset:1;
    u32_t local_hw_reset:1;
    u32_t hrst_req_frm_pe:1;
    u32_t hrst_cmplt_frm_pe:1;
    u32_t cbl_rst_req_frm_pe:1;
    u32_t cbl_rst_cmplt_frm_pe:1;
    u32_t pe_informed_cab_rst:1; ///< read only
    u32_t pe_informed_hrst:1; ///< read only
    u32_t prl_msg2cbl_txerr:1; ///< read then clear
    u32_t prl_msg_txerr:1; ///< read then clear
    u32_t prl_txmsg_sent_ind:1; ///< read then clear
    u32_t prl_reset_complete:1; ///< read then clear
    u32_t hard_reset_sig_frm_phy:1; ///< read then clear
    u32_t cable_reset_sig_frm_phy:1; ///< read then clear
    u32_t abort_flag:1;///< prl chunk abort flag  
    u32_t router_rx_chunk_flag:1;///< 
    u32_t router_tx_chunk_flag:1;///< 
    u32_t chunking_disable_flag:1;
    u32_t chunk_expect_num_req:4;
    u32_t chunk_data_size:9;
    u32_t pe_msg_chunk_flag:1;
    
    u32_t tx_msg_req_from_chunk:1;
    u32_t chunk_sender_num:4;
    u32_t resv:27;
    
    prl_msg_rx_desc_t rx_msg_head;
    prl_msg_tx_desc_t tx_msg_head;

    drv_timer_t chunk_sender_response_timer;
    drv_timer_t chunk_sender_request_timer;
#if 0  /* deleted by James Gan - 2020-09-11 */
    drv_timer_t retransmit_timer;
#endif /* #if 0 */

} prl_state_info_t;

#pragma pack()


/** Protocol state information */
extern volatile prl_state_info_t g_prl_state_info;
extern volatile tcpm_alert_t g_tcpm_alert_status;

#define tcpm_alert_cc_status() \
    (g_tcpm_alert_status.cc_status)
#define tcpm_alert_pwr_status() \
    (g_tcpm_alert_status.pwr_status)
#define tcpm_alert_rx_msg_status() \
    (g_tcpm_alert_status.rx_msg_status)
#define tcpm_alert_rx_hard_reset() \
    (g_tcpm_alert_status.rx_hard_reset)
#define tcpm_alert_tx_msg_failed() \
    (g_tcpm_alert_status.tx_msg_failed)
#define tcpm_alert_tx_msg_discarded() \
    (g_tcpm_alert_status.tx_msg_discarded)
#define tcpm_alert_tx_msg_sent() \
    (g_tcpm_alert_status.tx_msg_sent)
#define tcpm_alert_vbus_volt_alarm_hi() \
    (g_tcpm_alert_status.vbus_volt_alarm_hi)
#define tcpm_alert_vbus_volt_alarm_lo() \
    (g_tcpm_alert_status.vbus_volt_alarm_lo)
#define tcpm_alert_fault() \
    (g_tcpm_alert_status.fault)
#define tcpm_alert_rx_buf_overflow() \
    (g_tcpm_alert_status.rx_buf_overflow)
#define tcpm_alert_vbus_snk_disconnect_det() \
    (g_tcpm_alert_status.vbus_snk_disconnect_det)
#define tcpm_alert_rx_cable_reset() \
    (g_tcpm_alert_status.rx_cable_reset)
#define tcpm_alert_vd_alert() \
    (g_tcpm_alert_status.vd_alert)


#define tcpm_alert_cc_status_set(flg) \
    do { \
        g_tcpm_alert_status.cc_status = (flg); \
    } while(0)

#define tcpm_alert_pwr_status_set(flg) \
    do { \
        g_tcpm_alert_status.pwr_status = (flg); \
    } while(0)

#define tcpm_alert_rx_msg_status_set(flg) \
    do { \
        g_tcpm_alert_status.rx_msg_status = (flg); \
    } while(0)

#define tcpm_alert_rx_hard_reset_set(flg) \
    do { \
        g_tcpm_alert_status.rx_hard_reset = (flg); \
    } while(0)

#define tcpm_alert_tx_msg_failed_set(flg) \
    do { \
        g_tcpm_alert_status.tx_msg_failed = (flg); \
    } while(0)

#define tcpm_alert_tx_msg_discarded_set(flg) \
    do { \
        g_tcpm_alert_status.tx_msg_discarded = (flg); \
    } while(0)

#define tcpm_alert_tx_msg_sent_set(flg) \
    do { \
        g_tcpm_alert_status.tx_msg_sent = (flg); \
    } while(0)

#define tcpm_alert_vbus_volt_alarm_hi_set(flg) \
    do { \
        g_tcpm_alert_status.vbus_volt_alarm_hi = (flg); \
    } while(0)

#define tcpm_alert_vbus_volt_alarm_lo_set(flg) \
    do { \
        g_tcpm_alert_status.vbus_volt_alarm_lo = (flg); \
    } while(0)

#define tcpm_alert_fault_set(flg) \
    do { \
        g_tcpm_alert_status.fault = (flg); \
    } while(0)

#define tcpm_alert_rx_buf_overflow_set(flg) \
    do { \
        g_tcpm_alert_status.rx_buf_overflow = (flg); \
    } while(0)

#define tcpm_alert_vbus_snk_disconnect_det_set(flg) \
    do { \
        g_tcpm_alert_status.vbus_snk_disconnect_det = (flg); \
    } while(0)

#define tcpm_alert_rx_cable_reset_set(flg) \
    do { \
        g_tcpm_alert_status.rx_cable_reset = (flg); \
    } while(0)

#define tcpm_alert_vd_alert_set(flg) \
    do { \
        g_tcpm_alert_status.vd_alert = (flg); \
    } while(0)




void prl_state_init();
void prl_hr_reset();


#endif /* #ifndef __PRL_TCPM_H_ */

