Files
kunlun/startup/misc/plc/plc_main.c
2025-01-08 15:05:46 +08:00

657 lines
16 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.
****************************************************************************/
/* os shim includes */
#include "os_types.h"
#include "os_task.h"
#include "os_utils.h"
/* common includes */
#include "iot_io.h"
#include "iot_bitops.h"
#include "iot_pkt.h"
#include "iot_ipc.h"
#include "iot_plc_lib.h"
#include "iot_dbglog_api.h"
#include "iot_config.h"
#include "iot_version.h"
#include "iot_app.h"
#include "iot_share_task.h"
#include "iot_socket.h"
/* pib includes */
#include "iot_pib.h"
#include "iot_oem.h"
/* driver includes */
#include "cpu.h"
#include "iot_system.h"
#include "iot_clock.h"
#include "iot_uart.h"
#include "iot_wdg.h"
#include "iot_mtd.h"
#include "iot_crypto.h"
#include "iot_plc_led.h"
#include "iot_board.h"
#include "iot_led.h"
#include "iot_log_api.h"
#include "ahb.h"
#include "upgrade.h"
#include "snapshot.h"
#include "iot_efuse.h"
#include "iot_gptmr.h"
#include "iot_plc_pm.h"
#include "iot_plc_hw_tsfm.h"
#include "iot_plc_hw_topo.h"
#include "iot_power_mgmt.h"
#include "iot_energe_meter.h"
#include "iot_rtc.h"
#include "iot_dma.h"
#include "iot_eth.h"
#include "board_info.h"
/* cli includes */
#include "iot_cli.h"
#include "iot_uart_h.h"
/* ftm includes */
#include "iot_ftm.h"
#if IOT_PT_BOARD_SELECT
#include "iot_pt_board_mp.h"
#endif
/* debug includes*/
#include "dbg_io.h"
/* plc public includes */
#include "plc_const.h"
#include "mac_init_api.h"
#include "cvg_api.h"
#include "htbus_api.h"
#include "plc_protocol.h"
#include "hw_phy_init.h"
#include "phy_chn.h"
#include "phy_rf_chn.h"
#include "iot_cal_data.h"
#include "platform.h"
#include "mp_mode.h"
#include "mac_channel.h"
#include "iot_lwip.h"
#include "iot_upgrade_api.h"
#include "iot_busmon.h"
#include "iot_gpio_api.h"
#include "spi_flash.h"
#include "iot_oem_api.h"
#include "iot_crypto_async.h"
/* wireless include */
#include "phy_rf_init.h"
#include "phy_rf_chn.h"
#include "dbg_msg_pop.h"
#include "hw_mem_map.h"
#include "hw_phy_api.h"
uint8_t g_app_reg_done = 0;
extern void iot_board_info_init();
extern void heap_alloc_caps_init(uint32_t reserved_sz);
extern uint32_t vc_module_init();
extern int sec_cpu_start();
const iot_pkt_config_t g_pkt_config =
{
{
#if RUN_IN_PSRAM
/* for PSRAM enabled case, we need to make sure 520 PB allocated for
* PHY data receiving inside the internal RAM to fix band 0 ext TMI 1
* receive error, so choose the size carefully.
*/
/* reserve IOT_SG_UPGRADE_BLOCK block for upgrading */
{
PLC_SHORT_BUF_SIZE,
(PLC_SHORT_BUF_COUNT + IOT_SG_UPGRADE_BLOCK_CNT - 30),
PKT_OWNER_ALL, //PKT_OWNER_PLC
},
{
(PLC_SHORT_BUF_SIZE + 4),
30 + RF_SHORT_BUF_COUNT,
PKT_OWNER_ALL, //PKT_OWNER_PLC
},
#else /* RUN_IN_PSRAM */
/* reserve IOT_SG_UPGRADE_BLOCK block for upgrading */
{
PLC_SHORT_BUF_SIZE,
(PLC_SHORT_BUF_COUNT + IOT_SG_UPGRADE_BLOCK_CNT),
PKT_OWNER_ALL, //PKT_OWNER_PLC
},
#if RF_SHORT_BUF_COUNT
{
(PLC_SHORT_BUF_SIZE + 4),
RF_SHORT_BUF_COUNT,
PKT_OWNER_ALL, //PKT_OWNER_PLC
},
#endif
#endif /* RUN_IN_PSRAM */
{
PLC_SMALL_BUF_SIZE,
PLC_SMALL_BUF_COUNT,
PKT_OWNER_ALL, //PKT_OWNER_PLC
},
#if RF_SMALL_BUF_COUNT
{
(PLC_SMALL_BUF_SIZE + 4),
RF_SMALL_BUF_COUNT,
PKT_OWNER_ALL, //PKT_OWNER_PLC
},
#endif
{
PLC_LONG_BUF_SIZE,
PLC_LONG_BUF_COUNT,
PKT_OWNER_ALL, //PKT_OWNER_PLC
},
{
PLC_LARGE_BUF_SIZE,
PLC_LARGE_BUF_COUNT + DBGLOG_MAX_BUFFER_CNT,
PKT_OWNER_ALL, //PKT_OWNER_PLC
},
{
PLC_LIB_MSG_SHORT_BUF_SIZE,
PLC_LIB_MSG_SHORT_BUF_CNT,
PKT_OWNER_ALL,
},
{
PLC_HUGE_BUF_SIZE,
PLC_HUGE_BUF_COUNT,
PKT_OWNER_ALL,
},
#if RUN_IN_PSRAM
{
PLC_LIB_MSG_LONG_BUF_SIZE,
PLC_LIB_MSG_LONG_BUF_CNT,
PKT_OWNER_ALL,
},
#else /* RUN_IN_PSRAM */
#if !RF_SHORT_BUF_COUNT
{
0,
0,
PKT_OWNER_NONE,
},
#endif
{
0,
0,
PKT_OWNER_NONE,
},
#endif /* RUN_IN_PSRAM */
#if !RF_SMALL_BUF_COUNT
{
0,
0,
PKT_OWNER_NONE,
}
#endif
}
};
//mm mode or mp mode
#if (IOT_MP_SUPPORT == 1 && IOT_PT_BOARD_SELECT == 0)
/* plc mission mode initialization second half */
static void plc_mm_init2(iot_msg_entry_t *entry)
{
uint8_t ret;
(void)entry;
(void)ret;
extern os_task_h _init_handle;
os_delete_task(_init_handle);
os_delay(100);
/* launch applications */
ret = iot_app_start();
#if PLC_COMM_ENABLE && (!IOT_HTBUS_EN)
if (ret == 0) {
cvg_start_vdev(0, NULL, CVG_VDEV_START_BOOT_UP);
} else if (ret == 1) {
cvg_start_vdev(0, NULL, CVG_VDEV_START_BOOT_UP_CERT_ONLY);
}
#endif /* PLC_COMM_ENABLE && (!IOT_HTBUS_EN) */
g_app_reg_done = 1;
}
static void plc_mm_init2_cancel(iot_msg_entry_t *entry)
{
IOT_ASSERT(0);
}
#endif
void iot_mode_set(int fw_mode)
{
uint8_t reason = IOT_SYS_RST_REASON_UNKNOWN;
iot_board_get_boot_reason(&reason);
if (fw_mode == FTM_MODE || reason == IOT_SYS_RST_REASON_FTM) {
g_fw_mode = FTM_MODE;
} else {
g_fw_mode = MM_MODE;
}
#if ( (IOT_FTM_SUPPORT == 1 && IOT_MP_SUPPORT == 0) \
|| (IOT_MP_MODE_DIRECT_ENABLE == 1) \
|| (IOT_MP_RF_MODE_DIRECT_ENABLE == 1) \
|| (IOT_MP_TX_MODE_ENABLE == 1) \
|| (IOT_PT_BOARD_SELECT != IOT_PT_BOARD_NONE) \
|| (IOT_MP_RX_MODE_ENABLE == 1))
/* TBD. add GPIO detection for bring up mode */
g_fw_mode = FTM_MODE;
#endif
#if (IOT_STA_CONTROL_TYPE_STA == IOT_STA_CONTROL_MODE)
heap_alloc_caps_init(MEM_MAP_RESERVED_BUF_SZ);
#elif (IOT_FTM_SUPPORT == 1)
if (g_fw_mode == FTM_MODE) {
heap_alloc_caps_init(MEM_MAP_RESERVED_BUF_SZ);
}
#endif
}
// 硬件部分初始化
void iot_hw_init()
{
// 初始化efuse
iot_efuse_init();
// 初始化时钟
system_clock_init();
// 初始化boardid为0
iot_board_info_init();
// 加密
iot_crypto_init();
/*resource initializations;*/
// 初始化flash
mtd_device_init(0);
/* init oem module load oem cfg from file */
// 初始化oem状态 cco还是sta
#if (PLC_SUPPORT_CCO_ROLE)
iot_oem_init(1);
#else
iot_oem_init(0);
#endif
// 目前是空函数
iot_cal_data_init();
#if (IOT_FLASH_BUILD == 1)
/* board info config*/
iot_board_cfg_init();
#endif
iot_led_init();
system_uart_init();
#ifndef DISABLE_PRINT
dbg_uart_init_port(iot_board_get_uart(UART_PRINT), 1);
dbg_uart_stage1_init();
#endif
#if (IOT_FLASH_BUILD == 1)
// 打印oem配置信息
iot_board_cfg_dump();
#endif
iot_busmon_init();
/*platform intialization*/
// 这里初始化cpu中断 异常处理函数 gp定时器等
platform_init();
/* chip id checking */
iot_chip_id_check();
iot_wdg_enable();
}
// 子系统初始化
void iot_sub_system_init()
{
/* init common modules */
// 检查前导零数组是否正常
iot_bitops_init();
/* init os related modules and utilities */
os_utils_init();
/* rtc init */
iot_rtc_init();
/* init pkt module */
iot_pkt_init(&g_pkt_config);
/* init shared background task. It shall be ahead of pib init. */
iot_share_task_init();
/* init dbglog module */
iot_dbglog_init();
/* init dma module */
iot_dma_init();
/* init gpio module */
iot_gpio_module_init();
if (g_fw_mode == MM_MODE) {
/* init uart module */
iot_uart_init(0);
} else {
/* init uart module */
iot_uart_init(1);
}
/* init ipc module */
iot_ipc_init();
}
/* plc mission mode initialization first half */
void proto_mm_init()
{
#if (IOT_MP_SUPPORT == 1 && IOT_PT_BOARD_SELECT == 0)
bool_t upgraded = false;
/* efuse protect */
iot_efuse_protect();
/* gpio reset control */
iot_sys_reset_ctrl();
/* update boot paramters */
if (0 == dev_startup_param_value_check(false)) {
upgraded = true;
}
/* set first boot flag */
iot_upgrade_set_first_boot(upgraded);
/* init pib module and load pib cfg from file */
iot_pib_init();
iot_pib_load(upgraded);
/* load user build info if any */
iot_board_load_user_build_info();
/* update reboot log */
iot_board_reset_check();
/* log dev init */
iot_log_dev_init();
#if IOT_ETH_ENABLE
iot_eth_init();
#endif
#if IOT_EXT_FLASH_ENABLE
iot_spi_flash_init();
#endif
/* init upgrade module */
iot_upgrade_init();
/* init plc led module */
iot_plc_led_init(0);
/* init power management module */
iot_plc_pm_init();
/* init hw_tsfm management module */
iot_plc_hw_tsfm_init();
/* init hw_topo management module */
iot_plc_hw_topo_init();
/* init dbg msg */
dbg_msg_print_init();
#if IOT_SEC_CPU_SUPPORT
if (sec_cpu_start()) {
IOT_ASSERT(0);
}
#endif
/* init crypto async */
iot_crypto_async_init();
/* for tx/rx phase switch gpio init */
phy_trx_phase_switch_gpio_init();
#if PLC_COMM_ENABLE
/* phy init, please be noted with proto init */
uint32_t proto = PLC_PROTO_TYPE_SG;
#if SUPPORT_SMART_GRID
proto = PLC_PROTO_TYPE_SG;
#elif SUPPORT_SOUTHERN_POWER_GRID
proto = PLC_PROTO_TYPE_SPG;
#endif
uint32_t hw_band_id = IOT_PLC_PHY_BAND_DFT;
/* for yunnan spg */
iot_oem_cfg_t *oemcfg_t = NULL;
iot_oem_get_cfg(&oemcfg_t);
uint8_t user_type;
g_phy_cpu_share_ctxt.sym_num_fix_dis = 1;
if (PLC_PROTO_TYPE_SPG == proto && \
oemcfg_t->base_cfg.module_type != MODULE_TYPE_CCO) {
user_type = iot_oem_get_user_type();
if (USER_TYPE_SOUTHEN_POWER_GRID_YUNNAN == user_type) {
hw_band_id = IOT_SUPPORT_TONE_100_230;
} else if (USER_TYPE_SOUTHEN_POWER_GRID_GUIZHOU == user_type) {
hw_band_id = IOT_SUPPORT_TONE_80_490;
g_phy_cpu_share_ctxt.sym_num_fix_dis = 0;
}
} else if (PLC_PROTO_TYPE_SG == proto && \
((USER_TYPE_STATE_GRID_CQ == iot_oem_get_user_type()) ||
(USER_TYPE_STATE_GRID_JIBEI == iot_oem_get_user_type()))) {
hw_band_id = IOT_SUPPORT_TONE_100_230;
}
/* NOTE: ckq need bbai at phy first init */
phy_init(proto, hw_band_id, phy_band_to_tonemask_id_get(hw_band_id),
((IOT_STA_CONTROL_MODE == IOT_STA_CONTROL_TYPE_METER)||
(IOT_STA_CONTROL_MODE == IOT_STA_CONTROL_TYPE_PPM_DETECT)));
uint8_t rf_band_sel, opt, ch;
if (iot_board_is_overseas_rf_band()) {
rf_band_sel = PHY_RF_BAND_OVERSEAS;
} else {
rf_band_sel = PHY_RF_BAND_NSG;
}
phy_rf_band_info_init((PHY_RF_BAND_T)rf_band_sel);
phy_rf_get_default_cfg(&opt, &ch);
/* wireless phy init, plccpu and bbcpu sync init, rf rxdc diable */
phy_rf_init(proto, (uint32_t)opt, (uint32_t)ch, 1, 0, 0);
/* mac init */
mac_init(PHY_PROTO_TYPE_GET());
#if IOT_HTBUS_EN == 0
/* init plc cvg module */
cvg_init((uint8_t)PHY_PROTO_TYPE_GET());
#else
/* init plc htbus module */
htbus_init();
#endif
/* init plc lib */
iot_plc_lib_init((uint8_t)PHY_PROTO_TYPE_GET());
#endif /* PLC_COMM_ENABLE */
/* init tcpip stack */
iot_lwip_init();
/* create task for socket to receive data. */
iot_socket_task_init();
#if IOT_BT_EXT_ENABLE
extern uint32_t iot_bt_ext_open(void);
iot_bt_ext_open();
#endif
#if IOT_CLI_MODULE_ENABLE
/* init cli module 1 is uart*/
/* TODO: enable this after fixing the iot_pkt free issue */
iot_cli_init(1, 0);
#endif /* IOT_CLI_MODULE_ENABLE */
#if K48_STA_MULTI_CHANNEL_SELECT_ENABLE
iot_mac_k48sta_init_channelgpio();
#endif
#if K48_CCO_MULTI_CHANNEL_SELECT_ENABLE
iot_mac_k48cco_init_channelgpio();
#endif
dev_startup_param_value_check(true);
/* gp timer callback to share task. */
iot_gp_timer_task_register();
/* energe meter init*/
#if IOT_ENERGE_METER_ENABLE
iot_energe_meter_init();
#endif
#if INCLUDE_VIRTUAL_CHANNEL
vc_module_init();
#endif
/* start bottom half init in shared task context to free current task
* to save memory.
*/
iot_share_task_msg_register(IOT_SHARE_TASK_MT_BH_INIT,
plc_mm_init2, plc_mm_init2_cancel);
iot_share_task_post_msg(IOT_SHARE_TASK_QUEUE_HP, IOT_SHARE_TASK_MT_BH_INIT,
0, 0, NULL);
/* delay enough time to allow bottom half init to delete init task
* to free memory quickly
*/
os_delay(10000);
#endif
}
/* plc factory test mode initialization */
void proto_ftm_init()
{
bool_t upgraded = false;
/* gpio reset control */
iot_sys_reset_ctrl();
/* update boot paramters */
if (0 == dev_startup_param_value_check(false)) {
upgraded = true;
}
/* set first boot flag */
iot_upgrade_set_first_boot(upgraded);
/* init pib module and load pib cfg from file */
iot_pib_init();
iot_pib_load(upgraded);
#if (IOT_FLASH_BUILD)
iot_log_dev_init();
#endif
/* init plc led module */
iot_plc_led_init(1);
/* init dbg msg */
dbg_msg_print_init();
#if IOT_SEC_CPU_SUPPORT
if (sec_cpu_start()) {
IOT_ASSERT(0);
}
#endif
/* init crypto async */
iot_crypto_async_init();
/* for tx/rx phase switch gpio init */
phy_trx_phase_switch_gpio_init();
/* phy init */
uint32_t proto = PLC_PROTO_TYPE_SG;
#if SUPPORT_SMART_GRID
proto = PLC_PROTO_TYPE_SG;
#elif SUPPORT_SOUTHERN_POWER_GRID
proto = PLC_PROTO_TYPE_SPG;
#endif
phy_init(proto, IOT_PLC_PHY_BAND_DFT,
phy_band_to_tonemask_id_get(IOT_PLC_PHY_BAND_DFT),
!IOT_CLI_IC_TOOL_EN);
#if IOT_CLI_IC_TOOL_EN == 0
uint8_t rf_band_sel, opt, ch;
if (iot_board_is_overseas_rf_band()) {
rf_band_sel = PHY_RF_BAND_OVERSEAS;
} else {
rf_band_sel = PHY_RF_BAND_NSG;
}
phy_rf_band_info_init((PHY_RF_BAND_T)rf_band_sel);
phy_rf_get_default_cfg(&opt, &ch);
/* wireless phy init, plccpu and bbcpu sync init, rf rxdc diable */
phy_rf_init(proto, (uint32_t)opt, (uint32_t)ch, 1, 0, 0);
#endif
/* init plc lib */
iot_plc_lib_init((uint8_t)PHY_PROTO_TYPE_GET());
#if IOT_CLI_MODULE_ENABLE
/* init cli module */
iot_cli_init(1, 1);
#endif /* IOT_CLI_MODULE_ENABLE */
#if K48_STA_MULTI_CHANNEL_SELECT_ENABLE
iot_mac_k48sta_init_channelgpio();
#endif
#if K48_CCO_MULTI_CHANNEL_SELECT_ENABLE
iot_mac_k48cco_init_channelgpio();
#endif
/* init ftm module */
iot_ftm_init();
dev_startup_param_value_check(true);
/* 1. production test board open watch dog
* 2. 产测的DUT open watch dog
*/
if (IOT_PT_BOARD_SELECT || (IOT_MP_SUPPORT && phy_get_fw_mode() !=
MP_MODE)) {
} else {
iot_wdg_disable();
}
//mp mode entry
iot_init_mp_mode_entry();
return;
}