
/*
 * @file MT28ew128.h
 *
 *  @brief This file contains the macros,structure declaration,enumeration,
 *  function declaration of flash memory mt28ew128
 *
 *	@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_MT28EW128_H_
#define _INC_MT28EW128_H_

#include <stdint.h>

#define MT28EW128_SCS 0
#define MT28EW128_ERR 1

/*MT28ew128 memory parameters*/
#define MT28EW128_PAGE_SIZE 512           //512 words
#define MT28EW128_SECTOR_SIZE 64*1024     //64 kilo words
#define mt28ew128_mem_tIMEOUT 0xFF
#define MT28EW128_MEMORY_FLASH_SIZE     0x8000000 /* 128 MBits*/

#define NOR_MEMORY_ADRESS 0x60000000
/**
  * @brief MT28ew128 NOR flash device structure.
  */
typedef struct
{
  void  *io_intf_hdle;						/*!< interface handle				*/
  uint16_t device_id;						/*!< device_id						*/
  uint16_t manufacture_id;					/*!<manufacture_id					*/
} mt28ew128_mem_t;


/**
 * @brief Initialize Mx25l512 chip
 * @param[in] mt28ew128_mem_t: structure handle for MT28ew128 nor flash
 * @retval uint8_t - returns the success or error code
 */
uint8_t mt28ew128_init(mt28ew128_mem_t* flsh_hdlr);

/**
 * @brief Erase memory by sector mode
 * @param[in] mt28ew128_mem_t: structure handle for MT28ew128 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 mt28ew128_erase_block(mt28ew128_mem_t* flsh_hdlr, uint32_t b_address);

/**
 * @brief Erase memory by sector mode
 * @param[in] mt28ew128_mem_t: structure handle for MT28ew128 nor flash
 * @param[in] start_address: Start Erase to be erased
 * @param[in] erase_len: End Erase to be erased
 * @retval uint8_t - returns the success or error code
 */
uint8_t mt28ew128_erase(mt28ew128_mem_t* flsh_hdlr, uint32_t start_address , int32_t erase_len);

/**
 * @brief Erase memory by sector mode
 * @param[in] mt28ew128_mem_t: structure handle for MT28ew128 nor flash
 * @param[in] start_address: Start Erase to be erased
 * @param[in] erase_len: End Erase to be erased
 * @retval uint8_t - returns the success or error code
 */
uint8_t mt28ew128_erase_chip(mt28ew128_mem_t* flsh_hdlr);

/**
 * @brief Write data to memory
 * @param[in] mt28ew128_mem_t: structure handle for MT28ew128 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 mt28ew128_write(mt28ew128_mem_t* flsh_hdlr, uint32_t w_address, uint8_t *data, int32_t data_len);

/**
 * @brief Read data from memory
 * @param[in] mt28ew128_mem_t: structure handle for MT28ew128 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 mt28ew128_read(mt28ew128_mem_t* flsh_hdlr, uint32_t r_address, uint8_t *data, uint32_t data_len);

/**
 * @brief Erase full chip
 * @param[in] mt28ew128_mem_t: structure handle for MT28ew128 nor flash
 * @retval uint8_t - returns the success or error code
 */
uint8_t mt28ew128_erase_and_write(mt28ew128_mem_t* flsh_hdlr, uint32_t w_address, uint8_t *data, int32_t data_len);


#endif /* _INC_MT28ew128_H_ */
