OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [newlib-1.17.0/] [libgloss/] [or32/] [uart.c] - Diff between revs 158 and 180

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 158 Rev 180
Line 1... Line 1...
#include "board.h"
/* uart.c. UART support functions
 
 
 
   Copyright (C) 2004, Jacob Bower
 
   Copyright (C) 2010, Embecosm Limited <info@embecosm.com>
 
 
 
   Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
 
 
 
   This file is part of Newlib.
 
 
 
   The original work by Jacob Bower is provided as-is without any kind of
 
   warranty. Use it at your own risk!
 
 
 
   All subsequent work is bound by version 3 of the GPL as follows.
 
 
 
   This program is free software; you can redistribute it and/or modify it
 
   under the terms of the GNU General Public License as published by the Free
 
   Software Foundation; either version 3 of the License, or (at your option)
 
   any later version.
 
 
 
   This program is distributed in the hope that it will be useful, but WITHOUT
 
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 
   more details.
 
 
 
   You should have received a copy of the GNU General Public License along
 
   with this program.  If not, see <http:#www.gnu.org/licenses/>.             */
 
/* -------------------------------------------------------------------------- */
 
/* This program is commented throughout in a fashion suitable for processing
 
   with Doxygen.                                                              */
 
/* -------------------------------------------------------------------------- */
 
 
 
#include "or1ksim-board.h"
#include "uart.h"
#include "uart.h"
 
 
 
 
 
/*! Macro to access a UART register */
 
#define UREG8(reg) REG8 (UART_BASE + reg)
 
 
 
/*! Macro to check if transmit and transmit holding registers are both empty. */
#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
 
 
 
/*! Macro to wait until a char has been transmitted */
#define WAIT_FOR_XMITR \
#define WAIT_FOR_XMITR \
        do { \
  do                                            \
                lsr = REG8(UART_BASE + UART_LSR); \
    {                                           \
        } while ((lsr & BOTH_EMPTY) != BOTH_EMPTY)
      lsr = UREG8 (UART_LSR);                   \
 
    }                                           \
 
  while ((lsr & BOTH_EMPTY) != BOTH_EMPTY)
 
 
 
/*! Macro to wait until a char can be transmitted */
#define WAIT_FOR_THRE \
#define WAIT_FOR_THRE \
        do { \
  do                                            \
                lsr = REG8(UART_BASE + UART_LSR); \
    {                                           \
        } while ((lsr & UART_LSR_THRE) != UART_LSR_THRE)
      lsr = UREG8 (UART_LSR);                   \
 
    }                                           \
 
  while ((lsr & UART_LSR_THRE) != UART_LSR_THRE)
 
 
#define CHECK_FOR_CHAR (REG8(UART_BASE + UART_LSR) & UART_LSR_DR)
/* Macro to see if a character has been received */
 
#define CHECK_FOR_CHAR (UREG8 (UART_LSR) & UART_LSR_DR)
 
 
 
/* Macro to wait until a character is received */
#define WAIT_FOR_CHAR \
#define WAIT_FOR_CHAR \
         do { \
         do { \
                lsr = REG8(UART_BASE + UART_LSR); \
                lsr = UREG8 (UART_LSR); \
         } while ((lsr & UART_LSR_DR) != UART_LSR_DR)
         } while ((lsr & UART_LSR_DR) != UART_LSR_DR)
 
 
void uart_init(void)
 
 
/* -------------------------------------------------------------------------- */
 
/*!Initialize the UART                                                        */
 
/* -------------------------------------------------------------------------- */
 
void
 
_uart_init ()
{
{
        int divisor;
        int divisor;
 
 
        /* Reset receiver and transmiter */
        /* Reset receiver and transmiter */
        REG8(UART_BASE + UART_FCR) = UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT | UART_FCR_TRIGGER_14;
        UREG8 (UART_FCR) = UART_FCR_ENABLE_FIFO |
 
                           UART_FCR_CLEAR_RCVR  |
 
                           UART_FCR_CLEAR_XMIT  |
 
                           UART_FCR_TRIGGER_14;
 
 
        /* Disable all interrupts */
        /* Disable all interrupts */
        REG8(UART_BASE + UART_IER) = 0x00;
        UREG8 (UART_IER) = 0x00;
 
 
        /* Set 8 bit char, 1 stop bit, no parity */
        /* Set 8 bit char, 1 stop bit, no parity */
        REG8(UART_BASE + UART_LCR) = UART_LCR_WLEN8 & ~(UART_LCR_STOP | UART_LCR_PARITY);
        UREG8 (UART_LCR) = UART_LCR_WLEN8 & ~(UART_LCR_STOP | UART_LCR_PARITY);
 
 
        /* Set baud rate */
        /* Set baud rate */
        divisor = IN_CLK/(16 * UART_BAUD_RATE);
        divisor = IN_CLK/(16 * UART_BAUD_RATE);
        REG8(UART_BASE + UART_LCR) |= UART_LCR_DLAB;
 
        REG8(UART_BASE + UART_DLL) = divisor & 0x000000ff;
 
        REG8(UART_BASE + UART_DLM) = (divisor >> 8) & 0x000000ff;
 
        REG8(UART_BASE + UART_LCR) &= ~(UART_LCR_DLAB);
 
}
 
 
 
void uart_putc(char c)
        UREG8 (UART_LCR) |= UART_LCR_DLAB;
 
        UREG8 (UART_DLL)  = divisor & 0x000000ff;
 
        UREG8 (UART_DLM)  = (divisor >> 8) & 0x000000ff;
 
        UREG8 (UART_LCR) &= ~(UART_LCR_DLAB);
 
 
 
}       /* _uart_init () */
 
 
 
 
 
/* -------------------------------------------------------------------------- */
 
/*!Put a character out on the UART
 
 
 
   @param[in] c  The character to output                                      */
 
/* -------------------------------------------------------------------------- */
 
void _uart_putc (char  c)
{
{
        unsigned char lsr;
        unsigned char lsr;
 
 
 
  /* Wait until we are free to transmit */
        WAIT_FOR_THRE;
        WAIT_FOR_THRE;
        REG8(UART_BASE + UART_TX) = c;
 
        if(c == '\n') {
  /* Put the character to be trasmitted. If it is newline (line feed) add a
 
     carriage return,  and wait until it has gone. */
 
  UREG8 (UART_TX) = c;
 
 
 
  if ('\n' == c)
 
    {
                WAIT_FOR_THRE;
                WAIT_FOR_THRE;
                REG8(UART_BASE + UART_TX) = '\r';
      UREG8 (UART_TX) = '\r';
        }
        }
 
 
        WAIT_FOR_XMITR;
        WAIT_FOR_XMITR;
}
 
 
 
char uart_getc(void)
}       /* _uart_putc () */
 
 
 
 
 
/* -------------------------------------------------------------------------- */
 
/*!Get a character from the UART
 
 
 
   @reurn  The character read.                                                */
 
/* -------------------------------------------------------------------------- */
 
char
 
_uart_getc ()
{
{
          unsigned char lsr;
          unsigned char lsr;
          char c;
          char c;
 
 
 
  /* Wait until a char is available, then get it. */
          WAIT_FOR_CHAR;
          WAIT_FOR_CHAR;
          c = REG8(UART_BASE + UART_RX);
 
 
 
          return c;
  return  UREG8 (UART_RX);
}
 
 
}       /* _uart_getc () */
 
 
 No newline at end of file
 No newline at end of file

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.