//----------------------------------------------------
/**
	\file		ctk/hw/dataflash_at45db_cmd.h
	\ingroup	hardware
	\ingroup	all

	\brief		Knihovna pro pristup k DataFlash pameti AT45DB011
				<<< ALL ??? >>>
	\note		Urceno pro vlozeni do hwboard

	\author		ConTeK s.r.o., Pavel Bazika
	\version	01.02
	\date		02.04.2007
*/
//----------------------------------------------------
#ifndef DATAFLASH_AT45DB_CMD_H_
#define DATAFLASH_AT45DB_CMD_H_

#include <libsets.h>
#include <spi.h>

//----------------------------------------------------
//!	\name	Vyznam hodnot status registru
//@{

#define DF_STATUS_READY				0x80	//!< Dataflash je pripravena
#define DF_STATUS_NOMATCH			0x40	//!< Compare FALSE
#define DF_STATUS_DENSITY			0x3C	//!< Oblast ulozeni kapacity dataflash
#define DF_STATUS_DENSITY_ATDB011	0x08	//!< Kapacita 1Mbit, pozor na poslendi bit, overit
#define DF_STATUS_DENSITY_ATDB161D	0x2C	//!< Kapacita 16MbitD

// Pouze varianta D
#define DF_STATUS_PROTECT			0x02	//!< Sektory jsou HW ci SW chraneny
#define DF_STATUS_PAGESIZE_BIN		0x01 	//!< Dataflash je v rezimu 2^N velikosti stranek
//@}

//----------------------------------------------------
//!	\name	Vyznam hodnot v mape sektoru
//@{

//	Pouze varianta D
#define DF_SECTOR_0A_PROTECT		0xC0	//!< \brief	Sektor 0A chranen
#define DF_SECTOR_0B_PROTECT		0x30	//!< Sektor 0B chranen
#define DF_SECTOR_PROTECT			0xFF	//!< Sektor 1-x chranen
#define DF_SECTOR_UNPROTECT			0x00	//!< Sektro 1-x bez ochrany
//@}



//----------------------------------------------------
//!	\name	Konstanty s indexem 1. bufferu
//@{

// pouze varianta D
#define DO_WRITE_BUF1			DO_WRITE_BUF
#define DO_WRITE_BUF12MEM_E	DO_WRITE_BUF2MEM_E
#define DO_WRITE_BUF12MEM		DO_WRITE_BUF2MEM
#define DO_WRITE_MEM1			DO_WRITE_MEM
#define DO_READ_PAGE2BUF1		DO_READ_PAGE2BUF
#define DO_COMPARE1			DO_COMPARE
#define DO_AUTO_REWRITE1		DO_AUTO_REWRITE
//@}


//----------------------------------------------------
//!	Struktura obsahujici prikaz s adresou

typedef struct _DFC_ADDR
{
	uint8_t		OpCode;
	uint8_t		Addr[3];
} __attribute__((packed)) DFC_ADDR;


//----------------------------------------------------
//!	Struktura obsahujici prikaz s adresou a 1 vyplnovym bytem

typedef struct _DFC_ADDR_DUM1
{
	uint8_t		OpCode;
	uint8_t		Addr[3];
	uint8_t		Reserved;
} __attribute__((packed)) DFC_ADDR_DUM1;


//----------------------------------------------------
//!	Struktura obsahujici prikaz s adresou a 4 vyplnovymi byty

typedef struct _DFC_ADDR_DUM4
{
	uint8_t		OpCode;
	uint8_t		Addr[3];
	uint32_t	Reserved;
} __attribute__((packed)) DFC_ADDR_DUM4;


//----------------------------------------------------
//!	Struktura jednobytoveho prikazu

typedef struct _DFC_BYTE
{
	uint8_t		OpCode;
} __attribute__((packed)) DFC_BYTE;


//----------------------------------------------------
//!	Struktura prikazu v podobe 4 bytoveho magic cisla

typedef struct _DFC_MAGIC
{
	uint8_t		Command[4];
} __attribute__((packed)) DFC_MAGIC;


//----------------------------------------------------
//!	Struktura prikazu pro uzamknuti sektoru

typedef struct DFC_LOCKDOWN
{
	uint8_t		Command[4];
	uint8_t		Addr[3];
} __attribute__((packed)) DFC_LOCKDOWN;


//----------------------------------------------------
//!	Struktura mapy sektoru

typedef struct _DF_PROTECT_REG
{
	uint8_t		Sector0;
	uint8_t		Sectors[15];
} __attribute__((packed)) DF_PROTECT_REG;


//----------------------------------------------------
//!	Struktura JEDEC device ID

