402 lines
		
	
	
		
			17 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			402 lines
		
	
	
		
			17 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
#ifndef IOT_DEV_MGMT_FLASH_DATA_H
 | 
						||
#define IOT_DEV_MGMT_FLASH_DATA_H
 | 
						||
 | 
						||
/*
 | 
						||
----------------------------------------------------------------------
 | 
						||
客户数据类型取值值范围:
 | 
						||
A.1 : 秒级时间,占6Bytes;
 | 
						||
A.5 : -799.9~799.9,占2Bytes;
 | 
						||
A.7 : 0~999,占2Bytes;
 | 
						||
A.9 : -39.9999~39.9999 or -399.999~399.999,占3Bytes;
 | 
						||
A.11: 0~999999.99,占4Bytes;
 | 
						||
A.25:-799.999~799.999,占3Bytes。
 | 
						||
----------------------------------------------------------------------
 | 
						||
FLASH必须数据有:1. 阈值;2.表底值;            3. 事件;
 | 
						||
                 4.负荷记录(15分钟曲线,每15分钟采集一次负荷数据)
 | 
						||
详细数据:
 | 
						||
1. 阈值
 | 
						||
    A. 待机电流(A.25),工作电流(A.25),额定功率(A.9)
 | 
						||
2. 表底值
 | 
						||
    A. 表底值(A.11)
 | 
						||
3. 事件(C~F项使用单相/三相)
 | 
						||
    A. 类型(1byte),B. 时间(A.1),C. 电压(A.7),D. 电流(A.25),
 | 
						||
    E. 有功功率(A.9),F. 功率因数(A.5),G. 表底值(A.11)
 | 
						||
4. 负荷记录(同第3项中的B~G项)
 | 
						||
 | 
						||
    其中1、2项数只有一个数据记录,可按实际占用字节数(对其)分配空间大小。
 | 
						||
第3项是在每个事件发生时,记录当前负荷数据,有N个数据记录,此部分数据量较大。
 | 
						||
第4项是每15分钟记录一次的负荷数据,有N个数据记录,此部分数据量较大。
 | 
						||
    所以对于FLASH存储的数据,只需要对第3项与第4项的数据进行空间优化。对比两者
 | 
						||
数据,发现只需要优化负荷记录数据即可。
 | 
						||
----------------------------------------------------------------------
 | 
						||
负荷数据:
 | 
						||
        数据量       字节数       对齐字节数
 | 
						||
时间 :    F FF FF     2.5         4
 | 
						||
电压 :    FF FF       2           2
 | 
						||
电流 :    F FF FF     2.5         4
 | 
						||
功率 :    F FF FF     2.5         4
 | 
						||
因数 :    FF FF       2           2
 | 
						||
表底 :    F FF FF FF  3.5         4
 | 
						||
 | 
						||
总计 :           16(ROUNDUP(15))  20
 | 
						||
 | 
						||
    由以上统计,负荷数据若仅按照字节数紧凑排列,每条记录占用16字节,若按照对齐
 | 
						||
方式排列,则需要20字节,2者相差25%的数据量。按照3相,半年,每天2事件的频率来算,
 | 
						||
数据量相差206KB左右。若需要省空间,后期可以优化。
 | 
						||
----------------------------------------------------------------------
 | 
						||
 | 
						||
FLASH LAYOUT:
 | 
						||
 | 
						||
 ---  -------------------------------
 | 
						||
*       |      flash header         |  Descript for flash.
 | 
						||
*       |---------------------------|
 | 
						||
*       |   event ctrl blk desc     |  Descript for event control block.
 | 
						||
*       |---------------------------|
 | 
						||
*       |   load ctrl blk desc      |  Descript for load control block.
 | 
						||
*       |---------------------------|
 | 
						||
*       |   event rec blk desc      |  Descript for event record block.
 | 
						||
*       |---------------------------|
 | 
						||
*       |   load rec blk desc       |  Descript for load record block.
 | 
						||
*       |---------------------------|
 | 
						||
*       |   prv event ctrl blk desc |  Descript for private event control block.
 | 
						||
*       |---------------------------|
 | 
						||
*       |   prv event rec blk desc  |  Descript for private event record block.
 | 
						||
*       |---------------------------|
 | 
						||
*       |            ...            |  Reserved space.
 | 
						||
*       |---------------------------|
 | 
						||
*       |    event day item         |  Control block for event day item.
 | 
						||
*       |---------------------------|
 | 
						||
*       |    load day item          |  Control block for load day item.
 | 
						||
*       |---------------------------|
 | 
						||
*       |   event record            |  Record block for event.
 | 
						||
*       |---------------------------|
 | 
						||
*       |   load record             |  Record block for load.
 | 
						||
*       |---------------------------|
 | 
						||
*       | prv event day item        |  Control block for private event day item.
 | 
						||
*       |---------------------------|
 | 
						||
*       | prv event record          |  Record block for private event.
 | 
						||
*       |---------------------------|
 | 
						||
*       |           ...             |  Reserved space.
 | 
						||
*       |---------------------------|
 | 
						||
*       | undefined prv blk         |  Some other private block.
 | 
						||
*  ---  |---------------------------|
 | 
						||
 | 
						||
*/
 | 
						||
