/************************************************************************************************
 *                                          Include                                              *
 ************************************************************************************************/

#include "flash.h"
#include "stm32f1xx_hal.h"
#include <stdint.h>

/************************************************************************************************
 *                                          Config                                                *
 ************************************************************************************************/

/************************************************************************************************
 *                                          Data structs                                         *
 ************************************************************************************************/

/************************************************************************************************
 *                                          implements                                           *
 ************************************************************************************************/
FLASH_ProcessTypeDef p_flash;

// 解除写保护，清除fmc特定寄存器
void fmc_clear_flag_star(void)
{
    HAL_FLASH_Unlock();    // 解锁
    HAL_FLASH_OB_Unlock(); // FLASH擦除期间,必须禁止数据缓存
    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR);
}

// 设置写保护，清除fmc特定寄存器
void fmc_clear_flag_end(void)
{
    HAL_FLASH_OB_Lock(); // FLASH擦除结束,开启数据缓存
    HAL_FLASH_Lock();    // 上锁
}

// 读取一字节， 读取无需考虑字节对齐
uint8_t fmc_read_byte(uint32_t read_addr)
{
    return *(__IO uint8_t *)read_addr;
}

// 读取两字节， 读取需考虑字节对齐
uint16_t fmc_read_half_word(uint32_t read_addr)
{
    return *(__IO uint16_t *)read_addr;
}

// 读取N字节， 读取必须需考虑字节对齐
uint8_t fmc_read_n_half_word(uint32_t read_addr, uint16_t *buff, uint16_t count)
{
    uint16_t i = 0;

    __IO uint16_t *addr = (__IO uint16_t *)read_addr;

    if (read_addr % 2 != 0)
        return 1; // 错误返回

    for (i = 0; i < count; i++)
    {
        *buff++ = *addr++;
    }
    return 0;
}

// 读取1页， 地址必须对齐页首
uint8_t fmc_read_one_page(uint32_t read_addr, uint16_t *buff)
{
    uint16_t i = 0;

    __IO uint16_t *addr = (__IO uint16_t *)read_addr;

    if (read_addr % FMC_PAGE_SIZE != 0)
        return 1; // 错误返回

    for (i = 0; i < 1024; i++)
    {
        *buff++ = *addr++;
    }
    return 0;
}

// 擦出N页， 地址必须对齐页首
uint8_t fmc_erase_pages(uint32_t erase_addr, uint16_t len)
{
    uint16_t               i = 0, j = 0;
    FLASH_EraseInitTypeDef p_erase_init;
    uint32_t               page_error = 0;

    p_erase_init.TypeErase = FLASH_TYPEERASE_PAGES;
    p_erase_init.Banks     = FLASH_BANK_1;
    p_erase_init.NbPages   = 1;

    uint32_t *addr = (uint32_t *)erase_addr;

    if (erase_addr % FMC_PAGE_SIZE != 0)
        return 1; // 错误返回

    for (i = 0; i < len; i++)
    {
        p_erase_init.PageAddress = erase_addr + FMC_PAGE_SIZE * i;

        if (HAL_FLASHEx_Erase(&p_erase_init, &page_error) != HAL_OK)
            return 1;

        for (j = 0; j < 512; j++)
        {
            if (*addr != 0xFFFFFFFF)
                return 1;
            else
                addr++;
        }
    }
    return 0;
}

// 只写入，写入前需擦除，地址必须两字节对齐
uint8_t fmc_write_n_half_word(uint32_t write_addr, uint16_t *buff, uint16_t len)
{

    uint32_t end_addr = write_addr + len * 2;

    if (write_addr % 2 != 0)
        return 1;

    while (write_addr < end_addr)
    {

        HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, write_addr, *buff); // 写入数据
        if (*(__IO uint16_t *)write_addr != *buff)
        {
            return 1;
        }
        write_addr += 2;
        buff++;
    }
    return 0;
}

uint16_t firmware_crc16_ccitt_false(uint16_t init_value, uint8_t *pbuff, uint32_t len)
{
    uint16_t temp = 0;
    uint16_t crc  = init_value;

    for (uint32_t i = 0; i < len; i++)
    {
        for (uint32_t j = 0; j < 8; j++)
        {
            temp = ((pbuff[i] << j) & 0x80) ^ ((crc & 0x8000) >> 8);
            crc <<= 1;
            if (temp != 0)
            {
                crc ^= 0x1021;
            }
        }
    }
    return crc;
}