typedef struct _DF_DEVID
{
	uint8_t		ManufID;
	uint16_t	DeviceID;
	uint8_t		Extended;
} __attribute__((packed)) DF_DEVID;


//----------------------------------------------------
//!	Prikazy dataflash

typedef enum _DATAFLASH_OP
{
	//DO_READ_PAGE 		= 0x52,			// cteni stranky primo z pameti
	DO_READ_PAGE 		= 0xD2,			// cteni stranky primo z pameti
	DO_READ_PAGE2BUF	= 0x53,			// nahrani stranky z pameti do bufferu
	DO_READ_BUF			= 0x54,			// cteni bufferu

	DO_COMPARE			= 0x60,			// porovnani bufferu s obsahem pameti

	DO_ERASE			= 0x81,			// vymaz stranky
	DO_ERASE_BLOCK		= 0x50,			// vymaz bloku osmi stranek

	DO_WRITE_MEM		= 0x82,			// zapis primo do pameti pres buffer
	DO_WRITE_BUF2MEM_E	= 0x83,			// zapis z bufferu do pameti s automatickym vymazem
	DO_WRITE_BUF		= 0x84,			// zapis dat do bufferu
	DO_WRITE_BUF2MEM	= 0x88,			// zapis z bufferu do pameti bez automatickeho vymazu

	DO_AUTO_REWRITE		= 0x58,			// nacteni a zapis teze stranky

	DO_READ_STATUS		= 0xD7,			// cteni status registru
//	DO_READ_STATUS		= 0x57,			// cteni status registru

	DDO_READ_CONT_LEGACY	= 0xE8,		// souvisle cteni do 66 MHz, 4 byty inicializace
	DDO_READ_CONT_HI		= 0x0B,		// souvisle cteni do 66 MHz, 1 byte inicializace
	DDO_READ_CONT_LO		= 0x03,		// souvisle cteni do 33 MHz, bez inicializacnich bytu

	DDO_READ_PAGE2BUF2		= 0x55,		// cteni straky do bufferu 2

	DDO_READ_BUF1_HI		= 0xD4,		// cteni prvniho bufferu vysokou rychlosti
	DDO_READ_BUF1_LO		= 0xD1,		// cteni prvniho bufferu nizkou rychlosti

	DDO_READ_BUF2_HI		= 0xD6,		// cteni druheho bufferu vysokou rychlosti
	DDO_READ_BUF2_LO		= 0xD3,		// cteni druheho bufferu nizkou rychlosti

	DDO_COMPARE2			= 0x61,		// porovnani obsahu stranky s bufferem 2

	DDO_WRITE_BUF2			= 0x87,		// zapis dat do bufferu 2
	DDO_WRITE_BUF22MEM_E	= 0x86,		// zapis dat z bufferu 2 do flash s vymazem
	DDO_WRITE_BUF22MEM		= 0x89,		// zapis dat z buffery 2 do flash
	DDO_WRITE_MEM2			= 0x85,		// zapis primo do pameti pres buffer 2

	DDO_AUTO_REWRITE2		= 0x59,		// auto page rewrite za pomoci bufferu 2

	DDO_ERASE_SECTOR		= 0x7C,		// vymaz sektoru pameti

	DDO_SECPR_ENABLE		= 0xA9,		// zamknuti sektoru nastavenych v zamykacim registru
	DDO_SECPR_DISABLE		= 0x9A,		// odemknuti vsech sektoru
	DDO_SECPR_ERASE			= 0xCF,		// vymaz zamykaciho registru
	DDO_SECPR_WRITE			= 0xFC,		// zapis zamykaciho registru
	DDO_SECPR_READ			= 0x32, 	// cteni zamykaciho registru

	DDO_LOCKDOWN			= 0x30,		// uzamknuti sektoru, trvale
	DDO_READ_LOCKDOWN		= 0x35,		// cteni stavu uzamknutych sektoru

	DDO_SECREG_WRITE		= 0x9B,		// zapis do security registru
	DDO_SECREG_READ			= 0x77,		// cteni security registru

	DDO_LOWPWR_START		= 0xB9,		// start deep-power down mode
	DDO_LOWPWR_STOP			= 0xAB,		// stop deep-power down mode

	DDO_BINPAGE_M3			= 0x80,		// prikaz pro prepnuti do 512 bytovych stranek
	DDO_BINPAGE				= 0xA6,		// prikaz pro prepnuti do 512 bytovych stranek

	DDO_READ_DEVID			= 0x9F,		// cteni JEDEC ID

	DDO_SECPR_MAGIC1		= 0x3D,
	DDO_SECPR_MAGIC2		= 0x2A,
	DDO_SECPR_MAGIC3		= 0xFC

} DATAFLASH_OP;


struct _DATAFLASH_CTX;


