/* Tab Spacing = 4 */

/**********************************************************************
*
*   File Name:  Flash_API.h
*
*   Content: CPU Re-write functions for M16C and R8C and M32C.
*
*   Revision 2.1  July 8, 2004
*   Revision 2.2  February 2, 2006 (added R8C dervitives)
*   Revision 2.3  March 29, 2006 (modified order of _near & _far specifiers)
***********************************************************************/
#ifndef _FLASH_API_H
#define _FLASH_API_H

/*
  [ Availible Functions ]
  unsigned char FlashErase( unsigned char block );
  unsigned char FlashWrite( FLASH_PTR_TYPE flash_addr,
                            BUF_PTR_TYPE buffer_addr,
                            unsigned int bytes);
*/


/*******************
 * SELECT YOUR MCU *
 *******************
 * Select your desired device by removing the comment markers
 * from the beginging of the line.
 * Alternatively, you could define your MCU in your Hew project settings
 * Select only one.
 *
 *________________________________________________________________________*/
//#define M16C_62P 1
//#define M16C_6N4 1
//#define M16C_6N5 1
//#define M16C_1N2 1
//#define M32C_85  1
//#define M32C_83  1
//#define M16C_26  1
//#define M16C_26A 1
//#define M16C_28  1
//#define M16C_29  1
//#define R8C_10   1
//#define R8C_11   1
//#define R8C_12   1
#define R8C_13   1
//#define R8C_14   1
//#define R8C_15   1
//#define R8C_16   1
//#define R8C_17   1
//#define R8C_18   1
//#define R8C_19   1
//#define R8C_1A   1
//#define R8C_1B   1
//#define R8C_20   1
//#define R8C_21   1
//#define R8C_22   1
//#define R8C_23   1
//#define R8C_24   1
//#define R8C_25   1


/****************************
 * SPECIFY YOUR CLOCK SPEED *
 ****************************
 * You need to specify the speed of your system bus clock (BCLK)
 * so that the correct programming speed can be calculated
 * for communicating to the flash controller
 * The value should be in Hertz
 * Alternatively, you could define your Clock Speed in your Hew project settings
 *________________________________________________________________________*/
#define BCLK_FREQUENCY 20000000


/********************************
 * SELECT EW0 or EW1 Flash MODE *
 ********************************
 * You may select to use Erase-Write Mode 0 or Erase-Write Mode 1
 * for sending commands to the flash sequencer. Note EW0 mode
 * executes the flash commands to RAM and waits for the flash
 * operation to complete, where EW1 mode sends commands while
 * executing out of flash, but the MCU going into a hold state till
 * the flash operation is complete.
 * It is recomended to use use EW1 mode.
 *
 * Alternatively, you could define this in your Hew project settings
 * Select only one.
 *________________________________________________________________________*/
//#define EW_MODE 0
//    or
#define EW_MODE 1


/*********************************************
 * SPECIFY EW0 MODE RAM CODE SIZE/ALLOCATION *
 *********************************************
 * If using Erase-Write Mode 0, code will need to be executed out
 * of RAM when sending commands to the Flash controller. The following
 * lets you specify the size and allocation of that RAM area.
 *________________________________________________________________________
 * MAX_RAM_CODE_SIZE - This is the size in bytesof the RAM area that will be
 *                     allocated and used for both the Erase and Write routines.
 *                     Note that when determining this size, the FlashWrite
 *                     routine will require more space. */
#define RAM_CODE_SIZE 120

/* STATIC_RAM_CODE - With this option, you can specify if you want the RAM
 *                   code area to be statically allocated, or allocated on
 *                   the stack when an Erase or Write function is called.
 *                   A value of '1' will allocate RAM as a static array.
 *                   A value of '0' will dynamically allocate RAM on the stack. */
#define STATIC_RAM_CODE 1

