#include "xbasic_types.h"
#include "xio.h"
#include "xparameters.h"
#include "xuartlite_l.h"

#define BUFFSIZE 15000

int uart_interrupt_count = 0;
char uart_character;
char buffer[BUFFSIZE];
char *last_in = buffer;
char *last_out = buffer;
int last_in_loop_count = 0;

/*
 * Interrupt service routine for the UART
 */
void uart_handler(void *callback)
{
  Xuint32 IsrStatus;
  
  Xuint8 incoming_character;

  /* Check the ISR status register so we can identify the interrupt source */
  IsrStatus = XIo_In32(XPAR_MYUART_BASEADDR + XUL_STATUS_REG_OFFSET);
  
  if ((IsrStatus & (XUL_SR_RX_FIFO_FULL | XUL_SR_RX_FIFO_VALID_DATA)) != 0) {
    /* The input FIFO contains data: read it */
    incoming_character =
      (Xuint8) XIo_In32( XPAR_MYUART_BASEADDR + XUL_RX_FIFO_OFFSET );

    uart_character = incoming_character;
    ++uart_interrupt_count;
  }
 
  if ((IsrStatus & XUL_SR_TX_FIFO_EMPTY) != 0) {
    /* The output FIFO is empty: we can send another character */
  }

  /*
   * putting data into buffer 
   * will read it in main.c 
   */

  if (last_in == buffer + BUFFSIZE) // loop around
    {
      last_in = buffer;
      last_in_loop_count++;
    }

  *last_in = uart_character; //input a new character
  last_in++; //pointer
  
}
