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.17.0/] [libgloss/] [or32/] [uart.c] - Blame information for rev 617

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

Line No. Rev Author Line
1 180 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 148 jeremybenn
#include "uart.h"
34
 
35 180 jeremybenn
 
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 148 jeremybenn
#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
41
 
42 180 jeremybenn
/*! 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 148 jeremybenn
 
50 180 jeremybenn
/*! 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 148 jeremybenn
 
58 180 jeremybenn
/* Macro to see if a character has been received */
59
#define CHECK_FOR_CHAR (UREG8 (UART_LSR) & UART_LSR_DR)
60 148 jeremybenn
 
61 180 jeremybenn
/* Macro to wait until a character is received */
62 148 jeremybenn
#define WAIT_FOR_CHAR \
63
         do { \
64 180 jeremybenn
                lsr = UREG8 (UART_LSR); \
65 148 jeremybenn
         } while ((lsr & UART_LSR_DR) != UART_LSR_DR)
66
 
67 180 jeremybenn
 
68
/* -------------------------------------------------------------------------- */
69
/*!Initialize the UART                                                        */
70
/* -------------------------------------------------------------------------- */
71
void
72
_uart_init ()
73 148 jeremybenn
{
74
        int divisor;
75
 
76
        /* Reset receiver and transmiter */
77 180 jeremybenn
        UREG8 (UART_FCR) = UART_FCR_ENABLE_FIFO |
78
                           UART_FCR_CLEAR_RCVR  |
79
                           UART_FCR_CLEAR_XMIT  |
80
                           UART_FCR_TRIGGER_14;
81 148 jeremybenn
 
82
        /* Disable all interrupts */
83 180 jeremybenn
        UREG8 (UART_IER) = 0x00;
84 148 jeremybenn
 
85
        /* Set 8 bit char, 1 stop bit, no parity */
86 180 jeremybenn
        UREG8 (UART_LCR) = UART_LCR_WLEN8 & ~(UART_LCR_STOP | UART_LCR_PARITY);
87 148 jeremybenn
 
88
        /* Set baud rate */
89 180 jeremybenn
        divisor = IN_CLK / (16 * UART_BAUD_RATE);
90 148 jeremybenn
 
91 180 jeremybenn
        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
}       /* _uart_init () */
97
 
98
 
99
/* -------------------------------------------------------------------------- */
100
/*!Put a character out on the UART
101
 
102
   @param[in] c  The character to output                                      */
103
/* -------------------------------------------------------------------------- */
104
void _uart_putc (char  c)
105 148 jeremybenn
{
106 180 jeremybenn
  unsigned char lsr;
107 148 jeremybenn
 
108 180 jeremybenn
  /* Wait until we are free to transmit */
109
  WAIT_FOR_THRE;
110 148 jeremybenn
 
111 184 jeremybenn
  /* Put the character to be transmitted and wait until it has gone. */
112 180 jeremybenn
  UREG8 (UART_TX) = c;
113
  WAIT_FOR_XMITR;
114
 
115
}       /* _uart_putc () */
116
 
117
 
118
/* -------------------------------------------------------------------------- */
119
/*!Get a character from the UART
120
 
121
   @reurn  The character read.                                                */
122
/* -------------------------------------------------------------------------- */
123
char
124
_uart_getc ()
125 148 jeremybenn
{
126 180 jeremybenn
  unsigned char lsr;
127
  char c;
128 148 jeremybenn
 
129 180 jeremybenn
  /* Wait until a char is available, then get it. */
130
  WAIT_FOR_CHAR;
131
 
132
  return  UREG8 (UART_RX);
133
 
134
}       /* _uart_getc () */

powered by: WebSVN 2.1.0

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