#ifndef _FLASH_H
#define _FLASH_H
/**********************************************************************/
#define     UBYTE       unsigned char
#define     SBYTE                char
#define     UWORD       unsigned  int
#define     ULONG       unsigned  long int
//==============================================================================
#define        SectorsPerPage         32
#define        Flash_Sector_Size      512
#define        Flash_Page_Min         0x3E
#define        Flash_Page_Max         0x3F
#define        Flash_Blocks           1          // the number of flash blocks
#define        Flash_Pages_Per_Block  4          // the number of flash blocks
//==============================================================================
#define pg_sec(x,y)   (((((unsigned long)x)<<16) + ((((unsigned int)y)*Flash_Sector_Size)+0x8000UL)))
#define getCCR_disI() { asm TPA; asm STAA CCRbackup; asm SEI; } // save CCR disable interrupts
#define restoreCCR()  { asm LDAA CCRbackup; asm TAP;  }          // restore CCR
//==============================================================================
// flash error codes
//==============================================================================
#define FLASHerrOK           0         // OK
#define FLASHerrRANGE        1         // address is out of range
#define FLASHerrVALUE        2         // Read value is not equal to written value
#define FLASHerrNOTAVAIL     3         // Desired program/erase operation is not available
#define FLASHerrBUSY         4         // Flash is busy
#define ERR_NOTAVAIL         5         // Requested value or method not available.

//==============================================================================
// flash function definitions
//==============================================================================
#pragma CODE_SEG NON_BANKED
//==============================================================================
extern UBYTE Flash_Write_Word(ULONG addr,UWORD data);
extern UBYTE Flash_Erase_Sector(ULONG addr);
extern UBYTE Flash_Write_Block(ULONG address_source,ULONG address_destination,UWORD count);
extern UBYTE Flash_CatchError(UBYTE err, UBYTE _place);
//==============================================================================
#pragma CODE_SEG DEFAULT
//==============================================================================
extern UBYTE Flash_Init(unsigned long oscclk, unsigned long tbus); // [kHz,ns]
//==============================================================================
// Place flash routines into non banked memory because following function has 
// return code RTS
// Following wariables has to be placed in RAM
//==============================================================================
#pragma DATA_SEG DEFAULT
static UBYTE WaitForFlash[]={
 
 0xC6, 0x80,                           // LDAB #$80      // command buffer empty = 0
 0x7B, 0x01, 0x05,                     // STAB _FSTAT
 
 0xC7,                                 // CLRB           // clear return value
 
 0x1E, 0x01, 0x05, 0x20, 0x05,         // BRSET _FSTAT,#32,*+10 
 0x1F, 0x01, 0x05, 0x10, 0x03,         // BRCLR _FSTAT,#16,*+8 
 0xC6, 0x05,                           // LDAB  #5             ;ERR_NOTAVAIL
 0x3D,                                 // RTS 
 0x1F, 0x01, 0x05, 0x40, 0xFB,         // BRCLR _FSTAT,#64,*+0 
 0x3D                                  // RTS
};
//==============================================================================
// definition of sectors


#define        PG_3E_SEC_00        (0x3E8000UL)
#define        PG_3E_SEC_01        (0x3E8200UL)
#define        PG_3E_SEC_02        (0x3E8400UL)
#define        PG_3E_SEC_03        (0x3E8600UL)
#define        PG_3E_SEC_04        (0x3E8800UL)
#define        PG_3E_SEC_05        (0x3E8A00UL)
#define        PG_3E_SEC_06        (0x3E8C00UL)
#define        PG_3E_SEC_07        (0x3E8E00UL)
#define        PG_3E_SEC_08        (0x3E9000UL)
#define        PG_3E_SEC_09        (0x3E9200UL)
#define        PG_3E_SEC_10        (0x3E9400UL)
#define        PG_3E_SEC_11        (0x3E9600UL)
#define        PG_3E_SEC_12        (0x3E9800UL)
#define        PG_3E_SEC_13        (0x3E9A00UL)
#define        PG_3E_SEC_14        (0x3E9C00UL)
#define        PG_3E_SEC_15        (0x3E9E00UL)
#define        PG_3E_SEC_16        (0x3EA000UL)
#define        PG_3E_SEC_17        (0x3EA200UL)
#define        PG_3E_SEC_18        (0x3EA400UL)
#define        PG_3E_SEC_19        (0x3EA600UL)
#define        PG_3E_SEC_20        (0x3EA800UL)
#define        PG_3E_SEC_21        (0x3EAA00UL)
#define        PG_3E_SEC_22        (0x3EAC00UL)
#define        PG_3E_SEC_23        (0x3EAE00UL)
#define        PG_3E_SEC_24        (0x3EB000UL)
#define        PG_3E_SEC_25        (0x3EB200UL)
#define        PG_3E_SEC_26        (0x3EB400UL)
#define        PG_3E_SEC_27        (0x3EB600UL)
#define        PG_3E_SEC_28        (0x3EB800UL)
#define        PG_3E_SEC_29        (0x3EBA00UL)
#define        PG_3E_SEC_30        (0x3EBC00UL)
#define        PG_3E_SEC_31        (0x3EBE00UL)

#define        PG_3F_SEC_00        (0x3F8000UL)
#define        PG_3F_SEC_01        (0x3F8200UL)
#define        PG_3F_SEC_02        (0x3F8400UL)
#define        PG_3F_SEC_03        (0x3F8600UL)
#define        PG_3F_SEC_04        (0x3F8800UL)
#define        PG_3F_SEC_05        (0x3F8A00UL)
#define        PG_3F_SEC_06        (0x3F8C00UL)
#define        PG_3F_SEC_07        (0x3F8E00UL)
#define        PG_3F_SEC_08        (0x3F9000UL)
#define        PG_3F_SEC_09        (0x3F9200UL)
#define        PG_3F_SEC_10        (0x3F9400UL)
#define        PG_3F_SEC_11        (0x3F9600UL)
#define        PG_3F_SEC_12        (0x3F9800UL)
#define        PG_3F_SEC_13        (0x3F9A00UL)
#define        PG_3F_SEC_14        (0x3F9C00UL)
#define        PG_3F_SEC_15        (0x3F9E00UL)
#define        PG_3F_SEC_16        (0x3FA000UL)
#define        PG_3F_SEC_17        (0x3FA200UL)
#define        PG_3F_SEC_18        (0x3FA400UL)
#define        PG_3F_SEC_19        (0x3FA600UL)
#define        PG_3F_SEC_20        (0x3FA800UL)
#define        PG_3F_SEC_21        (0x3FAA00UL)
#define        PG_3F_SEC_22        (0x3FAC00UL)
#define        PG_3F_SEC_23        (0x3FAE00UL)
#define        PG_3F_SEC_24        (0x3FB000UL)
#define        PG_3F_SEC_25        (0x3FB200UL)
#define        PG_3F_SEC_26        (0x3FB400UL)
#define        PG_3F_SEC_27        (0x3FB600UL)
#define        PG_3F_SEC_28        (0x3FB800UL)
#define        PG_3F_SEC_29        (0x3FBA00UL)
#define        PG_3F_SEC_30        (0x3FBC00UL)
#define        PG_3F_SEC_31        (0x3FBE00UL)

/**********************************************************************/
#endif