/*
    IOHal.c, Copyright  (c) by George Fankhauser,
    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.
*/
/*
	
	File:                  $Source: /home/ruf/cvs_Topsy/cvs/Topsy/IO/mips/IOHal.c,v $
 	Author(s):             George Fankhauser
 	Affiliation:           ETH Zuerich, TIK
 	Version:               $Revision: 2922 $
 	Creation Date:         
 	Last Date of Change:   $Date: 2007-03-19 17:48:52 +0100 (Mon, 19 Mär 2007) $      by: $Author: baderma $
	
*/

#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...
 */
void ioConsoleInit(void)
{
#ifndef SIMOS
  char* command = (char*)(CONSOLE_BASE + COMMAND_REGISTER);
  char* mode = (char*)(CONSOLE_BASE + MODE_REGISTER);
  char* status = (char*)(CONSOLE_BASE + STATUS_REGISTER);
  char* interrupt = (char*)(UART_A_BASE + INTERRUPT_REGISTER);

  *command = 0xa;	/* disable rx/tx */
  ioDelayAtLeastCycles( NBCYCLESFORDELAY);
  *mode = 0x13;	/* no parity - 8 bits/char */
  ioDelayAtLeastCycles( NBCYCLESFORDELAY);
  *mode = 0x7;	/* 1 stop bit */
  ioDelayAtLeastCycles( NBCYCLESFORDELAY);
  *status = 0xbb;	/* 9600 baud */
  ioDelayAtLeastCycles( NBCYCLESFORDELAY);
  *interrupt = 0x0;	/* select external clock disable ints */
  ioDelayAtLeastCycles( NBCYCLESFORDELAY);
  *command = 0x5;	/* enable receive/transmit */
  ioDelayAtLeastCycles( NBCYCLESFORDELAY);
#else

  /* SimOS console is beautifully simple. */

#endif
}


void ioConsolePutChar(char c)
{
#ifndef SIMOS
  char* transmit;
  char* status;

  transmit = (char*)(CONSOLE_BASE + TX_REGISTER);
  status = (char*)(CONSOLE_BASE + STATUS_REGISTER);

  /* Wait for UART to become ready */
  while ((*status & READY_TO_SEND) != READY_TO_SEND)
  {
    ioDelayAtLeastCycles( NBCYCLESFORDELAY);
  }
  *transmit = c;			/* send it finally */
  ioDelayAtLeastCycles( NBCYCLESFORDELAY);

#else /* SIMOS */

  /* Again, the joy of simulating ``hardware'' *grin* */
  _SIMOS_CONSOLE->data = c;

#endif
}


