/*****************************************************************************
 * file    Core/Inc/flash_if.h 
 * author  WTOE
 * version 1.0.0
 * date    13-Jun-2022
 * brief   This file provides all the headers of the flash_if functions.
 ******************************************************************************
 * Attention
 *
 * <h2><center>&copy; COPYRIGHT(c) 2022 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 WTOE 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.
 *
 ******************************************************************************/

#ifndef __FLASH_IF_H__
#define __FLASH_IF_H__

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

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

#define FLASH_START_ADRESS    0x08000000
#define FLASH_PAGE_NBPERBANK  256

/* spi flash分配示意图.注意:以下地址必须是扇区对齐.
0x0     0x1000  0x2000  0x3000  0x4000  0x5000  0x8000     0x10000    0x100000   0x900000  0xB00000 0xD00000
*****************************************************************************************************
| CONF  | CONFB | RECO  | RECOB | INFO  | INFOB | TFTP IAP | TFTP IMG |   WARE   |   LOG   |  DATA  |
*****************************************************************************************************/
#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_IAP_ADDRESS (uint32_t)0x8000
#define TFTP_IAP_ADDRESS_END (uint32_t)0x10000
#define TFTP_APP_ADDRESS (uint32_t)0x10000
#define TFTP_APP_ADDRESS_END (uint32_t)0x100000
#define WARE_ADDRESS (uint32_t)0x100000
#define WARE_ADDRESS_END (uint32_t)0x900000
#define POSITION_ADDRESS (uint32_t)0x100000
#define POSITION_ADDRESS_END (uint32_t)0x300000
#define LOG_ADDRESS (uint32_t)0x900000
#define LOG_ADDRESS_END (uint32_t)0xB00000
#define FD_ADDRESS (uint32_t)0xB00000
#define FD_ADDRESS_END (uint32_t)0xD00000
#define ST_ADDRESS (uint32_t)0xD00000
#define ST_ADDRESS_END (uint32_t)0xF00000

#define SPI_FLASH_END_ADDRESS (uint32_t)0x1000000

/* 内部flash分配示意图.
0x08000000       APPLICATION_ADDRESS                   0x08040000
********************************************************
|      IAP       |                APP                  |
*******************************************************/
#define IAP_LEN 0x8000
#define APP_LEN 0xF8000
#define IAP_ADDRESS (uint32_t)0x08000000
#define APP_ADDRESS (uint32_t)0x08008000
#define USER_FLASH_END_ADDRESS (uint32_t)0x08100000

/* 页,扇区,块大小. */
#define SPI_FLASH_PAGE_SIZE 0x100/*256*/
#define SPI_FLASH_SECTOR_SIZE 0x1000/* 4K */
#define SPI_FLASH_BLOCK_SIZE 0x8000/* 32K */
#define SPI_FLASH_BLOCK64_SIZE 0x10000/* 64K */

/* 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_BLOCK64_ERASE 0xD8
#define SPI_CMD_CHIP_ERASE 0x60
#define SPI_CMD_READ_ID 0x9F

/* Exported types ------------------------------------------------------------*/
/* Extern global variables ---------------------------------------------------*/
/* Extern macro --------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */
extern void spi_flash_init_os(void);
extern uint32_t spi_flash_RDJDID(void);
extern HAL_StatusTypeDef spi_flash_RDSR(uint8_t *status);
extern HAL_StatusTypeDef spi_flash_erase(uint32_t addr, uint8_t erase_cmd);
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);

extern void FLASH_If_Init(void);
extern uint32_t FLASH_If_Erase(void);
extern uint32_t FLASH_If_Write(uint32_t destination, uint32_t *p_source, uint32_t length);
#endif
/******************* (C) COPYRIGHT LandPower ***** END OF FILE ****************/
