/* File name: UARTtest.c

    11Mar2004 .. initial version .. KM
    28Mar2004 .. a fix and added interrupt test .. KM
    
*/ 

#include <stdio.h>
#include <math.h>
#include <extaddr.h>

#define FOREVER 1

#define UART 0x500200
#define RBR (UART+0x00)
#define THR (UART+0x00)
#define DLL (UART+0x00)
#define DLM (UART+0x02)
#define IER (UART+0x02)
#define ISR (UART+0x04)
#define FCR (UART+0x04)
#define LCR (UART+0x06)
#define MCR (UART+0x08)
#define LSR (UART+0x0A)
#define MSR (UART+0x0C)
#define SPR (UART+0x0E)

extern unsigned volatile int U1RxChar;
extern unsigned volatile int U1RxFlag;
unsigned int U1IStatus;

int test = 6;

/* baud rate is 230,400/divisor
   for 38400 baud use a value of 6
*/

int RateLow = 6, RateHigh = 0;

void simple_print(int CHar)
{
    far_poke(THR, CHar);
    while ((far_peek(LSR)&0x20)==0);
}

void main(void)
{
    unsigned counter;
    int temp, temp2;
    unsigned long MCRch0;
     
    // set up baud rate

    far_poke(LCR, 0x80); // access baud rate registers
    far_poke(DLM, RateHigh);
    far_poke(DLL, RateLow);
    far_poke(LCR, 0x07); // use 8 data and 2 stop bits 
    
    if (test == 1) {    // can we write/read scratch register?
        far_poke(SPR, 0x53);
        temp = far_peek(SPR);
        printf("%4X\n", temp);
        return;
    }
    
    if (test == 2) {    // set up baud rate
        far_poke(LCR, 0x80); // access baud rate registers
        far_poke(DLM, RateHigh);
        far_poke(DLL, RateLow);
        far_poke(LCR, 0x07); // use 8 data and 2 stop bits
        temp  = far_peek(DLM);
        temp2 = far_peek(DLL);
        printf("%2X %2X\n", temp, temp2);
        return;
    }
        
    if (test == 3) {  // loop printing 256 character set
        far_poke(LCR, 0x80); // access baud rate registers
        far_poke(DLM, RateHigh);
        far_poke(DLL, RateLow);
        far_poke(LCR, 0x07); // use 8 data and 2 stop bits
        
        counter = 0;
        while (FOREVER) {
            far_poke(THR, counter++);
            while ((far_peek(LSR)&0x20)==0);
        }  
    }   

    if (test == 4) { // loop accepting input and echoing it
        far_poke(LCR, 0x80); // access baud rate registers
        far_poke(DLM, RateHigh);
        far_poke(DLL, RateLow);
        far_poke(LCR, 0x07); // now access xmt and rcv regs

        while (FOREVER) {
            while ((far_peek(LSR)&0x01)==0); // wait for character
            far_poke(THR, far_peek(RBR));     // read it and echo it
        }
    }
    
    if (test == 5) { // Test use of DTR on DB9M for timing
            far_poke(MCR, 0x00); // put into its reset state
            MCRch0 = 0x500008;
        while (FOREVER) {
            far_poke(MCRch0, 0x01);   // set DTR on channel 0
            far_poke(MCRch0, 0x00);   // reset the DTR on channel 0
            far_poke(MCRch0, 0x00);   // reset the DTR on channel 0
            far_poke(MCRch0, 0x00);   // reset the DTR on channel 0      
        }
    }
    if (test == 6) { // Tests for developing interrupt handler
        UART1setup();
        _enable_interrupts();
        while(FOREVER) {
            while (U1RxFlag == 0);
            far_poke(THR, U1RxChar);
            U1RxFlag = 0;
        }
    }
    if (test == 7) { // Demonstrates how to print a string
        char *str;
        
        while(FOREVER) {
            str = "1,2,3,4,5,6,\r\n9,8,7,6,5,4\r\n";
            while (*str != 0) simple_print(*str++);
        }
    }
}