/* ******************** common ******************* */
 | 
						||
 | 
						||
/* Length of chip manufacturer name. */
 | 
						||
#define IOT_DM_MANUFACTURER_NAME_LEN        17
 | 
						||
 | 
						||
/* Flash / sector / page size. */
 | 
						||
#define IOT_DM_SIZE_nB(n)           (n)
 | 
						||
#define IOT_DM_SIZE_nKB(n)          (1024 * (n))
 | 
						||
#define IOT_DM_SIZE_nMB(n)          (1024 * 1024 * (n))
 | 
						||
 | 
						||
/* Description of a flash block. */
 | 
						||
typedef struct _iot_dm_flash_block_descriptor_t {
 | 
						||
    uint32_t    start;  /* Start of block, offset from base of flash chip. */
 | 
						||
    uint32_t    size;   /* Size in bytes. */
 | 
						||
}iot_dm_flash_block_desc_t;
 | 
						||
 | 
						||
/* Description of a flash block used. */
 | 
						||
typedef struct _iot_dm_flash_block_usage_descriptor_t {
 | 
						||
    uint32_t    start;  /* Start of block, offset from base of flash chip. */
 | 
						||
    uint32_t    size;   /* Size in bytes. */
 | 
						||
    uint32_t    used_size;   /* Used size in bytes. */
 | 
						||
}iot_dm_flash_block_usage_desc_t;
 | 
						||
 | 
						||
/* Information of this flash chip. */
 | 
						||
typedef struct _iot_dm_flash_chip_information_t {
 | 
						||
    /* Manufacturer name */
 | 
						||
    uint8_t     manufacturer[IOT_DM_MANUFACTURER_NAME_LEN];
 | 
						||
    /* Manufacturer ID. */
 | 
						||
    uint8_t     m_id;
 | 
						||
    /* Device ID */
 | 
						||
    uint16_t    d_id;
 | 
						||
    /* Size of flash. */
 | 
						||
    uint32_t    chip_size;
 | 
						||
    /* Erase sector size. */
 | 
						||
    uint32_t    sec_size;
 | 
						||
    /* Read / write page size. */
 | 
						||
    uint32_t    page_size;
 | 
						||
}iot_dm_flash_chip_t;
 | 
						||
 | 
						||
/* Type of blocks in flash chip. */
 | 
						||
