www.pudn.com > writemac.rar > dm643_uart.c


/* 
 *  Copyright 2003 by Spectrum Digital Incorporated. 
 *  All rights reserved. Property of Spectrum Digital Incorporated. 
 */ 
  
/* 
 *  ======== dm643_uart.c ======== 
 *  UART module for the DM643 
 */ 
  
#include  
 
#include "dm643.h" 
#include "dm643_uart.h" 
 
 
/* 
 *  ======== DM643_UART_rset ======== 
 *  Set a UART register 
 */ 
void DM643_UART_rset(DM643_UART_Handle hUart, Int16 regnum, Int16 regval) 
{ 
    Int16 regindex, lcr; 
     
    /* Register index is determined by lower 3 bits and the target UART */ 
    regindex = regnum & 0x7; 
    if (hUart == 1) 
        regindex += 8; 
     
    /* If regnum between 0x08 and 0x0F, set bit 7 of LCR to access register */ 
    if ((regnum & 0x18) == 0x8) 
    { 
        lcr = DM643_UART_rget(hUart, DM643_UART_LCR); 
        DM643_UART_rset(hUart, DM643_UART_LCR, lcr | 0x80); 
        DM643_rset(regindex, regval); 
        DM643_UART_rset(hUart, DM643_UART_LCR, lcr); 
    } 
    else 
    { 
        DM643_rset(regindex, regval); 
    } 
} 
 
 
/* 
 *  ======== DM643_UART_rget ======== 
 *  Get the value of a UART register 
 */ 
Int16 DM643_UART_rget(DM643_UART_Handle hUart, Int16 regnum) 
{ 
    Int16 regindex, returnval, lcr; 
     
    /* Register index is determined by lower 3 bits and the target UART */ 
    regindex = regnum & 0x7; 
    if (hUart == 1) 
        regindex += 8; 
     
    /* If regnum between 0x08 and 0x0F, set bit 7 of LCR to access register */ 
    if ((regnum & 0x18) == 0x8) 
    { 
        lcr = DM643_UART_rget(hUart, DM643_UART_LCR); 
        DM643_UART_rset(hUart, DM643_UART_LCR, lcr | 0x80); 
        returnval = DM643_rget(regindex); 
        DM643_UART_rset(hUart, DM643_UART_LCR, lcr); 
    } 
    else 
    { 
        returnval = DM643_rget(regindex); 
    } 
     
    return returnval; 
} 
 
 
/* 
 *  ======== DM643_UART_open ======== 
 *  Initialize UART and return handle 
 */ 
DM643_UART_Handle DM643_UART_open(Int16 devid, Int16 baudrate, DM643_UART_Config *config) 
{ 
    DM643_UART_Handle hUart; 
     
    /* Assign handle */ 
    hUart = devid; 
     
    /* Set registers based on config structure */ 
    DM643_UART_rset(hUart, DM643_UART_IER, config -> regs[0]); 
    DM643_UART_rset(hUart, DM643_UART_FCR, config -> regs[1]); 
    DM643_UART_rset(hUart, DM643_UART_LCR, config -> regs[2]); 
    DM643_UART_rset(hUart, DM643_UART_MCR, config -> regs[3]); 
 
    /* Set up baud divisor clock */ 
    DM643_UART_rset(hUart, DM643_UART_DLL, baudrate & 0xff); 
    DM643_UART_rset(hUart, DM643_UART_DLH, (baudrate >> 8) & 0xff); 
     
    /* Clear any outstanding receive characters */ 
    DM643_UART_rget(hUart, DM643_UART_RBR); 
     
    return hUart; 
} 
 
/* 
 *  ======== DM643_UART_getChar ======== 
 *  Get one character of data from the UART 
 */ 
Int16 DM643_UART_getChar(DM643_UART_Handle hUart) 
{ 
    Int16 status; 
     
    while(1) 
    { 
        status = DM643_UART_rget(hUart, DM643_UART_LSR); 
        if ((status & 1) != 0)  // DR 
            break; 
    } 
     
    return DM643_UART_rget(hUart, DM643_UART_RBR); 
} 
 
/* 
 *  ======== DM643_UART_putChar ======== 
 *  Send one character of data to the UART 
 */ 
void DM643_UART_putChar(DM643_UART_Handle hUart, Uint16 data) 
{ 
    Int16 status; 
     
    while(1) 
    { 
        status = DM643_UART_rget(hUart, DM643_UART_LSR); 
        if ((status & 0x20) != 0)  // THRE 
            break; 
    } 
     
    DM643_UART_rset(hUart, DM643_UART_THR, data); 
}