first commit

This commit is contained in:
2025-07-03 23:58:20 +08:00
commit ce2b3cdfd4
444 changed files with 65256 additions and 0 deletions

View File

@ -0,0 +1,73 @@
#===============================================================================
# @brief cmake file
# Copyright (c) ShangHai High-flying Electronics Technology Co.,Ltd 2024-2024. All rights reserved.
#===============================================================================
set(COMPONENT_NAME "user_main")
set(SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/app_main.c
)
if (DEFINES MATCHES "HF_MCU_OTA")
list(APPEND SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/mcu_update.c)
endif()
set(PUBLIC_HEADER
${CMAKE_CURRENT_SOURCE_DIR}
)
set(PRIVATE_HEADER
)
set(PRIVATE_DEFINES
)
set(PUBLIC_DEFINES
)
# use this when you want to add ccflags like -include xxx
set(COMPONENT_PUBLIC_CCFLAGS #当前组件需要对外提供的编译选项
#-Wno-error=maybe-uninitialized #变量未初始化
#-Wno-error=pointer-sign #传递参数类型不匹配
#-Wno-error=char-subscripts #使用char类作为数组下标,char有符号
#-Wno-error=unused-label #未使用的标签比如用goto
#-Wno-error=strict-prototypes #函数的声明或定义没有参数
#-Wno-unused-parameter #未使用的函数参数
#-Wno-unused-variable #未使用的变量
#-Wno-unused-function #声明但未使用函数
#-Wno-unused-but-set-variable #设置了但未使用的变量
#-Wsign-compare #有符号与无符号比较
#-Wno-error=pointer-arith
#-Wno-error=sign-compare
#-Wno-error=jump-misses-init
#-Wno-error=incompatible-pointer-types
#-Wno-error=logical-op
#-Wno-error=empty-body
)
set(COMPONENT_CCFLAGS #当前组件内部生效的编译选项
-Wno-error=maybe-uninitialized #变量未初始化
-Wno-error=pointer-sign #传递参数类型不匹配
-Wno-error=char-subscripts #使用char类作为数组下标,char有符号
-Wno-error=unused-label #未使用的标签比如用goto
-Wno-error=strict-prototypes #函数的声明或定义没有参数
-Wno-unused-parameter #未使用的函数参数
-Wno-unused-variable #未使用的变量
-Wno-unused-function #声明但未使用函数
-Wno-unused-but-set-variable #设置了但未使用的变量
-Wsign-compare #有符号与无符号比较
-Wno-error=pointer-arith
-Wno-error=sign-compare
-Wno-error=jump-misses-init
-Wno-error=incompatible-pointer-types
-Wno-error=logical-op
-Wno-error=empty-body
)
set(WHOLE_LINK
true
)
set(MAIN_COMPONENT
true
)
build_component()

View File

