Files
2025-07-03 23:58:20 +08:00

423 lines
9.7 KiB
C
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2021-2021. All rights reserved.
* Description: main
*
* Create: 2021-03-09
*/
#include "boot_cmd_loop.h"
#include "boot_serial.h"
#include "boot_flash.h"
#include "sfc.h"
#include "chip_io.h"
#include "pinctrl.h"
#include "boot_delay.h"
#include "tcxo.h"
#include "watchdog.h"
#include "soc_porting.h"
#include "efuse.h"
#include "efuse_porting.h"
#include "boot_malloc.h"
#include "boot_erase.h"
#include "sfc_protect.h"
#define DELAY_100MS 100
__attribute__((section("BOOT_UART"))) uart_param_stru g_uart_prag;
const sfc_flash_config_t sfc_cfg = {
.read_type = FAST_READ_QUAD_OUTPUT,
.write_type = PAGE_PROGRAM,
.mapping_addr = 0x200000,
.mapping_size = 0x800000,
};
#if 1 //ADD LYL 2024 1122 --------
#define USERPAGE 0x003F2000 //AT参数存储区
#define BLE_CONKEYLEN 16
#pragma pack(push)
#pragma pack(1)
typedef struct
{
int8_t updateflag; //update flag
int8_t validflag; //valid flag
int8_t tmode_mode;
int8_t wmode_mode;
//STA, 140 bytes
uint8_t sta_ssid[33];
uint8_t sta_key[65];
int8_t wsauth;
int8_t wsencry;
uint8_t wslko;
uint8_t pmk_av;
uint8_t sta_padding[6];
uint8_t pmk[32];
//AP, 144 bytes
int8_t wap_mode;
int8_t wap_channel;
int8_t waauth;
int8_t waencry;
uint8_t walk_led;
uint8_t max_sta_num;
uint8_t ap_enable_hide;
uint8_t ap_ssid[33];
uint8_t ap_key[65];
uint8_t ap_pmk_av;
uint8_t ap_pmk[32];
uint8_t ap_padding[6];
//UART, 20 bytes
int8_t baudrate;
int8_t data_bits;
int8_t stop_bits;
int8_t parity;
int8_t ctsrts;
int8_t uart1_baudrate;
int8_t uart1_data_bits;
int8_t uart1_stop_bits;
int8_t uart1_parity;
int8_t uart1_debug;
int8_t uartfable;
int8_t fuartte_mode;
uint16_t uarttm;
uint16_t uartbuf;
uint8_t uart_padding[4];
//NETP, 108 bytes
int8_t protocol;
int8_t app_mode;
uint8_t maxsocket;
char cipadd[101];
uint16_t pro_port;
uint16_t tcpto;
//SOCKB, 112 bytes
uint16_t sockb_port;
uint16_t sockb_tcpto;
int8_t sockb_pro;
char sockb_cipadd[101];
int8_t tcpdis;
int8_t tcpdisb;
uint16_t udp_localporta;
uint16_t udp_localportb;
//NETWORK, 108 bytes
char wann_ipaddr[16];
char wann_mask[16];
char wann_gateway[16];
char lann_ipaddr[16];
char lann_mask[16];
uint8_t wann_mode;
uint8_t network_padding[2];
uint8_t domain_name[21];
uint32_t dns_addr;
//UTILS, 124 bytes
int8_t echo;
int8_t rlden;
uint8_t debug_level;
char wel_msg[11];
uint8_t event_onoff;
uint8_t noise_filter;
uint8_t wifi_switch;
uint8_t sta_dtim;
uint8_t utils_padding[40];
uint8_t dis_power_saving;
int8_t modechange;
uint8_t ps_interval;
uint8_t mid[21];
uint8_t aswd[21];
uint8_t cmdpw[21];
//HTTPD, 36 bytes
char user_name[16];
char user_password[16];
int8_t web_switch;
uint8_t web_language;
uint8_t web_encode_format;
uint8_t httpd_padding;
//DHCPD, 4 bytes
int8_t dhcpsw;
uint8_t dhcpd_addr_start;
uint8_t dhcpd_addr_end;
uint8_t dhcpd_padding;
//UPDATE, 124 bytes
char update_url[101];
char update_file[21];
uint8_t update_padding[2];
//NTP, 8 bytes
uint8_t ntp_time;
uint8_t ntp_enable;
uint8_t ntp_padding;
int8_t time_zone;
uint32_t ntpserver;
//SMTLK, 16 bytes
uint32_t smtlk_sign;
uint8_t smtlk_mode;
uint8_t smtlk_protocol;
uint8_t smtlk_rtype;
uint8_t smtlk_ak_random;
uint8_t connect_flag;
uint8_t sta_channel;
uint8_t sta_bssid[6];
//SMARTAPLINK, 44 bytes
uint8_t smartaplink_enable;
uint8_t smartaplink_padding;
uint8_t smartaplink_prefix[21];
uint8_t smartaplink_key[21];
//NETPID, 24 bytes
uint8_t netpa_iden;
char netpa_id[11];
uint8_t netpb_iden;
char netpb_id[11];
//NETPREG, 264 bytes
uint8_t netpa_regen;
uint8_t netpa_regtype;
uint8_t netpa_reg_padding;
char netpa_regdata[129];
uint8_t netpb_regen;
uint8_t netpb_regtype;
uint8_t netpb_reg_padding;
char netpb_regdata[129];
//HEART, 124 bytes
uint16_t heart_netp_time;
uint16_t heart_sockb_time;
uint16_t heart_uart_time;
char heart_netp_data[39];
char heart_sockb_data[39];
char heart_uart_data[39];
uint8_t heart_padding;
//HTTP, 236 bytes
uint8_t http_type;
uint8_t http_version;
uint8_t http_connect_type;
uint8_t http_recv_time;
char http_url[51];
char http_header[181];
//MQTT, 224 bytes
char mqtt_username[33];
char mqtt_password[33];
char mqtt_clientid[33];
char mqtt_pub_topic[61];
char mqtt_sub_topic[61];
uint8_t mqtt_qos;
uint16_t mqtt_heart_time;
//BLE, 188 bytes
uint16_t conn_min;
uint16_t conn_max;
uint16_t conn_latency;
uint16_t supervisionTO;
uint16_t adver_min;
uint16_t adver_max;
uint8_t ble_name[27]; //BLE广播名
uint8_t ble_dms_name[27]; //BLE配网名
uint8_t ble_adver_data[27];
uint8_t ble_adver_data_len;
uint8_t ble_switch;
uint8_t ble_uuid_server;
uint8_t dms_type;
uint8_t ble_padding;
uint8_t uuid_ntf_server[17];
uint8_t uuid_ntf_read[17];
uint8_t uuid_ntf_write[17];
uint8_t uuid_ind_server[17];
uint8_t uuid_ind_read[17];
uint8_t uuid_ind_write[17];
uint8_t adver_type;
uint8_t adver_channel;
uint8_t ble_ind;
uint8_t smartconfiglink;
uint8_t ble_conenable;
uint8_t ble_conkey[BLE_CONKEYLEN];
uint8_t ble_contimeout;
uint8_t netpc_iden;
char netpc_id[11];
uint8_t netpc_idflag;
uint8_t ble_padding2[17];
uint8_t mqtt_tls;
uint8_t mqtt_padding[3];
char iot_productkey[12];
char iot_devicename[48];
char iot_devicesecret[48];
char iot_pubtopic[96];
char iot_subtopic[96];
char iot_mode;
char iot_padding[3];
uint8_t product_mode;
int8_t ctsrts_pin;
char instance_flag;
char instance_id[32];
char ntp_domain_server[33];
int8_t smk_find;
uint32_t mcu_ota_size;
uint8_t scan_ch14;
uint32_t module_reset_reason;
uint8_t reserved1[60+512-4];
uint8_t validflag_magic;
int8_t validflag_end; //valid flag
}HF_CONFIG_FILE;
#pragma pack(pop)
HF_CONFIG_FILE g_hf_config_file_boot = {0};
int hfdbg_get_sw(void)
{
if(g_hf_config_file_boot.debug_level > 0 && g_hf_config_file_boot.debug_level<=20) //1~20允许打印
return 0;
if(g_hf_config_file_boot.uart1_debug == 1) //1允许打印
return 1;
return 0;
}
#endif
static uint32_t sfc_flash_init(void)
{
return uapi_sfc_init((sfc_flash_config_t *)&sfc_cfg);
}
static uint32_t sfc_flash_read(uint32_t flash_addr, uint32_t read_size, uint8_t *read_buffer)
{
return uapi_sfc_reg_read(flash_addr, read_buffer, read_size);
}
// addr 不是总线地址,没有 FLASH_START 偏移
static bool is_flash_addr(uint32_t addr, uint32_t size)
{
return ((addr >= 0) && (addr + size < FLASH_LEN));
}
static uint32_t sfc_flash_write(uint32_t flash_addr, uint32_t flash_write_size, const uint8_t *p_flash_write_data,
bool do_erase)
{
unused(do_erase);
if (is_flash_addr(flash_addr, flash_write_size)) {
return uapi_sfc_reg_write(flash_addr, (uint8_t *)p_flash_write_data, flash_write_size);
}
// 使用memcpy_s时操作的是总线地址需要加回偏移FLASH_START
uintptr_t store_addr = (uintptr_t)((int)flash_addr + (int)FLASH_START);
if (memcpy_s((void *)store_addr, flash_write_size, p_flash_write_data, flash_write_size) != EOK) {
return ERRCODE_FAIL;
}
return ERRCODE_SUCC;
}
static uint32_t sfc_flash_erase(uint32_t flash_addr, uint32_t flash_erase_size)
{
if (flash_erase_size == FLASH_CHIP_ERASE_SIZE) {
return uapi_sfc_reg_erase_chip();
}
return uapi_sfc_reg_erase(flash_addr - sfc_cfg.mapping_addr, flash_erase_size);
}
static void boot_flash_init(void)
{
flash_cmd_func flash_funcs = {0};
flash_funcs.init = sfc_flash_init;
flash_funcs.read = sfc_flash_read;
flash_funcs.write = sfc_flash_write;
flash_funcs.erase = sfc_flash_erase;
boot_regist_flash_cmd(&flash_funcs);
#define REG_PMU_CMU_CTL_PMU_SIG 0x40003154
#define PMU_FLASH_SW_EN_BIT 24
#define DELAY_5MS 5
reg32_setbit(REG_PMU_CMU_CTL_PMU_SIG, PMU_FLASH_SW_EN_BIT);
mdelay(DELAY_5MS);
uint32_t ret = sfc_flash_init();
if (ret != ERRCODE_SUCC) {
boot_msg1("Flash Init Fail! ret = ", ret);
} else {
boot_msg0("Flash Init Succ!");
}
switch_flash_clock_to_pll();
config_sfc_ctrl_ds();
}
static uint32_t ws63_loaderboot_init(void)
{
errcode_t err;
uapi_tcxo_init();
hiburn_uart_init(g_uart_prag, HIBURN_CODELOADER_UART);
boot_msg0("Loadboot Uart Init Succ!");
boot_flash_init();
err = sfc_port_fix_sr();
if (err != ERRCODE_SUCC) {
boot_msg1("SFC fix SR ret =", err);
}
set_efuse_period();
uapi_efuse_init(); // efuse函数初始化
boot_malloc_init();
return 0;
}
#define BOOT_WATCH_DOG_TIMOUT 15 // 15s
/* the entry of C. */
void start_loaderboot(void)
{
uart_ctx cmd_ctx = { 0 };
boot_clock_adapt();
uapi_watchdog_init(BOOT_WATCH_DOG_TIMOUT);
uapi_watchdog_enable(WDT_MODE_RESET);
ws63_loaderboot_init();
/* Enter the waiting command cycle. */
loader_ack(ACK_SUCCESS);
boot_msg0("================ Load Cmd Loop ================\r\n");
cmd_loop(&cmd_ctx);
while (1) {}
}
#ifdef CONFIG_LOADERBOOT_SUPPORT_SET_BUADRATE
uint32_t serial_port_set_baudrate(const uart_param_stru *uart)
{
#define CLDO_CRG_CLK_SEL 0x44001134
#define CLDO_SUB_CRG_CKEN_CTL1 0x44001104
#define UART_PLL_CLOCK 160000000
hiburn_uart_deinit();
// 切uart时钟
reg_clrbit(CLDO_SUB_CRG_CKEN_CTL1, 0, POS_18); // close uart clock
reg_setbit(CLDO_CRG_CLK_SEL, 0, POS_1); // switch uart clock to pll
reg_setbit(CLDO_SUB_CRG_CKEN_CTL1, 0, POS_18); // open uart clock
uart_port_set_clock_value(UART_BUS_0, UART_PLL_CLOCK);
hiburn_uart_init(*uart, HIBURN_CODELOADER_UART);
return ERRCODE_SUCC;
}
#endif