#include "d11_rw.h"

unsigned char ENDPT_INT;
unsigned char OTHER_INT;
unsigned char data DBUFFER[9];
unsigned char temp_buffer[2];

/* Set Mode Function */
void SetMode(unsigned char D11_MDATA, unsigned char D11_CLKDIV)
{
	Wr_command(0x0F3); //SetMode Command
        temp_buffer[0] = D11_MDATA; //SetMode, Configuration Byte
        temp_buffer[1] = D11_CLKDIV; //SetMode, Configuration Byte
        Wr_Ndata(temp_buffer, 2);
}

/* Set Address Function */
void SetAddress(unsigned char NewAddr)
{
	Wr_command(0x0D1); //SetAddr Command
	Wr_Ndata(&NewAddr, 1); //SetAddr Byte
}

/* Disable other device */
void DisOtherAddress(void)
{
unsigned char dummy;
        dummy = 0;
	Wr_command(0x0D0); //SetAddr Command
	Wr_Ndata(&dummy, 1); //SetAddr Byte
}

/* Set Endpoint Enable */
void SetEndptEnable(unsigned char Enable_data)
{
	Wr_command(0x0D8); //SetEndpoint Enable Command
	Wr_Ndata(&Enable_data, 1); //SetAddr Byte
}


/* Read Interrupt Register Function */
/* Write to pre-determined Registers */
void UpdateIntReg(void)
{
	Wr_command(0x0F4); //Read Interrupt Command
        Rd_Ndata(temp_buffer, 2);
	ENDPT_INT = temp_buffer[0];// Interrupt Register Byte1
	OTHER_INT = temp_buffer[1]; // Interrupt Register Byte2
}


/* Read the data buffer for incoming USB data */
/* Write to DBUFFER, Maximum of 8 bytes, the first byte */
/* gives the valid data contained in DBUFFER */
void ReadBuffer(void)
{
	Wr_command(0x0F0); //Read Buffer Command
	Rd_Bufdata(DBUFFER); //Read the number of Valid bytes
}

/* Writes the data buffer for outgoing USB data */
/* Write to DBUFFER, Maximum of 16 bytes, first byte */
/* gives the number of bytes to be sent in DBUFFER */
void WriteBuffer(void)
{
	Wr_command(0x0F0); //Write Buffer Command
        Wr_Bufdata(DBUFFER);
}

void Ack_SETUP(void)
{
	Wr_command(0x0F1); //Acknowledge Setup Command
}

void SendResume(void)
{
	Wr_command(0x0F6); //Send Remote Wakeup Command
}

unsigned char SelectEndpoint(unsigned char ENDPT)
{
	Wr_command(ENDPT); //Select Endpoint Command
        Rd_Ndata(temp_buffer, 1); // Read Endpoint status
        return(temp_buffer[0]);
}

unsigned char ReadEndptStatus(unsigned char ENDPT)
{
	Wr_command((0x040+ENDPT)); //Read Endpoint Status Command
        Rd_Ndata(temp_buffer, 1); // Read Endpoint status
        return(temp_buffer[0]);
}

unsigned char ReadEPStall(unsigned char ENDPT)
{
	Wr_command((0x080+ENDPT)); //Read Endpoint Status Command
        Rd_Ndata(temp_buffer, 1); // Read Endpoint status
        return(temp_buffer[0]);
}

void SetEndptStatus(unsigned char ENDPT, unsigned char ENDPTDATA)
{
	Wr_command((0x040+ENDPT)); //Read Endpoint Status Command
	Wr_Ndata(&ENDPTDATA, 1); //Write Endpoint status
}

void ClearBuffer(void)
{
	Wr_command(0x0F2); //Clear Buffer Command
}

void ValidateBuffer(void)
{
	Wr_command(0x0FA); //Validate Buffer Command
}

void SendZeroPacket(unsigned char ENDPT)
{
	Wr_command(ENDPT); //Select Ctrl IN buffer
	Wr_command(0x0F0); //Write Buffer Command
        temp_buffer[0] = 0x00;   //Fill buffer Reserve byte
        temp_buffer[1] = 0x00;   //Fill zero packet length
	Wr_Ndata(temp_buffer, 2); //Write to buffer
	Wr_command(0x0FA); //Validate Data
}

void Port1_Power(void)
{
unsigned char temp;
       temp = 3;
       Wr_command(0xE8);
       Wr_Ndata(&temp, 1);
       Wr_command(0xE8);
       Wr_Ndata(&temp, 1);
}
