sconsole.c 2.7 KB

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