@ -0,0 +1,255 @@
/* assis_thread.c
*
* Copyright (C) 2024 ShangHai High-flying Electronics Technology Co.,Ltd.
*
* This file is part of HSF.
*
*/
#include "hsf.h"
#ifdef HF_MCU_OTA
#include "mcu_update.h"
#endif
int g_module_id = HFM_TYPE_LPT262;
const int hf_lpt_262_gpio_fid_to_pid_map_table[HFM_MAX_FUNC_CODE]=
{
HFM_NOPIN, //HFGPIO_F_JTAG_TCK
HFM_NOPIN, //HFGPIO_F_JTAG_TDO
HFM_NOPIN, //HFGPIO_F_JTAG_TDI
HFM_NOPIN, //HFGPIO_F_JTAG_TMS
HFM_NOPIN, //HFGPIO_F_USBDP
HFM_NOPIN, //HFGPIO_F_USBDM
LPT26x_GPIO17, //HFGPIO_F_UART0_TX
HFM_NOPIN, //HFGPIO_F_UART0_RTS
LPT26x_GPIO18, //HFGPIO_F_UART0_RX
HFM_NOPIN, //HFGPIO_F_UART0_CTS
HFM_NOPIN, //HFGPIO_F_SPI_MISO
HFM_NOPIN, //HFGPIO_F_SPI_CLK
HFM_NOPIN, //HFGPIO_F_SPI_CS
HFM_NOPIN, //HFGPIO_F_SPI_MOSI
LPT26x_GPIO15, //HFGPIO_F_UART1_TX,
HFM_NOPIN, //HFGPIO_F_UART1_RTS,
LPT26x_GPIO16, //HFGPIO_F_UART1_RX,
HFM_NOPIN, //HFGPIO_F_UART1_CTS,
LPT26x_GPIO2, //HFGPIO_F_NLINK
LPT26x_GPIO10, //HFGPIO_F_NREADY
LPT26x_GPIO5, //HFGPIO_F_NRELOAD
HFM_NOPIN, //HFGPIO_F_SLEEP_RQ
HFM_NOPIN, //HFGPIO_F_SLEEP_ON
HFM_NOPIN, //HFGPIO_F_WPS
HFM_NOPIN, //HFGPIO_F_RESERVE1
HFM_NOPIN, //HFGPIO_F_RESERVE2
HFM_NOPIN, //HFGPIO_F_RESERVE3
HFM_NOPIN, //HFGPIO_F_RESERVE4
HFM_NOPIN, //HFGPIO_F_RESERVE5
HFM_NOPIN, //HFGPIO_F_USER_DEFINE
};
const int hf_lpt_263_gpio_fid_to_pid_map_table[HFM_MAX_FUNC_CODE]=
{
HFM_NOPIN, //HFGPIO_F_JTAG_TCK
HFM_NOPIN, //HFGPIO_F_JTAG_TDO
HFM_NOPIN, //HFGPIO_F_JTAG_TDI
HFM_NOPIN, //HFGPIO_F_JTAG_TMS
HFM_NOPIN, //HFGPIO_F_USBDP
HFM_NOPIN, //HFGPIO_F_USBDM
LPT26x_GPIO17, //HFGPIO_F_UART0_TX
HFM_NOPIN, //HFGPIO_F_UART0_RTS
LPT26x_GPIO18, //HFGPIO_F_UART0_RX
HFM_NOPIN, //HFGPIO_F_UART0_CTS
HFM_NOPIN, //HFGPIO_F_SPI_MISO
HFM_NOPIN, //HFGPIO_F_SPI_CLK
HFM_NOPIN, //HFGPIO_F_SPI_CS
HFM_NOPIN, //HFGPIO_F_SPI_MOSI
LPT26x_GPIO15, //HFGPIO_F_UART1_TX,
HFM_NOPIN, //HFGPIO_F_UART1_RTS,
LPT26x_GPIO16, //HFGPIO_F_UART1_RX,
HFM_NOPIN, //HFGPIO_F_UART1_CTS,
LPT26x_GPIO7, //HFGPIO_F_NLINK
LPT26x_GPIO8, //HFGPIO_F_NREADY
LPT26x_GPIO9, //HFGPIO_F_NRELOAD
HFM_NOPIN, //HFGPIO_F_SLEEP_RQ
HFM_NOPIN, //HFGPIO_F_SLEEP_ON
HFM_NOPIN, //HFGPIO_F_WPS
HFM_NOPIN, //HFGPIO_F_RESERVE1
HFM_NOPIN, //HFGPIO_F_RESERVE2
HFM_NOPIN, //HFGPIO_F_RESERVE3
HFM_NOPIN, //HFGPIO_F_RESERVE4
HFM_NOPIN, //HFGPIO_F_RESERVE5
HFM_NOPIN, //HFGPIO_F_USER_DEFINE
};
const int hf_lpt_162_gpio_fid_to_pid_map_table[HFM_MAX_FUNC_CODE]=
{
HFM_NOPIN, //HFGPIO_F_JTAG_TCK
HFM_NOPIN, //HFGPIO_F_JTAG_TDO
HFM_NOPIN, //HFGPIO_F_JTAG_TDI
HFM_NOPIN, //HFGPIO_F_JTAG_TMS
HFM_NOPIN, //HFGPIO_F_USBDP
HFM_NOPIN, //HFGPIO_F_USBDM
LPT26x_GPIO17, //HFGPIO_F_UART0_TX
HFM_NOPIN, //HFGPIO_F_UART0_RTS
LPT26x_GPIO18, //HFGPIO_F_UART0_RX
HFM_NOPIN, //HFGPIO_F_UART0_CTS
HFM_NOPIN, //HFGPIO_F_SPI_MISO
HFM_NOPIN, //HFGPIO_F_SPI_CLK
HFM_NOPIN, //HFGPIO_F_SPI_CS
HFM_NOPIN, //HFGPIO_F_SPI_MOSI
LPT26x_GPIO15, //HFGPIO_F_UART1_TX,
HFM_NOPIN, //HFGPIO_F_UART1_RTS,
LPT26x_GPIO16, //HFGPIO_F_UART1_RX,
HFM_NOPIN, //HFGPIO_F_UART1_CTS,
LPT26x_GPIO7, //HFGPIO_F_NLINK
LPT26x_GPIO8, //HFGPIO_F_NREADY
LPT26x_GPIO9, //HFGPIO_F_NRELOAD
HFM_NOPIN, //HFGPIO_F_SLEEP_RQ
HFM_NOPIN, //HFGPIO_F_SLEEP_ON
HFM_NOPIN, //HFGPIO_F_WPS
HFM_NOPIN, //HFGPIO_F_RESERVE1
HFM_NOPIN, //HFGPIO_F_RESERVE2
HFM_NOPIN, //HFGPIO_F_RESERVE3
HFM_NOPIN, //HFGPIO_F_RESERVE4
HFM_NOPIN, //HFGPIO_F_RESERVE5
HFM_NOPIN, //HFGPIO_F_USER_DEFINE
};
#ifdef ENABLE_BLE_SCAN
void hf_ble_scan_callback(gap_scan_result_data_t *scan_result_data)
{
// todo
return ;
}
int hfble_start_scan(void)
{
int ret = 0;
gap_ble_scan_params_t ble_scan_param = {
.scan_interval = 0x40, //30*8/5*2*2,
.scan_window = 0x20, //30*8/5/2,
.scan_type = 0x00,
.scan_phy = GAP_BLE_PHY_1M,
.scan_filter_policy = 0x00,
};
gap_ble_extern_scan_params_t ble_extern_scan_param = {
.filter_duplicate = 0,
.duration = 0,
.period = 0,
};
ret = gap_ble_set_scan_parameters(&ble_scan_param);
if(ret != 0){
HILINK_SAL_DEBUG("gap_ble_set_scan_parameters failed, errcode = 0x%X\r\n", ret);
return -1;
}
ret = gap_ble_set_scan_extern_parameters(&ble_extern_scan_param);
if(ret != 0){
HILINK_SAL_DEBUG("gap_ble_set_scan_extern_parameters failed, errcode = 0x%X\r\n", ret);
return -1;
}
ret = gap_ble_start_scan();
if(ret != 0){
HILINK_SAL_DEBUG("gap_ble_start_scan failed, errcode = 0x%X\r\n", ret);
return -1;
}
HILINK_SAL_DEBUG("*****************ble start scan*****************\r\n");
return 0;
}
#endif
static int USER_FUNC uart_recv_callback(uint32_t event,char *data,uint32_t len,uint32_t buf_len)
{
if(data == NULL)
return 0;
if(event == HFNET_UART0_DATA_READY)
HF_Debug(DEBUG_LEVEL_LOW,"[%d]uart 0 recv %d bytes data %d\n",event,len,buf_len);
else if(event == HFNET_UART1_DATA_READY)
HF_Debug(DEBUG_LEVEL_LOW,"[%d]uart 1 recv %d bytes data %d\n",event,len,buf_len);
if(hfsys_get_run_mode() == HFSYS_STATE_RUN_CMD)
return len;
return len;
}
void user_app_init(void)
{
u_printf("user app init\r\n");
}
#define APPVER "1.0.0"
int hf_atcmd_appver(pat_session_t s,int argc,char *argv[],char *rsp,int len)
{
if(argc == 0)
{
sprintf(rsp,"=LPT262_Harmonyos_%s_%s",APPVER,"20250227");
return 0;
}
return -3;
}
const hfat_cmd_t user_define_at_cmds_table[]=
{
{"APPVER", hf_atcmd_appver, "AT+APPVER: get version. \r\n",NULL},
#ifdef HF_MCU_OTA
{"MCUOTA", hf_atcmd_mcu, "AT+MCUOTA: start mcu ota. \r\n",NULL},
{"MCUVER", hf_atcmd_mcuver, "AT+MCUVER: get mcu version. \r\n",NULL},
{"OTAWAITTIME", hf_atcmd_ota_wait_time, "AT+OTAWAITTIME: set/get uart send mcu ota time\r\n", NULL},
#endif
{NULL, NULL, NULL, NULL} //the last item must be null
};
int USER_FUNC user_app_main(void)
{
//AT+UART uart0
if(hfnet_start_uart(HFTHREAD_PRIORITIES_LOW,(hfnet_callback_t)uart_recv_callback)!=HF_SUCCESS)
{
HF_Debug(DEBUG_WARN,"start uart fail!\r\n");
}
#ifdef HF_MCU_OTA
if(hf_mcu_init() != HF_SUCCESS)
HF_Debug(DEBUG_WARN,"init mcu ota fail!\r\n");
#endif
//See Wi-Fi Config tools APP for detailed usage of this thread, only debug mode open
if(hfdbg_get_level())
{
while(!hfnet_wifi_is_active())
{
msleep(50);
}
if(hfnet_start_assis(ASSIS_PORT)!=HF_SUCCESS)
{
HF_Debug(DEBUG_WARN,"start assis fail\r\n");
}
}
return 1;
}