/* !!!!! DO NOT MODIFY BELOW THIS LINE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/


// Define all un-used MCU names to 0 so we don't get a compiler warning
#ifndef M16C_62P
#define M16C_62P 0
#endif
#ifndef M16C_6N4
#define M16C_6N4 0
#endif
#ifndef M16C_6N5
#define M16C_6N5 0
#endif
#ifndef M16C_1N2
#define M16C_1N2 0
#endif
#ifndef M16C_26
#define M16C_26 0
#endif
#ifndef M16C_26A
#define M16C_26A 0
#endif
#ifndef M16C_28
#define M16C_28 0
#endif
#ifndef M16C_29
#define M16C_29 0
#endif
#ifndef M32C_85
#define M32C_85 0
#endif
#ifndef M32C_83
#define M32C_83 0
#endif
#ifndef R8C_10
#define R8C_10 0
#endif
#ifndef R8C_11
#define R8C_11 0
#endif
#ifndef R8C_12
#define R8C_12 0
#endif
#ifndef R8C_13
#define R8C_13 0
#endif
#ifndef R8C_14
#define R8C_14 0
#endif
#ifndef R8C_15
#define R8C_15 0
#endif
#ifndef R8C_16
#define R8C_16 0
#endif
#ifndef R8C_17
#define R8C_17 0
#endif
#ifndef R8C_18
#define R8C_18 0
#endif
#ifndef R8C_19
#define R8C_19 0
#endif
#ifndef R8C_1A
#define R8C_1A 0
#endif
#ifndef R8C_1B
#define R8C_1B 0
#endif
#ifndef R8C_20
#define R8C_20 0
#endif
#ifndef R8C_21
#define R8C_21 0
#endif
#ifndef R8C_22
#define R8C_22 0
#endif
#ifndef R8C_23
#define R8C_23 0
#endif
#ifndef R8C_24
#define R8C_24 0
#endif
#ifndef R8C_25
#define R8C_25 0
#endif

// ======== #define values ==========
//
// M16C_FLASH values
//  1: Standard lock functions
//  2: M16C/2X series fmr16 protect functions
//  3: M32C
//
// R8C_FLASH values
//  1: NO data blocks
//  2: 2k data blocks
//  3: 1k data blocks
//
// PROTECT_BITS values
//  0: Flash does NOT has protect bits
//  1: Flash has protect bits

#if   M16C_62P
#define M16C_FLASH 1
#elif M16C_6N4
#define M16C_FLASH 1
#elif M16C_6N5
#define M16C_FLASH 1
#elif M16C_1N2
#define M16C_FLASH 2
#elif M16C_26
#define M16C_FLASH 2
#elif M16C_26A
#define M16C_FLASH 2
#elif M16C_28
#define M16C_FLASH 2
#elif M16C_29
#define M16C_FLASH 2
#elif M32C_85
#define M16C_FLASH 3
#elif M32C_83
#define M16C_FLASH 3
#elif R8C_10
#define R8C_FLASH 1
#elif R8C_11
#define R8C_FLASH 1
#elif R8C_12
#define R8C_FLASH 2
#elif R8C_13
#define R8C_FLASH 2
#elif R8C_14
#define R8C_FLASH 1
#elif R8C_15
#define R8C_FLASH 3
#elif R8C_16
#define R8C_FLASH 1
#elif R8C_17
#define R8C_FLASH 3
#elif R8C_18
#define R8C_FLASH 1
#elif R8C_19
#define R8C_FLASH 3
#elif R8C_1A
#define R8C_FLASH 1
#elif R8C_1B
#define R8C_FLASH 3
#elif R8C_20
#define R8C_FLASH 4
#elif R8C_21
#define R8C_FLASH 5
#elif R8C_22
#define R8C_FLASH 4
#elif R8C_23
#define R8C_FLASH 5
#elif R8C_24
#define R8C_FLASH 4
#elif R8C_25
#define R8C_FLASH 5
#else
#error "You must specify your device in Flash_API.h first!!"
#endif

// Define all un-used MCU flash types to 0 so we don't get a compiler warning
#ifndef M16C_FLASH
#define M16C_FLASH 0
#endif
#ifndef R8C_FLASH
#define R8C_FLASH 0
#endif


#ifndef EW_MODE
#error "You must specify the Flash Mode (EW_MODE = 0 or 1) in Flash_API.h first!!"
#endif

#if   (EW_MODE == 0)
#define FlashErase FlashEraseEW0
#define FlashWrite FlashWriteEW0
#elif (EW_MODE == 1)
#define FlashErase FlashEraseEW1
#define FlashWrite FlashWriteEW1
#else
#error "You must specify the Flash Mode (EW_MODE = 0 or 1) in Flash_API.h first!!"
#endif

#ifndef   BCLK_FREQUENCY
#error "You must specify the Clock Frequency (BCLK_FREQUENCY) in Flash_API.h first!!"
#endif


/* No EW1 mode for M32C/83 */
#if   (M32C_83 == 1)
#if   (EW_MODE == 1)
#error "The M32C/83 only operates in EW0 Mode!!"
#endif
#endif


