blob: cd9d871b5aabf3a4b9d0d505321b5f9cfdd0344a [file] [log] [blame]
wdenka6db71d2003-04-08 23:25:21 +00001/*
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
29void (*sconsole_putc) (char) = 0;
30void (*sconsole_puts) (const char *) = 0;
31int (*sconsole_getc) (void) = 0;
32int (*sconsole_tstc) (void) = 0;
33void (*sconsole_setbrg) (void) = 0;
34
35int serial_init (void)
36{
37 sconsole_buffer_t *sb = SCONSOLE_BUFFER;
38
39 sb->pos = 0;
40 sb->size = 0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020041 sb->max_size = CONFIG_SYS_SCONSOLE_SIZE - sizeof (sconsole_buffer_t);
wdenka6db71d2003-04-08 23:25:21 +000042
43 return (0);
44}
45
46void 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
65void 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
84int serial_getc (void)
85{
86 if (sconsole_getc) {
87 return (*sconsole_getc) ();
88 } else {
89 return 0;
90 }
91}
92
93int serial_tstc (void)
94{
95 if (sconsole_tstc) {
96 return (*sconsole_tstc) ();
97 } else {
98 return 0;
99 }
100}
101
102void serial_setbrg (void)
103{
104 if (sconsole_setbrg) {
105 (*sconsole_setbrg) ();
106 }
107}
108
109void 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}