View File

@ -0,0 +1,259 @@
#include "mcu_update.h"
#include "string.h"
#include "hilink_open_ota_mcu_adapter.h"
#include "hilink_device.h"
mcu_info_t hf_mcu_info = {0};
unsigned char hf_mcu_step = OTA_STEP_NULL;
static int ota_wait_time=100;
int hf_atcmd_mcu(pat_session_t s,int argc,char *argv[],char *rsp,int len)
{
if(argc==0)
{
if(hf_mcu_info.ota == 0)
sprintf(rsp,"=%s","off");
else if(hf_mcu_info.ota == 1)
sprintf(rsp,"=%s","on");
return 0;
}
else if(argc==1)
{
if(strcasecmp(argv[0],"on") == 0)
{
if(hf_mcu_info.ota == 0)
{
hf_mcu_info.ota=1;
hf_save_mcu_info();
}
return 0;
}
else if(strcasecmp(argv[0],"off") == 0)
{
if(hf_mcu_info.ota == 1)
{
hf_mcu_info.ota=0;
hf_save_mcu_info();
}
return 0;
}
return -4;
}
return -3;
}
int hf_atcmd_mcuver(pat_session_t s,int argc,char *argv[],char *rsp,int len)
{
if(argc == 1)
{
if(strlen(argv[0]) > 10)
return -4;
hf_set_mcu_version((char *)argv[0]);
return 0;
}
return -3;
}
int hf_atcmd_ota_wait_time(pat_session_t s,int argc,char *argv[],char *rsp,int len)
{
if(argc == 0)
{
sprintf(rsp,"=%d",ota_wait_time);
return 0;
}
else if(argc == 1)
{
int time_wait=atoi(argv[0]);
if(time_wait<SEND_UART_WAIT_TIME_MIN || time_wait>SEND_UART_WAIT_TIME_MAX)
return -4;
ota_wait_time=time_wait;
if(hf_mcu_info.wait_time != time_wait)
{
hf_mcu_info.wait_time=ota_wait_time;
hf_save_mcu_info();
}
return 0;
}
return -3;
}
const char *hf_get_mcu_ver(void)
{
if(hf_mcu_info.head != MCU_INFO_HEAD)
{
memset(&hf_mcu_info, 0, sizeof(hf_mcu_info));
strcpy((char *)hf_mcu_info.ver, MCU_VER);
hf_save_mcu_info();
}
return (const char *)hf_mcu_info.ver;
}
void hf_set_mcu_version(char *ver)
{
if(strlen(ver) >= MCU_VER_MAX_SIZE)
return ;
strcpy((char *)hf_mcu_info.ver,ver);
if(hf_mcu_info.head != MCU_INFO_HEAD || strcmp((char *)hf_mcu_info.ver,ver) != 0)
{
memset((char *)hf_mcu_info.ver,0,sizeof(hf_mcu_info.ver));
strcpy((char *)hf_mcu_info.ver,ver);
hf_save_mcu_info();
}
u_printf("get mcu ver:%s\r\n",hf_mcu_info.ver);
}
int hf_save_mcu_info(void)
{
hf_mcu_info.head=MCU_INFO_HEAD;
hfuflash_erase_page(MCU_INFO_FILE,1);
hfuflash_write(MCU_INFO_FILE,&hf_mcu_info,sizeof(mcu_info_t));
return 0;
}
static int hf_mcu_ota_is_vaild(void)
{
return hf_mcu_info.ota;
}
void hf_save_mcu_status(unsigned char flag, int len)
{
if(flag == START_SEND_DATA)
{
u_printf("ready to recv mcu firmware...\n");
}
else if(flag == STOP_SEND_DATA)
{
u_printf("recv mcu firmware over...\n");
hf_mcu_info.updateFlag = 1;
hf_mcu_info.len = len;
hf_save_mcu_info();
}
else if(flag == SEND_DATA_ERROR)
{
u_printf("recv mcu firmware error...\n");
}
}
static void mcu_upgrade_thread(void *arg)
{
int i = 0;
int ret = 0;
int seq = 0;
int offset = 0;
unsigned char *buff = hfmem_malloc(512);
unsigned char *data = hfmem_malloc(1024);
if(data == NULL || buff == NULL)
return ;
u_printf("mcu ver:%s\r\n",hf_mcu_info.ver);
if((hf_mcu_info.updateFlag > 0) && (hf_mcu_info.len > 0))
hf_mcu_step = OTA_STEP_START;
if(hf_mcu_info.wait_time >= SEND_UART_WAIT_TIME_MIN && hf_mcu_info.wait_time <= SEND_UART_WAIT_TIME_MAX)
ota_wait_time=hf_mcu_info.wait_time;
else
ota_wait_time=100;
while(1)
{
switch (hf_mcu_step)
{
case OTA_STEP_START:
{
memset(buff, 0, 512);
if(HILINK_GetDevStatus() == HILINK_M2M_CLOUD_ONLINE)
{
sprintf((char *)buff, "AT+OTASTART=%d\r\n", hf_mcu_info.len);
hfuart_send(HFUART0,buff, strlen((char *)buff),0);
u_printf("mcu ota OTA_STEP_START\r\n");
hf_mcu_step += 0x01;
}
break;
}
case OTA_STEP_TRANS:
{
memset(buff, 0, 512);
memset(data, 0, 1024);
if(hf_mcu_info.len - offset >= OTA_READ_LEN)
{
flash_read(UPGRADE_ADDRESS + offset, buff, OTA_READ_LEN,0);
offset += OTA_READ_LEN;
seq += 0x01;
u_printf("mcu send data count :%d\r\n",offset/OTA_READ_LEN);
sprintf((char *)data, "AT+OTADATA=%d,%d,", seq, OTA_READ_LEN);
ret = strlen((char *)data);
for(i = 0; i < OTA_READ_LEN; i++)
sprintf((char *)data + ret + 2 * i, "%02x", buff[i]);
strncat((char *)data, "\r\n", 2);
hfuart_send(HFUART0,data, ret + (OTA_READ_LEN * 2) + 2,0);
}
else
{
flash_read(UPGRADE_ADDRESS + offset, buff, hf_mcu_info.len - offset,0);
seq += 0x01;
sprintf((char *)data, "AT+OTADATA=%d,%d,", seq, hf_mcu_info.len - offset);
ret = strlen((char *)data);
for(i = 0; i < (hf_mcu_info.len - offset); i++)
sprintf((char *)data + ret + 2 * i, "%02x", buff[i]);
strncat((char *)data, "\r\n", 2);
hfuart_send(HFUART0,data, ret + ((hf_mcu_info.len - offset) * 2) + 2,0);
offset = hf_mcu_info.len;
hf_mcu_step += 0x01;
}
break;
}
case OTA_STEP_END:
{
hfuart_send(HFUART0,(unsigned char *)"AT+OTAEND\r\n", strlen("AT+OTAEND\r\n"),0);
seq = 0;
offset = 0;
hf_mcu_step = OTA_STEP_NULL;
hf_mcu_info.updateFlag = 0;
hf_save_mcu_info();
goto EXIT;
break;
}
case OTA_STEP_ERROR:
{
hfuart_send(HFUART0,(unsigned char *)"AT+OTAERR\r\n", strlen("AT+OTAERR\r\n"),0);
hf_mcu_step = OTA_STEP_NULL;
goto EXIT;
break;
}
default:
break;
}
msleep(ota_wait_time);
}
EXIT:
hfmem_free(data);
hfmem_free(buff);
hfthread_destroy(0);
}
void hf_mcu_thread(void)
{
hfthread_create(mcu_upgrade_thread, "hf-mcu-thread", 1024, NULL, HFTHREAD_PRIORITIES_LOW, NULL, NULL);
}
int hf_mcu_init(void)
{
hfuflash_read(MCU_INFO_FILE,&hf_mcu_info,sizeof(mcu_info_t));
if(hf_mcu_ota_is_vaild())
{
return -1;
}
char *get_mcu_ver="AT+GETMCUVER\r\n";
hfuart_send(HFUART0,(unsigned char *)get_mcu_ver, strlen(get_mcu_ver),0);
if(hf_mcu_info.updateFlag && hf_mcu_info.len)
hf_mcu_thread();
return HF_SUCCESS;
}

