#include <io.h>
#include <progmem.h>
#include <eeprom.h>

#include "lcd.h"

/**** function prototype ****/
void ChipInit(void);

/*CPU wait routine*/
void CPU_wait(unsigned int time);	/* unit = 0.1ms */

/*EEPROM*/
#define EEPROMSIZE E2END+1	/*from ioxxxx.h*/

/*util*/
#define nop() __asm__ __volatile__ ("nop")

/*program memory constants*/
PROGMEM char hello[] = "Hello AVR world.";

/**** main function ****/
int main(void)
{
	unsigned char i;
	char c;
	char *s;
	
	ChipInit();
	LCD_Init();
	LedOff();
	
	LCD_clear();
	LCD_locate(0,0);
	
	/*direct access*/
	for(i = 0;i < 16;i++){
		c = PRG_RDB(&hello[i]);
		if(c == 0) break;
		LCD_data(c);
	}
	
	/*pointer access*/
	s = PSTR("EEPROM test");	/*get pointer for inline progmem const value*/
	LCD_locate(0,1);
	
	/*write eeprom*/
	for(i = 0;i < 16;i++){
		LedOn();
		while(1){
			if(eeprom_is_ready() == 1) break;	/*return 1 if EEPROM is ready */
		}
		LedOff();
		c = PRG_RDB(s++);
		eeprom_wb(i+1,c);
		if(c == 0) break;
	}
	
	/*read eeprom*/
	for(i = 0;i < 16;i++){
		LedOn();
		while(1){
			if(eeprom_is_ready() == 1) break;
		}
		LedOff();
		c = eeprom_rb(i+1);
		if(c == 0) break;
		LCD_data(c);
	}
	
	while(1){
		LedOn();
		CPU_wait(10000);
		LedOff();
		CPU_wait(10000);
	}
} 


/**** wait routine ****/
void CPU_wait(unsigned int time){
	unsigned int i;
	volatile unsigned char j;
	for(i = 0;i < time;i++){
		for(j = 0;j < 78;j++){		/*CLK = 8MHz,with avr-gcc -Os switch*/
		}
	}
}

void ChipInit(void)
{
	
}
