// macro FLASH_READ returns a word stored at a flash address
#if defined(__FLASHTYPE)
extern unsigned char flash_read(unsigned short addr);
#if EEPROM_SIZE > 0
#define FLASH_READ(addr) \
(EEADR=(addr)&0xff, \
EEADRH=(addr)>>8, \
WREN=0, \
EECON1 |= 0x80, \
RD=1, \
DC=0, \
DC=0, \
(EEDATH << 8) | EEDATA)
#else // FLASH_READ without EEPROM
#define FLASH_READ(addr) \
(EEADR=(addr)&0xff, \
EEADRH=(addr)>>8, \
RD=1, \
DC=0, \
DC=0, \
(EEDATH << 8) | EEDATA)
#endif
#endif // end FLASH_READ
// macro FLASH_WRITE used when writing only one word of data
#if __FLASHTYPE==2 || __FLASHTYPE==3
#define FLASH_WRITE(addr,data) \
do{ \
unsigned short x=data; \
flash_copy((const unsigned char *)&x,1,addr); \
}while(0)
extern void flash_copy(const unsigned char * source_addr,unsigned char length,unsigned short dest_addr);
#elif __FLASHTYPE==1
#define FLASH_WRITE(addr, value) \
EEADR=((addr)&0xff); \
EEADRH=((addr)>>8); \
EEDATH=((value)>>8); \
EEDATA=((value)&0xff); \
EECON1 |= 0x80; \
WREN=1; \
EECON2 = 0x55; \
EECON2 = 0xaa; \
WR=1; \
asm("nop"); \
asm("nop"); \
WREN=0
#endif // end FLASH_WRITE