View File

@ -0,0 +1,49 @@
#ifndef MCU_UPDATE_H
#define MCU_UPDATE_H
#include <stdint.h>
#include "hsf.h"
#define MCU_INFO_HEAD 0xA5A5
#define MCU_INFO_FILE 0
#define MCU_VER "1.0.0"
#define OTA_READ_LEN 256
#define OTA_STEP_NULL 0
#define OTA_STEP_START 1
#define OTA_STEP_TRANS 2
#define OTA_STEP_END 3
#define OTA_STEP_ERROR 4
#define MCU_VER_MAX_SIZE 8
#define SEND_UART_WAIT_TIME_MIN 20
#define SEND_UART_WAIT_TIME_MAX 500
#pragma pack(push)
#pragma pack(1)
typedef struct{
uint16_t head;
unsigned char ota;
unsigned char updateFlag;
uint16_t wait_time;
unsigned char ver[MCU_VER_MAX_SIZE];
unsigned int len;
unsigned char reserve[5];
}mcu_info_t;
#pragma pack(pop)
const char *hf_get_mcu_ver(void);
void hf_save_mcu_status(unsigned char flag, int len);
void hf_mcu_thread(void);
void hf_set_mcu_version(char *ver);
int hf_mcu_init(void);
int hf_save_mcu_info(void);
int hf_atcmd_mcu(pat_session_t s,int argc,char *argv[],char *rsp,int len);
int hf_atcmd_mcuver(pat_session_t s,int argc,char *argv[],char *rsp,int len);
int hf_atcmd_ota_wait_time(pat_session_t s,int argc,char *argv[],char *rsp,int len);
#endif