serial.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * (C) Copyright 2003, Psyent Corporation <www.psyent.com>
  3. * Scott McNutt <smcnutt@psyent.com>
  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 <common.h>
  24. #include <watchdog.h>
  25. #include <nios-io.h>
  26. /*------------------------------------------------------------------
  27. * JTAG acts as the serial port
  28. *-----------------------------------------------------------------*/
  29. #if defined(CONFIG_CONSOLE_JTAG)
  30. static nios_jtag_t *jtag = (nios_jtag_t *)CFG_NIOS_CONSOLE;
  31. void serial_setbrg( void ){ return; }
  32. int serial_init( void ) { return(0);}
  33. void serial_putc (char c)
  34. {
  35. while ((jtag->txcntl & NIOS_JTAG_TRDY) != 0)
  36. WATCHDOG_RESET ();
  37. jtag->txcntl = NIOS_JTAG_TRDY | (unsigned char)c;
  38. }
  39. void serial_puts (const char *s)
  40. {
  41. while (*s != 0)
  42. serial_putc (*s++);
  43. }
  44. int serial_tstc (void)
  45. {
  46. return (jtag->rxcntl & NIOS_JTAG_RRDY);
  47. }
  48. int serial_getc (void)
  49. {
  50. int c;
  51. while (serial_tstc() == 0)
  52. WATCHDOG_RESET ();
  53. c = jtag->rxcntl & 0x0ff;
  54. jtag->rxcntl = 0;
  55. return (c);
  56. }
  57. /*------------------------------------------------------------------
  58. * UART the serial port
  59. *-----------------------------------------------------------------*/
  60. #else
  61. static nios_uart_t *uart = (nios_uart_t *)CFG_NIOS_CONSOLE;
  62. #if defined(CFG_NIOS_FIXEDBAUD)
  63. /* Everything's already setup for fixed-baud PTF
  64. * assignment
  65. */
  66. void serial_setbrg (void){ return; }
  67. int serial_init (void) { return (0);}
  68. #else
  69. void serial_setbrg (void)
  70. {
  71. DECLARE_GLOBAL_DATA_PTR;
  72. unsigned div;
  73. div = (CONFIG_SYS_CLK_FREQ/gd->baudrate)-1;
  74. uart->divisor = div;
  75. return;
  76. }
  77. int serial_init (void)
  78. {
  79. serial_setbrg ();
  80. return (0);
  81. }
  82. #endif /* CFG_NIOS_FIXEDBAUD */
  83. /*-----------------------------------------------------------------------
  84. * UART CONSOLE
  85. *---------------------------------------------------------------------*/
  86. void serial_putc (char c)
  87. {
  88. if (c == '\n')
  89. serial_putc ('\r');
  90. while ((uart->status & NIOS_UART_TRDY) == 0)
  91. WATCHDOG_RESET ();
  92. uart->txdata = (unsigned char)c;
  93. }
  94. void serial_puts (const char *s)
  95. {
  96. while (*s != 0) {
  97. serial_putc (*s++);
  98. }
  99. }
  100. int serial_tstc (void)
  101. {
  102. return (uart->status & NIOS_UART_RRDY);
  103. }
  104. int serial_getc (void)
  105. {
  106. while (serial_tstc () == 0)
  107. WATCHDOG_RESET ();
  108. return( uart->rxdata & 0x00ff );
  109. }
  110. #endif /* CONFIG_JTAG_CONSOLE */