/*
 * @file mx25l512.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_MX25L512_H_
#define _INC_MX25L512_H_



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







/**
  * @brief MX25L512 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					*/
} mx25l512_dev_t;



/*MX25L512 memory parameters*/
#define MEMORY_FLASH_SIZE               0x4000000 /* 512 MBits => 64MBytes */
#define MEMORY_BLOCK_SIZE               0x10000   /* 1024 sectors of 64KBytes */
#define MEMORY_SECTOR_SIZE              0x1000    /* 16384 sub sectors of 4kBytes */
#define MEMORY_PAGE_SIZE                0x100     /* 262144 pages of 256 bytes */


/*MX25L512 operation commands */
#define WRITE_ENABLE_CMD 0x06
#define READ_STATUS_REG_CMD 0x05
#define READ_FLAG_STATUS_REG_CMD 0x70
#define WRITE_STATUS_REG_CMD 0x01
#define SECTOR_ERASE_CMD 0x20
#define CHIP_ERASE_CMD 0xC7
#define QUAD_IN_FAST_PROG_CMD 0x38
#define READ_CONFIGURATION_REG_CMD 0x15
#define QUAD_READ_IO_CMD 0xEC
#define QUAD_OUT_FAST_READ_CMD 0x6B
#define QPI_ENABLE_CMD 0x35
#define DUMMY_CLOCK_CYCLES_READ_QUAD 10
#define RESET_ENABLE_CMD 0x66
#define RESET_EXECUTE_CMD 0x99
#define DISABLE_QIP_MODE 0xf5




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

/**
 * @brief Erase memory by sector mode
 * @param[in] mx25l512_dev_t: structure handle for MX25l512 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 mx25l512_EraseSector(mx25l512_dev_t *dev_hdl, uint32_t EraseStartAddress, uint32_t EraseEndAddress, uint32_t erase_mode);

/**
 * @brief Write data to memory
 * @param[in] mx25l512_dev_t: structure handle for MX25l512 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 mx25l512_write(mx25l512_dev_t *dev_hdl, uint8_t* buffer, uint32_t address, uint32_t buffer_size);

/**
 * @brief Read data from memory
 * @param[in] mx25l512_dev_t: structure handle for MX25l512 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 mx25l512_read(mx25l512_dev_t *dev_hdl, uint8_t* buffer, uint32_t address,uint32_t buffer_size);

/**
 * @brief Read data from memory
 * @param[in] mx25l512_dev_t: structure handle for MX25l512 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 mx25l512_EnableMemoryMappedMode(mx25l512_dev_t *dev_hdl);

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







#endif /* _INC_MX25L512_H_ */
