#include <ezusb.h>
#include <ezregs.h>

#include "sci.h"

/**********/
/*  SCI0  */
/**********/
#if SCI0_ENABLE

static fifo_char_t fifo_tx0;
static fifo_char_t fifo_rx0;
static char buffer_tx0[SCI0_TX_BUFFER_SIZE + 1];
static char buffer_rx0[SCI0_RX_BUFFER_SIZE + 1];

/**
 * init SCI0
 * 
 */
void sci0_init(){
	fifo_char_init(&fifo_tx0, buffer_tx0, SCI0_TX_BUFFER_SIZE + 1);	
	fifo_char_init(&fifo_rx0, buffer_rx0, SCI0_RX_BUFFER_SIZE + 1);
}

/**
 * Regist sending data via SCI0
 * 
 * @param data pointer to data
 * @param size size of data
 * @return (int) the size of registed data to buffer
 */
int sci0_write(char *data, int size){
	int _size = sci_write(&fifo_tx0, data, size);
	/* 
	 * 
	 */
	if(_size > 0 && (SCI0.SCR.BIT.TIE == 0)){SCI0.SCR.BIT.TIE = 1;}
	return _size;
}

/**
 * Return the size of untransimitted data via SCI0
 * 
 * @return (int) the size
 */
int sci0_tx_size(){return sci_fifo_size(&fifo_tx0);}

/**
 * Get the recieved data via SCI0
 * 
 * @param buf buffer
 * @param size the size of buffer
 * @return (int) the real size of grabbed data
 */
int sci0_read(char *buf, int size){return sci_read(&fifo_rx0, buf, size);}

/**
 * Return the size of ungrabbed data via SCI0
 * 
 * @return (int) the size
 */
int sci0_rx_size(){return sci_fifo_size(&fifo_rx0);}

/* Interrupt(TI_0 / RI_0) */
void int_sci0 (void) interrupt 5 {
	sci_send(&SCI0, &fifo_tx0);
	sci_recieve(&SCI0, &fifo_rx0);
}

#endif /* SCI0_ENABLE */


/************/
/*   SCI1   */
/************/
#if SCI1_ENABLE

#endif /* SCI1_ENABLE */



