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

Subversion Repositories openrisc

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

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 527 julius
#include "or1k-support.h"
33 207 jeremybenn
#include "uart.h"
34
 
35
/*! Macro to access a UART register */
36 507 julius
#define UREG8(reg) REG8 (_board_uart_base + reg)
37 207 jeremybenn
 
38
/*! Macro to check if transmit and transmit holding registers are both empty. */
39
#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
40
 
41
/*! Macro to wait until a char has been transmitted */
42
#define WAIT_FOR_XMITR                          \
43
  do                                            \
44
    {                                           \
45
      lsr = UREG8 (UART_LSR);                   \
46
    }                                           \
47
  while ((lsr & BOTH_EMPTY) != BOTH_EMPTY)
48
 
49
/*! Macro to wait until a char can be transmitted */
50
#define WAIT_FOR_THRE                           \
51
  do                                            \
52
    {                                           \
53
      lsr = UREG8 (UART_LSR);                   \
54
    }                                           \
55
  while ((lsr & UART_LSR_THRE) != UART_LSR_THRE)
56
 
57
/* Macro to see if a character has been received */
58
#define CHECK_FOR_CHAR (UREG8 (UART_LSR) & UART_LSR_DR)
59
 
60
/* Macro to wait until a character is received */
61
#define WAIT_FOR_CHAR \
62
         do { \
63
                lsr = UREG8 (UART_LSR); \
64
         } while ((lsr & UART_LSR_DR) != UART_LSR_DR)
65
 
66
 
67
/* -------------------------------------------------------------------------- */
68
/*!Initialize the UART                                                        */
69
/* -------------------------------------------------------------------------- */
70
void
71 399 jeremybenn
__uart_init ()
72 207 jeremybenn
{
73
        int divisor;
74
 
75
        /* Reset receiver and transmiter */
76
        UREG8 (UART_FCR) = UART_FCR_ENABLE_FIFO |
77
                           UART_FCR_CLEAR_RCVR  |
78
                           UART_FCR_CLEAR_XMIT  |
79
                           UART_FCR_TRIGGER_14;
80
 
81
        /* Disable all interrupts */
82
        UREG8 (UART_IER) = 0x00;
83
 
84
        /* Set 8 bit char, 1 stop bit, no parity */
85
        UREG8 (UART_LCR) = UART_LCR_WLEN8 & ~(UART_LCR_STOP | UART_LCR_PARITY);
86
 
87
        /* Set baud rate */
88 557 yannv
        divisor = (_board_clk_freq / _board_uart_baud + 8) / 16;
89 207 jeremybenn
 
90
        UREG8 (UART_LCR) |= UART_LCR_DLAB;
91
        UREG8 (UART_DLL)  = divisor & 0x000000ff;
92
        UREG8 (UART_DLM)  = (divisor >> 8) & 0x000000ff;
93
        UREG8 (UART_LCR) &= ~(UART_LCR_DLAB);
94
 
95 399 jeremybenn
}       /* __uart_init () */
96 207 jeremybenn
 
97
 
98
/* -------------------------------------------------------------------------- */
99
/*!Put a character out on the UART
100
 
101
   @param[in] c  The character to output                                      */
102
/* -------------------------------------------------------------------------- */
103 399 jeremybenn
void __uart_putc (char  c)
104 207 jeremybenn
{
105
  unsigned char lsr;
106
 
107
  /* Wait until we are free to transmit */
108
  WAIT_FOR_THRE;
109
 
110
  /* Put the character to be transmitted and wait until it has gone. */
111
  UREG8 (UART_TX) = c;
112
  WAIT_FOR_XMITR;
113
 
114 399 jeremybenn
}       /* __uart_putc () */
115 207 jeremybenn
 
116
 
117
/* -------------------------------------------------------------------------- */
118
/*!Get a character from the UART
119
 
120
   @reurn  The character read.                                                */
121
/* -------------------------------------------------------------------------- */
122
char
123 399 jeremybenn
__uart_getc ()
124 207 jeremybenn
{
125
  unsigned char lsr;
126
  char c;
127
 
128
  /* Wait until a char is available, then get it. */
129
  WAIT_FOR_CHAR;
130
 
131
  return  UREG8 (UART_RX);
132
 
133 399 jeremybenn
}       /* __uart_getc () */

powered by: WebSVN 2.1.0

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