Files
kunlun/driver/extern/virtualchannel/vc_upgrade/vc_upgrade_driver.h
2024-09-28 14:24:04 +08:00

213 lines
6.6 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.
****************************************************************************/
#ifndef _VC_UPGRADE_DRIVER_H
#define _VC_UPGRADE_DRIVER_H
#include "os_types_api.h"
#include "iot_io_api.h"
#include "iot_module_api.h"
#define IOT_STM32_MODULE_ID IOT_DRIVER_MID
/* maximum length of read memory cmd, unit is byte */
#define STM32_MAX_RX_PAYLOAD_LENGTH 256
/* maximum length of write memory cmd, unit is byte */
#define STM32_MAX_TX_PAYLOAD_LENGTH (STM32_MAX_RX_PAYLOAD_LENGTH)
#ifndef STM32_DEBUG
#define STM32_DEBUG 0
#endif
#if STM32_DEBUG
#define stm32_dbg_printf(fmt, ...) do{\
iot_printf("[STM32 UPGRD DEBUG]L%04d@%s:" fmt,\
__LINE__, __FUNCTION__, ##__VA_ARGS__);\
}while(0)
#else
#define stm32_dbg_printf(fmt, ...)
#endif
typedef enum {
STM32_ERR_OK = 0,
/* Generic error */
STM32_ERR_UNKNOWN,
STM32_ERR_NACK,
/* Command not available in bootloader */
STM32_ERR_NO_CMD,
} stm32_err_t;
/* upgrade status, only for state machines */
typedef enum _internal_upgrade_status_t {
/* handshake command */
UPGRADE_SEND_INIT = 1,
/* get version command */
UPGRADE_SEND_GET_VERSION,
/* get info command */
UPGRADE_SEND_GET,
/* get chip id command */
UPGRADE_SEND_GET_ID,
/* erase memory command */
UPGRADE_SEND_ERASE,
/* send erase memory command */
UPGRADE_SEND_ERASE_DATA,
/* send start page and page number */
UPGRADE_SEND_ERASE_PAGE_INFO,
/* write data command */
UPGRADE_SEND_WRITE,
/* send write addr */
UPGRADE_SEND_WRITE_ADDR,
/* send write data */
UPGRADE_SEND_WRITE_DATA,
/* read data command */
UPGRADE_SEND_READ,
/* send read addr */
UPGRADE_SEND_READ_ADDR,
/* send read lenght */
UPGRADE_SEND_READ_LENGTH,
/* send readout protect cmd, we'll get two ACK */
UPGRADE_SEND_PROTECT,
/* send readout unprotect cmd, we'll get two ACK */
UPGRADE_SEND_UNPROTECT,
/* invalid value */
UPGRADE_STATE_MAX,
} internal_upgrade_status_t;
typedef struct _stm32_cmd_t {
uint8_t get;
uint8_t gvr;
uint8_t gid;
uint8_t rm;
uint8_t go;
uint8_t wm;
uint8_t er; /* this may be extended erase */
uint8_t wp;
uint8_t uw;
uint8_t rp;
uint8_t ur;
uint8_t crc;
} stm32_cmd_t;
typedef struct _stm32_dev_t {
uint16_t id;
const char *name;
uint32_t ram_start;
uint32_t ram_end;
uint32_t fl_start;
uint32_t fl_end;
/* pages per sector */
uint16_t fl_pps;
/* page size */
uint32_t *fl_ps;
uint32_t opt_start;
uint32_t opt_end;
uint32_t mem_start;
uint32_t mem_end;
uint32_t flags;
} stm32_dev_t;
/* define the communication interface(serial port) between K68 and stm32 */
typedef struct _port_interface_t {
uint32_t (*read)(uint8_t *buf, uint32_t nbyte);
uint32_t (*write)(uint8_t *buf, uint32_t nbyte);
} port_interface_t;
typedef struct _stm32_t {
port_interface_t *port;
uint8_t bl_version;
uint8_t version;
uint8_t option1;
uint8_t option2;
uint16_t pid;
stm32_cmd_t *cmd;
const stm32_dev_t *dev;
uint8_t is_connect;
uint8_t state;
} stm32_t;
/**
* @brief is_addr_in_ram - determine whether the address is in RAM space.
* @return 0 -- addr is not in ram space.
* @return 1 -- addr is in ram space.
*/
int is_addr_in_ram(const stm32_t *stm, uint32_t addr);
/**
* @brief is_addr_in_flash - determine whether the address is in flash space.
* @return 0 -- addr is not in flash space.
* @return 1 -- addr is in flash space.
*/
int is_addr_in_flash(const stm32_t *stm, uint32_t addr);
/**
* @brief is_addr_in_opt_bytes - determine whether the address is in option space.
* @return 0 -- addr is not in option bytes space.
* @return 1 -- addr is in option bytes space.
*/
int is_addr_in_opt_bytes(const stm32_t *stm, uint32_t addr);
/**
* @brief is_addr_in_sysmem - determine whether the address is in sysmem space.
* @return 0 -- addr is not in system space.
* @return 1 -- addr is in system space.
*/
int is_addr_in_sysmem(const stm32_t *stm, uint32_t addr);
/**
* @brief flash_addr_to_page_floor - returns the page that contains address "addr"
* @return page number
*/
int flash_addr_to_page_floor(const stm32_t *stm, uint32_t addr);
/**
* @brief flash_addr_to_page_ceil - returns the first page
* whose start addr is >= "addr"
* @return page number
*/
int flash_addr_to_page_ceil(const stm32_t *stm, uint32_t addr);
/**
* @brief flash_page_to_addr - returns the lower address of flash page "page"
* @return flash addr
*/
uint32_t flash_page_to_addr(const stm32_t *stm, int page);
stm32_err_t stm32_get_ack(const stm32_t *stm);
stm32_err_t stm32_send_init_cmd(const stm32_t *stm);
stm32_err_t stm32_send_init_handle(const stm32_t *stm);
stm32_err_t stm32_send_get_version_cmd(const stm32_t *stm);
stm32_err_t stm32_send_get_version_handle(stm32_t *stm);
stm32_err_t stm32_send_get_cmd(const stm32_t *stm);
stm32_err_t stm32_send_get_handle(stm32_t *stm);
stm32_err_t stm32_send_get_id_cmd(const stm32_t *stm);
stm32_err_t stm32_send_get_id_handle(stm32_t *stm);
stm32_err_t stm32_send_erase_cmd(const stm32_t *stm);
stm32_err_t stm32_send_erase_page_info(const stm32_t *stm, uint32_t spage,
uint32_t pages);
stm32_err_t stm32_send_write_cmd(const stm32_t *stm);
stm32_err_t stm32_send_write_addr(const stm32_t *stm, uint32_t address);
stm32_err_t stm32_send_write_data(const stm32_t *stm,
const uint8_t data[], unsigned int len);
stm32_err_t stm32_send_read_cmd(const stm32_t *stm);
stm32_err_t stm32_send_read_addr(const stm32_t *stm, uint32_t address);
stm32_err_t stm32_send_read_length(const stm32_t *stm, uint32_t len);
stm32_err_t stm32_send_read_rdata(const stm32_t *stm,
uint8_t data[], uint32_t len);
#endif //_VC_UPGRADE_DRIVER_H