/*!
 * @file        flash_eeprom.h
 *
 * @brief       This file contains all the functions for the eeprom emulation.
 *
 * @version     V1.0.0
 *
 * @date        2026-02-25
 *
 * @attention
 *
 *  Copyright (C) 2026 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (Geehy Semiconductor Software License Agreement).
 *
 *  The program is only for reference, which is distributed in the hope
 *  that it will be useful and instructional for customers to develop
 *  their software. Unless required by applicable law or agreed to in
 *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
 *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the Geehy Semiconductor Software License Agreement for the governing permissions
 *  and limitations under the License.
 */

#ifndef FLASH_EEPROM_H
#define FLASH_EEPROM_H

#ifdef __cplusplus
extern "C" {
#endif

/* Includes */
#include "g32a10xx.h"
#include "g32a10xx_fls.h"

/* The following is the g32a1085 Flash region planning for this example. */

/*
 +---------------------------------------------------------------------------------------+
 |                                        |Flash|                                        |
 +---------------------------------------------------------------------------------------+
 |                    Code                   |                Flash EEPROM               |
 +----------+----------+----------+----------+----------+----------+----------+----------+
 |  0.5KB   |  0.5KB   |  0.5KB   |  0.5KB   |  0.5KB   |  0.5KB   |  0.5KB   |  0.5KB   |
 |  page 0  |  page 1  |  page 2  |  page N  | page N+1 | page N+2 | page N+3 | page N+N |
 |          |          |          |          |          |          |          |          |
 +----------+----------+----------+----------+----------+----------+----------+----------+
*/

/** @addtogroup Examples
  @{
*/

/** @addtogroup FLASH_Examples
  @{
*/

/** @addtogroup FMC_Flash_EEPROM
  @{
*/

/** @defgroup FMC_Flash_EEPROM_Macros Macros
  @{
*/

/* Flash EEPROM Page number, can use multiple MCU flash pages */
#define FLASH_EE_PAGE_NUM              ((uint32_t)1U)

/* MCU Flash Page size */
#define G32_FLASH_PAGE_SIZE          ((uint32_t)(1U * 512U))

/* MCU Flash size */
#define G32_FLASH_SIZE               (64U * 512U)


/* The following macro definitions do not require user changes. */
/* Flash EEPROM Page size, can contain multiple MCU flash pages */
#define FLASH_EE_PAGE_SIZE             ((uint32_t)(FLASH_EE_PAGE_NUM * G32_FLASH_PAGE_SIZE))

/* Flash EEPROM start address in Flash */
#define FLASH_EE_START_ADDRESS         ((uint32_t)(0x08040000U + G32_FLASH_SIZE - (FLASH_EE_PAGE_SIZE * 2U)))

/* Flash EEPROM Page0 and Page1 start address */
#define PAGE0_START_ADDRESS            ((uint32_t)(FLASH_EE_START_ADDRESS))
#define PAGE1_START_ADDRESS            ((uint32_t)(PAGE0_START_ADDRESS + FLASH_EE_PAGE_SIZE))

/* The maximum variables of number that can be stored */
#define NUMBER_OF_VARIABLES            ((uint32_t)((FLASH_EE_PAGE_SIZE / 8U) - 1U))

/* Flash EEPROM valid page definitions */
#define PAGE0_VALID                    ((uint32_t)0x00000000U)  /*!< Page0 valid */
#define PAGE1_VALID                    ((uint32_t)0x00000001U)  /*!< Page1 valid */
#define NO_VALID_PAGE                  ((uint32_t)0x00000002U)  /*!< No valid page */

/* Flash EEPROM Page status definitions */
#define PAGE_STATUS_ERASED             ((uint32_t)0xFFFFFFFFU)  /* PAGE is empty */
#define PAGE_STATUS_TRANSFER           ((uint32_t)0xEEEEEEEEU)  /* PAGE is marked to transfer status */
#define PAGE_STATUS_VALID              ((uint32_t)0x00000000U)  /* PAGE containing valid data */

/* Get valid pages in read and write mode defines */
#define GET_VALID_PAGE_READ            ((uint8_t)0x01U)
#define GET_VALID_PAGE_WRITE           ((uint8_t)0x02U)

/**@} end of group FMC_Flash_EEPROM_Macros */

/** @defgroup FMC_Flash_EEPROM_Functions Functions
  @{
*/

Fmc_StateType Flash_EE_Init(void);
uint32_t Flash_EE_ReadData(uint32_t VirtAddress, uint32_t *PData);
Fmc_StateType Flash_EE_WriteData(uint32_t VirtAddress, uint32_t Data);

/**@} end of group FMC_Flash_EEPROM_Functions */
/**@} end of group FMC_Flash_EEPROM */
/**@} end of group FLASH_Examples */
/**@} end of group Examples */

#ifdef __cplusplus
}
#endif

#endif /* __FLASH_EEPROM_H */
