Files
kunlun/plc/halmac/mac_cfg/mac_cfg.c
2024-09-28 14:24:04 +08:00

450 lines
13 KiB
C
Executable File

/****************************************************************************
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 "mac_cfg.h"
#include "plc_const.h"
#include "iot_pib.h"
#include "iot_errno_api.h"
#include "os_types.h"
#include "mac_desc_engine.h"
#include "iot_io_api.h"
#include "iot_board.h"
#include "iot_oem_api.h"
#include "mac_reset.h"
#include "mac_tx_power.h"
#include "phy_chn.h"
#if HPLC_RF_DEV_SUPPORT
#include "mac_rf_common_hw.h"
#endif
/* pib commit function callback enable */
#define MAC_CFG_PIB_COMMIT_CB_EN 1
/* mac config final info */
static mac_cfg_buf_info_t g_mac_cfg_buf_info = {0};
/* mac config pib info, store ro/rw pib ptr */
static mac_cfg_info_t g_mac_cfg_info = {0};
#if (HW_PLATFORM == HW_PLATFORM_SIMU)
extern uint8_t ucIsClientMode;
#else
#if PLC_SUPPORT_CCO_ROLE
/* for FPGA or chip, use the global variable temply */
uint32_t g_is_sta = 0;
#else
uint32_t g_is_sta = 1;
#endif
#endif
#if (MAC_CFG_PIB_COMMIT_CB_EN)
static void mac_cfg_pib_commit_cb(uint8_t status, uint16_t ticket)
{
(void)status;
(void)ticket;
iot_printf("%s, status = %d, ticket = %d\r\n", \
__FUNCTION__, status, ticket);
return;
}
#endif
static void mac_cfg_buf_info_store_default(void)
{
#if MAC_CFG_STORE_PARA_TODO
uint8_t role;
#if (HW_PLATFORM == HW_PLATFORM_SIMU)
role = (uint8_t)(ucIsClientMode ? STA_PIB_TYPE : CCO_PIB_TYPE);
#else //HW_PLATFORM == HW_PLATFORM_SIMU
role = (uint8_t)(g_is_sta ? STA_PIB_TYPE : CCO_PIB_TYPE);
#endif
g_mac_cfg_buf_info.peer_cnt = MAC_CFG_DEF_PEER_CNT;
if (STA_PIB_TYPE == role)
{
g_mac_cfg_buf_info.pa_cnt = MAC_CFG_DEF_STA_PA_CNT;
}
else
{
g_mac_cfg_buf_info.pa_cnt = MAC_CFG_DEF_CCO_PA_CNT;
}
g_mac_cfg_buf_info.tput_mbps_size = MAC_CFG_DEF_PERFORMANCE_MBPS;
g_mac_cfg_buf_info.tx_buf_ratio = MAC_CFG_DEF_TX_BUF_RATIO;
g_mac_cfg_buf_info.rx_buf_ratio = MAC_CFG_DEF_RX_BUF_RATIO;
g_mac_cfg_buf_info.plc_long_buf_cnt = PLC_LONG_BUF_COUNT;
g_mac_cfg_buf_info.plc_large_buf_cnt = PLC_LARGE_BUF_COUNT;
g_mac_cfg_buf_info.plc_small_buf_cnt = PLC_SMALL_BUF_COUNT;
g_mac_cfg_buf_info.plc_short_buf_cnt = PLC_SHORT_BUF_COUNT;
#endif //MAC_CFG_STORE_PARA_TODO
os_mem_set(g_mac_cfg_buf_info.mac_zc_compens, 0, \
sizeof(g_mac_cfg_buf_info.mac_zc_compens));
}
static void mac_cfg_load_pib(void)
{
uint8_t *section, *ro_section;
uint8_t count;
uint8_t pib_type, ro_pib_type;
uint8_t ro_crc = 0, rw_crc = 0;
uint8_t role;
uint32_t ret;
mac_cfg_rw_pib_t *cfg;
#if (HW_PLATFORM == HW_PLATFORM_SIMU)
role = (uint8_t)(ucIsClientMode ? STA_PIB_TYPE : CCO_PIB_TYPE);
#else
role = (uint8_t)(g_is_sta ? STA_PIB_TYPE : CCO_PIB_TYPE);
#endif
iot_pib_get_crc_status(&ro_crc, &rw_crc);
/* get ro pib data */
ret = iot_pib_get_section(IOT_PIB_MAC_ID, &ro_section, &ro_pib_type, \
IOT_PIB_GET_READ_SECTION);
if ((ERR_OK == ret) && ro_crc) {
if (role != ro_pib_type) {
iot_printf("%s ro_pib role err!\n", __FUNCTION__);
//IOT_ASSERT(0); //TODO:
}
g_mac_cfg_info.ro_cfg = (mac_cfg_ro_pib_t *)ro_section;
iot_printf("mac cfg load ro pib success\r\n");
} else {
g_mac_cfg_info.ro_cfg->dis_high_power_req_ena = 0;
g_mac_cfg_info.ro_cfg->pwr_dbuv = PHY_FULL_PWR_DBUV;
g_mac_cfg_info.ro_cfg->rf_pwr_valid = 0;
g_mac_cfg_info.ro_cfg->rf_pwr_dbm = 0;
iot_printf("%s ro_pib crc or ret:%d err!\n", __FUNCTION__, ret);
return;
}
/* get rw pib data */
ret = iot_pib_get_section(IOT_PIB_MAC_ID, &section, &pib_type, \
IOT_PIB_GET_WRITE_SECTION);
/* don't care mac ro pib */
if ((ERR_OK == ret) && rw_crc)
{
if (role != pib_type)
{
iot_printf("%s rw_pib role err!\n", __FUNCTION__);
//IOT_ASSERT(0); //TODO:
}
cfg = (mac_cfg_rw_pib_t *)section;
g_mac_cfg_info.rw_cfg = cfg;
iot_printf("mac cfg load pib success\r\n");
}
else
{
mac_cfg_buf_info_store_default();
iot_printf("%s rw_pib crc or ret:%d err!\n", __FUNCTION__, ret);
return;
}
if (MAC_CFG_RW_MARK != cfg->mark)
{
iot_pib_acquire_commit_ref(IOT_PIB_MAC_ID, &count);
cfg->mark = MAC_CFG_RW_MARK;
#if MAC_CFG_STORE_PARA_TODO
cfg->role.peer_cnt = MAC_CFG_DEF_PEER_CNT;
cfg->role.tput_mbps = MAC_CFG_DEF_PERFORMANCE_MBPS;
/* tx rx buf ratio */
cfg->role.tx_buf_ratio = MAC_CFG_DEF_TX_BUF_RATIO; // 8/16
cfg->role.rx_buf_ratio = MAC_CFG_DEF_RX_BUF_RATIO; // 8/16
/* all size buff ratio */
cfg->role.long_buf_cnt_ratio = MAC_CFG_DEF_LONG_BUF_CNT_RATIO; // 1/16
cfg->role.large_buf_cnt_ratio = MAC_CFG_DEF_LARGE_BUF_CNT_RATIO; // 1/16
cfg->role.small_buf_cnt_ratio = MAC_CFG_DEF_SMALL_BUF_CNT_RATIO; // 6/16
cfg->role.short_buf_cnt_ratio = MAC_CFG_DEF_SHORT_BUF_CNT_RATIO; // 8/16
if (pib_type == STA_PIB_TYPE)
{
cfg->role.pa_cnt = MAC_CFG_DEF_STA_PA_CNT;
}
else
{
cfg->role.pa_cnt = MAC_CFG_DEF_CCO_PA_CNT;
}
#endif //MAC_CFG_STORE_NOUSE_PARA
iot_pib_release_commit_ref(IOT_PIB_MAC_ID, &count);
iot_printf("mac config pib mark[0x%X] dont match\r\n", cfg->mark);
}
#if MAC_CFG_STORE_PARA_TODO
g_mac_cfg_buf_info.peer_cnt = cfg->role.peer_cnt;
g_mac_cfg_buf_info.pa_cnt = (uint16_t)cfg->role.pa_cnt;
g_mac_cfg_buf_info.tput_mbps_size = cfg->role.tput_mbps;
g_mac_cfg_buf_info.tx_buf_ratio = cfg->role.tx_buf_ratio;
g_mac_cfg_buf_info.rx_buf_ratio = cfg->role.rx_buf_ratio;
uint32_t tmp;
tmp = cfg->role.long_buf_cnt_ratio + cfg->role.large_buf_cnt_ratio + \
cfg->role.short_buf_cnt_ratio + cfg->role.small_buf_cnt_ratio;
/* cal long buf cnt */
g_mac_cfg_buf_info.plc_long_buf_cnt = (uint16_t)(PCO_RX_DATA_BUF_KB \
/ PLC_LONG_BUF_SIZE * cfg->role.long_buf_cnt_ratio / tmp);
if (g_mac_cfg_buf_info.plc_long_buf_cnt < PLC_LONG_BUF_MIN_NUM)
{
g_mac_cfg_buf_info.plc_long_buf_cnt = PLC_LONG_BUF_MIN_NUM; //4 TODO: 2 or 4???
}
/* cal largr buf cnt */
g_mac_cfg_buf_info.plc_large_buf_cnt = (uint16_t)(PCO_RX_DATA_BUF_KB \
/ PLC_LARGE_BUF_SIZE * cfg->role.large_buf_cnt_ratio / tmp);
/* cal short buf cnt */
g_mac_cfg_buf_info.plc_short_buf_cnt = (uint16_t)(PCO_RX_DATA_BUF_KB \
/ PLC_SHORT_BUF_SIZE * cfg->role.short_buf_cnt_ratio / tmp);
/* cal small buf cnt */
g_mac_cfg_buf_info.plc_small_buf_cnt = (uint16_t)(PCO_RX_DATA_BUF_KB \
/ PLC_SMALL_BUF_SIZE * cfg->role.small_buf_cnt_ratio / tmp);
#endif //MAC_CFG_STORE_PARA_TODO
/* load zc compens value */
os_mem_cpy(g_mac_cfg_buf_info.mac_zc_compens, cfg->role.mac_zc_compens, \
sizeof(g_mac_cfg_buf_info.mac_zc_compens));
}
static void mac_cfg_load_oem(void)
{
uint8_t role;
#if (HW_PLATFORM == HW_PLATFORM_SIMU)
role = (uint8_t)(ucIsClientMode ? PLC_DEV_ROLE_STA : PLC_DEV_ROLE_CCO);
#else
role = (uint8_t)(g_is_sta ? PLC_DEV_ROLE_STA : PLC_DEV_ROLE_CCO);
#endif
uint8_t user_type;
g_mac_cfg_buf_info.vendor_id = iot_oem_get_vendor_id();
user_type = iot_oem_get_user_type();
if (user_type == USER_TYPE_SUNSOLAR1) {
g_mac_cfg_buf_info.bcn_def_retry_cnt = (role == PLC_DEV_ROLE_CCO ?
PLC_BCN_DEF_CCO_RETRY_CNT_IOT_SUNSONAL :
PLC_BCN_DEF_STA_RETRY_CNT_IOT_SUNSONAL);
} else {
g_mac_cfg_buf_info.bcn_def_retry_cnt = (role == PLC_DEV_ROLE_CCO ?
PLC_BCN_DEF_CCO_RETRY_CNT :
PLC_BCN_DEF_STA_RETRY_CNT);
}
}
uint8_t mac_cfg_get_bcn_def_retry_cnt(void)
{
return g_mac_cfg_buf_info.bcn_def_retry_cnt;
}
uint32_t mac_cfg_save_pib(void)
{
uint8_t count;
uint16_t commit_ticket;
uint32_t ret;
mac_cfg_rw_pib_t cfg;
if (NULL == g_mac_cfg_info.rw_cfg)
{
return ERR_FAIL;
}
//TODO: the value of all cfg are obtained from g_mac_cfg_buf_info
os_mem_cpy(&cfg, g_mac_cfg_info.rw_cfg, sizeof(cfg));
//update mac_zc_compens value
os_mem_cpy(cfg.role.mac_zc_compens, g_mac_cfg_buf_info.mac_zc_compens, \
sizeof(cfg.role.mac_zc_compens));
if (os_mem_cmp(g_mac_cfg_info.rw_cfg, &cfg, sizeof(mac_cfg_rw_pib_t)))
{
iot_pib_acquire_commit_ref(IOT_PIB_MAC_ID, &count);
os_mem_cpy(g_mac_cfg_info.rw_cfg, &cfg, sizeof(mac_cfg_rw_pib_t));
iot_pib_release_commit_ref(IOT_PIB_MAC_ID, &count);
ret = iot_pib_commit(IOT_PIB_MAC_ID, COMMIT_TYPE_WAIT_REFZERO, \
&commit_ticket);
}
else
{
ret = ERR_OK;
}
return ret;
}
uint32_t mac_cfg_init(void)
{
uint32_t ret = ERR_OK;
mac_cfg_load_oem();
mac_cfg_load_pib();
mac_dma_ckl_sel(true);
#if MAC_CFG_PIB_COMMIT_CB_EN
if (NULL != g_mac_cfg_info.rw_cfg) //don't care ro pib
{
ret = iot_pib_register_commit_cb(IOT_PIB_MAC_ID, \
(iot_pib_report_commit_status_func_t)mac_cfg_pib_commit_cb);
}
#endif
return ret;
}
//TODO: attention to match vendor code and the return value
uint8_t mac_cfg_get_special_info(void)
{
uint8_t special_code = 0; /* 0: normal code, 1: special code */
if (0 == g_mac_cfg_buf_info.vendor_id) {
mac_cfg_load_oem(); /* reload oem config */
}
switch (g_mac_cfg_buf_info.vendor_id) {
/* special vendor */
case MAC_CFG_VENDOR_CODE_JSMT:
special_code = 1;
break;
/* normal vendor */
case MAC_CFG_VENDOR_CODE_HTZD:
case MAC_CFG_VENDOR_CODE_GLDQ:
case MAC_CFG_VENDOR_CODE_WQRD:
special_code = 0;
break;
default:
special_code = 0;
break;
}
return special_code;
}
int32_t mac_cfg_get_zc_compens(uint8_t phase_num)
{
if ((phase_num > PLC_PHASE_CNT) || (phase_num == PLC_PHASE_ALL)) {
phase_num = PLC_PHASE_A;
}
return g_mac_cfg_buf_info.mac_zc_compens[phase_num - 1];
}
uint8_t mac_cfg_set_zc_compens(uint8_t phase_num, int32_t zc_compens)
{
if ((phase_num > PLC_PHASE_CNT) || (phase_num == PLC_PHASE_ALL)) {
return ERR_INVAL;
}
g_mac_cfg_buf_info.mac_zc_compens[phase_num -1] = zc_compens;
return ERR_OK;
}
uint8_t mac_cfg_get_auto_high_power_req_ena()
{
#if AUTO_PLC_HIGH_PWR
return (uint8_t)(!g_mac_cfg_info.ro_cfg->dis_high_power_req_ena);
#else
return 0;
#endif
}
uint8_t mac_cfg_get_auto_high_power_dbuv()
{
uint8_t pwr_dbuv;
uint8_t role;
uint8_t sta_tx_power = PHY_FULL_PWR_DBUV - STA_TX_POWER_REDUCE_DB_DEFAULT;
#if (HW_PLATFORM == HW_PLATFORM_SIMU)
role = (uint8_t)(ucIsClientMode ? PLC_DEV_ROLE_STA : PLC_DEV_ROLE_CCO);
#else
iot_oem_base_cfg_t *base_cfg;
uint32_t band_id = phy_band_id_get();
role = (uint8_t)(g_is_sta ? PLC_DEV_ROLE_STA : PLC_DEV_ROLE_CCO);
if (role != PLC_DEV_ROLE_CCO) {
iot_oem_get_base_cfg(&base_cfg);
switch (base_cfg->module_type) {
case MODULE_TYPE_STA:
{
switch (band_id) {
case IOT_SUPPORT_TONE_80_490:
case IOT_SUPPORT_TONE_100_230:
{
sta_tx_power = PHY_FULL_PWR_DBUV -
STA_TX_POWER_REDUCE_DB_BAND_01;
break;
}
case IOT_SUPPORT_TONE_TIDE:
case IOT_SUPPORT_TONE_100_230_CE:
case IOT_SUPPORT_TONE_80_490_CE:
case IOT_SUPPORT_TONE_231_490_CE:
{
sta_tx_power = PHY_FULL_PWR_DBUV;
break;
}
default:
sta_tx_power = PHY_FULL_PWR_DBUV -
STA_TX_POWER_REDUCE_DB_BAND_OTHER;
break;
}
break;
}
case MODULE_TYPE_CCO:
case MODULE_TYPE_COLLECTOR_II:
case MODULE_TYPE_STA_TEST:
case MODULE_TYPE_3_PHASE_STA:
default:
break;
}
}
#endif
if (g_mac_cfg_info.ro_cfg->pwr_dbuv == 0) {
if (role == PLC_DEV_ROLE_CCO) {
pwr_dbuv = PHY_FULL_PWR_DBUV;
if (iot_board_is_5v_power_in()) {
pwr_dbuv -= CCO_TX_POWER_REDUCE_DB_NEW_TYPE;
}
} else {
pwr_dbuv = sta_tx_power;
}
} else {
pwr_dbuv = (uint8_t)g_mac_cfg_info.ro_cfg->pwr_dbuv;
}
return pwr_dbuv;
}
#if HPLC_RF_DEV_SUPPORT
int8_t mac_cfg_get_rf_auto_high_power_dbm()
{
int8_t pwr_dbm;
uint32_t rf_ver;
rf_ver = mac_rf_get_rf_ver();
if (g_mac_cfg_info.ro_cfg->rf_pwr_valid == 0) {
if (rf_ver == RF_VER_1 || rf_ver == RF_VER_UNKNOWN) {
pwr_dbm = RF_TX_PWR_INVALID;
} else {
phy_rf_get_power(NULL, NULL, NULL, &pwr_dbm);
}
} else {
pwr_dbm = (int8_t)g_mac_cfg_info.ro_cfg->rf_pwr_dbm;
}
return pwr_dbm;
}
#else
int8_t mac_cfg_get_rf_auto_high_power_dbm()
{
return RF_TX_PWR_INVALID;
}
#endif