serial.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * cpu/mc9328/serial.c
  3. *
  4. * (c) Copyright 2004
  5. * Techware Information Technology, Inc.
  6. * http://www.techware.com.tw/
  7. *
  8. * Ming-Len Wu <minglen_wu@techware.com.tw>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #include <common.h>
  26. #include <mc9328.h>
  27. #if defined(CONFIG_UART1)
  28. /* GPIO PORT B */
  29. #define reg_GIUS MX1_GIUS_C
  30. #define reg_GPR MX1_GPR_B
  31. #define GPIO_MASK 0xFFFFE1FF
  32. #define UART_BASE 0x00206000
  33. #elif defined (CONFIG_UART2)
  34. /* GPIO PORT C */
  35. #define reg_GIUS MX1_GIUS_C
  36. #define reg_GPR MX1_GPR_C
  37. #define GPIO_MASK 0x0FFFFFFF
  38. #define UART_BASE 0x207000
  39. #endif
  40. #define reg_URXD (*((volatile u32 *)(UART_BASE+0x00)))
  41. #define reg_UTXD (*((volatile u32 *)(UART_BASE+0x40)))
  42. #define reg_UCR1 (*((volatile u32 *)(UART_BASE+0x80)))
  43. #define reg_UCR2 (*((volatile u32 *)(UART_BASE+0x84)))
  44. #define reg_UCR3 (*((volatile u32 *)(UART_BASE+0x88)))
  45. #define reg_UCR4 (*((volatile u32 *)(UART_BASE+0x8C)))
  46. #define reg_UFCR (*((volatile u32 *)(UART_BASE+0x90)))
  47. #define reg_USR1 (*((volatile u32 *)(UART_BASE+0x94)))
  48. #define reg_USR2 (*((volatile u32 *)(UART_BASE+0x98)))
  49. #define reg_UESC (*((volatile u32 *)(UART_BASE+0x9C)))
  50. #define reg_UTIM (*((volatile u32 *)(UART_BASE+0xA0)))
  51. #define reg_UBIR (*((volatile u32 *)(UART_BASE+0xA4)))
  52. #define reg_UBMR (*((volatile u32 *)(UART_BASE+0xA8)))
  53. #define reg_UBRC (*((volatile u32 *)(UART_BASE+0xAC)))
  54. #define TXFE_MASK 0x4000 /* Tx buffer empty */
  55. #define RDR_MASK 0x0001 /* receive data ready */
  56. void serial_setbrg (void) {
  57. /* config I/O pins for UART */
  58. reg_GIUS &= GPIO_MASK;
  59. reg_GPR &= GPIO_MASK;
  60. /* config UART */
  61. reg_UCR1 = 5;
  62. reg_UCR2 = 0x4027;
  63. reg_UCR4 = 1;
  64. reg_UFCR = 0xA81;
  65. reg_UBIR = 0xF;
  66. reg_UBMR = 0x8A;
  67. reg_UBRC = 8;
  68. }
  69. /*
  70. * Initialise the serial port with the given baudrate. The settings
  71. * are always 8 data bits, no parity, 1 stop bit, no start bits.
  72. *
  73. */
  74. int serial_init (void) {
  75. serial_setbrg ();
  76. return (0);
  77. }
  78. /*
  79. * Read a single byte from the serial port. Returns 1 on success, 0
  80. * otherwise. When the function is succesfull, the character read is
  81. * written into its argument c.
  82. */
  83. int serial_getc (void) {
  84. while (!(reg_USR2 & RDR_MASK)) ; /* wait until RDR bit set */
  85. return (u8)reg_URXD;
  86. }
  87. /*
  88. * Output a single byte to the serial port.
  89. */
  90. void serial_putc (const char c) {
  91. while (!(reg_USR2 & TXFE_MASK)); /* wait until TXFE bit set */
  92. reg_UTXD = (u16) c;
  93. if (c == '\n') { /* carriage return ? append line-feed */
  94. while (!(reg_USR2 & TXFE_MASK)); /* wait until TXFE bit set */
  95. reg_UTXD = '\r';
  96. }
  97. }
  98. /*
  99. * Test whether a character is in the RX buffer
  100. */
  101. int serial_tstc (void) {
  102. return reg_USR2 & RDR_MASK;
  103. }
  104. void serial_puts (const char *s) {
  105. while (*s) {
  106. serial_putc (*s++);
  107. }
  108. }