/*
 * @file exo_io_al_fmc_nor_flash_common.h
 *
 * @brief This file contains structures,enumerations
 * and function declaration for FMC-NOR-FLASH interface
 *
 * @copyright Copyright 2023 Antaris, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef EXO_IO_AL_FMC_NOR_FLASH_COMMON_H
#define EXO_IO_AL_FMC_NOR_FLASH_COMMON_H

#define NOR_INTF_ENB

#include "exo_hal_io_al_common.h"
#include "exo_io_al_common.h"
#include "exo_hal_common.h"

/**
 * @brief IO-HAl FMC NOR flash interface control block handle structure definition
 */
typedef struct _ioal_fmc_nor_hdle
{
    ioal_intf_gen_info intf_gen_info; 						/*!< Hold general information of interface */
}ioal_fmc_nor_hdle;


/**
  * @brief  FMC NOR ID typedef
  */
typedef struct
{
  uint16 manufacturer_code;  /*!< Defines the device's manufacturer code used to identify the memory       */
  uint16 dvc_code1;
  uint16 dvc_code2;
  uint16 dvc_code3;       /*!< Defines the device's codes used to identify the memory.
                                    These codes can be accessed by performing read operations with specific
                                    control signals and addresses set.They can also be accessed by issuing
                                    an Auto Select command                                                   */
}ioal_nor_id_t;

/**
  * @brief  FMC NOR CFI typedef
  */
typedef struct
{
  /*!< Defines the information stored in the memory's Common flash interface
       which contains a description of various electrical and timing parameters,
       density information and functions supported by the memory                   */
  uint16 cfi_1;
  uint16 cfi_2;
  uint16 cfi_3;
  uint16 cfi_4;
} ioal_nor_cfi_t;

/**
  * @brief  HAL SRAM State structures definition
  */
typedef enum
{
  IOAL_NOR_STATE_RESET             = 0x00U,  /*!< NOR not yet initialized or disabled  */
  IOAL_NOR_STATE_READY             = 0x01U,  /*!< NOR initialized and ready for use    */
  IOAL_NOR_STATE_BUSY              = 0x02U,  /*!< NOR internal processing is ongoing   */
  IOAL_NOR_STATE_ERROR             = 0x03U,  /*!< NOR error state                      */
  IOAL_NOR_STATE_PROTECTED         = 0x04U   /*!< NOR NORSRAM device write protected   */

}ioal_nor_state_t;

/**
  * @brief  FMC NOR Status typedef
  */
typedef enum
{
  IOAL_NOR_STATUS_SUCCESS  = 0U,
  IOAL_NOR_STATUS_ONGOING,
  IOAL_NOR_STATUS_ERROR,
  IOAL_NOR_STATUS_TIMEOUT

} ioal_nor_status_t;


/**
 * @brief  This API initializes the control block memory
 * and do the basic configurations for FMC-NOR-FLASH
 */
hal_ret_sts io_hal_fmc_nor_flash_init(ioal_fmc_nor_hdle *ioal_nor_flash, io_inst_id inst_id);

/**
 * @brief  This API write data in FMC-NOR-FLASH
 */
hal_ret_sts io_hal_fmc_nor_flash_program(ioal_fmc_nor_hdle *ioal_nor, uint32 address, uint16* data);

/**
 * @brief  This API erase and write data in FMC-NOR-FLASH
 */
hal_ret_sts io_hal_fmc_nor_flash_program_buffer(ioal_fmc_nor_hdle *ioal_nor, uint32 address, uint8 *data, uint32 buff_size);

/**
 * @brief  This API read data from FMC-NOR-FLASH
 */
hal_ret_sts io_hal_fmc_nor_flash_read(ioal_fmc_nor_hdle *ioal_nor, uint32 address, uint16* data);

/**
 * @brief  This API read data from FMC-NOR-FLASH
 */
hal_ret_sts io_hal_fmc_nor_flash_read_buffer(ioal_fmc_nor_hdle *ioal_nor, uint32 address, uint8 *data, uint32 buff_size);

/**
 * @brief  This API erase the specified block of the NOR memory
 */
hal_ret_sts io_hal_fmc_nor_flash_erase_block(ioal_fmc_nor_hdle *ioal_nor, uint32 block_addr);

/**
 * @brief  This API erase the entire NOR chip.
 */
hal_ret_sts io_hal_fmc_nor_flash_erase_chip(ioal_fmc_nor_hdle* ioal_nor);

/**
 * @brief  This API read data from FMC-NOR-FLASH
 */
ioal_nor_state_t io_hal_fmc_nor_flash_get_state(ioal_fmc_nor_hdle* ioal_nor);

/**
 * @brief  This API erase the specified block of the NOR memory
 */
ioal_nor_status_t io_hal_fmc_nor_flash_get_status(ioal_fmc_nor_hdle* ioal_nor, uint32 address, uint32 timeout);

/**
 * @brief  This API erase the entire NOR chip.
 */
hal_ret_sts io_hal_fmc_nor_flash_write_enable(ioal_fmc_nor_hdle* ioal_nor);

/**
 * @brief  This API read data from FMC-NOR-FLASH
 */
hal_ret_sts io_hal_fmc_nor_flash_write_disable(ioal_fmc_nor_hdle* ioal_nor);

/**
 * @brief  This API erase the specified block of the NOR memory
 */
hal_ret_sts io_hal_fmc_nor_flash_return_to_read_mode(ioal_fmc_nor_hdle* ioal_nor);

/**
 * @brief  This API erase the entire NOR chip.
 */
hal_ret_sts io_hal_fmc_nor_flash_read_id(ioal_fmc_nor_hdle *ioal_nor, ioal_nor_id_t *id);

/**
 * @brief  This API erase the entire NOR chip.
 */
hal_ret_sts io_hal_fmc_nor_flash_read_cfi(ioal_fmc_nor_hdle *ioal_nor, ioal_nor_cfi_t *cfi);


/**
 * @brief  This API Re-initializes the control block memory
 * and do the basic configurations for FMC-NOR-FLASH
 */
hal_ret_sts io_hal_fmc_nor_flash_reinit(io_inst_id inst_id);

#endif
