wdenk | a6db71d | 2003-04-08 23:25:21 +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 | |
| 29 | void (*sconsole_putc) (char) = 0; |
| 30 | void (*sconsole_puts) (const char *) = 0; |
| 31 | int (*sconsole_getc) (void) = 0; |
| 32 | int (*sconsole_tstc) (void) = 0; |
| 33 | void (*sconsole_setbrg) (void) = 0; |
| 34 | |
| 35 | int serial_init (void) |
| 36 | { |
| 37 | sconsole_buffer_t *sb = SCONSOLE_BUFFER; |
| 38 | |
| 39 | sb->pos = 0; |
| 40 | sb->size = 0; |
| 41 | sb->max_size = CFG_SCONSOLE_SIZE - sizeof (sconsole_buffer_t); |
| 42 | |
| 43 | return (0); |
| 44 | } |
| 45 | |
| 46 | void serial_putc (char c) |
| 47 | { |
| 48 | if (sconsole_putc) { |
| 49 | (*sconsole_putc) (c); |
| 50 | } else { |
| 51 | sconsole_buffer_t *sb = SCONSOLE_BUFFER; |
| 52 | |
| 53 | if (c) { |
| 54 | sb->data[sb->pos++] = c; |
| 55 | if (sb->pos == sb->max_size) { |
| 56 | sb->pos = 0; |
| 57 | } |
| 58 | if (sb->size < sb->max_size) { |
| 59 | sb->size++; |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | void serial_puts (const char *s) |
| 66 | { |
| 67 | if (sconsole_puts) { |
| 68 | (*sconsole_puts) (s); |
| 69 | } else { |
| 70 | sconsole_buffer_t *sb = SCONSOLE_BUFFER; |
| 71 | |
| 72 | while (*s) { |
| 73 | sb->data[sb->pos++] = *s++; |
| 74 | if (sb->pos == sb->max_size) { |
| 75 | sb->pos = 0; |
| 76 | } |
| 77 | if (sb->size < sb->max_size) { |
| 78 | sb->size++; |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | int serial_getc (void) |
| 85 | { |
| 86 | if (sconsole_getc) { |
| 87 | return (*sconsole_getc) (); |
| 88 | } else { |
| 89 | return 0; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | int serial_tstc (void) |
| 94 | { |
| 95 | if (sconsole_tstc) { |
| 96 | return (*sconsole_tstc) (); |
| 97 | } else { |
| 98 | return 0; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void serial_setbrg (void) |
| 103 | { |
| 104 | if (sconsole_setbrg) { |
| 105 | (*sconsole_setbrg) (); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void sconsole_flush (void) |
| 110 | { |
| 111 | if (sconsole_putc) { |
| 112 | sconsole_buffer_t *sb = SCONSOLE_BUFFER; |
| 113 | unsigned int end = sb->pos < sb->size |
| 114 | ? sb->pos + sb->max_size - sb->size |
| 115 | : sb->pos - sb->size; |
| 116 | |
| 117 | while (sb->size) { |
| 118 | (*sconsole_putc) (sb->data[end++]); |
| 119 | if (end == sb->max_size) { |
| 120 | end = 0; |
| 121 | } |
| 122 | sb->size--; |
| 123 | } |
| 124 | } |
| 125 | } |