Fixed tools/env utilities
[u-boot-openmoko/mini2440.git] / board / purple / sconsole.c
blobf52d50d0a558d911b37cc922549b3443e474ac32
1 /*
2 * (C) Copyright 2002
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
6 * project.
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.
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.
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
24 #include <config.h>
25 #include <common.h>
27 #include "sconsole.h"
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;
35 int serial_init (void)
37 sconsole_buffer_t *sb = SCONSOLE_BUFFER;
39 sb->pos = 0;
40 sb->size = 0;
41 sb->max_size = CFG_SCONSOLE_SIZE - sizeof (sconsole_buffer_t);
43 return (0);
46 void serial_putc (char c)
48 if (sconsole_putc) {
49 (*sconsole_putc) (c);
50 } else {
51 sconsole_buffer_t *sb = SCONSOLE_BUFFER;
53 if (c) {
54 sb->data[sb->pos++] = c;
55 if (sb->pos == sb->max_size) {
56 sb->pos = 0;
58 if (sb->size < sb->max_size) {
59 sb->size++;
65 void serial_puts (const char *s)
67 if (sconsole_puts) {
68 (*sconsole_puts) (s);
69 } else {
70 sconsole_buffer_t *sb = SCONSOLE_BUFFER;
72 while (*s) {
73 sb->data[sb->pos++] = *s++;
74 if (sb->pos == sb->max_size) {
75 sb->pos = 0;
77 if (sb->size < sb->max_size) {
78 sb->size++;
84 int serial_getc (void)
86 if (sconsole_getc) {
87 return (*sconsole_getc) ();
88 } else {
89 return 0;
93 int serial_tstc (void)
95 if (sconsole_tstc) {
96 return (*sconsole_tstc) ();
97 } else {
98 return 0;
102 void serial_setbrg (void)
104 if (sconsole_setbrg) {
105 (*sconsole_setbrg) ();
109 void sconsole_flush (void)
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;
117 while (sb->size) {
118 (*sconsole_putc) (sb->data[end++]);
119 if (end == sb->max_size) {
120 end = 0;
122 sb->size--;