//----------------------------------------------------
/**
	\brief		Nacteni stranky z dataflash do pameti procesoru

	\param		ctx				Kontext dataflash
	\param		CmdStruct		Struktura, ktera bude naplnena prikazem, je prenasena pres DMA
	\param		Page			Cislo stranky, ze ktere se bude cist
	\param		Addr			Adresa v ramci stranky
	\param		Buffer			Buffer pro nactena data
	\param		Size			Pocet bytu k precteni
	\param		Callback		Callback volany po dokonceni transferu prikazu
	\param		UserData		Data predana callbacku

	\return		FALSE, pokud by cteni presahlo hranici stranky, jinak TRUE
*/
//----------------------------------------------------

bool_t DataFlashCmdReadPage(struct _DATAFLASH_CTX * const _ctx, DFC_ADDR_DUM4 * const _cmdStruct, const uint16_t _page,
		const uint16_t _addr, void * const _buffer, const uint16_t _size, const SPI_CALLBACK _callBack, void * const _cbData);


//----------------------------------------------------
/**
	\name		Nacteni stranky z dataflash do interniho bufferu

	\param		ctx				Kontext dataflash
	\param		CmdStruct		Struktura, ktera bude naplnena prikazem, je prenasena pres DMA
	\param		Page			Cislo stranky, do ktere se bude zapisovat
	\param		Callback		Callback volany po dokonceni transferu prikazu
	\param		UserData		Data predana callbacku

	\return		FALSE pri spatne adresaci, nebo v pripade chyby

	\note		Dokonceni operace je indikovano ve status registru dataflash
*/
//-----------------------------------------------------
bool_t DataFlashCmdReadPage2Buf1(struct _DATAFLASH_CTX * const ctx, DFC_ADDR * const CmdStruct,
		const uint16_t Page, const SPI_CALLBACK Callback, void * const UserData);


//----------------------------------------------------
/**
	\name		Nacteni interniho bufferu do pameti procesoru

	\param		ctx				Kontext dataflash
	\param		CmdStruct		Struktura, ktera bude naplnena prikazem, je prenasena pres DMA
	\param		Addr			Adresa v bufferu, od ktere se zacne cist
	\param		Buffer			Buffer pro nactena data
	\param		Size			Pocet ctenych bytu
	\param		Callback		Callback volany po dokonceni transferu prikazu
	\param		UserData		Data predana callbacku

	\return		FALSE, pokud by cteni presahlo hranici interniho bufferu, jinak TRUE
*/
//----------------------------------------------------
//@{

//-------------------------
//!	\brief		Nacteni bufferu 1, ci jedineho bufferu

bool_t DataFlashCmdReadBuf1(struct _DATAFLASH_CTX * const ctx, DFC_ADDR_DUM1 * const CmdStruct, const uint16_t Addr,
		void * const Buffer, const uint16_t Size, const SPI_CALLBACK Callback, void * const UserData);


//----------------------------------------------------
/**
	\name		Porovnani obsahu bufferu s obsahem flash

	\param		ctx				Kontext dataflash
	\param		CmdStruct		Struktura, ktera bude naplnena prikazem, je prenasena pres DMA
	\param		Page			Cislo stranky, se kterou se bude porovnavat
	\param		Callback		Callback volany po dokonceni transferu prikazu
	\param		UserData		Data predana callbacku

	\return		FALSE pri spatne adresaci

	\note		Dokonceni operace je indikovano ve status registru dataflash
	\note		Vysledek operace porovnani je k dispozici ve stavovem registru dataflash
*/
//----------------------------------------------------
//@{

//-------------------------
//!	\brief		Porovnani bufferu 1, ci jedineho bufferu

bool_t DataFlashCmdCompare1(struct _DATAFLASH_CTX * const ctx, DFC_ADDR * const CmdStruct, const uint16_t Page,
		const SPI_CALLBACK Callback, void * const UserData);


//-------------------------
//!	\brief		Porovnani bufferu 2

bool_t DataFlashDCmdCompare2(struct _DATAFLASH_CTX * const ctx, DFC_ADDR * const CmdStruct, const uint16_t Page,
		const SPI_CALLBACK Callback, void * const UserData);
//@}

//----------------------------------------------------
/**
	\name		Zapis dat do stranky

	\param		ctx				Kontext dataflash
	\param		CmdStruct		Struktura, ktera bude naplnena prikazem, je prenasena pres DMA
	\param		Page			Cislo stranky, do ktere se bude zapisovat
	\param		Addr			Adresa v ramci stranky
	\param		Buffer			Data k zapisu
	\param		Size			Pocet bytu k zapisu
	\param		Callback		Callback volany po dokonceni transferu prikazu
	\param		UserData		Data predana callbacku

	\return		FALSE, pokud by zapis presahl hranici stranky, jinak TRUE

	\note		Dokonceni operace je indikovano ve status registru dataflash
*/
//----------------------------------------------------
//@{

