rs485.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * (C) Copyright 2003
  3. * Martin Krause, TQ-Systems GmbH, <martin.krause@tqs.de>
  4. *
  5. * Based on cpu/arm920t/serial.c, by Gary Jennejohn
  6. * (C) Copyright 2002 Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (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, MA 02111-1307 USA
  21. *
  22. */
  23. #include <common.h>
  24. #include <s3c2400.h>
  25. #include "rs485.h"
  26. static void rs485_setbrg (void);
  27. static void rs485_cfgio (void);
  28. static void set_rs485re(unsigned char rs485re_state);
  29. static void set_rs485de(unsigned char rs485de_state);
  30. static void rs485_setbrg (void);
  31. #ifdef NOT_USED
  32. static void trab_rs485_disable_tx(void);
  33. static void trab_rs485_disable_rx(void);
  34. #endif
  35. #define UART_NR S3C24X0_UART1
  36. /* CPLD-Register for controlling TRAB hardware functions */
  37. #define CPLD_RS485_RE ((volatile unsigned long *)0x04028000)
  38. static void rs485_setbrg (void)
  39. {
  40. S3C24X0_UART * const uart = S3C24X0_GetBase_UART(UART_NR);
  41. int i;
  42. unsigned int reg = 0;
  43. /* value is calculated so : (int)(PCLK/16./baudrate) -1 */
  44. /* reg = (33000000 / (16 * gd->baudrate)) - 1; */
  45. reg = (33000000 / (16 * 38400)) - 1;
  46. /* FIFO enable, Tx/Rx FIFO clear */
  47. uart->UFCON = 0x07;
  48. uart->UMCON = 0x0;
  49. /* Normal,No parity,1 stop,8 bit */
  50. uart->ULCON = 0x3;
  51. /*
  52. * tx=level,rx=edge,disable timeout int.,enable rx error int.,
  53. * normal,interrupt or polling
  54. */
  55. uart->UCON = 0x245;
  56. uart->UBRDIV = reg;
  57. for (i = 0; i < 100; i++);
  58. }
  59. static void rs485_cfgio (void)
  60. {
  61. S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
  62. gpio->PFCON &= ~(0x3 << 2);
  63. gpio->PFCON |= (0x2 << 2); /* configure GPF1 as RXD1 */
  64. gpio->PFCON &= ~(0x3 << 6);
  65. gpio->PFCON |= (0x2 << 6); /* configure GPF3 as TXD1 */
  66. gpio->PFUP |= (1 << 1); /* disable pullup on GPF1 */
  67. gpio->PFUP |= (1 << 3); /* disable pullup on GPF3 */
  68. gpio->PACON &= ~(1 << 11); /* set GPA11 (RS485_DE) to output */
  69. }
  70. /*
  71. * Initialise the rs485 port with the given baudrate. The settings
  72. * are always 8 data bits, no parity, 1 stop bit, no start bits.
  73. *
  74. */
  75. int rs485_init (void)
  76. {
  77. rs485_cfgio ();
  78. rs485_setbrg ();
  79. return (0);
  80. }
  81. /*
  82. * Read a single byte from the rs485 port. Returns 1 on success, 0
  83. * otherwise. When the function is succesfull, the character read is
  84. * written into its argument c.
  85. */
  86. int rs485_getc (void)
  87. {
  88. S3C24X0_UART * const uart = S3C24X0_GetBase_UART(UART_NR);
  89. /* wait for character to arrive */
  90. while (!(uart->UTRSTAT & 0x1));
  91. return uart->URXH & 0xff;
  92. }
  93. /*
  94. * Output a single byte to the rs485 port.
  95. */
  96. void rs485_putc (const char c)
  97. {
  98. S3C24X0_UART * const uart = S3C24X0_GetBase_UART(UART_NR);
  99. /* wait for room in the tx FIFO */
  100. while (!(uart->UTRSTAT & 0x2));
  101. uart->UTXH = c;
  102. /* If \n, also do \r */
  103. if (c == '\n')
  104. rs485_putc ('\r');
  105. }
  106. /*
  107. * Test whether a character is in the RX buffer
  108. */
  109. int rs485_tstc (void)
  110. {
  111. S3C24X0_UART * const uart = S3C24X0_GetBase_UART(UART_NR);
  112. return uart->UTRSTAT & 0x1;
  113. }
  114. void rs485_puts (const char *s)
  115. {
  116. while (*s) {
  117. rs485_putc (*s++);
  118. }
  119. }
  120. /*
  121. * State table:
  122. * RE DE Result
  123. * 1 1 XMIT
  124. * 0 0 RCV
  125. * 1 0 Shutdown
  126. */
  127. /* function that controls the receiver enable for the rs485 */
  128. /* rs485re_state reflects the level (0/1) of the RE pin */
  129. static void set_rs485re(unsigned char rs485re_state)
  130. {
  131. if(rs485re_state)
  132. *CPLD_RS485_RE = 0x010000;
  133. else
  134. *CPLD_RS485_RE = 0x0;
  135. }
  136. /* function that controls the sender enable for the rs485 */
  137. /* rs485de_state reflects the level (0/1) of the DE pin */
  138. static void set_rs485de(unsigned char rs485de_state)
  139. {
  140. S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
  141. /* This is on PORT A bit 11 */
  142. if(rs485de_state)
  143. gpio->PADAT |= (1 << 11);
  144. else
  145. gpio->PADAT &= ~(1 << 11);
  146. }
  147. void trab_rs485_enable_tx(void)
  148. {
  149. set_rs485de(1);
  150. set_rs485re(1);
  151. }
  152. void trab_rs485_enable_rx(void)
  153. {
  154. set_rs485re(0);
  155. set_rs485de(0);
  156. }
  157. #ifdef NOT_USED
  158. static void trab_rs485_disable_tx(void)
  159. {
  160. set_rs485de(0);
  161. }
  162. static void trab_rs485_disable_rx(void)
  163. {
  164. set_rs485re(1);
  165. }
  166. #endif