/* Determine the length of the pointer into flash and of the source location */
/* This is because R8C flash does not extend past 0xFFFF */
#if (M16C_FLASH )
#define FLASH_PTR_TYPE unsigned int _far *
#define   BUF_PTR_TYPE unsigned int _far *
#elif (R8C_FLASH)
#define FLASH_PTR_TYPE unsigned char _near *
#define   BUF_PTR_TYPE unsigned char _near *
#endif

/* Function Prototypes */
unsigned char FlashErase( unsigned char block );

unsigned char FlashWrite(   FLASH_PTR_TYPE flash_addr,
                            BUF_PTR_TYPE buffer_addr,
                            unsigned int bytes);


/***************************************************************/
/* M16C/62P, M16C/6N4, M16C/6N5                                */
/***************************************************************/
#if (M16C_FLASH == 1)

/* Flash protect bits */
#define PROTECT_BITS 0

/* User Block Area */
#define BLOCK_0 0       /*  4KB: FF000 - FFFFF */
#define BLOCK_1 1       /*  4KB: FE000 - FEFFF */
#define BLOCK_2 2       /*  8KB: FC000 - FDFFF */
#define BLOCK_3 3       /*  8KB: FA000 - FBFFF */
#define BLOCK_4 4       /*  8KB: F8000 - F9FFF */
#define BLOCK_5 5       /* 32KB: F0000 - F7FFF */
#define BLOCK_6 6       /* 64KB: E0000 - EFFFF */
#define BLOCK_7 7       /* 64KB: D0000 - DFFFF */
#define BLOCK_8 8       /* 64KB: C0000 - CFFFF */
#define BLOCK_9 9       /* 64KB: B0000 - BFFFF */
#define BLOCK_10 10     /* 64KB: A0000 - AFFFF */
#define BLOCK_11 11     /* 64KB: 90000 - 9FFFF */
#define BLOCK_12 12     /* 64KB: 80000 - 8FFFF */

/* Data Block Area */
#define BLOCK_A 13      /* 4KB: F000 - FFFF */

/* Defines an array of highest even addresses for each block */
#define DEFINE_ADDR_LOOKUP const unsigned long block_addresses[14] = {0xFFFFE,0xFEFFE,0xFDFFE,0xFBFFE,0xF9FFE,0xF7FFE,0xEFFFE,0xDFFFE,0xCFFFE,0xBFFFE,0xAFFFE,0x9FFFE,0x8FFFE,0xFFFE };

#endif  /* M16C_FLASH == 1 */


/***************************************************************/
/* M16C/26, M16C/26A, M16C/28, M16C/29, M16C/1N2               */
/***************************************************************/
#if (M16C_FLASH == 2)

/* Flash protect bits */
#define PROTECT_BITS 1

/* User Block Area */
#define BLOCK_0 0       /*   8KB: FE000 - FFFFF */
#define BLOCK_1 1       /*   8KB: FC000 - FDFFF */
#define BLOCK_2 2       /*  16KB: F8000 - FBFFF */
#define BLOCK_3 3       /*  32KB: F0000 - F7FFF */
#define BLOCK_4 4       /*  32KB: E8000 - EFFFF */
#define BLOCK_5 5       /*  32KB: E0000 - E7FFF */

