uart.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (C) 2005-2007 by egnite Software GmbH
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. *
  34. */
  35. /*
  36. * $Id: uart.c 2935 2010-04-01 12:14:17Z haraldkipp $
  37. */
  38. #include "utils.h"
  39. #include "uart.h"
  40. #ifndef NUT_CPU_FREQ
  41. #define NUT_CPU_FREQ 73728000
  42. #endif
  43. #define USART_BASE 0xFFFD0000 /*!< \brief USART 0 base address. */
  44. #define US_ID 2 /*!< \brief USART 0 ID. */
  45. #define US_CR (USART_BASE + 0x00) /*!< \brief Channel 0 control register address. */
  46. #define US_IDR (USART_BASE + 0x0C) /*!< \brief Interrupt disable register address. */
  47. #define US_RCR (USART_BASE + 0x34) /*!< \brief Receive counter register address. */
  48. #define US_TCR (USART_BASE + 0x3C) /*!< \brief Transmit counter register address. */
  49. #define US_BRGR (USART_BASE + 0x20) /*!< \brief USART 0 baud rate register address. */
  50. #define US_MR (USART_BASE + 0x04) /*!< \brief Channel 0 mode register address. */
  51. #define US_CSR (USART_BASE + 0x14) /*!< \brief Channel 0 status register address. */
  52. #define US_RHR (USART_BASE + 0x18) /*!< \brief Channel 0 receiver holding register address. */
  53. #define US_THR (USART_BASE + 0x1C) /*!< \brief USART 0 transmitter holding register address. */
  54. #define US_RXRDY 0x00000001 /*!< \brief Receiver ready */
  55. #define US_TXRDY 0x00000002 /*!< \brief Transmitter ready */
  56. #define US_RSTRX 0x00000004 /*!< \brief Reset receiver */
  57. #define US_RSTTX 0x00000008 /*!< \brief Reset transmitter */
  58. #define US_RXEN 0x00000010 /*!< \brief Receiver enable */
  59. #define US_RXDIS 0x00000020 /*!< \brief Receiver disable */
  60. #define US_TXEN 0x00000040 /*!< \brief Transmitter enable */
  61. #define US_TXDIS 0x00000080 /*!< \brief Transmitter disable */
  62. #define US_CHRL_8 0x000000C0 /*!< \brief 8 data bits. */
  63. #define US_PAR_NO 0x00000800 /*!< \brief No parity. */
  64. #define US_NBSTOP_1 0x00000000 /*!< \brief 1 stop bit. */
  65. #define US_CHMODE_NORMAL 0x00000000 /*!< \brief Normal mode. */
  66. #define PS_BASE 0xFFFF4000 /*!< \brief PS base address. */
  67. #define PS_PCER (PS_BASE + 0x04) /*!< \brief Peripheral clock enable register address. */
  68. #define PIO_BASE 0xFFFF0000 /*!< \brief PIO base address. */
  69. #define PIO_PDR (PIO_BASE + 0x04) /*!< \brief PIO disable register. */
  70. #define inr(_reg) (*((volatile unsigned int *)(_reg)))
  71. #define outr(_reg, _val) (*((volatile unsigned int *)(_reg)) = (_val))
  72. #define _BV(bit) (1 << bit)
  73. #define AT91_US_BAUD(baud) ((NUT_CPU_FREQ / (8 * (baud)) + 1) / 2)
  74. void UartInit(void)
  75. {
  76. /* Enable UART clock. */
  77. outr(PS_PCER, _BV(US_ID));
  78. /* Disable GPIO on UART tx/rx pins. */
  79. outr(PIO_PDR, _BV(14) | _BV(15));
  80. /* Reset UART. */
  81. outr(US_CR, US_RSTRX | US_RSTTX | US_RXDIS | US_TXDIS);
  82. /* Disable all UART interrupts. */
  83. outr(US_IDR, 0xFFFFFFFF);
  84. /* Clear UART counter registers. */
  85. outr(US_RCR, 0);
  86. outr(US_TCR, 0);
  87. /* Set UART baud rate generator register. */
  88. outr(US_BRGR, AT91_US_BAUD(115200));
  89. /* Set UART mode to 8 data bits, no parity and 1 stop bit. */
  90. outr(US_MR, US_CHMODE_NORMAL | US_CHRL_8 | US_PAR_NO | US_NBSTOP_1);
  91. /* Enable UART receiver and transmitter. */
  92. outr(US_CR, US_RXEN | US_TXEN);
  93. MicroDelay(1000);
  94. }
  95. int UartRxWait(unsigned int tmo)
  96. {
  97. while ((inr(US_CSR) & US_RXRDY) == 0) {
  98. if (tmo-- == 0) {
  99. return -1;
  100. }
  101. }
  102. return 0;
  103. }
  104. char UartRx(void)
  105. {
  106. while ((inr(US_CSR) & US_RXRDY) == 0);
  107. return (char) inr(US_RHR);
  108. }
  109. void UartTx(char ch)
  110. {
  111. while ((inr(US_CSR) & US_TXRDY) == 0);
  112. outr(US_THR, ch);
  113. }