stm32_uart5.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright (C) 2010 by Ulrich Prinz (uprinz2@netscape.net)
  3. * Copyright (C) 2010 by Rittal GmbH & Co. KG. All rights reserved.
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the copyright holders nor the names of
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * For additional information see http://www.ethernut.de/
  34. */
  35. /*
  36. * \verbatim
  37. * $Id: stm32_uart5.c 5715 2014-05-23 14:55:37Z u_bonnes $
  38. * \endverbatim
  39. */
  40. #include <cfg/os.h>
  41. #include <cfg/clock.h>
  42. #include <cfg/arch.h>
  43. #include <cfg/uart.h>
  44. #include <cfg/arch/gpio.h>
  45. #include <string.h>
  46. #include <sys/atom.h>
  47. #include <sys/event.h>
  48. #include <sys/timer.h>
  49. #include <dev/irqreg.h>
  50. #include <dev/gpio.h>
  51. #include <dev/usart.h>
  52. #include <arch/cm3/stm/stm32xxxx.h>
  53. #include <arch/cm3/stm/stm32_gpio.h>
  54. #include <arch/cm3/stm/stm32_usart.h>
  55. /*!
  56. * \addtogroup xgNutArchCm3Stm32
  57. */
  58. /*@{*/
  59. /*!
  60. * \brief UART5 device control block structure.
  61. */
  62. static USARTDCB dcb_uart5 = {
  63. 0, /* dcb_modeflags */
  64. 0, /* dcb_statusflags */
  65. 0, /* dcb_rtimeout */
  66. 0, /* dcb_wtimeout */
  67. { 0,0,0,0,0,0,0}, /* dcb_tx_rbf */
  68. { 0,0,0,0,0,0,0}, /* dcb_rx_rbf */
  69. 0, /* dcb_last_eol */
  70. Stm32UsartInit, /* dcb_init */
  71. Stm32UsartDeinit, /* dcb_deinit */
  72. Stm32UsartTxStart, /* dcb_tx_start */
  73. Stm32UsartRxStart, /* dcb_rx_start */
  74. Stm32UsartSetFlowControl, /* dcb_set_flow_control */
  75. Stm32UsartGetFlowControl, /* dcb_get_flow_control */
  76. Stm32UsartSetSpeed, /* dcb_set_speed */
  77. Stm32UsartGetSpeed, /* dcb_get_speed */
  78. Stm32UsartSetDataBits, /* dcb_set_data_bits */
  79. Stm32UsartGetDataBits, /* dcb_get_data_bits */
  80. Stm32UsartSetParity, /* dcb_set_parity */
  81. Stm32UsartGetParity, /* dcb_get_parity */
  82. Stm32UsartSetStopBits, /* dcb_set_stop_bits */
  83. Stm32UsartGetStopBits, /* dcb_get_stop_bits */
  84. Stm32UsartSetStatus, /* dcb_set_status */
  85. Stm32UsartGetStatus, /* dcb_get_status */
  86. Stm32UsartSetClockMode, /* dcb_set_clock_mode */
  87. Stm32UsartGetClockMode, /* dcb_get_clock_mode */
  88. };
  89. /*!
  90. * \name STM32 UART5 Device
  91. */
  92. /*@{*/
  93. /*!
  94. * \brief UART5 device information structure.
  95. *
  96. * An application must pass a pointer to this structure to
  97. * NutRegisterDevice() before using the serial communication
  98. * driver of the STM32s on-chip UART5.
  99. *
  100. * The device is named \b uart5.
  101. *
  102. * \showinitializer
  103. */
  104. NUTDEVICE devUartStm32_5 = {
  105. 0, /* Pointer to next device, dev_next. */
  106. {'u', 'a', 'r', 't', '5', 0, 0, 0, 0}, /* Unique device name, dev_name. */
  107. IFTYP_CHAR, /* Type of device, dev_type. */
  108. UART5_BASE, /* Base address, dev_base. */
  109. UART5_IRQn, /* First interrupt number, dev_irq. */
  110. NULL, /* Interface control block, dev_icb. */
  111. &dcb_uart5, /* Driver control block, dev_dcb. */
  112. UsartInit, /* Driver initialization routine, dev_init. */
  113. UsartIOCtl, /* Driver specific control function, dev_ioctl. */
  114. UsartRead, /* Read from device, dev_read. */
  115. UsartWrite, /* Write to device, dev_write. */
  116. UsartOpen, /* Open a device or file, dev_open. */
  117. UsartClose, /* Close a device or file, dev_close. */
  118. UsartSize, /* Request file size, dev_size. */
  119. UsartSelect, /* Select function, dev_select. */
  120. };
  121. /*@}*/
  122. /*!
  123. * \brief UART5 GPIO configuartion and assignment.
  124. */
  125. /* No alternate pins for F1/L1/F2/F4 so long */
  126. #if defined(MCU_STM32F1)
  127. #undef STM_USART_REMAP_MASK
  128. #elif defined(MCU_STM32F3)
  129. #define STM_USART_REMAP GPIO_AF_5
  130. #else
  131. #define STM_USART_REMAP GPIO_AF_UART5
  132. #endif
  133. #define TX_GPIO_PORT NUTGPIO_PORTC
  134. #define TX_GPIO_PIN 12
  135. #define RX_GPIO_PORT NUTGPIO_PORTD
  136. #define RX_GPIO_PIN 2
  137. #ifdef UART5_RS485_CTRL
  138. #define USART_485_CTRL
  139. #ifdef UART5_485RE_INV
  140. #define USART_4485RE_INV
  141. #endif
  142. #ifdef UART5_485DE_INV
  143. #define USART_4485DE_INV
  144. #endif
  145. #if defined(UART5_485DE_PORT) && defined(UART5_485DE_PIN)
  146. #define DE_GPIO_BASE GPIO_ID2GPIO(UART5_485DE_PORT)
  147. #define DE_GPIO_PORT UART5_485DE_PORT
  148. #define DE_GPIO_PIN UART5_485DE_PIN
  149. #endif
  150. #if defined(UART5_485RE_PORT) && defined(UART5_485RE_PIN)
  151. #define NRE_GPIO_BASE GPIO_ID2GPIO(UART5_485RE_PORT)
  152. #define NRE_GPIO_PORT UART5_485RE_PORT
  153. #define NRE_GPIO_PIN UART5_485RE_PIN
  154. #endif
  155. #endif /* UART5_RS485_CTRL */
  156. /*!
  157. * \brief UART5 base configuration.
  158. */
  159. static void StmUsartClkEnable(int enable)
  160. {
  161. if (enable)
  162. RCC->APB1ENR |= RCC_APB1ENR_UART5EN;
  163. RCC->APB1RSTR |= RCC_APB1RSTR_UART5RST;
  164. RCC->APB1RSTR &= ~RCC_APB1RSTR_UART5RST;
  165. if (!enable)
  166. RCC->APB1ENR &= ~RCC_APB1ENR_UART5EN;
  167. }
  168. #ifdef UART5_INIT_BAUDRATE
  169. #define USART_INIT_BAUTRATE UART5_INIT_BAUDRATE
  170. #endif
  171. #undef USART_HWFLOWCTRL
  172. #undef US_MODE_HWHANDSHAKE
  173. #ifdef UART5_XONXOFF_CONTROL
  174. #define USART_XONXOFF_CONTROL
  175. #else
  176. #undef USART_XONXOFF_CONTROL
  177. #endif
  178. #ifdef UART5_MODE_IRDA
  179. #define USART_MODE_IRDA
  180. #else
  181. #undef USART_MODE_IRDA
  182. #endif
  183. #ifdef UART5_HARDWARE_HDX
  184. #define USART_HARDWARE_HDX
  185. #else
  186. #undef USART_HARDWARE_HDX
  187. #endif
  188. #ifdef UART5_SUPPORT_DMA
  189. #if defined(MCU_STM32F2)||defined(MCU_STM32F4)
  190. #define UART_DMA_TXCHANNEL UART5_TX_DMA
  191. #define UART_DMA_RXCHANNEL UART5_RX_DMA
  192. #else
  193. #warning "STM32 family has no implemented DMA"
  194. #endif
  195. #else
  196. #undef UART_DMA_TXCHANNEL
  197. #undef UART_DMA_RXCHANNEL
  198. #endif
  199. #if defined(USART5_TX_PIN_INV)
  200. #define USART_TX_PIN_INV
  201. #else
  202. #undef USART_TX_PIN_INV
  203. #endif
  204. #if defined(USART5_RX_PIN_INV)
  205. #define USART_RX_PIN_INV
  206. #else
  207. #undef USART_RX_PIN_INV
  208. #endif
  209. #if defined(USART5_SWAP)
  210. #define USART_SWAP
  211. #else
  212. #undef USART_SWAP
  213. #endif
  214. #define USARTn UART5
  215. #define USARTnBase UART5_BASE
  216. #define USARTirqn UART5_IRQn
  217. #define USARTclk NUT_HWCLK_PCLK1
  218. #define UART_DR_PTR (uint32_t*)(USARTnBase+4)
  219. #define SigUSART sig_UART5
  220. #define DcbUSART dcb_uart5
  221. /*@}*/
  222. #include "stm32_usart.c"