/**
 * @Author: 李建
 * @Date: 2024/8/12 13:21
 * Description: 
 * Copyright: Copyright (©) 2024 永续绿建. All rights reserved.
 */
#ifndef ERV_HUMIDIFICATION_BOARD_FLASH_H
#define ERV_HUMIDIFICATION_BOARD_FLASH_H
#include "main.h"
#define SLAVE_ADDRESS (uint32_t)0x0800FC00   //FLASH地址，第64页首地址，每次擦除最少一页

#ifdef FLASH_BASE
#undef FLASH_BASE
#define FLASH_BASE 0x08000000U
#endif

#ifdef FLASH_PAGE_SIZE
#undef FLASH_PAGE_SIZE
#define FLASH_PAGE_SIZE 1024  //byte
#endif


//flash物理地址(绝对地址)与扇区编号转换
//例如0x8006000转换成24
#define FLASH_PAGE_TO_ADDR(page)          ((uint32_t)(FLASH_BASE+(FLASH_PAGE_SIZE)*(page))) /* Base @ of page address*/
#define ADDR_TO_FLASH_PAGE(addr)          (((addr)-FLASH_BASE)/(FLASH_PAGE_SIZE))

#define FLASH_USER_START_ADDR       FLASH_PAGE_TO_ADDR(24)   /* Start @ of user Flash area */
#define FLASH_USER_END_ADDR         FLASH_PAGE_TO_ADDR(34)   /* End @ of user Flash area */


typedef enum
{
    FLASH_SUCCESS = 0,
    FLASH_PARAM_ERROR,
    FLASH_ADDR_ERROR,
    FLASH_WRITE_WORD_ERROR,
    FLASH_WRITE_HALF_WORD_ERROR,
    FLASH_WRITE_BYTE_ERROR,
    FLASH_READ_ERROR,
    FLASH_ERASE_ERROR,
} FLASH_ERROR_CODE_E;


FLASH_ERROR_CODE_E flash_write(uint32_t address, const uint8_t* pdata, uint32_t size);
uint32_t flash_read ( uint32_t address, uint8_t* pdata, uint32_t size );
FLASH_ERROR_CODE_E flash_erase( uint32_t start_addr,uint32_t end_addr);
#endif //ERV_HUMIDIFICATION_BOARD_FLASH_H
