wdenk | f8cac65 | 2002-08-26 22:36:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include <config.h> |
| 25 | #include <common.h> |
| 26 | |
| 27 | #include "sconsole.h" |
| 28 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 29 | DECLARE_GLOBAL_DATA_PTR; |
| 30 | |
wdenk | f8cac65 | 2002-08-26 22:36:39 +0000 | [diff] [blame] | 31 | void (*sconsole_putc) (char) = 0; |
| 32 | void (*sconsole_puts) (const char *) = 0; |
| 33 | int (*sconsole_getc) (void) = 0; |
| 34 | int (*sconsole_tstc) (void) = 0; |
| 35 | void (*sconsole_setbrg) (void) = 0; |
| 36 | |
| 37 | int serial_init (void) |
| 38 | { |
wdenk | f8cac65 | 2002-08-26 22:36:39 +0000 | [diff] [blame] | 39 | sconsole_buffer_t *sb = SCONSOLE_BUFFER; |
| 40 | |
| 41 | sb->pos = 0; |
| 42 | sb->size = 0; |
| 43 | sb->baud = gd->baudrate; |
| 44 | sb->max_size = CFG_SCONSOLE_SIZE - sizeof (sconsole_buffer_t); |
| 45 | |
| 46 | return (0); |
| 47 | } |
| 48 | |
| 49 | void serial_putc (char c) |
| 50 | { |
| 51 | if (sconsole_putc) { |
| 52 | (*sconsole_putc) (c); |
| 53 | } else { |
| 54 | sconsole_buffer_t *sb = SCONSOLE_BUFFER; |
| 55 | |
| 56 | if (c) { |
| 57 | sb->data[sb->pos++] = c; |
| 58 | if (sb->pos == sb->max_size) { |
| 59 | sb->pos = 0; |
| 60 | } |
| 61 | if (sb->size < sb->max_size) { |
| 62 | sb->size++; |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | void serial_puts (const char *s) |
| 69 | { |
| 70 | if (sconsole_puts) { |
| 71 | (*sconsole_puts) (s); |
| 72 | } else { |
| 73 | sconsole_buffer_t *sb = SCONSOLE_BUFFER; |
| 74 | |
| 75 | while (*s) { |
| 76 | sb->data[sb->pos++] = *s++; |
| 77 | if (sb->pos == sb->max_size) { |
| 78 | sb->pos = 0; |
| 79 | } |
| 80 | if (sb->size < sb->max_size) { |
| 81 | sb->size++; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | int serial_getc (void) |
| 88 | { |
| 89 | if (sconsole_getc) { |
| 90 | return (*sconsole_getc) (); |
| 91 | } else { |
| 92 | return 0; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | int serial_tstc (void) |
| 97 | { |
| 98 | if (sconsole_tstc) { |
| 99 | return (*sconsole_tstc) (); |
| 100 | } else { |
| 101 | return 0; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | void serial_setbrg (void) |
| 106 | { |
wdenk | f8cac65 | 2002-08-26 22:36:39 +0000 | [diff] [blame] | 107 | if (sconsole_setbrg) { |
| 108 | (*sconsole_setbrg) (); |
| 109 | } else { |
| 110 | sconsole_buffer_t *sb = SCONSOLE_BUFFER; |
| 111 | |
| 112 | sb->baud = gd->baudrate; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | int sconsole_get_baudrate (void) |
| 117 | { |
| 118 | sconsole_buffer_t *sb = SCONSOLE_BUFFER; |
| 119 | |
| 120 | return sb->baud; |
| 121 | } |
| 122 | |
| 123 | void sconsole_flush (void) |
| 124 | { |
| 125 | if (sconsole_putc) { |
| 126 | sconsole_buffer_t *sb = SCONSOLE_BUFFER; |
| 127 | unsigned int end = sb->pos < sb->size |
| 128 | ? sb->pos + sb->max_size - sb->size |
| 129 | : sb->pos - sb->size; |
| 130 | |
| 131 | while (sb->size) { |
| 132 | (*sconsole_putc) (sb->data[end++]); |
| 133 | if (end == sb->max_size) { |
| 134 | end = 0; |
| 135 | } |
| 136 | sb->size--; |
| 137 | } |
| 138 | } |
| 139 | } |