lpc176x_usart3.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (C) 2012 by Ole Reinhardt (ole.reinhardt@embedded-it.de)
  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. * \verbatim
  36. * $Id: $
  37. * \endverbatim
  38. */
  39. #include <cfg/arch.h>
  40. #include <cfg/uart.h>
  41. #include <sys/atom.h>
  42. #include <sys/event.h>
  43. #include <sys/timer.h>
  44. #include <dev/irqreg.h>
  45. #include <dev/gpio.h>
  46. #include <dev/usart.h>
  47. #include <arch/cm3/nxp/lpc176x.h>
  48. #include <arch/cm3/nxp/lpc176x_clk.h>
  49. #include <arch/cm3/nxp/lpc17xx_usart.h>
  50. /*!
  51. * \addtogroup xgNutArchCm3Lpc176xUsart
  52. */
  53. /*@{*/
  54. /*----------------------------------------------------------------------------*
  55. Public functions
  56. *----------------------------------------------------------------------------*/
  57. static uint32_t Lpc17xxUsartGetSpeed(void);
  58. static int Lpc17xxUsartSetSpeed(uint32_t baudrate);
  59. static uint8_t Lpc17xxUsartGetDataBits(void);
  60. static int Lpc17xxUsartSetDataBits(uint8_t bits);
  61. static uint8_t Lpc17xxUsartGetParity(void);
  62. static int Lpc17xxUsartSetParity(uint8_t mode);
  63. static uint8_t Lpc17xxUsartGetStopBits(void);
  64. static int Lpc17xxUsartSetStopBits(uint8_t bits);
  65. static uint32_t Lpc17xxUsartGetFlowControl(void);
  66. static int Lpc17xxUsartSetFlowControl(uint32_t flags);
  67. static uint32_t Lpc17xxUsartGetStatus(void);
  68. static int Lpc17xxUsartSetStatus(uint32_t flags);
  69. static uint8_t Lpc17xxUsartGetClockMode(void);
  70. static int Lpc17xxUsartSetClockMode(uint8_t mode);
  71. static void Lpc17xxUsartTxStart(void);
  72. static void Lpc17xxUsartRxStart(void);
  73. static int Lpc17xxUsartInit(void);
  74. static int Lpc17xxUsartDeinit(void);
  75. /*
  76. * USART3 device control block structure.
  77. */
  78. static USARTDCB dcb_usart3 = {
  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,0}, /* dcb_tx_rbf */
  84. { 0,0,0,0,0,0,0,0,0}, /* dcb_rx_rbf */
  85. 0, /* dbc_last_eol */
  86. Lpc17xxUsartInit, /* dcb_init */
  87. Lpc17xxUsartDeinit, /* dcb_deinit */
  88. Lpc17xxUsartTxStart, /* dcb_tx_start */
  89. Lpc17xxUsartRxStart, /* dcb_rx_start */
  90. Lpc17xxUsartSetFlowControl, /* dcb_set_flow_control */
  91. Lpc17xxUsartGetFlowControl, /* dcb_get_flow_control */
  92. Lpc17xxUsartSetSpeed, /* dcb_set_speed */
  93. Lpc17xxUsartGetSpeed, /* dcb_get_speed */
  94. Lpc17xxUsartSetDataBits, /* dcb_set_data_bits */
  95. Lpc17xxUsartGetDataBits, /* dcb_get_data_bits */
  96. Lpc17xxUsartSetParity, /* dcb_set_parity */
  97. Lpc17xxUsartGetParity, /* dcb_get_parity */
  98. Lpc17xxUsartSetStopBits, /* dcb_set_stop_bits */
  99. Lpc17xxUsartGetStopBits, /* dcb_get_stop_bits */
  100. Lpc17xxUsartSetStatus, /* dcb_set_status */
  101. Lpc17xxUsartGetStatus, /* dcb_get_status */
  102. Lpc17xxUsartSetClockMode, /* dcb_set_clock_mode */
  103. Lpc17xxUsartGetClockMode, /* dcb_get_clock_mode */
  104. };
  105. /*!
  106. * \name LPC17xx USART3 Device
  107. */
  108. /*@{*/
  109. /*!
  110. * \brief USART3 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 LPC17xx's on-chip USART3.
  115. *
  116. * The device is named usart3.
  117. *
  118. * \showinitializer
  119. */
  120. NUTDEVICE devUsartLpc17xx_3 = {
  121. 0, /* Pointer to next device, dev_next. */
  122. {'u', 's', 'a', 'r', 't', '3', 0, 0, 0}, /* Unique device name, dev_name. */
  123. IFTYP_CHAR, /* Type of device, dev_type. */
  124. LPC_UART3_BASE, /* Base address, dev_base. */
  125. UART3_IRQn, /* First interrupt number, dev_irq. */
  126. NULL, /* Interface control block, dev_icb. */
  127. &dcb_usart3, /* 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. * \brief USART3 GPIO configuartion and assignment.
  140. */
  141. #define TX_GPIO_PORT NUTGPIO_PORT0
  142. #define TX_GPIO_PIN 2
  143. #define TX_GPIO_PIN_CFG GPIO_CFG_OUTPUT | GPIO_CFG_PERIPHERAL1
  144. #define RX_GPIO_PORT NUTGPIO_PORT0
  145. #define RX_GPIO_PIN 3
  146. #define RX_GPIO_PIN_CFG GPIO_CFG_INPUT | GPIO_CFG_PERIPHERAL1
  147. /*!
  148. * \brief USART3 base configuration.
  149. */
  150. #ifdef USART3_INIT_BAUDRATE
  151. #define USART_INIT_BAUTRATE USART3_INIT_BAUDRATE
  152. #endif
  153. /* USART3 does not support hardware handshake */
  154. #define USART_HWFLOWCTRL USART_HardwareFlowControl_None
  155. #undef US_MODE_HWHANDSHAKE
  156. #ifdef USART3_XONXOFF_CONTROL
  157. #define USART_XONXOFF_CONTROL
  158. #else
  159. #undef USART_XONXOFF_CONTROL
  160. #endif
  161. #ifdef USART3_MODE_IRDA
  162. #define USART_MODE_IRDA
  163. #else
  164. #undef USART_MODE_IRDA
  165. #endif
  166. /* TODO: DMA Support */
  167. /*
  168. #ifdef USART3_SUPPORT_DMA
  169. #define UART_DMA_TXCHANNEL DMA1_C4
  170. #define UART_DMA_RXCHANNEL DMA1_C5
  171. #define UART_DMA_TXIRQ sig_DMA1_CH4
  172. #define UART_DMA_RXIRQ sig_DMA1_CH5
  173. #else
  174. #undef UART_DMA_TXCHANNEL
  175. #undef UART_DMA_RXCHANNEL
  176. #endif
  177. */
  178. #define USARTn LPC_UART3
  179. #define USARTnBase LPC_UART3_BASE
  180. #define USARTirqn UART3_IRQn
  181. #define SigUSART sig_USART3
  182. #define DcbUSART dcb_usart3
  183. /*@}*/
  184. #include "lpc17xx_usart.c"