sconsole.c 2.5 KB

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