/*
 * @Author       : stark1898y 1658608470@qq.com
 * @Date         : 2024-12-12 15:54:57
 * @LastEditors  : stark1898y 1658608470@qq.com
 * @LastEditTime : 2024-12-12 16:47:22
 * @FilePath     : \BLE_TYQ_CH592F\BSP\src\bsp_flash.c
 * @Description  :
 *
 * Copyright (c) 2024 by yzy, All Rights Reserved.
 */
#include "bsp_flash.h"
#include "CONFIG.h"
#include "string.h"
#include "bsp_valve.h"

#undef LOG_ENABLE
#define LOG_ENABLE 1

#include "log.h"


void Flash_ErasePage_ReadCfgInfo(uint8_t *page_buf)
{
    EEPROM_READ(FLASH_INFO_ADDR, page_buf, DATA_FLASH_PAGE_SIZE);
    EEPROM_ERASE(FLASH_INFO_ADDR, DATA_FLASH_PAGE_SIZE);
}

int Flash_Write_CfgInfo(uint8_t *page_buf)
{
    EEPROM_WRITE(FLASH_INFO_ADDR, page_buf, DATA_FLASH_PAGE_SIZE);

    return 0;
}

void BSP_FLASH_Init(void)
{
    uint32_t flash_write_flag = 0;
    EEPROM_READ(FLASH_INFO_WRITE_FLAG_ADDR, &flash_write_flag, FLASH_INFO_WRITE_FLAG_LEN);
    // 判断是否需要初始化
    if (flash_write_flag != FLASH_INFO_WRITE_FLAG_VAL) // 用以防止刷掉写入的数据
    {
        flash_write_flag = FLASH_INFO_WRITE_FLAG_VAL;
        // 擦除数据
        EEPROM_ERASE(FLASH_INFO_ADDR, DATA_FLASH_PAGE_SIZE); // 擦除待写区
        EEPROM_WRITE(FLASH_INFO_WRITE_FLAG_ADDR, &flash_write_flag, FLASH_INFO_WRITE_FLAG_LEN);

        logWarning("First Init flash_write_flag:%d", flash_write_flag);
    }
    logDebug("BSP_FLASH_Init");
}
