763 lines
		
	
	
		
			26 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			763 lines
		
	
	
		
			26 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
/****************************************************************************
 | 
						|
 *
 | 
						|
 * Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED.
 | 
						|
 *
 | 
						|
 * This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT
 | 
						|
 * be copied by any method or incorporated into another program without
 | 
						|
 * the express written consent of Aerospace C.Power. This Information or any portion
 | 
						|
 * thereof remains the property of Aerospace C.Power. The Information contained herein
 | 
						|
 * is believed to be accurate and Aerospace C.Power assumes no responsibility or
 | 
						|
 * liability for its use in any way and conveys no license or title under
 | 
						|
 * any patent or copyright and makes no representation or warranty that this
 | 
						|
 * Information is free from patent or copyright infringement.
 | 
						|
 *
 | 
						|
 * ****************************************************************************/
 | 
						|
#include "hw_reg_api.h"
 | 
						|
#include "rx_entry.h"
 | 
						|
#include "plc_utils.h"
 | 
						|
#include "mac_reset.h"
 | 
						|
#include "mac_sys_reg.h"
 | 
						|
#include "mac_rx_reg.h"
 | 
						|
#include "hw_phy_init.h"
 | 
						|
#include "iot_pkt_api.h"
 | 
						|
#include "os_mem.h"
 | 
						|
#include "rx_pb_reorder.h"
 | 
						|
#include "phy_reg.h"
 | 
						|
#include "phy_bb.h"
 | 
						|
#include "phy_chn.h"
 | 
						|
#include "hw_tonemask.h"
 | 
						|
#include "iot_io.h"
 | 
						|
#include "phy_ana.h"
 | 
						|
#include "mac_rx_buf_ring.h"
 | 
						|
#include "iot_config.h"
 | 
						|
#include "plc_protocol.h"
 | 
						|
#include "phy_rxtd_reg.h"
 | 
						|
#include "phy_dfe_reg.h"
 | 
						|
#include "tx_mpdu_start.h"
 | 
						|
#include "phy_cal.h"
 | 
						|
#include "iot_errno_api.h"
 | 
						|
#if IOT_DTEST_ONLY_SUPPORT == 0
 | 
						|
#include "iot_cal_data.h"
 | 
						|
#endif
 | 
						|
#include "math_log10.h"
 | 
						|
#include "hw_phy_api.h"
 | 
						|
#include "tx_entry.h"
 | 
						|
#include "iot_gpio_api.h"
 | 
						|
#include "iot_crc_api.h"
 | 
						|
#include "mac_init_api.h"
 | 
						|
#include "hw_tx.h"
 | 
						|
#include "hw_rx.h"
 | 
						|
#include "mpdu_header.h"
 | 
						|
#include "hal_rx.h"
 | 
						|
 | 
						|
uint8_t rx_buf[RX_BUF_NUM][RX_BUF_BYTE_SIZE] = {0};
 | 
						|
 | 
						|
