Root/xbboot/target-common/serial.c

1//
2// Authors: Wolfgang Spraul <wolfgang@qi-hardware.com>
3//
4// This program is free software; you can redistribute it and/or
5// modify it under the terms of the GNU General Public License
6// as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version.
8//
9
10#include "serial.h"
11#include "jz4740.h"
12
13u32 UART_BASE;
14
15void serial_putc(char c)
16{
17    volatile u8* uart_lsr = (volatile u8*)(UART_BASE + OFF_LSR);
18    volatile u8* uart_tdr = (volatile u8*)(UART_BASE + OFF_TDR);
19
20    if (c == '\n') serial_putc ('\r');
21
22    /* Wait for fifo to shift out some bytes */
23    while ( !((*uart_lsr & (UART_LSR_TDRQ | UART_LSR_TEMT)) == 0x60) );
24
25    *uart_tdr = (u8) c;
26}
27
28void serial_puts(const char *s)
29{
30    while (*s) serial_putc(*s++);
31}
32
33void serial_put_hex(unsigned int v)
34{
35    unsigned char c[12];
36    char i;
37    for(i = 0; i < 8;i++)
38    {
39        c[i] = (v >> ((7 - i) * 4)) & 0xf;
40        if(c[i] < 10)
41            c[i] += 0x30;
42        else
43            c[i] += (0x41 - 10);
44    }
45    c[8] = '\n';
46    c[9] = 0;
47    serial_puts(c);
48}
49
50int serial_getc()
51{
52    volatile u8* uart_rdr = (volatile u8*)(UART_BASE + OFF_RDR);
53    while (!serial_tstc());
54    return *uart_rdr;
55}
56
57int serial_tstc()
58{
59    volatile u8* uart_lsr = (volatile u8*)(UART_BASE + OFF_LSR);
60    if (*uart_lsr & UART_LSR_DR) {
61        /* Data in rfifo */
62        return 1;
63    }
64    return 0;
65}
66

Archive Download this file



interactive