/* Data Block Area */
#define BLOCK_A 6       /* 2KB: F800 - FFFF */
#define BLOCK_B 7       /* 2KB: F000 - F7FF */

/* Defines an array of highest even addresses for each block */
#define DEFINE_ADDR_LOOKUP const unsigned long block_addresses[8] = {0xFFFFE,0xFDFFE,0xFBFFE,0xF7FFE,0xEFFFE,0xE7FFE,0xFFFE,0xF7FE };

#endif  /* M16C_FLASH == 2 */


#if (M16C_FLASH == 3)
/***************************************************************/
/* M32C/83                                                     */
/***************************************************************/
/* Flash protect bits */
#if (M32C_83 == 1)

#define PROTECT_BITS 0

/* User Block Area */
#define BLOCK_0 0       /* 16KB: FFC000 - FFFFFF */
#define BLOCK_1 1       /*  8KB: FFA000 - FFBFFF */
#define BLOCK_2 2       /*  8KB: FF8000 - FF9FFF */
#define BLOCK_3 3       /* 32KB: FF0000 - FF7FFF */
#define BLOCK_4 4       /* 64KB: FE0000 - FEFFFF */
#define BLOCK_5 5       /* 64KB: FD0000 - FDFFFF */
#define BLOCK_6 6       /* 64KB: FC0000 - FCFFFF */
#define BLOCK_7 7       /* 64KB: FB0000 - FBFFFF */
#define BLOCK_8 8       /* 64KB: FA0000 - FAFFFF */
#define BLOCK_9 9       /* 64KB: F90000 - F9FFFF */
#define BLOCK_10 10     /* 64KB: F80000 - F8FFFF */

/* Defines an array of highest even addresses for each block */
#define DEFINE_ADDR_LOOKUP const unsigned long block_addresses[11] = {0xFFFFFE,0xFFBFFE,0xFF9FFE,0xFF7FFE,0xFEFFFE,0xFDFFFE,0xFCFFFE,0xFBFFFE,0xFAFFFE,0xF9FFFE,0xF8FFFE};

#else
/***************************************************************/
/* M32C/85                                                     */
/***************************************************************/

/* Flash protect bits */
#define PROTECT_BITS 0

/* User Block Area */
#define BLOCK_0 0       /*  4KB: FFF000 - FFFFFF */
#define BLOCK_1 1       /*  4KB: FFE000 - FFEFFF */
#define BLOCK_2 2       /*  8KB: FFC000 - FFDFFF */
#define BLOCK_3 3       /*  8KB: FFA000 - FFBFFF */
#define BLOCK_4 4       /*  8KB: FF8000 - FXF9FFF */
#define BLOCK_5 5       /* 32KB: FF0000 - FF7FFF */
#define BLOCK_6 6       /* 64KB: FE0000 - FEFFFF */
#define BLOCK_7 7       /* 64KB: FD0000 - FDFFFF */
#define BLOCK_8 8       /* 64KB: FC0000 - FCFFFF */
#define BLOCK_9 9       /* 64KB: FB0000 - FBFFFF */
#define BLOCK_10 10     /* 64KB: FA0000 - FAFFFF */
#define BLOCK_11 11     /* 64KB: F90000 - F9FFFF */
#define BLOCK_12 12     /* 64KB: F80000 - F8FFFF */

/* Data Block Area */
#define BLOCK_A 13      /* 4KB: F000 - FFFF */

/* Defines an array of highest even addresses for each block */
#define DEFINE_ADDR_LOOKUP const unsigned long block_addresses[14] = {0xFFFFFE,0xFFEFFE,0xFFDFFE,0xFFBFFE,0xFF9FFE,0xFF7FFE,0xFEFFFE,0xFDFFFE,0xFCFFFE,0xFBFFFE,0xFAFFFE,0xF9FFFE,0xF8FFFE,0xFFFE };

