#ifndef __APP_FLASH_H__
#define __APP_FLASH_H__
/*************************************
 *INCLUDES
 */
#include "exception.h"
#include "app_power.h"
#include "nrf_gpio.h"
#include "nrf_delay.h"
#include "nrf_sdm.h"

//ERROR CODE
#define ZONE_OP_SUCCESS										0x00																//ZONE操作成功
#define ZONE_ERROR_READ_FAIL							(ZONE_OP_SUCCESS+0x01)							//读取该区域失败
#define ZONE_ERROR_WRITE_FAIL							(ZONE_OP_SUCCESS+0x02)							//写入该区域失败
#define ZONE_ERROR_ERASE_FAIL							(ZONE_OP_SUCCESS+0x03)							//擦除该区域失败
#define ZONE_ERROR_ADDRESS_FAIL						(ZONE_OP_SUCCESS+0x04)							//输入地址越界

/********************************************
*API FUCTIONS
*/
#define RecordMacAddrL 6 //需要保存左右鞋的MAC地址

typedef struct __attribute__((packed,aligned(4))) _FlashBackup{
	uint32_t  head;	
	uint8_t 	macAddr_L[RecordMacAddrL];
	uint8_t 	macAddr_R[RecordMacAddrL];
	uint32_t 	hardVersion;
	uint16_t 	sotfVersion;
	uint8_t 	isConfig;
	uint8_t  	isHost;
	uint8_t   ErrStartCnt;
	uint8_t   RestartCnt;
	uint8_t   LR_FLAG;
}FlashBackup_t;

//size:36
typedef struct _FlashStep{
	uint8_t 	startTime[8];		   //当前时间
	uint32_t 	stepCur[2];				 //当前永久步数
	uint32_t 	step[2];					 //每小时记录的左右鞋永久步数0左1右
	uint32_t 	step_num;					 //已经记录的每小时步数数量
}FlashStep_t;
//size:12
typedef struct _FlashClient{
	uint8_t 	isConfig;
	uint32_t 	hardVersion;
	uint16_t 	sotfVersion;
	uint8_t	 	macAddr[RecordMacAddrL];
}FlashClient_t;
//size:
typedef struct _FlashLog{
	uint8_t Errorflag;
	uint8_t logData[50];
}FlashLog;
//size:64
typedef struct _Flash_Param{
  uint32_t      head;	
	uint8_t  			isHost;
	uint8_t       LR_FLAG;
	uint8_t 			macHost[RecordMacAddrL];
	FlashStep_t 	mStep;
	FlashClient_t mClient;
	FlashLog 			mFlashLog;
	battercb_t 		mbattercb_t;
}Flash_t;

extern Flash_t mFlash;
extern FlashBackup_t 	mBackup;

/*1.当新API读到旧的数据的时候，新API多的参数用默认值填充，写回的时候按照新API的格式写回。（向后兼容）
  2.当旧API读到新数据，自己不认识的那段buff，要保存起来，写回的时候，将这段buff原样memcpy。（向前兼容，没实现）
*/
 
/********************************************
 *API FUCTIONS
 */
void Flash_Initialize(void);
//存储步数
uint32_t Flash_SaveStep(void);
//获取步数区域首地址
uint32_t Flash_GetStepZoneStartAddr(void);

//删除所有步数
uint32_t Flash_DeleteAllStep(void);
//删除information的数据
uint32_t Flash_DeleteAllInfor(void);

uint32_t Flash_DeleteAllBackup(void);
	
uint32_t Flash_GetBackup(FlashBackup_t *pbackup);

uint32_t Flash_SaveBackup(void);
	
//存储基本信息
uint32_t Flash_SaveInfomation(void);
//获取脚步的起始地址
uint32_t flash_GetStep_StartAddress(void);
//读取指定flash的内容
uint32_t flash_Data_Read(uint32_t addr, uint32_t *pData, uint32_t dataLen);
//保存日志信息
uint32_t Flash_SaveLog(uint32_t id, uint32_t pc, uint32_t info);

//测试接口
void TestHalFlashInterface(void);

void flash_SetClearStepFlag(void);

#endif





