typedef enum _iot_dm_flash_block_type_e {
 | 
						||
    /*
 | 
						||
    This block holds information about every block, like offset from flash base,
 | 
						||
    and block size, also some variable which with value changed hardly.
 | 
						||
    If this block damaged, only this block will be erased and restored with
 | 
						||
    default values. See iot_dm_flash_t.
 | 
						||
    */
 | 
						||
    DM_FLASH_BLOCK_HEADER,          /* Block of chip header. */
 | 
						||
 | 
						||
    /*
 | 
						||
    This block holds the value of some variables that changed easily. Considering
 | 
						||
    about times for erasing/writing flash are limited, a new variable will not
 | 
						||
    be written on the fixed position on chip but a new offset next to the old
 | 
						||
    position. When the last position was written, the first position will be written
 | 
						||
    for next new variable. Every time system boots up, flash module will search
 | 
						||
    the newest variables on the flash.
 | 
						||
    If this block damaged, all records (data of event / load) will be lost,
 | 
						||
    because there are some variables designed to tell the information about every
 | 
						||
    control-block. See iot_dm_vari_t.
 | 
						||
    */
 | 
						||
    DM_FLASH_BLOCK_VARIABLE,        /* Variable block, for data easy to change. */
 | 
						||
 | 
						||
    /*
 | 
						||
    Those following blocks is named control-block which hold the information
 | 
						||
    for a day with timestamp and position of first record on record-block.
 | 
						||
    Item on those blocks is written in cycle like block of DM_FLASH_BLOCK_VARIABLE.
 | 
						||
    if this block damaged, related type of day-information and records will be lost.
 | 
						||
    */
 | 
						||
    DM_FLASH_BLOCK_LOAD_CTRL,       /* Control block of load. */
 | 
						||
    DM_FLASH_BLOCK_EVENT_CTRL,      /* Control block of event. */
 | 
						||
    DM_FLASH_BLOCK_PRIV_EVENT_CTRL,  /* Control block of private event. */
 | 
						||
 | 
						||
    /*
 | 
						||
    Those following blocks is named record-block which hold all records.
 | 
						||
    Item on those blocks is written in cycle like block of DM_FLASH_BLOCK_VARIABLE.
 | 
						||
    if this block damaged, related type of records will be lost.
 | 
						||
    */
 | 
						||
    DM_FLASH_BLOCK_LOAD_REC,        /* Record block of load. */
 | 
						||
    DM_FLASH_BLOCK_EVENT_REC,       /* Record block of event. */
 | 
						||
    DM_FLASH_BLOCK_PRIV_EVENT_REC,   /* Record block of private event. */
 | 
						||
 | 
						||
    /*
 | 
						||
    Not used.
 | 
						||
    */
 | 
						||
    DM_FLASH_BLOCK_PRV_DATA,        /* Private data used. */
 | 
						||
 | 
						||
    DM_FLASH_BLOCK_MAX
 | 
						||
}iot_dm_flash_block_type_e;
 | 
						||
 | 
						||
/* ******************** event ******************* */
 | 
						||
/* Item of each event. In event record block. */
 | 
						||
typedef struct _iot_dm_flash_event_record_item_t {
 | 
						||
    uint32_t        etype:4;    /* event type */
 | 
						||
    uint32_t        state:8;    /* state type */
 | 
						||
    uint32_t        seconds:20; /* seconds from 00:00:00 of that day. */
 | 
						||
    uint16_t        v;  /* voltage, 0.1V */
 | 
						||
    int16_t         f;  /* power factor, 0.1% */
 | 
						||
    int32_t         i;  /* electrical current, mA */
 | 
						||
    uint32_t        p;  /* active power, 0.1mW(1mA * 0.1V) */
 | 
						||
    uint32_t        w;  /* work, 1mKW*h(1A * 1V * 1h) */
 | 
						||
}iot_dm_event_record_t;
 | 
						||
 | 
						||
/* Summury of event for each day.In event control block. */
 | 
						||
typedef struct _iot_em_flash_event_day_item_t {
 | 
						||
    /* First item index from event record block. */
 | 
						||
    uint32_t        index; /* keep first item of struct to check valid */
 | 
						||
    /* Timestamp of this day. */
 | 
						||
    uint32_t        timestamp;
 | 
						||
}iot_dm_event_day_t;
 | 
						||
 | 
						||
/* ******************** load-record ******************* */
 | 
						||
/* Item of each load data. In load record block. */
 | 
						||
typedef struct _iot_dm_flash_load_record_item_t {
 | 
						||
    uint32_t        reserved:12; /* reserved bits */
 | 
						||
    uint32_t        seconds:20; /* seconds from 00:00:00 of that day. */
 | 
						||
    uint16_t        v;  /* voltage, 0.1V */
 | 
						||
    int16_t         f;  /* power factor, 0.1% */
 | 
						||
    int32_t         i;  /* electrical current, mA */
 | 
						||
    uint32_t        p;  /* active power, 0.1mW(1mA * 0.1V) */
 | 
						||
    uint32_t        w;  /* work, 1mKw*h(1A * 1V * 1h) */
 | 
						||
}iot_dm_load_record_t;
 | 
						||
 | 
						||
