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 | |
| 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 | DECLARE_GLOBAL_DATA_PTR; |
| 38 | |
| 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 | { |
| 107 | DECLARE_GLOBAL_DATA_PTR; |
| 108 | |
| 109 | if (sconsole_setbrg) { |
| 110 | (*sconsole_setbrg) (); |
| 111 | } else { |
| 112 | sconsole_buffer_t *sb = SCONSOLE_BUFFER; |
| 113 | |
| 114 | sb->baud = gd->baudrate; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | int sconsole_get_baudrate (void) |
| 119 | { |
| 120 | sconsole_buffer_t *sb = SCONSOLE_BUFFER; |
| 121 | |
| 122 | return sb->baud; |
| 123 | } |
| 124 | |
| 125 | void sconsole_flush (void) |
| 126 | { |
| 127 | if (sconsole_putc) { |
| 128 | sconsole_buffer_t *sb = SCONSOLE_BUFFER; |
| 129 | unsigned int end = sb->pos < sb->size |
| 130 | ? sb->pos + sb->max_size - sb->size |
| 131 | : sb->pos - sb->size; |
| 132 | |
| 133 | while (sb->size) { |
| 134 | (*sconsole_putc) (sb->data[end++]); |
| 135 | if (end == sb->max_size) { |
| 136 | end = 0; |
| 137 | } |
| 138 | sb->size--; |
| 139 | } |
| 140 | } |
| 141 | } |