/*
 * MICETEK International Inc., (shanghai) 
 * 	By Qinwei , 2003.05.28 (CopyRight)
 * 
 *  Version 0.1
 */
#include <stdio.h>
#include "uart_ev40800.h"


static unsigned int uart_base;
// static unsigned int SYS_CLK;
static int CurrBaud;

//dev_uart_init returns non-negative number on  succuess, 0 on failing.
int dev_uart_init (int sys_clock,  int reg_base, int ch)
{
        //*((unsigned int *)PS_PCER) = (1<<ch);
       
        if (reg_base == 0)
                reg_base = 0xFFF00000;
        if (sys_clock == 0)
                sys_clock = 133000000;
      	if (ch) /* If is uart1*/
      	        uart_base = reg_base + 0xCC000;
      	else
      	        uart_base = reg_base + 0xD0000;
	
         //* Reset receiver and transmitter
    	*((unsigned int *)(uart_base+US_CR)) = US_RSTRX | US_RSTTX | US_RXDIS | US_TXDIS ;
        
        //* Clear Transmit and Receive Counters
        *((unsigned int *)(uart_base+US_RCR)) = 0;
        *((unsigned int *)(uart_base+US_TCR)) = 0;
        
        //*Disable ITs
        *((unsigned int *)(uart_base+US_IDR)) = 0xFFFFFFFF;

		*((unsigned int *)PIO_PDR) = (7<<13);
        
        //WordLength | Parity | StopBit | IRDA_mode, and now 8N1 mode |we use external clock (UCLK) now;
        *((unsigned int *)(uart_base+US_MR)) = 0x8C0;
        
	// Now set to 9600, and can change later
        *((unsigned int *)(uart_base+US_BRGR)) = B9600; 

        //* Enable receiver and transmitter
    	*((unsigned int *)(uart_base+US_CR)) = US_RXEN | US_TXEN ;
        
        CurrBaud = 9600;
        return 1;
}


//crtio returns the old baud rate on success, 0 on failing.
int crtio (int baud)
{
        int bbaud, ret ;
	
        ret = CurrBaud;
        switch (baud) {
        case 9600:
                bbaud = B9600;		
                CurrBaud  = 9600;
                break;
        case 19200:
                bbaud = B19200;	
                CurrBaud  = 19200;	
                break;
        case 38400:
                bbaud = B38400;	
                CurrBaud  = 38400;	
                break;
        case 57600:
                bbaud = B57600;	
                CurrBaud  = 57600;	
                break;
        case 115200:
                bbaud = B115200;
                CurrBaud  = 115200;
                break;
        default:
                bbaud = B9600;
                CurrBaud  = 9600;
                break;
        }
        *((unsigned int *)(uart_base+US_BRGR)) = bbaud; // Now change the baudrate

        return ret;
}

//outbyte returns the written char on success or EOF on failing.
int outbyte (int ch)
{
        volatile unsigned int uart_temp;

        while (1) {
                uart_temp = (*((unsigned int *)(uart_base+US_CSR)))&0x3FF;
                if (uart_temp & US_TXRDY)
                        break;
        }     
          
	*((unsigned int *)(uart_base+US_THR)) = ch; /*Send the char*/
        	
        return ch;
}

//inbyte returns the byte it reads on success or EOF on failing.
int inbyte (void)
{
        unsigned int uart_temp = (*((unsigned int *)(uart_base+US_CSR)) )& 0x3FF;
	
        if (uart_temp & US_RXRDY ) {
                unsigned int ch = (*((unsigned int *)(uart_base+US_RHR)) )& 0xFF;    /* get the data byte */
                return ch;
        }
	
        return EOF;
}


int send(char *buf,int size)
{
	unsigned int uart_temp = *((unsigned int *)(uart_base+US_CSR));
	
	while((uart_temp & US_ENDTX) == 0);
	
	*((unsigned int *)(uart_base+US_TPR)) = (unsigned int) buf;
	
	*((unsigned int *)(uart_base+US_TCR)) = size;
	
	return 1;
	}










