/*
 * init_1.c
 *
 * Created: 14. 5. 2015 18:24:36
 *  Author: Tom
 */ 

#include "init_1.h"



void delayCnt(uint32_t cnt)
{
	volatile uint32_t i;
	for(i=0; i<cnt; i++) {;}
}

void delay1(uint32_t a)
{
	delayCnt(a/100);
}

void lcdPortInit(void)
{
	//AVR32_GPIO.port[0].gpers |= 0xFF << 13;		// set enable D0 - D7
	//AVR32_GPIO.port[0].oders |= 0xFF << 13;		// set output D0 - D7
	//AVR32_GPIO.port[0].ovrs |= 0xFF << 13;		// set value 1 D0 - D7

	AVR32_GPIO.port[0].gpers |= 0xFF << 10;	//nova deska
	AVR32_GPIO.port[0].oders |= 0xFF << 10;
	AVR32_GPIO.port[0].ovrs |= 0xFF << 10;

	//AVR32_GPIO.port[0].gpers |= 0x73 << 21;		// set enable WR, RD, CS, RES, D/C
	//AVR32_GPIO.port[0].oders |= 0x73 << 21;		// set output WR, RD, CS, RES, D/C
	//AVR32_GPIO.port[0].ovrs |= 0x73 << 21;		// set value 1 WR, RD, CS, RES, D/C

	//AVR32_GPIO.port[0].gpers |= 0x1C0003 << 7;
	//AVR32_GPIO.port[0].oders |= 0x1C0003 << 7;
	//AVR32_GPIO.port[0].ovrs |= 0x1C0003 << 7;

//---------------------------------------------

	AVR32_GPIO.port[0].gpers |= 0x03 << 7;
	AVR32_GPIO.port[0].oders |= 0x03 << 7;
	AVR32_GPIO.port[0].ovrs |= 0x03 << 7;
	
	AVR32_GPIO.port[0].gpers |= 0x07 << 25;
	AVR32_GPIO.port[0].oders |= 0x07 << 25;
	AVR32_GPIO.port[0].ovrs |= 0x07 << 25;	
}





void setLcdPort(uint8_t a)
{
	if(a & (1<<0)){SET_PIN(PIN_D0);} else{CLR_PIN(PIN_D0);}
	if(a & (1<<1)){SET_PIN(PIN_D1);} else{CLR_PIN(PIN_D1);}
	if(a & (1<<2)){SET_PIN(PIN_D2);} else{CLR_PIN(PIN_D2);}
	if(a & (1<<3)){SET_PIN(PIN_D3);} else{CLR_PIN(PIN_D3);}
	if(a & (1<<4)){SET_PIN(PIN_D4);} else{CLR_PIN(PIN_D4);}
	if(a & (1<<5)){SET_PIN(PIN_D5);} else{CLR_PIN(PIN_D5);}
	if(a & (1<<6)){SET_PIN(PIN_D6);} else{CLR_PIN(PIN_D6);}
	if(a & (1<<7)){SET_PIN(PIN_D7);} else{CLR_PIN(PIN_D7);}
}

void writeCommand(uint8_t a)
{
	CLR_PIN(PIN_DC);
	setLcdPort(a);
	CLR_PIN(PIN_CS);
	WAIT_LCD;
	CLR_PIN(PIN_WR);
	WAIT_LCD;
	SET_PIN(PIN_WR);
	WAIT_LCD;
}

void writeData(uint8_t a)
{
	SET_PIN(PIN_DC);
	setLcdPort(a);
	CLR_PIN(PIN_CS);
	WAIT_LCD;
	CLR_PIN(PIN_WR);
	WAIT_LCD;
	SET_PIN(PIN_WR);
	WAIT_LCD;
}

void lcdInit(void)
{
	lcdPortInit();
	delay1(10);
	CLR_PIN(PIN_RST);
	delay1(10);
	SET_PIN(PIN_RST);
	delay1(10);
	
	writeCommand(SSD1351_CMD_COMMANDLOCK);  // set command lock
	writeData(0x12);
	
	writeCommand(SSD1351_CMD_COMMANDLOCK);  // set command lock
	writeData(0xB1);
	
	writeCommand(SSD1351_CMD_DISPLAYOFF);  	// 0xAE
}

void lcdOn(void)
{
	
	writeCommand(SSD1351_CMD_CLOCKDIV);  		// 0xB3
	writeCommand(0xF1);  						// 7:4 = Oscillator Frequency, 3:0 = CLK Div Ratio (A[3:0]+1 = 1..16)
	
	writeCommand(SSD1351_CMD_MUXRATIO);
	writeData(127);
	
	writeCommand(SSD1351_CMD_SETREMAP);
	writeData(0x74);
	
	writeCommand(SSD1351_CMD_SETCOLUMN);
	writeData(0x00);
	writeData(0x7F);
	writeCommand(SSD1351_CMD_SETROW);
	writeData(0x00);
	writeData(0x7F);

	writeCommand(SSD1351_CMD_STARTLINE); 		// 0xA1
	if (SSD1351HEIGHT == 96)
	{
		writeData(96);
	}
	else
	{
		writeData(0);
	}


	writeCommand(SSD1351_CMD_DISPLAYOFFSET); 	// 0xA2
	writeData(0x0);

	writeCommand(SSD1351_CMD_SETGPIO);
	writeData(0x00);
	
	writeCommand(SSD1351_CMD_FUNCTIONSELECT);
	writeData(0x01); // internal (diode drop)
	//writeData(0x01); // external bias

	writeCommand(SSSD1351_CMD_SETPHASELENGTH);
	writeData(0x32);

	writeCommand(SSD1351_CMD_PRECHARGE);  		// 0xB1
	writeCommand(0x32);
	
	writeCommand(SSD1351_CMD_VCOMH);  			// 0xBE
	writeCommand(0x05);

	writeCommand(SSD1351_CMD_NORMALDISPLAY);  	// 0xA6

	writeCommand(SSD1351_CMD_CONTRASTABC);
	writeData(0xC8);
	writeData(0x80);
	writeData(0xC8);

	writeCommand(SSD1351_CMD_CONTRASTMASTER);
	writeData(0x0F);

	writeCommand(SSD1351_CMD_SETVSL );
	writeData(0xA0);
	writeData(0xB5);
	writeData(0x55);
	
	writeCommand(SSD1351_CMD_PRECHARGE2);
	writeData(0x01);
	
	writeCommand(SSD1351_CMD_DISPLAYON);		//--turn on oled panel

}