stm32_uart4.c 8.3 KB

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