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

264 lines
7.3 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 "os_utils.h"
#include "mac_init_api.h"
#include "cvg_api.h"
#include "plc_protocol.h"
#include "iot_bitops.h"
#include "iot_pkt.h"
#include "iot_ipc.h"
#include "iot_io.h"
#include "plc_const.h"
#include "plc_fr.h"
#include "phy_rf_chn.h"
#include "iot_plc_lib.h"
#include "iot_plc_api.h"
#include "mac_vdev_api.h"
#include "mac_vdev.h"
#include "hw_phy_init.h"
#include "iot_version.h"
#include "iot_share_task.h"
#include "iot_rtc.h"
#include "iot_socket.h"
#if HW_PLATFORM == HW_PLATFORM_SIMU
#include "iot_uart_h.h"
#include "iot_cli.h"
#include "iot_dbglog_api.h"
#include "iot_pib.h"
#include "iot_app.h"
#include "phy_chn.h"
#include "iot_oem_api.h"
#include "iot_plc_led.h"
#include "upgrade.h"
#include "iot_plc_pm.h"
#include "iot_board.h"
#include "iot_lwip.h"
// disbable warning 4127: conditional expression is constant
// GCC won't report this warning
#pragma warning(disable:4127)
extern uint8_t ucIsClientMode;
extern uint8_t g_interfaceType;
extern uint8_t g_module_type;
/* working mode for fw
* 0 - Mission Mode
* 1 - FTM mode
*/
extern uint32_t g_fw_mode;
static const iot_pkt_config_t g_pkt_config =
{
{
{
PLC_SMALL_BUF_SIZE,
PLC_SMALL_BUF_COUNT + RF_SMALL_BUF_COUNT,
PKT_OWNER_ALL, //PKT_OWNER_PLC
},
/* reserve IOT_SG_UPGRADE_BLOCK block for upgrading */
{
PLC_SHORT_BUF_SIZE,
(PLC_SHORT_BUF_COUNT + IOT_SG_UPGRADE_BLOCK_CNT
+ RF_SHORT_BUF_COUNT),
PKT_OWNER_ALL, //PKT_OWNER_PLC
},
{
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_LIB_MSG_LONG_BUF_SIZE,
PLC_LIB_MSG_LONG_BUF_CNT,
PKT_OWNER_ALL,
},
{
PLC_HUGE_BUF_SIZE,
PLC_HUGE_BUF_COUNT,
PKT_OWNER_ALL,
},
{
0,
0,
PKT_OWNER_NONE,
},
}
};
void plc_init(void* param) {
uint32_t ret;
{
uint8_t r_crc = 0;
uint8_t w_crc = 0;
(void)param;
/* init common modules */
iot_bitops_init();
/* init os related modules and utilities */
os_utils_init();
/* init rtc module */
iot_rtc_init();
/* init shared background task. It shall be ahead of pib init. */
iot_share_task_init();
/* init pib module and load pib cfg from file */
ret = iot_pib_init();
if (ret) {
iot_printf("iot_pib_init fail\n");
}
ret = iot_pib_load(0);
if (ret) {
iot_printf("iot_pib_load fail\n");
} else {
iot_printf("iot_pib_load success\n");
}
iot_pib_get_crc_status(&r_crc, &w_crc);
iot_printf("pib r_crc:%d w_crc:%d\n", r_crc, w_crc);
/* use the dev type configured in PIB */
{
uint8_t *secion;
uint8_t pib_type;
ret = iot_pib_get_section(IOT_PIB_HEAD_ID, &secion, &pib_type,
IOT_PIB_APP_GET_READ_SECTION);
if (ret == ERR_OK) {
ucIsClientMode = (pib_type == STA_PIB_TYPE);
}
if (ucIsClientMode == 0)
g_module_type = MODULE_TYPE_CCO;
else if (g_module_type == MODULE_TYPE_CCO)
g_module_type = MODULE_TYPE_STA_TEST;
switch (g_module_type) {
case MODULE_TYPE_CCO:
iot_printf("module_type is cco\n");
break;
case MODULE_TYPE_STA:
iot_printf("module_type is power meter\n");
break;
case MODULE_TYPE_3_PHASE_STA:
iot_printf("module_type is 3 phase power meter\n");
break;
case MODULE_TYPE_COLLECTOR_II:
iot_printf("module_type is type II collector\n");
break;
case MODULE_TYPE_STA_TEST:
iot_printf("module_type is sta test\n");
break;
default:
printf("module_type is unknown\n");
IOT_ASSERT(0);
break;
}
}
/* load user build info if any */
iot_board_load_user_build_info();
/* init pkt module */
iot_pkt_init(&g_pkt_config);
/*init dbglog module*/
iot_dbglog_init();
iot_printf("test %02x ffs - %d, ffz - %d, cbs - %d, cbz - %d, "
"fls - %d, flz - %d\n", 0xD0,
iot_bitops_ffs(0xD0), iot_bitops_ffz(0xD0),
iot_bitops_cbs(0xD0), iot_bitops_cbz(0xD0),
iot_bitops_fls(0xD0), iot_bitops_flz(0xD0));
iot_printf("test %02x ffs - %d, ffz - %d, cbs - %d, cbz - %d ,"
"fls - %d, flz - %d\n", 0xCF,
iot_bitops_ffs(0xCF), iot_bitops_ffz(0xCF),
iot_bitops_cbs(0xCF), iot_bitops_cbz(0xCF),
iot_bitops_fls(0xCF), iot_bitops_flz(0xCF));
/* init uart interface */
iot_uart_init(0);
/* init upgrade module */
iot_upgrade_init();
/* init ipc module */
iot_ipc_init();
/* init plc led module */
iot_plc_led_init(0);
/* init power management module */
iot_plc_pm_init();
}
{
phy_rf_band_info_init(PHY_RF_BAND_NSG);
/* init phy and mac */
phy_init(PHY_PROTO_TYPE_GET(), \
IOT_PLC_PHY_BAND_DFT, TONE_MASK_ID_NULL, true);
mac_init(PHY_PROTO_TYPE_GET());
}
{
/* init plc cvg module */
cvg_init((uint8_t)PHY_PROTO_TYPE_GET());
/* init plc lib */
iot_plc_lib_init((uint8_t)PHY_PROTO_TYPE_GET());
}
/* 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 */
iot_cli_init(g_interfaceType, 0);
#endif /* IOT_CLI_MODULE_ENABLE */
/* start applicaiton */
ret = iot_app_start();
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);
}
return;
}
#endif