usart0.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*!
  2. * Copyright (C) 2001-2010 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. * $Log: usart0.c,v $
  36. *
  37. */
  38. #include <cfg/os.h>
  39. #include <cfg/clock.h>
  40. #include <cfg/arch.h>
  41. #include <cfg/uart.h>
  42. #include <cfg/arch/gpio.h>
  43. #include <string.h>
  44. #include <sys/atom.h>
  45. #include <sys/event.h>
  46. #include <sys/timer.h>
  47. #include <dev/irqreg.h>
  48. #include <dev/usartavr32.h>
  49. #include <dev/gpio.h>
  50. /*
  51. * Local function prototypes.
  52. */
  53. static uint32_t Avr32UsartGetSpeed(void);
  54. static int Avr32UsartSetSpeed(uint32_t rate);
  55. static uint8_t Avr32UsartGetDataBits(void);
  56. static int Avr32UsartSetDataBits(uint8_t bits);
  57. static uint8_t Avr32UsartGetParity(void);
  58. static int Avr32UsartSetParity(uint8_t mode);
  59. static uint8_t Avr32UsartGetStopBits(void);
  60. static int Avr32UsartSetStopBits(uint8_t bits);
  61. static uint32_t Avr32UsartGetFlowControl(void);
  62. static int Avr32UsartSetFlowControl(uint32_t flags);
  63. static uint32_t Avr32UsartGetStatus(void);
  64. static int Avr32UsartSetStatus(uint32_t flags);
  65. static uint8_t Avr32UsartGetClockMode(void);
  66. static int Avr32UsartSetClockMode(uint8_t mode);
  67. static void Avr32UsartTxStart(void);
  68. static void Avr32UsartRxStart(void);
  69. static int Avr32UsartInit(void);
  70. static int Avr32UsartDeinit(void);
  71. /*!
  72. * \addtogroup xgNutArchArmAvr32Us
  73. */
  74. /*@{*/
  75. /*!
  76. * \brief USART0 device control block structure.
  77. */
  78. static USARTDCB dcb_usart0 = {
  79. 0, /* dcb_modeflags */
  80. 0, /* dcb_statusflags */
  81. 0, /* dcb_rtimeout */
  82. 0, /* dcb_wtimeout */
  83. {0, 0, 0, 0, 0, 0, 0, 0}, /* dcb_tx_rbf */
  84. {0, 0, 0, 0, 0, 0, 0, 0}, /* dcb_rx_rbf */
  85. 0, /* dbc_last_eol */
  86. Avr32UsartInit, /* dcb_init */
  87. Avr32UsartDeinit, /* dcb_deinit */
  88. Avr32UsartTxStart, /* dcb_tx_start */
  89. Avr32UsartRxStart, /* dcb_rx_start */
  90. Avr32UsartSetFlowControl, /* dcb_set_flow_control */
  91. Avr32UsartGetFlowControl, /* dcb_get_flow_control */
  92. Avr32UsartSetSpeed, /* dcb_set_speed */
  93. Avr32UsartGetSpeed, /* dcb_get_speed */
  94. Avr32UsartSetDataBits, /* dcb_set_data_bits */
  95. Avr32UsartGetDataBits, /* dcb_get_data_bits */
  96. Avr32UsartSetParity, /* dcb_set_parity */
  97. Avr32UsartGetParity, /* dcb_get_parity */
  98. Avr32UsartSetStopBits, /* dcb_set_stop_bits */
  99. Avr32UsartGetStopBits, /* dcb_get_stop_bits */
  100. Avr32UsartSetStatus, /* dcb_set_status */
  101. Avr32UsartGetStatus, /* dcb_get_status */
  102. Avr32UsartSetClockMode, /* dcb_set_clock_mode */
  103. Avr32UsartGetClockMode, /* dcb_get_clock_mode */
  104. };
  105. /*!
  106. * \name Avr32 USART0 Device
  107. */
  108. /*@{*/
  109. /*!
  110. * \brief USART0 device information structure.
  111. *
  112. * An application must pass a pointer to this structure to
  113. * NutRegisterDevice() before using the serial communication
  114. * driver of the Avr32's on-chip USART0.
  115. *
  116. * The device is named \b uart0.
  117. *
  118. * \showinitializer
  119. */
  120. NUTDEVICE devUsartAvr320 = {
  121. 0, /* Pointer to next device, dev_next. */
  122. {'u', 'a', 'r', 't', '0', 0, 0, 0, 0}, /* Unique device name, dev_name. */
  123. IFTYP_CHAR, /* Type of device, dev_type. */
  124. 0, /* Base address, dev_base (not used). */
  125. 0, /* First interrupt number, dev_irq (not used). */
  126. 0, /* Interface control block, dev_icb (not used). */
  127. &dcb_usart0, /* Driver control block, dev_dcb. */
  128. UsartInit, /* Driver initialization routine, dev_init. */
  129. UsartIOCtl, /* Driver specific control function, dev_ioctl. */
  130. UsartRead, /* Read from device, dev_read. */
  131. UsartWrite, /* Write to device, dev_write. */
  132. UsartOpen, /* Open a device or file, dev_open. */
  133. UsartClose, /* Close a device or file, dev_close. */
  134. UsartSize, /* Request file size, dev_size. */
  135. UsartSelect, /* Select function, dev_select. */
  136. };
  137. /*@}*/
  138. /*@}*/
  139. #define SIG_UART sig_UART0
  140. #define dcb_usart dcb_usart0
  141. #define USARTn_BASE AVR32_USART0_ADDRESS
  142. #if UART0_ALT_PINSET == ALTERNATE_PIN_SET2
  143. # if defined(AVR32_USART0_TXD_0_0_PIN)
  144. # define USART_RX_PIN AVR32_USART0_RXD_0_1_PIN
  145. # define USART_RX_FUNCTION AVR32_USART0_RXD_0_1_FUNCTION
  146. # define USART_TX_PIN AVR32_USART0_TXD_0_1_PIN
  147. # define USART_TX_FUNCTION AVR32_USART0_TXD_0_1_FUNCTION
  148. # elif defined(AVR32_USART0_TXD_0_PIN)
  149. # define USART_RX_PIN AVR32_USART0_RXD_1_PIN
  150. # define USART_RX_FUNCTION AVR32_USART0_RXD_1_FUNCTION
  151. # define USART_TX_PIN AVR32_USART0_TXD_1_PIN
  152. # define USART_TX_FUNCTION AVR32_USART0_TXD_1_FUNCTION
  153. # endif
  154. #elif UART0_ALT_PINSET == ALTERNATE_PIN_SET3
  155. # if defined(AVR32_USART0_TXD_0_0_PIN)
  156. # define USART_RX_PIN AVR32_USART0_RXD_0_2_PIN
  157. # define USART_RX_FUNCTION AVR32_USART0_RXD_0_2_FUNCTION
  158. # define USART_TX_PIN AVR32_USART0_TXD_0_2_PIN
  159. # define USART_TX_FUNCTION AVR32_USART0_TXD_0_2_FUNCTION
  160. # elif defined(AVR32_USART0_TXD_0_PIN)
  161. # define USART_RX_PIN AVR32_USART0_RXD_2_PIN
  162. # define USART_RX_FUNCTION AVR32_USART0_RXD_2_FUNCTION
  163. # define USART_TX_PIN AVR32_USART0_TXD_2_PIN
  164. # define USART_TX_FUNCTION AVR32_USART0_TXD_2_FUNCTION
  165. # endif
  166. #else // ALTERNATE_PIN_SET1
  167. # if defined(AVR32_USART0_TXD_0_0_PIN)
  168. # define USART_RX_PIN AVR32_USART0_RXD_0_0_PIN
  169. # define USART_RX_FUNCTION AVR32_USART0_RXD_0_0_FUNCTION
  170. # define USART_TX_PIN AVR32_USART0_TXD_0_0_PIN
  171. # define USART_TX_FUNCTION AVR32_USART0_TXD_0_0_FUNCTION
  172. # elif defined(AVR32_USART0_TXD_0_PIN)
  173. # define USART_RX_PIN AVR32_USART0_RXD_0_PIN
  174. # define USART_RX_FUNCTION AVR32_USART0_RXD_0_FUNCTION
  175. # define USART_TX_PIN AVR32_USART0_TXD_0_PIN
  176. # define USART_TX_FUNCTION AVR32_USART0_TXD_0_FUNCTION
  177. # endif
  178. #endif
  179. #include "usart.c"