#endif  /* (M32C_83 == 1) */
#endif  /* M16C_FLASH == 3 */

/***************************************************************/
/* R8C/10 , R8C/11 , R8C/14 , R8C/16, R8C/18, R8C/1A           */
/***************************************************************/
#if (R8C_FLASH ==  1)

/* Flash protect bits */
#define PROTECT_BITS 1

/* User Block Area */
#define BLOCK_0 0       /*   8KB: E000 - FFFF */
#define BLOCK_1 1       /*   4KB: D000 - DFFF (or) 8KB: C000 - DFFF */

/* Defines an array of highest even addresses for each block */
#define DEFINE_ADDR_LOOKUP const unsigned int block_addresses[2] = {0xFFFE,0xDFFE};
#endif /* R8C_FLASH */

/***************************************************************/
/* R8C/12 , R8C/13,                                            */
/***************************************************************/
#if (R8C_FLASH ==  2)

/* Flash protect bits */
#define PROTECT_BITS 1

/* User Block Area */
#define BLOCK_0 0       /*   8KB: E000 - FFFF */
#define BLOCK_1 1       /*   4KB: D000 - DFFF (or) 8KB: C000 - DFFF */

/* Data Block Area */
#define BLOCK_A 2       /* 2KB: 2000 - 27FF */
#define BLOCK_B 3       /* 2KB: 2800 - 2FFF */

/* Defines an array of highest even addresses for each block */
#define DEFINE_ADDR_LOOKUP const unsigned int block_addresses[4] = {0xFFFE,0xDFFE,0x27FE,0x2FFE};
#endif /* R8C_FLASH */

/***************************************************************/
/* R8C/15 , R8C/17, R8C/19, R8C/1B                             */
/***************************************************************/
#if (R8C_FLASH ==  3)

/* Flash protect bits */
#define PROTECT_BITS 1

/* User Block Area */
#define BLOCK_0 0       /*   8KB: E000 - FFFF */
#define BLOCK_1 1       /*   4KB: D000 - DFFF (or) 8KB: C000 - DFFF */

/* Data Block Area */
#define BLOCK_A 2       /* 1KB: 2400 - 27FF */
#define BLOCK_B 3       /* 1KB: 2800 - 2BFF */

/* Defines an array of highest even addresses for each block */
#define DEFINE_ADDR_LOOKUP const unsigned int block_addresses[4] = {0xFFFE,0xDFFE,0x27FE,0x2BFE};
#endif /* R8C_FLASH */

/***************************************************************/
/* R8C/20 , R8C/22, R8C/24                                     */
/***************************************************************/
#if (R8C_FLASH ==  4)

/* Flash protect bits */
#define PROTECT_BITS 1

/* User Block Area */
#define BLOCK_0 0       /*  16KB: C000 - FFFF */
#define BLOCK_1 1       /*  16KB: 8000 - BFFF (or) 32KB: 4000 - BFFF */

/* Defines an array of highest even addresses for each block */
#define DEFINE_ADDR_LOOKUP const unsigned int block_addresses[4] = {0xFFFE,0xBFFE};
#endif /* R8C_FLASH */

/***************************************************************/
/* R8C/21 , R8C/23, R8C/25                                     */
/***************************************************************/
#if (R8C_FLASH ==  5)

/* Flash protect bits */
#define PROTECT_BITS 1

/* User Block Area */
#define BLOCK_0 0       /*  16KB: C000 - FFFF */
#define BLOCK_1 1       /*  16KB: 8000 - BFFF (or) 32KB: 4000 - BFFF */

/* Data Block Area */
#define BLOCK_A 2       /* 1KB: 2400 - 27FF */
#define BLOCK_B 3       /* 1KB: 2800 - 2BFF */

/* Defines an array of highest even addresses for each block */
#define DEFINE_ADDR_LOOKUP const unsigned int block_addresses[4] = {0xFFFE,0xBFFE,0x27FE,0x2BFE};
#endif /* R8C_FLASH */


#endif /* _FLASH_API_H (END OF FILE) */