/******************************************************************************
  * @file    Inc/flash_if.h 
  * @author  MCD Application Team
  * @version 1.0.0
  * @date    10-Jun-2022
  * @brief   This file provides all the headers of the flash_if functions.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT(c) 2019 LandPower</center></h2>
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************/

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __FLASH_IF_H
#define __FLASH_IF_H

/* Includes ------------------------------------------------------------------*/
#include "stm32l4xx_hal.h"

/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/* Error code */
enum 
{
  FLASHIF_OK = 0,
  FLASHIF_ERASEKO,
  FLASHIF_WRITINGCTRL_ERROR,
  FLASHIF_WRITING_ERROR,
  FLASHIF_PROTECTION_ERRROR
};

/* protection type */  
enum{
  FLASHIF_PROTECTION_NONE         = 0,
  FLASHIF_PROTECTION_PCROPENABLED = 0x1,
  FLASHIF_PROTECTION_WRPENABLED   = 0x2,
  FLASHIF_PROTECTION_RDPENABLED   = 0x4,
};

/* protection update */
enum {
    FLASHIF_WRP_ENABLE,
    FLASHIF_WRP_DISABLE
};

/* 表示是否升级和配置是否被初始化过的标志位. */
#define INIT_DONE_MAGIC 0x0F1E2D3C
#define UPDATE_MAGIC 0x3A6E55D9
#define UPDATE_OK_MAGIC 0x6D90125B
#define UPDATE_ERROR_MAGIC 0x9EFC845A

/* SPI FLASH 分配示意图. 注意: 以下地址必须是扇区对齐.
0x0     0x1000  0x2000  0x3000  0x4000  0x5000  0x8000     0x10000    0x800000
***********************************************************************
| CONF  | CONFB | RECO  | RECOB | INFO  | INFOB | TFTP IAP | TFTP IMG |
***********************************************************************/
#define CONFIG_ADDRESS (uint32_t)0x0
#define CONFIG_ADDRESS_BAK (uint32_t)0x1000
#define RECORD_ADDRESS (uint32_t)0x2000
#define RECORD_ADDRESS_BAK (uint32_t)0x3000
#define INFO_ADDRESS (uint32_t)0x4000
#define INFO_ADDRESS_BAK (uint32_t)0x5000
#define TFTP_APP_ADDRESS (uint32_t)0x10000
#define TFTP_APP_ADDRESS_END (uint32_t)0x100000
#define SPI_FLASH_END_ADDRESS (uint32_t)0x800000

/* 内部flash分配示意图.
0x08000000       0x08008000                            0x080FFFFF
********************************************************
|      IAP       |                APP                  |
*******************************************************/
#define IAP_ADDRESS (uint32_t)0x08000000
#define APPLICATION_ADDRESS (uint32_t)0x08008000
/* 内部flash结束地址. */
#define USER_FLASH_END_ADDRESS (uint32_t)0x08100000

#define SPI_FLASH_ID_LEN 3
#define SPI_CMD_HEADER_LEN 4

#define SPI_FLASH_PAGE_SIZE 256
#define SPI_FLASH_SECTOR_SIZE 4096/* 4K */
#define SPI_FLASH_BLOCK_SIZE 32768/* 32K */

#define SPI_BUS_TIMEOUT 1000

/* spi flash命令字. */
#define SPI_CMD_PAGE_WRITE 0x02
#define SPI_CMD_FLASH_READ 0x03
#define SPI_CMD_STATE_READ 0x05
#define SPI_CMD_WRITE_ENABLE 0x06
#define SPI_CMD_SECTOR_ERASE 0x20
#define SPI_CMD_BLOCK32_ERASE 0x52
#define SPI_CMD_CHIP_ERASE 0x60
#define SPI_CMD_READ_ID 0x9F
#define SPI_CMD_BLOCK64_ERASE 0xD8

/* spi flash状态寄存器bit mask. */
#define SPI_FLASH_BUSY_MASK 0x01
#define SPI_FLASH_WEL_MASK 0x02

#define SPI_FLASH_CS_ENABLE() HAL_GPIO_WritePin(SPI_FLASH_CS_GPIO_Port, SPI_FLASH_CS_Pin, GPIO_PIN_RESET)
#define SPI_FLASH_CS_DISABLE() HAL_GPIO_WritePin(SPI_FLASH_CS_GPIO_Port, SPI_FLASH_CS_Pin, GPIO_PIN_SET)


/* Define the user application size */
#define USER_FLASH_SIZE               ((uint32_t)0x00100000) /* Small default template application */

/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */
extern void FLASH_If_Init(void);
extern uint32_t FLASH_If_Erase(uint32_t start);
extern uint32_t FLASH_If_Write(uint32_t destination, uint32_t *p_source, uint32_t length);
extern uint32_t FLASH_If_GetWriteProtectionStatus(void);
extern uint32_t FLASH_If_WriteProtectionConfig(uint32_t modifier);

extern uint32_t spi_flash_read_id(void);
extern HAL_StatusTypeDef spi_flash_erase(uint32_t addr, uint8_t erase_cmd);
extern HAL_StatusTypeDef spi_flash_erase_chip(void);
extern HAL_StatusTypeDef spi_flash_read(uint32_t addr, uint8_t* data, uint16_t size);
extern HAL_StatusTypeDef spi_flash_write(uint32_t addr, uint8_t *data, uint16_t size);

#endif  /* __FLASH_IF_H */

/************************ (C) COPYRIGHT LandPower *****END OF FILE****/
