#ifndef dev_flash_h__
#define dev_flash_h__


#include "stdint.h"



#pragma pack (1)
typedef struct{
  uint32_t file_size;//文件长度，不包括文件名
  char name[120];
  int used; // 初始值为0xffffffff，每使用一次添加一个0
}flash_file;
#pragma pack ()
#define FLASH_FILE_HEAD_SIZE  128



int flash_erase_param(void);
int flash_erase_app1(void);
int flash_erase_app2(void);
int flash_erase_slave(void);
int flash_erase_scheme(void);
int flash_erase_lua(void);
int flash_erase_jwtcode(void);

uint32_t *flash_get_param(void);
uint32_t *flash_get_app2(void);
uint32_t *flash_get_slave(void);
uint32_t *flash_get_scheme(void);
uint32_t *flash_get_lua(void);
uint32_t *flash_get_jwtcode(void);

int flash_write(uint8_t *addr,const uint8_t *data,int len);
int flash_operate_end(void);
int flash_write_with_erase(uint8_t *addr,const uint8_t *data,int len);
int flash_erase_addr(uint32_t addr);




#pragma pack (1)
typedef struct{
  uint32_t size;
  char pack_time[20];
  uint32_t crc;
  uint8_t local_ip[4];
  uint8_t host_ip[4];
  int host_port;
  char host_if[8];
  char device_type[12];
}rom_head;
#pragma pack ()

#define ROM_HEAD_SIZE 128


uint8_t *flash_get_rom(rom_head **head);

int flash_updata_app(uint8_t *rom,uint32_t size);
int flash_updata_boot(uint8_t *rom,uint32_t size);




// 系统参数
#pragma pack (1)
typedef struct{
  char pack_time[20];// 打包时间
  char host_if[8];// 主机接口
  char device_type[12];// 设备类型 ，批检仪还是赋码仪
  uint8_t mac[8];// mac地址
  int local_id;// 本机识别号
  int uartbsp;// 串口的波特率
  int hard_version;// 硬件版本号
  int resistor_diff;// 电阻校准值
}sys_param_def;
#pragma pack ()


int flash_save_param(sys_param_def *par);
const sys_param_def *sys_param(void);

#pragma pack (1)
#if 1
typedef struct{
  uint16_t max;
  uint16_t min;
  uint8_t err;
}scheme_range_def;
typedef struct 
{
  uint8_t taskid;
  uint8_t taskindex;
  uint8_t item_num;
  uint8_t err;
  scheme_range_def range[16];
}scheme_task_def;
#else
__packed
typedef struct{
  uint32_t max;
  uint32_t min;
  uint32_t err;
}scheme_range_def;
__packed
typedef struct 
{
  uint32_t taskid;
  uint32_t taskindex;
  uint32_t item_num;
  uint32_t err;
  scheme_range_def range[16];
}scheme_task_def;
#endif
typedef struct{
  uint8_t err;
  uint8_t suberr_num;
  uint8_t suberr[30];
}marerr_def;
// 方案参数
typedef struct{
  uint8_t slave_data[2048];
  uint32_t plan_id;
  uint32_t timeout_m;
  uint32_t task_num;
  uint32_t marerr_num;
  marerr_def marerr[21];
  scheme_task_def task[0];
}scheme_def;
#pragma pack ()


const scheme_def *check_scheme(void);


#endif

