lpc177x_8x_usart0.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. #if defined(MCU_LPC177x_8x)
  48. #include <arch/cm3/nxp/lpc177x_8x.h>
  49. #include <arch/cm3/nxp/lpc177x_8x_clk.h>
  50. #elif defined(MCU_LPC407x_8x)
  51. #include <arch/cm3/nxp/lpc407x_8x.h>
  52. #include <arch/cm3/nxp/lpc407x_8x_clk.h>
  53. #else
  54. #warning "Unknown LPC familiy"
  55. #endif
  56. #include <arch/cm3/nxp/lpc17xx_usart.h>
  57. /*!
  58. * \addtogroup xgNutArchCm3Lpc177x_8xUsart
  59. */
  60. /*@{*/
  61. /*----------------------------------------------------------------------------*
  62. Public functions
  63. *----------------------------------------------------------------------------*/
  64. static uint32_t Lpc17xxUsartGetSpeed(void);
  65. static int Lpc17xxUsartSetSpeed(uint32_t baudrate);
  66. static uint8_t Lpc17xxUsartGetDataBits(void);
  67. static int Lpc17xxUsartSetDataBits(uint8_t bits);
  68. static uint8_t Lpc17xxUsartGetParity(void);
  69. static int Lpc17xxUsartSetParity(uint8_t mode);
  70. static uint8_t Lpc17xxUsartGetStopBits(void);
  71. static int Lpc17xxUsartSetStopBits(uint8_t bits);
  72. static uint32_t Lpc17xxUsartGetFlowControl(void);
  73. static int Lpc17xxUsartSetFlowControl(uint32_t flags);
  74. static uint32_t Lpc17xxUsartGetStatus(void);
  75. static int Lpc17xxUsartSetStatus(uint32_t flags);
  76. static uint8_t Lpc17xxUsartGetClockMode(void);
  77. static int Lpc17xxUsartSetClockMode(uint8_t mode);
  78. static void Lpc17xxUsartTxStart(void);
  79. static void Lpc17xxUsartRxStart(void);
  80. static int Lpc17xxUsartInit(void);
  81. static int Lpc17xxUsartDeinit(void);
  82. /*
  83. * USART0 device control block structure.
  84. */
  85. static USARTDCB dcb_usart0 = {
  86. 0, /* dcb_modeflags */
  87. 0, /* dcb_statusflags */
  88. 0, /* dcb_rtimeout */
  89. 0, /* dcb_wtimeout */
  90. { 0,0,0,0,0,0,0,0,0}, /* dcb_tx_rbf */
  91. { 0,0,0,0,0,0,0,0,0}, /* dcb_rx_rbf */
  92. 0, /* dbc_last_eol */
  93. Lpc17xxUsartInit, /* dcb_init */
  94. Lpc17xxUsartDeinit, /* dcb_deinit */
  95. Lpc17xxUsartTxStart, /* dcb_tx_start */
  96. Lpc17xxUsartRxStart, /* dcb_rx_start */
  97. Lpc17xxUsartSetFlowControl, /* dcb_set_flow_control */
  98. Lpc17xxUsartGetFlowControl, /* dcb_get_flow_control */
  99. Lpc17xxUsartSetSpeed, /* dcb_set_speed */
  100. Lpc17xxUsartGetSpeed, /* dcb_get_speed */
  101. Lpc17xxUsartSetDataBits, /* dcb_set_data_bits */
  102. Lpc17xxUsartGetDataBits, /* dcb_get_data_bits */
  103. Lpc17xxUsartSetParity, /* dcb_set_parity */
  104. Lpc17xxUsartGetParity, /* dcb_get_parity */
  105. Lpc17xxUsartSetStopBits, /* dcb_set_stop_bits */
  106. Lpc17xxUsartGetStopBits, /* dcb_get_stop_bits */
  107. Lpc17xxUsartSetStatus, /* dcb_set_status */
  108. Lpc17xxUsartGetStatus, /* dcb_get_status */
  109. Lpc17xxUsartSetClockMode, /* dcb_set_clock_mode */
  110. Lpc17xxUsartGetClockMode, /* dcb_get_clock_mode */
  111. };
  112. /*!
  113. * \name LPC17xx USART0 Device
  114. */
  115. /*@{*/
  116. /*!
  117. * \brief USART0 device information structure.
  118. *
  119. * An application must pass a pointer to this structure to
  120. * NutRegisterDevice() before using the serial communication
  121. * driver of the LPC17xx's on-chip USART0.
  122. *
  123. * The device is named usart0.
  124. *
  125. * \showinitializer
  126. */
  127. NUTDEVICE devUsartLpc17xx_0 = {
  128. 0, /* Pointer to next device, dev_next. */
  129. {'u', 's', 'a', 'r', 't', '0', 0, 0, 0}, /* Unique device name, dev_name. */
  130. IFTYP_CHAR, /* Type of device, dev_type. */
  131. LPC_UART0_BASE, /* Base address, dev_base. */
  132. UART0_IRQn, /* First interrupt number, dev_irq. */
  133. NULL, /* Interface control block, dev_icb. */
  134. &dcb_usart0, /* Driver control block, dev_dcb. */
  135. UsartInit, /* Driver initialization routine, dev_init. */
  136. UsartIOCtl, /* Driver specific control function, dev_ioctl. */
  137. UsartRead, /* Read from device, dev_read. */
  138. UsartWrite, /* Write to device, dev_write. */
  139. UsartOpen, /* Open a device or file, dev_open. */
  140. UsartClose, /* Close a device or file, dev_close. */
  141. UsartSize, /* Request file size, dev_size. */
  142. UsartSelect, /* Select function, dev_select. */
  143. };
  144. /*@}*/
  145. /*!
  146. * \brief USART0 GPIO configuartion and assignment.
  147. */
  148. #define TX_GPIO_PORT NUTGPIO_PORT0
  149. #define TX_GPIO_PIN 2
  150. #define TX_GPIO_PIN_CFG GPIO_CFG_OUTPUT | GPIO_CFG_PERIPHERAL1
  151. #define RX_GPIO_PORT NUTGPIO_PORT0
  152. #define RX_GPIO_PIN 3
  153. #define RX_GPIO_PIN_CFG GPIO_CFG_INPUT | GPIO_CFG_PERIPHERAL1
  154. /*!
  155. * \brief USART0 base configuration.
  156. */
  157. #ifdef USART0_INIT_BAUDRATE
  158. #define USART_INIT_BAUTRATE USART0_INIT_BAUDRATE
  159. #endif
  160. /* USART0 does not support hardware handshake */
  161. #define USART_HWFLOWCTRL USART_HardwareFlowControl_None
  162. #undef US_MODE_HWHANDSHAKE
  163. #ifdef USART0_XONXOFF_CONTROL
  164. #define USART_XONXOFF_CONTROL
  165. #else
  166. #undef USART_XONXOFF_CONTROL
  167. #endif
  168. #ifdef USART0_MODE_IRDA
  169. #define USART_MODE_IRDA
  170. #else
  171. #undef USART_MODE_IRDA
  172. #endif
  173. /* TODO: DMA Support */
  174. /*
  175. #ifdef USART0_SUPPORT_DMA
  176. #define UART_DMA_TXCHANNEL DMA1_C4
  177. #define UART_DMA_RXCHANNEL DMA1_C5
  178. #define UART_DMA_TXIRQ sig_DMA1_CH4
  179. #define UART_DMA_RXIRQ sig_DMA1_CH5
  180. #else
  181. #undef UART_DMA_TXCHANNEL
  182. #undef UART_DMA_RXCHANNEL
  183. #endif
  184. */
  185. #define USARTn LPC_UART0
  186. #define USARTnBase LPC_UART0_BASE
  187. #define USARTirqn UART0_IRQn
  188. #define SigUSART sig_USART0
  189. #define DcbUSART dcb_usart0
  190. /*@}*/
  191. #include "lpc17xx_usart.c"