/* Summury of load data for each day.In load data control block. */
 | 
						||
typedef struct _iot_dm_flash_load_day_item_t {
 | 
						||
    /* First item index from load record block. */
 | 
						||
    uint32_t        index; /* keep first item of struct to check valid */
 | 
						||
    /* Timestamp of this day. */
 | 
						||
    uint32_t        timestamp;
 | 
						||
}iot_dm_load_day_t;
 | 
						||
 | 
						||
/* ******************** meter block data ********************** */
 | 
						||
 | 
						||
/* Threshold data. */
 | 
						||
typedef struct _iot_dm_flash_threshold_t {
 | 
						||
    uint32_t    i_standby;  /* Current threshold for standby.*/
 | 
						||
    uint32_t    i_work;     /* Current threshold for working.*/
 | 
						||
    uint32_t    p_trap;     /* Power threshold for runing in trap.*/
 | 
						||
}iot_dm_threshold_t;
 | 
						||
 | 
						||
/* Data of meter. In header block. */
 | 
						||
typedef struct _iot_dm_flash_meter_data_t {
 | 
						||
    iot_dm_threshold_t  threshold;  /* Threshold for triggering event. */
 | 
						||
}iot_dm_meter_t;
 | 
						||
 | 
						||
/* Type of threshold, reference to iot_dm_threshold_t. */
 | 
						||
typedef enum _iot_dm_flash_threshold_e {
 | 
						||
    DM_FLASH_THRESHOLD_CURRENT_STANDBY,
 | 
						||
    DM_FLASH_THRESHOLD_CURRENT_WORKING,
 | 
						||
    DM_FLASH_THRESHOLD_POWER_TRAPPING,
 | 
						||
    DM_FLASH_THRESHOLD_TIME_TRAPPING_WARN
 | 
						||
}iot_dm_flash_threshold_e;
 | 
						||
 | 
						||
/* ********************* Variable block ******************* */
 | 
						||
typedef struct _iot_dm_block_information_t {
 | 
						||
    volatile uint16_t    item_rec_idx; /* The index of last item written on record block. */
 | 
						||
    volatile uint16_t    item_ctrl_idx; /* The index of last item written on contrl block. */
 | 
						||
    volatile uint32_t    item_timestamp; /* The time last item written on contrl block. */
 | 
						||
}iot_dm_block_info_t;
 | 
						||
 | 
						||
/* roll write in variable block. size = 28B, 146 item per sector. */
 | 
						||
typedef struct _iot_dm_flash_vari_data_t {
 | 
						||
    iot_dm_block_info_t load;   /* for load control and load record block. */
 | 
						||
    iot_dm_block_info_t event;  /* for event control and load record block. */
 | 
						||
    iot_dm_block_info_t priv_event;  /* for private event control and load record block. */
 | 
						||
    uint32_t    meter_base; /* Work of meter. WH(1A * 1V * 1h). */
 | 
						||
}iot_dm_vari_t;
 | 
						||
 | 
						||
/* ******************** Flash header ********************** */
 | 
						||
#define IOT_DM_FLASH_HEADER_MAGIC   0xD5C6B7A8
 | 
						||
 | 
						||
/* Top of flash. In header block */
 | 
						||
typedef struct _iot_dm_flash_t {
 | 
						||
    /* Magic number, 0xD5C6B7A8.*/
 | 
						||
    uint32_t    magic;
 | 
						||
    /* Data of meter. */
 | 
						||
    iot_dm_meter_t              meter;
 | 
						||
    /* block information. */
 | 
						||
    iot_dm_flash_block_desc_t   block[DM_FLASH_BLOCK_MAX];
 | 
						||
}iot_dm_flash_t;
 | 
						||
 | 
						||
/* ******************** Flash export API ********************** */
 | 
						||
 | 
						||
/**
 | 
						||
 * @brief iot_dm_flash_init_module() - Initialize the extern flash module.
 | 
						||
 * @return ERR_FAIL -- Operation failed.
 | 
						||
 * @return ERR_OK -- Operation Successful.
 | 
						||
 */
 | 
						||
