
/*
 * @file mt25ql512.h
 *
 *  @brief This file contains the macros,structure declaration,enumeration,
 *  function declaration of flash memory mx25l512
 *
 *	@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_MT25QL512_H_
#define _INC_MT25QL512_H_

#include <stdint.h>
/**
  * @brief MT25QL512 NOR flash device structure.
  */
typedef struct
{
  void  *io_intf_hdle;						/*!< interface handle				*/
  uint8_t slave_address;					/*!< slave_address					*/
  uint16_t device_id;						/*!< device_id						*/
  uint16_t manufacture_id;					/*!<manufacture_id					*/
} mt25ql512_dev_t;


/**
 * @brief Initialize Mx25l512 chip
 * @param[in] mt25ql512_dev_t: structure handle for MT25QL512 nor flash
 * @retval uint8_t - returns the success or error code
 */
uint8_t mt25ql512_init(mt25ql512_dev_t *dev_hdl);

/**
 * @brief Erase memory by sector mode
 * @param[in] mt25ql512_dev_t: structure handle for MT25QL512 nor flash
 * @param[in] EraseStartAddress: Start Erase to be erased
 * @param[in] EraseStartAddress: End Erase to be erased
 * @retval uint8_t - returns the success or error code
 */
uint8_t mt25ql512_EraseSector(mt25ql512_dev_t *dev_hdl, uint32_t EraseStartAddress, uint32_t EraseEndAddress);

/**
 * @brief Write data to memory
 * @param[in] mt25ql512_dev_t: structure handle for MT25QL512 nor flash
 * @param[in] buffer: data to be written in memory
 * @param[in] address: Address to write data
 * @param[in] buffer_size: Data size
 * @retval uint8_t - returns the success or error code
 */
uint8_t mt25ql512_write(mt25ql512_dev_t *dev_hdl, uint8_t* buffer, uint32_t address, uint32_t buffer_size);

/**
 * @brief Read data from memory
 * @param[in] mt25ql512_dev_t: structure handle for MT25QL512 nor flash
 * @param[in] buffer: data to be read from memory
 * @param[in] address: data read from address
 * @param[in] buffer_size: Data size
 * @retval uint8_t - returns the success or error code
 */
uint8_t mt25ql512_read(mt25ql512_dev_t *dev_hdl, uint8_t* buffer, uint32_t address,uint32_t buffer_size);


/**
 * @brief Read data from memory
 * @param[in] mt25ql512_dev_t: structure handle for MT25QL512 nor flash
 * @param[in] buffer: data to be read from memory
 * @param[in] address: data read from address
 * @param[in] buffer_size: Data size
 * @retval uint8_t - returns the success or error code
 */
uint8_t mt25ql512_EnableMemoryMappedMode(mt25ql512_dev_t *dev_hdl);

/**
 * @brief Erase full chip
 * @param[in] mt25ql512_dev_t: structure handle for MT25QL512 nor flash
 * @retval uint8_t - returns the success or error code
 */
uint8_t mt25ql512_Erase_Chip (mt25ql512_dev_t *dev_hdl);






/*MT25QL512 memory parameters*/
#define MEMORY_FLASH_SIZE               0x4000000 /* 512 MBits*/
#define MEMORY_SECTOR_SIZE              0x10000   /* 64kBytes */
#define MEMORY_SUBSECTOR1_SIZE          0x8000    /* 32kBytes */
#define MEMORY_SUBSECTOR2_SIZE          0x1000    /* 4kBytes */
#define MEMORY_PAGE_SIZE                0x100     /* 256 bytes */


/*MT25QL512 commands */
#define WRITE_ENABLE_CMD 				0x06
#define READ_STATUS_REG_CMD 			0x05
#define ENTER_4_BYTE_ADD_CMD 			0xB7
#define WRITE_VOL_CFG_REG_CMD 			0x81
#define SECTOR_ERASE_CMD 				0xD8
#define SUB_SECTOR_ERASE_CMD1 			0x52
#define SUB_SECTOR_ERASE_CMD2 			0x20
#define CHIP_ERASE_CMD	 				0xC7
#define QUAD_IN_FAST_PROG_CMD 			0x32
#define READ_CONFIGURATION_REG_CMD 		0x85
#define QUAD_OUT_FAST_READ_CMD 			0x6B
#define DUMMY_CLOCK_CYCLES_READ_QUAD 	8
#define RESET_ENABLE_CMD 				0x66
#define RESET_EXECUTE_CMD 				0x99

/*MT25QL512 timeouts*/
#define QUADSPI_MAX_ERASE_TIMEOUT 		460000 /* 460s max */

#endif /* _INC_MT25QL512_H_ */
