mcu_hi3321_watch/tjd/ble/firmware_ota/ble_ota_execute.h
2025-05-26 20:15:20 +08:00

144 lines
4.6 KiB
C
Raw 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.

#ifndef _OTA_EXECUTE_H_
#define _OTA_EXECUTE_H_
#include <stdint.h>
#include <stdbool.h>
#include "wsf_types.h"
#include "rom_api.h"
//
// Fixed external SPI Flash storage start address to be defined by user.
//
#define SYS_PARA_BP_ADDR (0) // System parameter backup area
#define OTA_PARA_ADDR (1020*1024) //OTA parameter area
#define FACTOYR_FIRMWARE_ADDR (4*1024 + OTA_PARA_ADDR) //Factory test area
#define COMMERCIAL_FIRMWARE_ADDR (1024*1024 + FACTOYR_FIRMWARE_ADDR) //Commercial firmware area
#define RES_STRUCT_ADDR (0x3FF000) //resource parameter area
#define TP_FIRMWARE_ADDR (2*1024*1024 + COMMERCIAL_FIRMWARE_ADDR) // TP firmware area
#define GPS_FIRMWARE_ADDR (64*1024 + TP_FIRMWARE_ADDR) // GPS firmware area
#define RESOURCE_FILE_ADDR (960*1024 + GPS_FIRMWARE_ADDR) // resource file area
#define OTA_BATT_THRESHOLD 10 // 电量百分比
//#define FW_HEAD_INFO_IEN 256 // 固件头信息长度
#define FW_HEAD_INFO_IEN 512 // 固件头信息长度
#define APPLY_FW_DATA_LEN 4096 // 设备向APP申请的数据长度
#define AMOTAS_TEMP_BUFSIZE ( MCU_ROM_PAGE_SIZE ) //AM_HAL_FLASH_PAGE_SIZE
//#define AMOTAS_TEMP_BUFSIZE ( 512 ) //AM_HAL_FLASH_PAGE_SIZE
//
// ota state
//
typedef enum
{
OTA_STATE_IDLE = 0,
OTA_STATE_FW_HEADER,
OTA_STATE_FW_DATA,
OTA_STATE_FW_VERIFY,
OTA_STATE_FW_RESET,
OTA_STATE_MAX,
}eotaState;
enum
{
OTA_PARAM_EROOR = 0x80, //OTA 输入参数为空 非法数据
BATT_LOW_OTA_ERROR, //OTA 电池电量低
DEVICE_FW_OTA_ERROR, //OTA 固件异常
FLASH_ERASE_OTA_ERROR, //OTA 设备擦除FLASH 失败
DEVICE_FORBIT_OTA_ERROR, //OTA 设备禁止升级
DATA_OTA_ERROR, //OTA 文件传输过程中失败
DOWNLOAD_OTA_ERROR, //OTA 蓝牙下载数据失败
UPDATA_OTA_ERROR, //OTA 蓝牙升级失败
FLASH_WRITE_OTA_ERROR, //OTA 蓝牙写数据失败
DATA_CRC_OTA_ERROR, //OTA 数据校验失败
DATA_PACK_SIZE_ERROR, //OTA 设备检测升级包大小异常
PACK_WAIE_TIMEOUT_ERROR, //OTA 设备等待固件包超时
DEVICE_DISCONNECT, //OTA 设备与APP断开蓝牙连接
USER_CANCEL_OTA, //OTA 用户取消升级
};
typedef struct
{
uint32_t FileSize; // FW len + FW head len
uint32_t offset; // pkt offset
uint32_t ApplyLen; // pkt length applied to app
}
NewFwPacket_t;
//
// Data structure for New firmware to storage
//
typedef struct
{
uint32_t addr;
uint32_t offset;
}NewFwFlashInfo_t;
//
// FW header information
//
typedef struct
{
uint32_t encrypted;
uint32_t fwStartAddr; // Address to install the image
uint32_t fwLength;
uint32_t fwCrc;
uint32_t secInfoLen;
uint32_t storageAddr;
uint32_t resvd2;
uint32_t version[2];
uint32_t fwDataType; //binary type
uint32_t storageType;
uint32_t resvd4;
}HeaderInfo_t;
//
// OTA control block
//
typedef struct
{
HeaderInfo_t HeaderInfo; //文件头信息,打包方式可以变化
NewFwPacket_t pkt;
NewFwFlashInfo_t newFwFlashInfo;
eotaState ota_status; //
bool_t ota_start_flag; //ota is start or stop
bool_t crc_verify; //5.9.4收完所有数据后crc校验结果
bool_t ota_reset_flag; //ota校验完成复位断开前置1防止断连后到复位前的这段时间其他线程又能写flash
}OtaCtrlBlock_t;
//
// Data structure for flash operation
//
typedef struct
{
uint8_t writeBuffer[AMOTAS_TEMP_BUFSIZE] __attribute__((aligned(4))); // needs to be 32-bit word aligned.
uint16_t bufferIndex;
}otasFlashOp_t;
extern otasFlashOp_t otaFlashOp;
void OtaTimeoutReset(void);
void OtaTimeoutTimerStop(void);
void OtaCreatTimeoutTimer(void);
void OtaFrameResendTimerLoad(void);
void OtaFrameResendTimerStop(void);
bool_t OtaIsStarted(void);
void GetOtaCtrlBlock(OtaCtrlBlock_t *ctrl);
uint8_t OtaRequest(uint32_t FwLen);
void OtaStop(void);
void OtaFailed(uint8_t reason);
void OtaSucceed(void);
void OtaStart(void);
uint32_t OtaPacketHandler(uint16_t len, uint8_t *buf);
void OtaUpdateFwInfo(void);
#endif