| 1 | // |
| 2 | // Authors: Wolfgang Spraul <wolfgang@sharism.cc> |
| 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 "jz4740.h" |
| 11 | #include "serial.h" |
| 12 | |
| 13 | void serial_putc(char c) |
| 14 | { |
| 15 | volatile u8* uart_lsr = (volatile u8*)(UART_BASE + OFF_LSR); |
| 16 | volatile u8* uart_tdr = (volatile u8*)(UART_BASE + OFF_TDR); |
| 17 | |
| 18 | if (c == '\n') serial_putc ('\r'); |
| 19 | |
| 20 | /* Wait for fifo to shift out some bytes */ |
| 21 | while ( !((*uart_lsr & (UART_LSR_TDRQ | UART_LSR_TEMT)) == 0x60) ); |
| 22 | |
| 23 | *uart_tdr = (u8) c; |
| 24 | } |
| 25 | |
| 26 | void serial_puts(const char *s) |
| 27 | { |
| 28 | while (*s) serial_putc(*s++); |
| 29 | } |
| 30 | |
| 31 | void serial_put_hex(unsigned int v) |
| 32 | { |
| 33 | unsigned char c[12]; |
| 34 | char i; |
| 35 | for(i = 0; i < 8;i++) |
| 36 | { |
| 37 | c[i] = (v >> ((7 - i) * 4)) & 0xf; |
| 38 | if(c[i] < 10) |
| 39 | c[i] += 0x30; |
| 40 | else |
| 41 | c[i] += (0x41 - 10); |
| 42 | } |
| 43 | c[8] = '\n'; |
| 44 | c[9] = 0; |
| 45 | serial_puts(c); |
| 46 | } |
| 47 | |
| 48 | int serial_getc() |
| 49 | { |
| 50 | volatile u8* uart_rdr = (volatile u8*)(UART_BASE + OFF_RDR); |
| 51 | while (!serial_tstc()); |
| 52 | return *uart_rdr; |
| 53 | } |
| 54 | |
| 55 | int serial_tstc() |
| 56 | { |
| 57 | volatile u8* uart_lsr = (volatile u8*)(UART_BASE + OFF_LSR); |
| 58 | if (*uart_lsr & UART_LSR_DR) { |
| 59 | /* Data in rfifo */ |
| 60 | return 1; |
| 61 | } |
| 62 | return 0; |
| 63 | } |
| 64 | |