serial.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 <nios-io.h>
  25. static nios_uart_t *uart = (nios_uart_t *)CFG_NIOS_CONSOLE;
  26. #if defined(CFG_NIOS_FIXEDBAUD)
  27. /* Everything's already setup for fixed-baud PTF
  28. * assignment
  29. */
  30. void serial_setbrg( void ){ return; }
  31. int serial_init( void ) { return(0);}
  32. #else
  33. void serial_setbrg( void )
  34. {
  35. DECLARE_GLOBAL_DATA_PTR;
  36. unsigned div;
  37. div = (CONFIG_SYS_CLK_FREQ/gd->baudrate)-1;
  38. uart->divisor = div;
  39. return;
  40. }
  41. int serial_init( void )
  42. {
  43. serial_setbrg();
  44. return(0);
  45. }
  46. #endif /* CFG_NIOS_FIXEDBAUD */
  47. void serial_putc( char c )
  48. {
  49. if (c == '\n')
  50. serial_putc('\r');
  51. while( (uart->status & NIOS_UART_TRDY) == 0 )
  52. ;
  53. uart->txdata = (unsigned char)c;
  54. }
  55. void serial_puts( const char *s )
  56. {
  57. while( *s != 0 ) {
  58. serial_putc( *s++ );
  59. }
  60. }
  61. int serial_tstc( void )
  62. {
  63. return( uart->status & NIOS_UART_RRDY);
  64. }
  65. int serial_getc( void )
  66. {
  67. while( serial_tstc() == 0 )
  68. ;
  69. return( uart->rxdata & 0x00ff );
  70. }