//-------------------------
//!	\brief		Zapis dat pres buffer 2

bool_t DataFlashDCmdWrite2(struct _DATAFLASH_CTX * const ctx, DFC_ADDR * const CmdStruct, const uint16_t Page,
		const uint16_t Addr, void * const Buffer, const uint16_t Size, const SPI_CALLBACK Callback, void * const UserData);
//@}


//----------------------------------------------------
/**
	\name		Zapis dat z bufferu do stranky dataflash s automatickym vymazem

	\param		ctx				Kontext dataflash
	\param		CmdStruct		Struktura, ktera bude naplnena prikazem, je prenasena pres DMA
	\param		Page			Cislo stranky, do ktere se bude zapisovat
	\param		Callback		Callback volany po dokonceni transferu prikazu
	\param		UserData		Data predana callbacku

	\return		FALSE pri spatne adresaci

	\note		Dokonceni operace je indikovano ve status registru dataflash
*/
//----------------------------------------------------
//@{

//-------------------------
//!	\brief		Zapis z bufferu 1

bool_t DataFlashCmdWriteBuf12MemAutoErase(struct _DATAFLASH_CTX * const ctx, DFC_ADDR * const CmdStruct,
		const uint16_t Page, const SPI_CALLBACK Callback, void * const UserData);


//-------------------------
//!	\brief		Zapis z bufferu 2

bool_t DataFlashDCmdWriteBuf22MemAutoErase(struct _DATAFLASH_CTX * const ctx, DFC_ADDR * const CmdStruct,
		const uint16_t Page, const SPI_CALLBACK Callback, void * const UserData);
//@}

//----------------------------------------------------
/**
	\name		Zapis dat z pameti procesoru do interniho bufferu

	\param		ctx				Kontext dataflash
	\param		CmdStruct		Struktura, ktera bude naplnena prikazem, je prenasena pres DMA
	\param		Addr			Adresa v ramci bufferu
	\param		Buffer			Data k zapisu
	\param		Size			Pocet bytu k zapisu
	\param		Callback		Callback volany po dokonceni transferu prikazu
	\param		UserData		Data predana callbacku

	\return		FALSE, pokud by zapis presahl hranici bufferu, jinak TRUE
*/
//----------------------------------------------------
//@{

//-------------------------
//!	\brief		Zapis do bufferu 1, ci jedineho bufferu

bool_t DataFlashCmdWriteBuf1(struct _DATAFLASH_CTX * const ctx, DFC_ADDR * const CmdStruct, const uint16_t Addr,
		void * const Buffer, const uint16_t Size, const SPI_CALLBACK Callback, void * const UserData);


//-------------------------
//!	\brief		Zapis do bufferu 2

bool_t DataFlashDCmdWriteBuf2(struct _DATAFLASH_CTX * const ctx, DFC_ADDR * const CmdStruct, const uint16_t Addr,
		void * const Buffer, const uint16_t Size, const SPI_CALLBACK Callback, void * const UserData);
//@}

//----------------------------------------------------
/**
	\brief		Vymaz stranky flash

	\param		ctx				Kontext dataflash
	\param		CmdStruct		Struktura, ktera bude naplnena prikazem, je prenasena pres DMA
	\param		Page			Cislo stranky, ktera se bude mazat
	\param		Callback		Callback volany po dokonceni transferu prikazu
	\param		UserData		Data predana callbacku

	\return		FALSE pri spatne adresaci

	\note		Dokonceni operace je indikovano ve status registru dataflash
*/
//----------------------------------------------------


bool_t DataFlashCmdErase(struct _DATAFLASH_CTX * const _ctx, DFC_ADDR * const _cmdStruct, const uint16_t _page,
		const SPI_CALLBACK _callBack, void * const _cbData);


//----------------------------------------------------
/**
	\brief		Cteni status registru

	\param		ctx				Kontext dataflash
	\param		CmdStruct		Struktura, ktera bude naplnena prikazem, je prenasena pres DMA
	\param		Buffer			Misto pro ulozeni obsahu status registru, prenos pres DMA
	\param		Callback		Callback volany po dokonceni transferu prikazu
	\param		UserData		Data predana callbacku
*/
//----------------------------------------------------

bool_t DataFlashCmdReadStatus(struct _DATAFLASH_CTX * const _ctx, DFC_BYTE * const _cmdStruct, uint8_t * const _buffer,
		const SPI_CALLBACK _callBack, void * const _cbData);


#endif // DATAFLASH_AT45DB_CMD_H_
