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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [hello-uart/] [hello.c] - Blame information for rev 1774

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

Line No. Rev Author Line
1 1223 dries
#include "board.h"
2
#include "uart.h"
3
 
4
#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
5
 
6
#define WAIT_FOR_XMITR \
7
        do { \
8
                lsr = REG8(UART_BASE + UART_LSR); \
9
        } while ((lsr & BOTH_EMPTY) != BOTH_EMPTY)
10
 
11
#define WAIT_FOR_THRE \
12
        do { \
13
                lsr = REG8(UART_BASE + UART_LSR); \
14
        } while ((lsr & UART_LSR_THRE) != UART_LSR_THRE)
15
 
16
#define CHECK_FOR_CHAR (REG8(UART_BASE + UART_LSR) & UART_LSR_DR)
17
 
18
#define WAIT_FOR_CHAR \
19
         do { \
20
                lsr = REG8(UART_BASE + UART_LSR); \
21
         } while ((lsr & UART_LSR_DR) != UART_LSR_DR)
22
 
23
void uart_init(void)
24
{
25
  int divisor;
26
 
27
  /* Reset receiver and transmiter */
28
  REG8(UART_BASE + UART_FCR) = UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT | UART_FCR_TRIGGER_14;
29
 
30
  /* Disable all interrupts */
31
  REG8(UART_BASE + UART_IER) = 0x00;
32
 
33
  /* Set 8 bit char, 1 stop bit, no parity */
34
  REG8(UART_BASE + UART_LCR) = UART_LCR_WLEN8 & ~(UART_LCR_STOP | UART_LCR_PARITY);
35
 
36
  /* Set baud rate */
37
  divisor = IN_CLK/(16 * UART_BAUD_RATE);
38
  REG8(UART_BASE + UART_LCR) |= UART_LCR_DLAB;
39
  REG8(UART_BASE + UART_DLL) = divisor & 0x000000ff;
40
  REG8(UART_BASE + UART_DLM) = (divisor >> 8) & 0x000000ff;
41
  REG8(UART_BASE + UART_LCR) &= ~(UART_LCR_DLAB);
42
}
43
 
44
void uart_putc(char c)
45
{
46
  unsigned char lsr;
47
 
48
  WAIT_FOR_THRE;
49
  REG8(UART_BASE + UART_TX) = c;
50
  if(c == '\n') {
51
    WAIT_FOR_THRE;
52
    REG8(UART_BASE + UART_TX) = '\r';
53
  }
54
  WAIT_FOR_XMITR;
55
}
56
 
57
char uart_getc(void)
58
{
59
  unsigned char lsr;
60
  char c;
61
 
62
  WAIT_FOR_CHAR;
63
  c = REG8(UART_BASE + UART_RX);
64
  return c;
65
}
66
 
67
char *str = "Hello world!!!\n";
68
int main (void)
69
{
70
  char *s;
71
 
72
  uart_init ();
73
  for (s = str; *s; s++)
74
    uart_putc (*s);
75
 
76
  while (1)
77
    uart_putc (uart_getc () + 1);
78
 
79
  return 0;
80
}
81
 

powered by: WebSVN 2.1.0

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