/*  ============================================================================
 *   Copyright (c) Texas Instruments Inc 2012
 *
 *   Use of this software is controlled by the terms and conditions found in the
 *   license agreement under which this software has been supplied.
 *   ===========================================================================
 */
/** ============================================================================
 example for SPI EEPROM driver on KeyStone DSP. 
 Tested on AT225640B and N25Q128.
 * =============================================================================
 *  Revision History
 *  ===============
 *  Feb 20, 2012 Brighton Feng  file created
 * =============================================================================*/
#ifndef _SPI_EEPROM_DRV_H
#define _SPI_EEPROM_DRV_H

#include <tistdtypes.h>

typedef enum {
	SPI_EEPROM_WRSR =1, /*Write Status Register      */
	SPI_EEPROM_WRITE=2, /*Write Data to Memory Array */
	SPI_EEPROM_READ =3, /*Read Data from Memory Array*/
	SPI_EEPROM_WRDI =4, /*Reset Write Enable Latch   */
	SPI_EEPROM_RDSR =5, /*Read Status Register       */
	SPI_EEPROM_WREN =6,  /*Set Write Enable Latch     */
	SPI_EEPROM_RDID         =0x9F,  /*read ID*/
	SPI_EEPROM_EN4B         =0xB7,  /*enter 4-byte mode*/
	SPI_EEPROM_SUB_SECTOR_ERASE =0x20,  /*Sub-Sector (4KB) Erase*/
	SPI_EEPROM_SECTOR_ERASE =0xD8,  /*Sector (64KB) Erase  */
	SPI_EEPROM_BULK_ERASE   =0xC7  /*Bulk Erase    */
}SPI_EEPROM_Instruction;

extern SPI_Data_Format EepromDataFormat;
extern SPI_Transfer_Param EepromTransferParam;

extern Uint32 uiSPI_EEPROM_page_size; 	/*must be power of 2*/
extern Uint32 uiSPI_EEPROM_sector_size;	/*must be power of 2*/
extern Bool needEraseBeforeWrite; 	

/*write a commond to EEPROM*/
static inline Uint32 SPI_EEPROM_command(Uint8 command) 
{ 
	Uint8 spiBuffer[4]; 

	spiBuffer[0]= command;	/*command*/
	return KeyStone_SPI_TxRx(spiBuffer, 0, 1,	
		NULL, 0, 0, &EepromTransferParam);
}

/*read "uiByteCount" data to "ucBuffer" from "data_address" of SPI EEPROM.
return number of bytes transfered*/
extern Uint32 SPI_EEPROM_read(Uint32 uiAddress, 
	Uint32 address_width, Uint32 uiByteCount, Uint8 * ucBuffer);

/*write "uiByteCount" data in "ucBuffer" to address "data_address" 
in the SPI EEPROM.*/
extern Uint32 SPI_EEPROM_write(Uint32 uiAddress, 
	Uint32 address_width, Uint32 uiByteCount, Uint8 * ucBuffer);

/*erase the whole EEPROM*/
extern Uint32 SPI_EEPROM_bulk_erase();

/*erase a section in EEPROM*/
extern Uint32 SPI_EEPROM_erase(Uint32 start_address, 
	Uint32 byteCount, Uint32 address_width);

/*read the ID of the EEPROM*/
extern Uint32 SPI_EEPROM_ID_read(Uint8 * ucBuffer); 

#endif