uint8_t *rx_recev_packet()
 | 
						|
{
 | 
						|
    uint8_t *rx_buf_info = NULL;
 | 
						|
    uint32_t start_time = 0, end_time = 0;
 | 
						|
    uint64_t time_span = 0;
 | 
						|
 | 
						|
    start_time = RGF_MAC_READ_REG(CFG_RD_NTB_ADDR);
 | 
						|
    while(true)//is_rx_ring0_empty()
 | 
						|
    {
 | 
						|
        end_time = RGF_MAC_READ_REG(CFG_RD_NTB_ADDR);
 | 
						|
        time_span = end_time - start_time;
 | 
						|
        if (time_span < 0) { // wrap around
 | 
						|
            time_span = (0x100000000LL) - start_time + end_time;
 | 
						|
        }
 | 
						|
        if((uint64_t)time_span > 2000*TICKS_MS) {
 | 
						|
            iot_printf("[fail]golden unit is not online\n");
 | 
						|
            return NULL;
 | 
						|
        }
 | 
						|
        if(((uint64_t)time_span)%(200*TICKS_MS) < 100
 | 
						|
            && time_span / (200 * TICKS_MS) != 0) {
 | 
						|
            mac_tx_mpdu_test(NULL, mpdu_start);
 | 
						|
            dt_mac_tx_hwq0_re_trig();
 | 
						|
        }
 | 
						|
        if(!is_rx_ring0_empty()) {
 | 
						|
            rx_buf_info = pop_rx_buf_from_ring(0);
 | 
						|
            if (rx_buf_info) {
 | 
						|
                return rx_buf_info;
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
uint32_t mac_rx_init(uint32_t band_id)
 | 
						|
{
 | 
						|
    static volatile uint8_t first_rx_init_flag = 0;
 | 
						|
 | 
						|
    if(!first_rx_init_flag)
 | 
						|
    {
 | 
						|
        uint32_t tmp = RX_BUF_NUM;
 | 
						|
#if IOT_DTEST_ONLY_SUPPORT == 0
 | 
						|
        tmp = RX_BUF_NUM;
 | 
						|
        iot_pkt_t *pkt_buf = NULL;
 | 
						|
        while(tmp--){
 | 
						|
            IOT_PKT_GET(pkt_buf, RX_BUF_BYTE_SIZE, 0, IOT_FTM_MID);
 | 
						|
            if(!pkt_buf) {
 | 
						|
                iot_printf("iot_pkt_get rx_buf fail\n");
 | 
						|
                return ERR_FAIL;
 | 
						|
            }
 | 
						|
            g_plc_dt_ctxt.indep.rx_buf[tmp] = \
 | 
						|
                (uint8_t *)iot_pkt_put(pkt_buf, RX_BUF_BYTE_SIZE);
 | 
						|
        }
 | 
						|
        tmp = MAX_TMI_NUM;
 | 
						|
        while(tmp--){
 | 
						|
            IOT_PKT_GET(pkt_buf, MAX_PB_NUM * 4, 0, IOT_FTM_MID);
 | 
						|
            if(!pkt_buf) {
 | 
						|
                iot_printf("iot_pkt_get tmi_beacon_cnt fail\n");
 | 
						|
                return ERR_FAIL;
 | 
						|
            }
 | 
						|
            g_plc_dt_ctxt.indep.tmi_beacon_cnt[tmp] = \
 | 
						|
                (uint32_t *)iot_pkt_put(pkt_buf, MAX_PB_NUM * 4);
 | 
						|
        }
 | 
						|
        tmp = MAX_TMI_NUM;
 | 
						|
        while(tmp--){
 | 
						|
            IOT_PKT_GET(pkt_buf, MAX_PB_NUM * 4, 0, IOT_FTM_MID);
 | 
						|
            if(!pkt_buf) {
 | 
						|
                iot_printf("iot_pkt_get tmi_sof_cnt fail\n");
 | 
						|
                return ERR_FAIL;
 | 
						|
            }
 | 
						|
            g_plc_dt_ctxt.indep.tmi_sof_cnt[tmp] = \
 | 
						|
                (uint32_t *)iot_pkt_put(pkt_buf, MAX_PB_NUM * 4);
 | 
						|
        }
 | 
						|
        IOT_PKT_GET(pkt_buf, sizeof(mac_rx_phy_info_t), 0, IOT_FTM_MID);
 | 
						|
        if(!pkt_buf) {
 | 
						|
            iot_printf("iot_pkt_get phy_info fail\n");
 | 
						|
            return ERR_FAIL;
 | 
						|
        }
 | 
						|
        g_plc_dt_ctxt.indep.phy_info = \
 | 
						|
            (mac_rx_phy_info_t *)iot_pkt_put(pkt_buf, sizeof(mac_rx_phy_info_t));
 | 
						|
#else
 | 
						|
        g_plc_dt_ctxt.indep.phy_info = &g_plc_dt_ctxt.indep.phy_info_tmp;
 | 
						|
        tmp = RX_BUF_NUM;
 | 
						|
        while(tmp--) {
 | 
						|
            g_plc_dt_ctxt.indep.rx_buf[tmp] = rx_buf[tmp];
 | 
						|
        }
 | 
						|
#endif
 | 
						|
        /* update flag */
 | 
						|
        first_rx_init_flag = 1;
 | 
						|
    }
 | 
						|
 | 
						|
#if IOT_DTEST_ONLY_SUPPORT == 0
 | 
						|
    /* mac pre init */
 | 
						|
    mac_pre_phy_reinit();
 | 
						|
    /* reset phy */
 | 
						|
    phy_reset(PHY_RST_REASON_COLD);
 | 
						|
    /* release idle mode */
 | 
						|
    mac_force_phy_tx_ready(0, 0);
 | 
						|
    mac_set_sw_idle_mode(1,0);
 | 
						|
    mac_set_sw_idle_mode(0,0);
 | 
						|
#endif
 | 
						|
 | 
						|
    /* ada soft reset */
 | 
						|
    warm_rst_ada();
 | 
						|
 | 
						|
    mac_dma_ckl_sel(1);
 | 
						|
 | 
						|
    phy_init(glb_cfg.m_type, band_id, phy_band_to_tonemask_id_get(band_id),
 | 
						|
        true);
 | 
						|
 | 
						|
    /* init phy dtest path */
 | 
						|
    phy_rx_path_init();
 | 
						|
 | 
						|
    /* init mac path */
 | 
						|
    mac_rx_path_init();
 | 
						|
 | 
						|
    return 0;
 | 
						|
}
 | 
						|
 | 
						|
void rx_ring_setup_hw(uint32_t ring_id, rx_ring_cfg_t *cfg)
 | 
						|
{
 | 
						|
    (void)ring_id;
 | 
						|
    (void)cfg;
 | 
						|
 | 
						|
    uint32_t tmp;
 | 
						|
    uint32_t i;
 | 
						|
 | 
						|
    /* malloc pointer array to ensure access by mac */
 | 
						|
    g_plc_dt_ctxt.indep.rx_buf_ptr_list = \
 | 
						|
        (uint8_t **)os_mem_malloc(IOT_FTM_MID, sizeof(uint8_t*)*RX_BUF_NUM);
 | 
						|
 | 
						|
    /* init the rx ring buf */
 | 
						|
    for (i = 0; i < RX_BUF_NUM; i++) {
 | 
						|
        g_plc_dt_ctxt.indep.rx_buf_ptr_list[i] = g_plc_dt_ctxt.indep.rx_buf[i];
 | 
						|
    }
 | 
						|
    RGF_RX_WRITE_REG(CFG_BUFFER_RING0_0_ADDR, \
 | 
						|
        (uint32_t)g_plc_dt_ctxt.indep.rx_buf_ptr_list);
 | 
						|
 | 
						|
    /* set ring sz and buf len */
 | 
						|
    tmp = RGF_RX_READ_REG(CFG_BUFFER_RING0_1_ADDR);
 | 
						|
    REG_FIELD_SET(CFG_RING0_BUF_SIZE, tmp, RX_BUF_DW_SIZE);
 | 
						|
    REG_FIELD_SET(CFG_RING0_BUF_NUM, tmp, RX_BUF_NUM);
 | 
						|
    RGF_RX_WRITE_REG(CFG_BUFFER_RING0_1_ADDR, tmp);
 | 
						|
 | 
						|
    /* set rd idx = 0 as default */
 | 
						|
    tmp = RGF_RX_READ_REG(CFG_BUFFER_RING0_2_ADDR);
 | 
						|
    REG_FIELD_SET(CFG_RING0_RD_IDX, tmp, 0);
 | 
						|
    RGF_RX_WRITE_REG(CFG_BUFFER_RING0_2_ADDR, tmp);
 | 
						|
 | 
						|
    /* set desc and payload offset */
 | 
						|
    tmp = RGF_RX_READ_REG(CFG_BUFFER_RING0_3_ADDR);
 | 
						|
    REG_FIELD_SET(CFG_RING0_DESC_EN, tmp, 1);
 | 
						|
    REG_FIELD_SET(CFG_RING0_PAYLOAD_EN, tmp, 1);
 | 
						|
    REG_FIELD_SET(CFG_RING0_DESC_OFFSET, tmp, 0);
 | 
						|
    REG_FIELD_SET(CFG_RING0_PAYLOAD_OFFSET, tmp, \
 | 
						|
        iot_ceil(sizeof(rx_buf_hdr_t), 4));
 | 
						|
    RGF_RX_WRITE_REG(CFG_BUFFER_RING0_3_ADDR, tmp);
 | 
						|
 | 
						|
    return;
 | 
						|
}
 | 
						|
 | 
						|
void rx_ring_enable(uint32_t ring_id, uint32_t enable)
 | 
						|
{
 | 
						|
    (void)ring_id;
 | 
						|
    uint32_t tmp;
 | 
						|
    tmp = RGF_RX_READ_REG(CFG_BUFFER_RING0_2_ADDR);
 | 
						|
    REG_FIELD_SET(CFG_RING0_EN, tmp, enable);
 | 
						|
    RGF_RX_WRITE_REG(CFG_BUFFER_RING0_2_ADDR, tmp);
 | 
						|
 | 
						|
    return;
 | 
						|
}
 | 
						|
 | 
						|
uint32_t is_rx_ring0_empty()
 | 
						|
{
 | 
						|
    uint32_t rd_idx, wr_idx;
 | 
						|
    rd_idx = RX_RING_GET_RD_IDX(0);
 | 
						|
    wr_idx = RX_RING_GET_WR_IDX(0);
 | 
						|
 | 
						|
    return (rd_idx == wr_idx);
 | 
						|
}
 | 
						|
 | 
						|
/*TODO: use api or  formal parameter instead of Macro*/
 | 
						|
uint8_t *pop_rx_buf_from_ring(uint32_t ring_id)
 | 
						|
{
 | 
						|
    (void)ring_id;
 | 
						|
    uint32_t rd_idx = 0, nid = 0;
 | 
						|
    uint8_t *rx_buf_start;
 | 
						|
    rx_buf_hdr_t *pb_buf;
 | 
						|
    static uint32_t enq_time = 0, cur_time = 0;
 | 
						|
    static int64_t time_span = 0;
 | 
						|
    uint8_t pre_sym = 0, max_spur_id = 0;
 | 
						|
    int8_t snr = 0;
 | 
						|
 | 
						|
    rd_idx = RX_RING_GET_RD_IDX(0);
 | 
						|
    rx_buf_start = g_plc_dt_ctxt.indep.rx_buf[rd_idx];
 | 
						|
    pb_buf = (rx_buf_hdr_t *)rx_buf_start;
 | 
						|
#if SUPPORT_SMART_GRID
 | 
						|
#if SUPPORT_IEEE_1901
 | 
						|
    volatile i1901_frame_control_t *fc_1901 =
 | 
						|
            (i1901_frame_control_t *)&pb_buf->mpdu_st.fc0;
 | 
						|
#else
 | 
						|
    volatile frame_control_t *sg_fc = \
 | 
						|
        (frame_control_t *)mac_rx_mpdu_st_get_fc_addr(&pb_buf->mpdu_st);
 | 
						|
#endif
 | 
						|
#endif
 | 
						|
#if SUPPORT_SOUTHERN_POWER_GRID
 | 
						|
    volatile spg_frame_control_t *spg_fc = \
 | 
						|
        (spg_frame_control_t *)mac_rx_mpdu_st_get_fc_addr(&pb_buf->mpdu_st);
 | 
						|
#endif
 | 
						|
#if SUPPORT_GREEN_PHY
 | 
						|
    volatile hpav_frame_control *hpav_fc = \
 | 
						|
        (hpav_frame_control *)mac_rx_mpdu_st_get_fc_addr(&pb_buf->mpdu_st);
 | 
						|
#endif
 | 
						|
 | 
						|
    /* get nid */
 | 
						|
#if SUPPORT_IEEE_1901
 | 
						|
    nid = fc_1901->nid;
 | 
						|
#else
 | 
						|
    nid = mac_get_nid_from_fc(phy_proto_type_get(),
 | 
						|
        mac_rx_mpdu_st_get_fc_addr(&pb_buf->mpdu_st));
 | 
						|
#endif
 | 
						|
 | 
						|
#if PHY_PPM_CAL_SUPPORT == 1
 | 
						|
    /* get fc delimiter */
 | 
						|
    uint32_t delimiter = mac_get_rx_delimiter_from_fc( \
 | 
						|
        phy_proto_type_get(), \
 | 
						|
        mac_rx_mpdu_st_get_fc_addr(&pb_buf->mpdu_st) \
 | 
						|
        );
 | 
						|
 | 
						|
#if PHY_RX_DBG_EN
 | 
						|
    if (dut_flag == false) {
 | 
						|
        iot_printf("[Debug]pop pb_buf addr:%x, dut:%d\r\n", \
 | 
						|
            g_plc_dt_ctxt.indep.pb_buf, g_plc_dt_ctxt.indep.dut_flag);
 | 
						|
        for (uint32_t j = 5; j < 8; j++)
 | 
						|
        {
 | 
						|
            iot_printf( \
 | 
						|
                "[Debug] addr:%d, value:%02x\r\n", j, rx_buf_start[j]);
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    /* check fail pkt */
 | 
						|
    if (nid != glb_cfg.nid) {
 | 
						|
        iot_printf("[Debug]rx mpdu done:%d, nid%x \n", \
 | 
						|
            mac_rx_att_get_rx_mpdu_done(&pb_buf->att), nid);
 | 
						|
    }
 | 
						|
 | 
						|
#endif //PHY_RX_DBG_EN
 | 
						|
 | 
						|
    /* TODO: mpdu_done valid only for first pb and nid */
 | 
						|
    if ((pb_buf->pb_ed.rx_pb_done == 1) && (
 | 
						|
        nid == glb_cfg.nid)) {
 | 
						|
 | 
						|
        if (g_plc_dt_ctxt.indep.dut_flag == false && \
 | 
						|
            delimiter == FC_DELIM_BEACON) {
 | 
						|
#if IOT_DTEST_ONLY_SUPPORT == 0
 | 
						|
            if (g_phy_cpu_share_ctxt.pt_mode_entry) {
 | 
						|
#else
 | 
						|
            if (0) {
 | 
						|
#endif
 | 
						|
                mac_ntb_ppm_cali_start(pb_buf, PHY_PT_PPM_CAL_NTB);
 | 
						|
            } else {
 | 
						|
                /* ctrl beacon ppm cal cycle */
 | 
						|
                if (g_plc_dt_ctxt.indep.ppm_cal_start) {
 | 
						|
                    if ((RGF_MAC_READ_REG(CFG_RD_NTB_ADDR) - \
 | 
						|
                        g_plc_dt_ctxt.indep.ppm_save_ntb) >= PHY_DT_PPM_CAL_NTB) {
 | 
						|
                        mac_ntb_ppm_cali_start(pb_buf, PHY_DT_PPM_CAL_NTB);
 | 
						|
                        g_plc_dt_ctxt.indep.ppm_cal_start = false;
 | 
						|
                    }
 | 
						|
                } else {
 | 
						|
                    g_plc_dt_ctxt.indep.ppm_save_ntb = \
 | 
						|
                        RGF_MAC_READ_REG(CFG_RD_NTB_ADDR);
 | 
						|
                    mac_ntb_ppm_cali_start(pb_buf, PHY_DT_PPM_CAL_NTB);
 | 
						|
                    g_plc_dt_ctxt.indep.ppm_cal_start = true;
 | 
						|
                }
 | 
						|
            }
 | 
						|
        }
 | 
						|
#else //PHY_PPM_CAL_SUPPORT == 1
 | 
						|
    if ((mac_rx_pb_end_get_rx_pb_done(&pb_buf->pb_ed) == 1) && \
 | 
						|
        (nid == glb_cfg.nid)) {
 | 
						|
#endif //PHY_PPM_CAL_SUPPORT == 1
 | 
						|
        if ((mac_rx_pb_end_get_rx_pb_crc_err(&pb_buf->pb_ed) == 0) && \
 | 
						|
            (mac_rx_att_get_is_fcserr(&pb_buf->att) == 0)) {
 | 
						|
            if (1 == mac_rx_mpdu_end_get_rx_buf_num(&pb_buf->mpdu_ed)) {
 | 
						|
                g_plc_dt_ctxt.indep.fisrt_pb_crc_ok_cnt++;
 | 
						|
            } else if (2 == mac_rx_mpdu_end_get_rx_buf_num(&pb_buf->mpdu_ed)) {
 | 
						|
                g_plc_dt_ctxt.indep.second_pb_crc_ok_cnt++;
 | 
						|
            } else if (3 == mac_rx_mpdu_end_get_rx_buf_num(&pb_buf->mpdu_ed)) {
 | 
						|
                g_plc_dt_ctxt.indep.third_pb_crc_ok_cnt++;
 | 
						|
            } else if (4 == mac_rx_mpdu_end_get_rx_buf_num(&pb_buf->mpdu_ed)) {
 | 
						|
                g_plc_dt_ctxt.indep.last_pb_crc_ok_cnt++;
 | 
						|
            }
 | 
						|
#if SUPPORT_SMART_GRID
 | 
						|
            if (PHY_PROTO_TYPE_GET() == PLC_PROTO_TYPE_SG) {
 | 
						|
#if SUPPORT_IEEE_1901
 | 
						|
                uint32_t test_sg_tmi = 0, test_sg_etmi = 0;
 | 
						|
                uint32_t pbsz;
 | 
						|
 | 
						|
                switch(fc_1901->delimiter_type)
 | 
						|
                {
 | 
						|
                    case FC_DELIM_BEACON:
 | 
						|
                        g_plc_dt_ctxt.indep.tmi_beacon_cnt[fc_1901->vf.bcn.tmi]\
 | 
						|
                        [pb_buf->mpdu_ed.rx_buf_num-1] += 1;
 | 
						|
                        break;
 | 
						|
                    case FC_DELIM_SOF:
 | 
						|
                        phy_tmi_i1901_to_sg(fc_1901->vf.sof.tmi,
 | 
						|
                            fc_1901->vf.sof.tmi_ext, &test_sg_tmi, &test_sg_etmi);
 | 
						|
                        phy_get_pb_size(PLC_PROTO_TYPE_SG, test_sg_tmi,
 | 
						|
                            test_sg_etmi, &pbsz);
 | 
						|
                        g_plc_dt_ctxt.indep.tmi_sof_cnt[test_sg_etmi + \
 | 
						|
                        test_sg_tmi]\
 | 
						|
                        [pb_buf->mpdu_ed.rx_buf_num-1] += 1;
 | 
						|
                        break;
 | 
						|
                    case FC_DELIM_SACK:
 | 
						|
                        g_plc_dt_ctxt.indep.tmi_sack_cnt[mac_rx_mpdu_end_get_rx_buf_num(\
 | 
						|
                            &pb_buf->mpdu_ed)-1] += 1;
 | 
						|
                        break;
 | 
						|
                    case FC_DELIM_NNCCO:
 | 
						|
                        g_plc_dt_ctxt.indep.tmi_nncco_cnt[mac_rx_mpdu_end_get_rx_buf_num(\
 | 
						|
                            &pb_buf->mpdu_ed)-1] += 1;
 | 
						|
                        break;
 | 
						|
                    default:
 | 
						|
                        break;
 | 
						|
                }
 | 
						|
#else
 | 
						|
                switch(sg_fc->delimiter_type)
 | 
						|
                {
 | 
						|
                    case FC_DELIM_BEACON:
 | 
						|
                        g_plc_dt_ctxt.indep.tmi_beacon_cnt[sg_fc->vf.bcn.tmi]\
 | 
						|
                        [pb_buf->mpdu_ed.rx_buf_num-1] += 1;
 | 
						|
                        break;
 | 
						|
                    case FC_DELIM_SOF:
 | 
						|
                        g_plc_dt_ctxt.indep.tmi_sof_cnt[sg_fc->vf.sof.tmi_ext + \
 | 
						|
                        sg_fc->vf.sof.tmi]\
 | 
						|
                        [pb_buf->mpdu_ed.rx_buf_num-1] += 1;
 | 
						|
                        break;
 | 
						|
                    case FC_DELIM_SACK:
 | 
						|
                        g_plc_dt_ctxt.indep.tmi_sack_cnt[mac_rx_mpdu_end_get_rx_buf_num(\
 | 
						|
                            &pb_buf->mpdu_ed)-1] += 1;
 | 
						|
                        break;
 | 
						|
                    case FC_DELIM_NNCCO:
 | 
						|
                        g_plc_dt_ctxt.indep.tmi_nncco_cnt[mac_rx_mpdu_end_get_rx_buf_num(\
 | 
						|
                            &pb_buf->mpdu_ed)-1] += 1;
 | 
						|
                        break;
 | 
						|
                    default:
 | 
						|
                        break;
 | 
						|
                }
 | 
						|
#endif
 | 
						|
            } else
 | 
						|
#endif
 | 
						|
#if SUPPORT_SOUTHERN_POWER_GRID
 | 
						|
            if (PHY_PROTO_TYPE_GET() == PLC_PROTO_TYPE_SPG) {
 | 
						|
                switch(spg_fc->delimiter_type)
 | 
						|
                {
 | 
						|
                    case FC_DELIM_BEACON:
 | 
						|
                        g_plc_dt_ctxt.indep.tmi_beacon_cnt[spg_fc->vf.bcn.tmi]\
 | 
						|
                        [mac_rx_mpdu_end_get_rx_buf_num(&pb_buf->mpdu_ed)-1] += 1;
 | 
						|
                        break;
 | 
						|
                    case FC_DELIM_SOF:
 | 
						|
                        g_plc_dt_ctxt.indep.tmi_sof_cnt[spg_fc->vf.sof.tmi_ext + \
 | 
						|
                        spg_fc->vf.sof.tmi]\
 | 
						|
                        [mac_rx_mpdu_end_get_rx_buf_num(&pb_buf->mpdu_ed)-1] += 1;
 | 
						|
                        break;
 | 
						|
                    case FC_DELIM_SACK:
 | 
						|
                        g_plc_dt_ctxt.indep.tmi_sack_cnt[mac_rx_mpdu_end_get_rx_buf_num(\
 | 
						|
                            &pb_buf->mpdu_ed)-1] += 1;
 | 
						|
                        break;
 | 
						|
                    case FC_DELIM_NNCCO:
 | 
						|
                        g_plc_dt_ctxt.indep.tmi_nncco_cnt[mac_rx_mpdu_end_get_rx_buf_num(\
 | 
						|
                            &pb_buf->mpdu_ed)-1] += 1;
 | 
						|
                        break;
 | 
						|
                    default:
 | 
						|
                        break;
 | 
						|
                }
 | 
						|
            } else
 | 
						|
#endif
 | 
						|
#if SUPPORT_GREEN_PHY
 | 
						|
            if (PHY_PROTO_TYPE_GET() == PLC_PROTO_TYPE_GP) {
 | 
						|
                switch(hpav_fc->delimiter_type)
 | 
						|
                {
 | 
						|
                    case FC_DELIM_BEACON:
 | 
						|
                        g_plc_dt_ctxt.indep.tmi_beacon_cnt[0][mac_rx_mpdu_end_get_rx_buf_num(\
 | 
						|
                            &pb_buf->mpdu_ed)-1] += 1;
 | 
						|
                        break;
 | 
						|
                    case FC_DELIM_SOF:
 | 
						|
                        g_plc_dt_ctxt.indep.tmi_sof_cnt[hpav_fc->vf_av.sof.tmi_av]\
 | 
						|
                        [mac_rx_mpdu_end_get_rx_buf_num(&pb_buf->mpdu_ed)-1] += 1;
 | 
						|
                        break;
 | 
						|
                    case FC_DELIM_SACK:
 | 
						|
                        g_plc_dt_ctxt.indep.tmi_sack_cnt[mac_rx_mpdu_end_get_rx_buf_num(\
 | 
						|
                            &pb_buf->mpdu_ed)-1] += 1;
 | 
						|
                        break;
 | 
						|
                    case FC_DELIM_NNCCO:
 | 
						|
                        g_plc_dt_ctxt.indep.tmi_nncco_cnt[mac_rx_mpdu_end_get_rx_buf_num(\
 | 
						|
                            &pb_buf->mpdu_ed)-1] += 1;
 | 
						|
                        break;
 | 
						|
                    default:
 | 
						|
                        break;
 | 
						|
                }
 | 
						|
            }
 | 
						|
#endif
 | 
						|
            {
 | 
						|
                //do not del this case
 | 
						|
            }
 | 
						|
 | 
						|
            /* rate and band info */
 | 
						|
            if ((mac_rx_att_get_rx_band_sel(&pb_buf->att) < MAX_HW_BAND) && \
 | 
						|
                (mac_rx_att_get_rx_rate_mode(&pb_buf->att) < MAC_BB_MAX_RATE)) {
 | 
						|
                g_plc_dt_ctxt.indep.band_cnt_rx[mac_rx_att_get_rx_rate_mode(&pb_buf->att)]\
 | 
						|
                    [mac_rx_att_get_rx_band_sel(&pb_buf->att)] += 1;
 | 
						|
            } else {
 | 
						|
                IOT_ASSERT(0);
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        /* update fc phy info depend on fc crc */
 | 
						|
        if (!mac_rx_att_get_is_fcserr(&pb_buf->att)) {
 | 
						|
            /* fresh gain by cco */
 | 
						|
#if PHY_RX_PKT_FILTER_CCO == 1
 | 
						|
            if(
 | 
						|
#if SUPPORT_SOUTHERN_POWER_GRID
 | 
						|
                spg_fc->vf.bcn.src_tei == 1 || \
 | 
						|
                spg_fc->vf.sof.src_tei == 1 ||
 | 
						|
#else
 | 
						|
                (0) ||
 | 
						|
#endif
 | 
						|
#if SUPPORT_SMART_GRID
 | 
						|
                sg_fc->vf.bcn.src_tei == 1 || \
 | 
						|
                sg_fc->vf.sof.src_tei == 1 ||
 | 
						|
#else
 | 
						|
                (0) ||
 | 
						|
#endif
 | 
						|
#if SUPPORT_GREEN_PHY
 | 
						|
                hpav_fc->vf_av.sof.src_tei == 1 || \
 | 
						|
                hpav_fc->vf_av.rtscts.src_tei == 1 || \
 | 
						|
                hpav_fc->vf_av.sound.stei == 1
 | 
						|
#else
 | 
						|
                (0)
 | 
						|
#endif
 | 
						|
                )
 | 
						|
#else
 | 
						|
            if(1)
 | 
						|
#endif
 | 
						|
            {
 | 
						|
                /*fix char print error*/
 | 
						|
                g_plc_dt_ctxt.indep.phy_info->gain = \
 | 
						|
                    mac_rx_mpdu_st_get_agc_gain_entry(&pb_buf->mpdu_st);
 | 
						|
                g_plc_dt_ctxt.indep.phy_info->rmi = \
 | 
						|
                    mac_rx_mpdu_st_get_adc_power(&pb_buf->mpdu_st);
 | 
						|
 | 
						|
                /* deal with sign bit */
 | 
						|
                if (mac_rx_mpdu_st_get_estimated_dc(&pb_buf->mpdu_st) & 0x10) {
 | 
						|
                    g_plc_dt_ctxt.indep.phy_info->est_dc = mac_rx_mpdu_st_get_estimated_dc(\
 | 
						|
                        &pb_buf->mpdu_st) | 0xE0;
 | 
						|
                } else {
 | 
						|
                    g_plc_dt_ctxt.indep.phy_info->est_dc = mac_rx_mpdu_st_get_estimated_dc(\
 | 
						|
                        &pb_buf->mpdu_st) & 0x1F;
 | 
						|
                }
 | 
						|
                g_plc_dt_ctxt.indep.phy_info->est_ppm = mac_rx_mpdu_st_get_estimated_ppm(\
 | 
						|
                        &pb_buf->mpdu_st);
 | 
						|
 | 
						|
                /* snr cal */
 | 
						|
                snr = mac_rx_mpdu_st_get_avg_snr(&pb_buf->mpdu_st); // raw_snr_from_phy
 | 
						|
                g_plc_dt_ctxt.indep.phy_info->avr_snr = snr;
 | 
						|
                pre_sym = mac_rx_mpdu_st_get_preamble_symb_for_detect(\
 | 
						|
                    &pb_buf->mpdu_st);
 | 
						|
                max_spur_id = mac_rx_mpdu_st_get_max_spur_id(&pb_buf->mpdu_st);
 | 
						|
            }
 | 
						|
#ifdef MAC_RX_TIME_STAMP_SUPPORT
 | 
						|
            /* pkt time stamp */
 | 
						|
            if (!spg_fc->delimiter_type) {
 | 
						|
                time_stamp[pkt_idx++] = pb_buf->mpdu_ed.local_ts;
 | 
						|
            }
 | 
						|
#endif
 | 
						|
 | 
						|
            /* clr done sts */
 | 
						|
            mac_rx_att_set_rx_mpdu_done(&pb_buf->att, 0);
 | 
						|
            mac_rx_pb_end_set_rx_pb_done(&pb_buf->pb_ed, 0);
 | 
						|
        }
 | 
						|
 | 
						|
        /* cal phy info counter */
 | 
						|
        phy_info_cnt_cal(g_plc_dt_ctxt.indep.phy_info);
 | 
						|
 | 
						|
        if(++rd_idx >= RX_BUF_NUM)
 | 
						|
            rd_idx = 0;
 | 
						|
        RX_RING_SET_RD_IDX(0, rd_idx);
 | 
						|
 | 
						|
        cur_time = RGF_MAC_READ_REG(CFG_RD_NTB_ADDR);
 | 
						|
        time_span = cur_time - enq_time;
 | 
						|
        if (time_span < 0) { // wrap around
 | 
						|
            time_span = (0x100000000LL) - enq_time + cur_time;
 | 
						|
        }
 | 
						|
 | 
						|
        /*agc gain info*/
 | 
						|
        if ((uint64_t)time_span > g_plc_dt_ctxt.indep.print_period_ms*TICKS_MS) {
 | 
						|
            /* print phy info */
 | 
						|
            phy_info_cnt_print(g_plc_dt_ctxt.indep.phy_info);
 | 
						|
 | 
						|
            iot_printf("pre_num:%hd, maxspr:%hd\r\n",\
 | 
						|
                pre_sym, max_spur_id);
 | 
						|
            enq_time = cur_time;
 | 
						|
#ifdef MAC_RX_TIME_STAMP_SUPPORT
 | 
						|
            if(pkt_idx > 0){
 | 
						|
                for(uint32_t i= 0; i<pkt_idx; i++){
 | 
						|
                    iot_printf("[T]:raw-%-10d\r\n", time_stamp[i]);
 | 
						|
                }
 | 
						|
                pkt_idx = 0;
 | 
						|
            }
 | 
						|
#endif
 | 
						|
        }
 | 
						|
        return rx_buf_start;
 | 
						|
    } else {
 | 
						|
        if (++rd_idx >= RX_BUF_NUM) {
 | 
						|
            rd_idx = 0;
 | 
						|
        }
 | 
						|
        RX_RING_SET_RD_IDX(0, rd_idx);
 | 
						|
 | 
						|
        return NULL;
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
/* mac rx pkt single */
 | 
						|
uint32_t mac_rx_single_mode(bool_t need_ack)
 | 
						|
{
 | 
						|
    uint8_t tmp = 0;
 | 
						|
    uint32_t start_time = 0, end_time = 0;
 | 
						|
    uint64_t time_span = 0;
 | 
						|
    rx_buf_hdr_t *pb_buf;
 | 
						|
    static uint32_t rx_ok_cnt = 0;
 | 
						|
    uint32_t crc32 = 0;
 | 
						|
    uint32_t crc32_cnt = 0;
 | 
						|
    uint32_t pb_size = 0;
 | 
						|
    uint16_t cpu1_shift_cnt = 0;
 | 
						|
#if IOT_DTEST_ONLY_SUPPORT == 0 || \
 | 
						|
    IOT_DTEST_LED_SUPPORT == 1
 | 
						|
    bool_t gpio_val = 0;
 | 
						|
#endif
 | 
						|
    uint32_t ret = ERR_FAIL;
 | 
						|
 | 
						|
    /* mac tx common init interface */
 | 
						|
    if (need_ack) {
 | 
						|
        tx_common_init(IOT_PLC_PHY_BAND_DFT);
 | 
						|
    }
 | 
						|
 | 
						|
    /* add led init */
 | 
						|
#if IOT_DTEST_LED_SUPPORT == 1
 | 
						|
    gpio_mtx_enable();
 | 
						|
    apb_enable(APB_GPIO);
 | 
						|
    gpio_pin_select(32, 3);
 | 
						|
    gpio_pin_select(33, 3);
 | 
						|
#endif
 | 
						|
    phy_force_sack_power_set();
 | 
						|
 | 
						|
    /* fix A phase */
 | 
						|
    phy_rx_phase_force_set(true, PLC_PHASE_A);
 | 
						|
 | 
						|
    /* check if rx ring has content */
 | 
						|
    uint8_t *rx_buf_tmp;
 | 
						|
    start_time = RGF_MAC_READ_REG(CFG_RD_NTB_ADDR);
 | 
						|
    do {
 | 
						|
#ifdef MAC_RX_FC_ERR_PRINT_PPM_SUPPORT
 | 
						|
       int32_t tmp1 = PHY_READ_REG(CFG_BB_FC_CRC_ERROR_CNTR_ADDR);
 | 
						|
       if(tmp1)
 | 
						|
       {
 | 
						|
            tmp1 = PHY_RX_FD_READ_REG(CFG_BB_FREQ_ERROR_ADDR);
 | 
						|
            tmp1 = REG_FIELD_GET(SW_FREQ_ERROR_PPM,tmp1);
 | 
						|
            if(tmp1 > 511){
 | 
						|
                tmp1 = tmp1 - 1024;
 | 
						|
            }
 | 
						|
            iot_printf("fc err, ppm=%d\r\n", tmp1);
 | 
						|
#ifdef MAC_RX_TIME_STAMP_SUPPORT
 | 
						|
            tmp1 = RGF_MAC_READ_REG(CFG_RD_NTB_ADDR);
 | 
						|
            iot_printf("fc err time stamp=%d\r\n", tmp1);
 | 
						|
#endif
 | 
						|
            PHY_WRITE_REG(CFG_BB_FC_PLD_CNTR_CLR_ADDR,0x2);
 | 
						|
        }
 | 
						|
#endif
 | 
						|
        while (!is_rx_ring0_empty()) {
 | 
						|
            rx_buf_tmp = pop_rx_buf_from_ring(0);
 | 
						|
            if (rx_buf_tmp) {
 | 
						|
                if (need_ack) {
 | 
						|
                    /* tx back with phy info */
 | 
						|
                    uint8_t *pb_addr_ptr = \
 | 
						|
                        (uint8_t *)phy_get_pb_buf_ptr_from_mpdu(mpdu_start);
 | 
						|
                    *pb_addr_ptr++ = 'W';
 | 
						|
                    *pb_addr_ptr++ = 'U';
 | 
						|
                    *pb_addr_ptr++ = '-';
 | 
						|
                    *pb_addr_ptr++ = 'Q';
 | 
						|
                    *pb_addr_ptr++ = 'I';
 | 
						|
                    mac_tx_mpdu_test(NULL, mpdu_start);
 | 
						|
                    dt_mac_tx_hwq0_re_trig();
 | 
						|
                }
 | 
						|
 | 
						|
                pb_buf = (rx_buf_hdr_t *)rx_buf_tmp;
 | 
						|
                if (!pb_buf->pb_ed.rx_pb_crc_err) {
 | 
						|
                    rx_ok_cnt++;
 | 
						|
                    /* led on */
 | 
						|
#if IOT_DTEST_LED_SUPPORT == 1
 | 
						|
                    gpio_val = !gpio_val;
 | 
						|
                    if (gpio_val) {
 | 
						|
                        gpio_pin_wpu(32,1);
 | 
						|
                        gpio_pin_wpd(32,0);
 | 
						|
                        gpio_pin_wpu(33,1);
 | 
						|
                        gpio_pin_wpd(33,0);
 | 
						|
                    } else {
 | 
						|
                        gpio_pin_wpu(32,0);
 | 
						|
                        gpio_pin_wpd(32,1);
 | 
						|
                        gpio_pin_wpu(33,0);
 | 
						|
                        gpio_pin_wpd(33,1);
 | 
						|
                    }
 | 
						|
#endif
 | 
						|
                }
 | 
						|
 | 
						|
                tmp = PHY_DFE_READ_REG(CFG_BB_SW_ADJUST_GAIN_ADDR);
 | 
						|
                if (REG_FIELD_GET(SW_RX_GAIN_LEFT_SHIFT, tmp) == 1 && \
 | 
						|
                    REG_FIELD_GET(SW_RX_GAIN_SHIFT_BITS, tmp) == 2) {
 | 
						|
                    cpu1_shift_cnt++;
 | 
						|
                }
 | 
						|
 | 
						|
                /* get fc delimiter */
 | 
						|
                uint32_t delimiter = mac_get_rx_delimiter_from_fc( \
 | 
						|
                    phy_proto_type_get(), \
 | 
						|
                    mac_rx_mpdu_st_get_fc_addr(&pb_buf->mpdu_st) \
 | 
						|
                    );
 | 
						|
 | 
						|
                /* init fc message */
 | 
						|
                rx_fc_msg_t rx_fc_msg = { 0 };
 | 
						|
                /* get info from fc */
 | 
						|
                mac_get_rx_frm_msg_from_fc( \
 | 
						|
                    phy_proto_type_get(), \
 | 
						|
                    mac_rx_mpdu_st_get_fc_addr(&pb_buf->mpdu_st), \
 | 
						|
                    &rx_fc_msg);
 | 
						|
 | 
						|
                if (delimiter == FC_DELIM_SOF) {
 | 
						|
                    /* pb crc check: first word for soft crc */
 | 
						|
                    ret = phy_get_pb_size( \
 | 
						|
                        glb_cfg.m_type, \
 | 
						|
                        rx_fc_msg.tmi, \
 | 
						|
                        rx_fc_msg.tmi_ext, \
 | 
						|
                        &pb_size);
 | 
						|
 | 
						|
                    /* check valid */
 | 
						|
                    if (ret != ERR_OK || !pb_size) {
 | 
						|
                        continue;
 | 
						|
                    }
 | 
						|
 | 
						|
                    /* use sw crc32 and bypass hw crc */
 | 
						|
                    crc32 = iot_getcrc32( \
 | 
						|
                        rx_buf_tmp + sizeof(rx_buf_hdr_t) + sizeof(uint32_t), \
 | 
						|
                         pb_size - (sizeof(uint32_t) << 1));
 | 
						|
 | 
						|
                    if(crc32 != \
 | 
						|
                        *(uint32_t *)(rx_buf_tmp + sizeof(rx_buf_hdr_t))) {
 | 
						|
                        crc32_cnt++;
 | 
						|
#if PHY_DBG_EN
 | 
						|
                        iot_printf("[CRC]fail: crc:%x\n", \
 | 
						|
                            *(uint32_t *)(rx_buf_tmp + sizeof(rx_buf_hdr_t)));
 | 
						|
#endif
 | 
						|
                    }
 | 
						|
                }
 | 
						|
            }
 | 
						|
        }
 | 
						|
        end_time = RGF_MAC_READ_REG(CFG_RD_NTB_ADDR);
 | 
						|
        time_span = end_time - start_time;
 | 
						|
        if (time_span < 0) { // wrap around
 | 
						|
            time_span = (0x100000000LL) - start_time + end_time;
 | 
						|
        }
 | 
						|
        if ((uint64_t)time_span > g_plc_dt_ctxt.indep.print_period_ms*TICKS_MS) {
 | 
						|
            start_time = end_time;
 | 
						|
            mac_pkt_info_cnt_print();
 | 
						|
 | 
						|
            /* nf cal */
 | 
						|
            g_phy_ctxt.dep.nf = phy_rx_nf_by_rxtd_get(14);
 | 
						|
            iot_printf("current nf:%d!\n", g_phy_ctxt.dep.nf);
 | 
						|
 | 
						|
            /* crc cnt */
 | 
						|
            iot_printf("check pb crc fail cnt:%d!\n", crc32_cnt);
 | 
						|
            crc32_cnt = 0;
 | 
						|
 | 
						|
            /* dbg cnt */
 | 
						|
#if IOT_DTEST_ONLY_SUPPORT == 0
 | 
						|
            iot_printf("[DBG]:192p:%d, 384p:%d, 768p:%d, 1536p:%d, " \
 | 
						|
                "3072p:%d, 6144p:%d, shift:%d\n", \
 | 
						|
                g_phy_cpu_share_ctxt.nf_192p, \
 | 
						|
                g_phy_cpu_share_ctxt.nf_384p, \
 | 
						|
                g_phy_cpu_share_ctxt.nf_768p, \
 | 
						|
                g_phy_cpu_share_ctxt.nf_1536p, \
 | 
						|
                g_phy_cpu_share_ctxt.nf_3072p, \
 | 
						|
                g_phy_cpu_share_ctxt.nf_6144p, \
 | 
						|
                cpu1_shift_cnt);
 | 
						|
#else
 | 
						|
            iot_printf("[DBG]:shift:%d\n", cpu1_shift_cnt);
 | 
						|
#endif
 | 
						|
            cpu1_shift_cnt = 0;
 | 
						|
        }
 | 
						|
 | 
						|
#if IOT_DTEST_ONLY_SUPPORT == 0
 | 
						|
        /* led fresh */
 | 
						|
        if (rx_ok_cnt > 0) {
 | 
						|
            gpio_val = !gpio_val;
 | 
						|
            iot_gpio_value_set(32, gpio_val);
 | 
						|
            iot_gpio_value_set(33, gpio_val);
 | 
						|
            rx_ok_cnt = 0;
 | 
						|
        }
 | 
						|
#endif
 | 
						|
    } while (true); /* keep check */
 | 
						|
 | 
						|
    return 0;
 | 
						|
}
 | 
						|
 |