uint32_t iot_dm_flash_init_module(void);
 | 
						||
 | 
						||
/**
 | 
						||
 * @brief iot_dm_flash_get_chipinfo() - Get information of flash chip.
 | 
						||
 * @param info: buffer to store information.
 | 
						||
 * @return ERR_FAIL -- Operation failed.
 | 
						||
 * @return ERR_OK -- Operation Successful.
 | 
						||
 */
 | 
						||
uint32_t iot_dm_flash_get_chipinfo(iot_dm_flash_chip_t *info);
 | 
						||
 | 
						||
/**
 | 
						||
 * @brief iot_dm_flash_get_chipstatus() - Get block information of flash chip.
 | 
						||
 * @param block: Type of block, reference to iot_dm_flash_block_type_e.
 | 
						||
 *          if block = DM_FLASH_BLOCK_MAX, will return information of all blocks.
 | 
						||
 * @param block_info: buffer to store information.
 | 
						||
 * @return ERR_FAIL -- Operation failed.
 | 
						||
 * @return ERR_OK -- Operation Successful.
 | 
						||
 */
 | 
						||
uint32_t iot_dm_flash_get_blockinfo(uint32_t block, iot_dm_flash_block_usage_desc_t block_info[]);
 | 
						||
 | 
						||
/**
 | 
						||
 * @brief iot_dm_flash_write_event() - Store an event record in flash.
 | 
						||
 * @param timestamp: Unix timestamp, start from 1970-01-01 00:00:00.
 | 
						||
 * @param record: An event record to store.
 | 
						||
 * @return ERR_FAIL -- Operation failed.
 | 
						||
 * @return ERR_OK -- Operation Successful.
 | 
						||
 */
 | 
						||
uint32_t iot_dm_flash_write_event(uint32_t timestamp, iot_dm_event_record_t *record);
 | 
						||
 | 
						||
/**
 | 
						||
 * @brief iot_dm_flash_write_load() - Store an load record in flash.
 | 
						||
 * @param timestamp: Unix timestamp, start from 1970-01-01 00:00:00.
 | 
						||
 * @param record: A load record to store.
 | 
						||
 * @return ERR_FAIL -- Operation failed.
 | 
						||
 * @return ERR_OK -- Operation Successful.
 | 
						||
 */
 | 
						||
uint32_t iot_dm_flash_write_load(uint32_t timestamp, iot_dm_load_record_t *record);
 | 
						||
 | 
						||
/**
 | 
						||
 * @brief iot_dm_flash_write_threshold() - Store an threshold in flash.
 | 
						||
 * @param threshold: Value of threshold.
 | 
						||
 *          Unit : 1MA for current, 1W for power, 1S for time.
 | 
						||
 * @return ERR_FAIL -- Operation failed.
 | 
						||
 * @return ERR_OK -- Operation Successful.
 | 
						||
 */
 | 
						||
uint32_t iot_dm_flash_write_threshold(iot_dm_threshold_t *threshold);
 | 
						||
 | 
						||
/**
 | 
						||
 * @brief iot_dm_flash_write_meter_base() - Store the meter base in flash.
 | 
						||
 * @param base_value: Base value to store. Unit : 1WH (1A * 1V * 1H)
 | 
						||
 * @return ERR_FAIL -- Operation failed.
 | 
						||
 * @return ERR_OK -- Operation Successful.
 | 
						||
 */
 | 
						||
uint32_t iot_dm_flash_write_meter_base(uint32_t base_value);
 | 
						||
 | 
						||
/**
 | 
						||
 * @brief iot_dm_flash_read_event_num_of_day() - Get event record number on given day.
 | 
						||
 * @param timestamp: Time of a day, Unix timestamp, start from 1970-01-01 00:00:00.
 | 
						||
 * @param num: Buffer to store the number.
 | 
						||
 * @return ERR_FAIL -- Operation failed.
 | 
						||
 * @return ERR_OK -- Operation Successful.
 | 
						||
 */
 | 
						||
uint32_t iot_dm_flash_read_event_num_of_day(uint32_t timestamp, uint32_t *num);
 | 
						||
 | 
						||
