OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [gnu-src/] [newlib-1.18.0/] [libgloss/] [or32/] [uart.c] - Blame information for rev 399

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 207 jeremybenn
/* uart.c. UART support functions
2
 
3
   Copyright (C) 2004, Jacob Bower
4
   Copyright (C) 2010, Embecosm Limited <info@embecosm.com>
5
 
6
   Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
7
 
8
   This file is part of Newlib.
9
 
10
   The original work by Jacob Bower is provided as-is without any kind of
11
   warranty. Use it at your own risk!
12
 
13
   All subsequent work is bound by version 3 of the GPL as follows.
14
 
15
   This program is free software; you can redistribute it and/or modify it
16
   under the terms of the GNU General Public License as published by the Free
17
   Software Foundation; either version 3 of the License, or (at your option)
18
   any later version.
19
 
20
   This program is distributed in the hope that it will be useful, but WITHOUT
21
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
23
   more details.
24
 
25
   You should have received a copy of the GNU General Public License along
26
   with this program.  If not, see <http:#www.gnu.org/licenses/>.             */
27
/* -------------------------------------------------------------------------- */
28
/* This program is commented throughout in a fashion suitable for processing
29
   with Doxygen.                                                              */
30
/* -------------------------------------------------------------------------- */
31
 
32
#include "or1ksim-board.h"
33
#include "uart.h"
34
 
35
 
36
/*! Macro to access a UART register */
37
#define UREG8(reg) REG8 (UART_BASE + reg)
38
 
39
/*! Macro to check if transmit and transmit holding registers are both empty. */
40
#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
41
 
42
/*! Macro to wait until a char has been transmitted */
43
#define WAIT_FOR_XMITR                          \
44
  do                                            \
45
    {                                           \
46
      lsr = UREG8 (UART_LSR);                   \
47
    }                                           \
48
  while ((lsr & BOTH_EMPTY) != BOTH_EMPTY)
49
 
50
/*! Macro to wait until a char can be transmitted */
51
#define WAIT_FOR_THRE                           \
52
  do                                            \
53
    {                                           \
54
      lsr = UREG8 (UART_LSR);                   \
55
    }                                           \
56
  while ((lsr & UART_LSR_THRE) != UART_LSR_THRE)
57
 
58
/* Macro to see if a character has been received */
59
#define CHECK_FOR_CHAR (UREG8 (UART_LSR) & UART_LSR_DR)
60
 
61
/* Macro to wait until a character is received */
62
#define WAIT_FOR_CHAR \
63
         do { \
64
                lsr = UREG8 (UART_LSR); \
65
         } while ((lsr & UART_LSR_DR) != UART_LSR_DR)
66
 
67
 
68
/* -------------------------------------------------------------------------- */
69
/*!Initialize the UART                                                        */
70
/* -------------------------------------------------------------------------- */
71
void
72 399 jeremybenn
__uart_init ()
73 207 jeremybenn
{
74
        int divisor;
75
 
76
        /* Reset receiver and transmiter */
77
        UREG8 (UART_FCR) = UART_FCR_ENABLE_FIFO |
78
                           UART_FCR_CLEAR_RCVR  |
79
                           UART_FCR_CLEAR_XMIT  |
80
                           UART_FCR_TRIGGER_14;
81
 
82
        /* Disable all interrupts */
83
        UREG8 (UART_IER) = 0x00;
84
 
85
        /* Set 8 bit char, 1 stop bit, no parity */
86
        UREG8 (UART_LCR) = UART_LCR_WLEN8 & ~(UART_LCR_STOP | UART_LCR_PARITY);
87
 
88
        /* Set baud rate */
89
        divisor = IN_CLK / (16 * UART_BAUD_RATE);
90
 
91
        UREG8 (UART_LCR) |= UART_LCR_DLAB;
92
        UREG8 (UART_DLL)  = divisor & 0x000000ff;
93
        UREG8 (UART_DLM)  = (divisor >> 8) & 0x000000ff;
94
        UREG8 (UART_LCR) &= ~(UART_LCR_DLAB);
95
 
96 399 jeremybenn
}       /* __uart_init () */
97 207 jeremybenn
 
98
 
99
/* -------------------------------------------------------------------------- */
100
/*!Put a character out on the UART
101
 
102
   @param[in] c  The character to output                                      */
103
/* -------------------------------------------------------------------------- */
104 399 jeremybenn
void __uart_putc (char  c)
105 207 jeremybenn
{
106
  unsigned char lsr;
107
 
108
  /* Wait until we are free to transmit */
109
  WAIT_FOR_THRE;
110
 
111
  /* Put the character to be transmitted and wait until it has gone. */
112
  UREG8 (UART_TX) = c;
113
  WAIT_FOR_XMITR;
114
 
115 399 jeremybenn
}       /* __uart_putc () */
116 207 jeremybenn
 
117
 
118
/* -------------------------------------------------------------------------- */
119
/*!Get a character from the UART
120
 
121
   @reurn  The character read.                                                */
122
/* -------------------------------------------------------------------------- */
123
char
124 399 jeremybenn
__uart_getc ()
125 207 jeremybenn
{
126
  unsigned char lsr;
127
  char c;
128
 
129
  /* Wait until a char is available, then get it. */
130
  WAIT_FOR_CHAR;
131
 
132
  return  UREG8 (UART_RX);
133
 
134 399 jeremybenn
}       /* __uart_getc () */

powered by: WebSVN 2.1.0

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