/*
    IOHal.c, Copyright (c) by Silvio Dragone & David Reist,
    Swiss Federal Institute of Technology,
    Computer Engineering and Networks Laboratory.
 
    TOPSY -- A Teachable Operating System.
             Implementation of a tiny and simple
             micro kernel for teaching purposes.
 
    For further information, please visit http://www.tik.ee.ethz.ch/~topsy
 
    This software is provided under the terms of the GNU General Public Licence.
 
    A full copy of the GNU GPL is provided in the file COPYING found in the 
    development root of Topsy.
*/

#include "Topsy.h"
#include "IOHal.h"
#include "IODevice.h"

/* very simple console output routine. accesses hardware directly, used at
 * startup and on catastrophic events/debugging...
 */

/* This is a "light" version of the IXP2400_UART driver,
	 refer to IXP2400_UART.c & IXP2400_UART.h for further details
 */

void ioConsoleInit()
{
  unsigned int *lcr = (unsigned int*) UART_LCR_REGISTER;
  unsigned int *ier = (unsigned int*) UART_IER_REGISTER;
  *lcr = LCR_WLS_8BIT;              //all other bits must be 0
  *ier = IER_RAVIE_YES | IER_UUE_YES | IER_RLSE_YES;  //enable data avail interrupt && enable uart
}


void ioConsolePutChar(char c)
{
  unsigned int *thr = (unsigned int*) UART_THR_REGISTER;
  unsigned int *lsr = (unsigned int*) UART_LSR_REGISTER;

  /* Wait for UART to become ready */
  while (!(*lsr & LSR_TDRQ))
  {
    ioDelayAtLeastCycles( NBCYCLESFORDELAY);
  }
  *thr = (unsigned int) c; /* send it finally */
  //  ioDelayAtLeastCycles( NBCYCLESFORDELAY);
}