/**
 | 
						||
 * @brief iot_dm_flash_read_load_num_of_day() - Get load record number on given day.
 | 
						||
 * @param timestamp: Time of a day, Unix timestamp, start from 1970-01-01 00:00:00.
 | 
						||
 * @param num: Buffer to store the number.
 | 
						||
 * @return ERR_FAIL -- Operation failed.
 | 
						||
 * @return ERR_OK -- Operation Successful.
 | 
						||
 */
 | 
						||
uint32_t iot_dm_flash_read_load_num_of_day(uint32_t timestamp, uint32_t *num);
 | 
						||
 | 
						||
 | 
						||
/**
 | 
						||
 * @brief iot_dm_flash_read_event() - Get event record(s) from flash.
 | 
						||
 * @param timestamp: From when, Unix timestamp, start from 1970-01-01 00:00:00.
 | 
						||
 * @param start_idx: Start index of event record item, start from 0 to end_idx.
 | 
						||
 * @param end_idx: End index of event record item, start from start_idx to max.
 | 
						||
 * @param record: Event record buffer.
 | 
						||
 * @param valid_count: Event record returned in this buffer.
 | 
						||
 * @return ERR_FAIL -- Operation failed.
 | 
						||
 * @return ERR_OK -- Operation Successful.
 | 
						||
 */
 | 
						||
uint32_t iot_dm_flash_read_event(uint32_t timestamp, uint32_t start_idx,
 | 
						||
    uint32_t end_idx, iot_dm_event_record_t *record, uint32_t *valid_count);
 | 
						||
 | 
						||
 | 
						||
/**
 | 
						||
 * @brief iot_dm_flash_read_load() - read count of load records just behind the time
 | 
						||
 *  that timestamp tells. cause all records are on the same day,
 | 
						||
 * *valid_count may be little than count.
 | 
						||
 * @param timestamp: From when, Unix timestamp, start from 1970-01-01 00:00:00.
 | 
						||
 * @param count: count of records that requests.
 | 
						||
 * @param record: load record buffer.
 | 
						||
 * @param valid_count: Event record returned in this buffer.
 | 
						||
 * @return ERR_FAIL -- Operation failed.
 | 
						||
 * @return ERR_OK -- Operation Successful.
 | 
						||
 */
 | 
						||
uint32_t iot_dm_flash_read_load(uint32_t timestamp, uint32_t count,
 | 
						||
    iot_dm_load_record_t *record, uint32_t *valid_count);
 | 
						||
 | 
						||
 | 
						||
/**
 | 
						||
 * @brief iot_dm_flash_read_threshold() - Get an threshold from flash.
 | 
						||
 * @param threshold: Buffer for threshold.
 | 
						||
 *          Unit : 1MA for current, 1W for power, 1S for time.
 | 
						||
 * @return ERR_FAIL -- Operation failed.
 | 
						||
 * @return ERR_OK -- Operation Successful.
 | 
						||
 */
 | 
						||
uint32_t iot_dm_flash_read_threshold(iot_dm_threshold_t *threshold);
 | 
						||
 | 
						||
/**
 | 
						||
 * @brief iot_dm_flash_read_meter_base() - Get the meter base from flash.
 | 
						||
 * @param base_value: Buffer for base value. Unit : 1WH (1A * 1V * 1H)
 | 
						||
 * @return ERR_FAIL -- Operation failed.
 | 
						||
 * @return ERR_OK -- Operation Successful.
 | 
						||
 */
 | 
						||
uint32_t iot_dm_flash_read_meter_base(uint32_t *base_value);
 | 
						||
 | 
						||
/**
 | 
						||
 * @brief iot_dm_flash_read_last_powerdown_timestamp() - Get last poweroff time.
 | 
						||
 * @param timestamp: Buffer for timestamp returned.
 | 
						||
 * @return ERR_FAIL -- Operation failed.
 | 
						||
 * @return ERR_OK -- Operation Successful.
 | 
						||
 */
 | 
						||
uint32_t iot_dm_flash_read_last_record_timestamp(uint32_t *timestamp);
 | 
						||
 | 
						||
 | 
						||
#endif
 |