/**
 * @file exo_ahw_al_ext_flash_common.h
 *
 * @brief This file contains function prototype,structure and enumeration for external flash memories
 *
 * @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 _INC_EXO_AHW_AL_EXT_MEMORY_H_
#define _INC_EXO_AHW_AL_EXT_MEMORY_H_


#include <stdint.h>
#include "exo_io_al_common.h"
#include "exo_hal_common.h"
#include "exo_ahw_al_common.h"
#include "exo_osal.h"

#ifdef COREBOARD
#define EXT_MEM_QSPI_FLASH EXT_MEM_MT25QL512_QSPI 		/// coreboard qspi flash
#else
#define EXT_MEM_QSPI_FLASH EXT_MEM_MX25L512_QSPI		/// discovery qspi flash
#endif

#define AHAL_FLASH_MAX_SEM_TIMEOUT 12*1000
#define ERASE_BY_SECTOR 	0
#define ERASE_BY_BLOCK64 	1

/**
 * @brief external flash memory structure.
 */
typedef struct
{
	ahw_al_gen_info ahw_gen_info;
	os_sem_handle_ptr semaphore_hdle;
	uint8_t erase_mode;
	uint32_t flash_size;
}ahw_al_ext_mem_hdl;

/**
 * @brief  API to initialize the external flash memory
 * @param[in]mem_arg : structure handle of external flash memory handle.
 * @ahw_id  : app hardware ID
 * @retval hal_ret_sts - returns the success or error code
 */
hal_ret_sts ahw_al_ext_mem_driver_init(ahw_inst_id_t ahw_id);

/**
 * @brief  API to write data in external flash memory
 * @param[in]mem_arg : structure handle of external flash memory handle.
 * @param address : memory address where the data will be written
 * @param data    : input data buffer to be written in address
 * @param size    : Number of data to be written
 * @retval hal_ret_sts - returns the success or error code
 */
hal_ret_sts ahw_al_ext_mem_write(ahw_inst_id_t ahw_id, uint32_t address, uint8_t *data, uint32_t size);

/**
 * @brief API to read from external flash memory
 * @param[in]mem_arg : structure handle of external flash memory handle.
 * @param address : memory address where the data will be read
 * @param data    : output data buffer to be used to hold read data from external flash memory
 * @param size    : Number of data to be read
 * @retval hal_ret_sts - returns the success or error code
 */
hal_ret_sts ahw_al_ext_mem_read(ahw_inst_id_t ahw_id, uint32_t address, uint8_t *data, uint32_t size);

/**
 * @brief API to erase external flash memory
 * @param[in]mem_arg : structure handle of external flash memory handle.
 * @param start_address  : erase start address
 * @param end_address    : erase end address
 * @retval hal_ret_sts - returns the success or error code
 */
hal_ret_sts ahw_al_ext_mem_erase(ahw_inst_id_t ahw_id, uint32_t start_address, uint32_t end_address);

/**
 * @brief API to enable memory mapped mode
 * @param[in]mem_arg : structure handle of external flash memory handle.
 * @retval hal_ret_sts - returns the success or error code
 */
hal_ret_sts ahw_al_ext_mem_enable_memory_map(ahw_inst_id_t ahw_id);

hal_ret_sts ahw_al_ext_mem_erase_and_write(ahw_inst_id_t ahw_id, uint32_t address, uint8_t *data, uint32_t size);

/**
 * @brief  API to Re-initialize the external flash memory
 * @param[in]mem_arg : structure handle of external flash memory handle.
 * @ahw_id  : app hardware ID
 * @retval hal_ret_sts - returns the success or error code
 */
hal_ret_sts ahw_al_ext_mem_reinit(ahw_inst_id_t ahw_id);




#endif /* _INC_EXO_AHW_AL_EXT_MEMORY_H_ */
