stm32_usart2.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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_usart2.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 USART2 device control block structure.
  60. */
  61. static USARTDCB dcb_usart2 = {
  62. 0, /* dcb_modeflags */
  63. 0, /* dcb_statusflags */
  64. 0, /* dcb_rtimeout */
  65. 0, /* dcb_wtimeout */
  66. { 0,0,0,0,0,0,0,0,0}, /* dcb_tx_rbf */
  67. { 0,0,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 USART2 Device
  90. */
  91. /*@{*/
  92. /*!
  93. * \brief USART2 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 USART2.
  98. *
  99. * The device is named \b uart2.
  100. *
  101. * \showinitializer
  102. */
  103. NUTDEVICE devUsartStm32_2 = {
  104. 0, /* Pointer to next device, dev_next. */
  105. {'u', 's', 'a', 'r', 't', '2', 0, 0, 0}, /* Unique device name, dev_name. */
  106. IFTYP_CHAR, /* Type of device, dev_type. */
  107. USART2_BASE, /* Base address, dev_base. */
  108. USART2_IRQn, /* First interrupt number, dev_irq. */
  109. NULL, /* Interface control block, dev_icb. */
  110. &dcb_usart2, /* 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 USART2 GPIO configuartion and assignment.
  123. */
  124. /* F1 NOREMAP REMAP
  125. * L1/F0/F2/F4 F3 F3 F0
  126. * CTS PA0 PD3
  127. * RTS PA1 PD4
  128. * TX PA2 PD5 PA14 PB3 PA14
  129. * RX PA3 PD6 PA15 PB4 PA15
  130. * CK PA4 PD7 PB5
  131. */
  132. #if defined(MCU_STM32F1)
  133. #define STM_USART_REMAP_MASK AFIO_MAPR_USART2_REMAP
  134. #if defined(USART2_REMAP_USART)
  135. #define STM_USART_REMAP_VALUE AFIO_MAPR_USART2_REMAP
  136. #define TX_GPIO_PORT NUTGPIO_PORTD
  137. #define TX_GPIO_PIN 5
  138. #define RX_GPIO_PORT NUTGPIO_PORTD
  139. #define RX_GPIO_PIN 6
  140. #ifdef USART2_HARDWARE_HANDSHAKE
  141. #define RTS_GPIO_PORT NUTGPIO_PORTD
  142. #define RTS_GPIO_PIN 4
  143. #define CTS_GPIO_PORT NUTGPIO_PORTD
  144. #define CTS_GPIO_PIN 3
  145. #endif /* USART2_HARDWARE_HANDSHAKE */
  146. #else
  147. #define STM_USART_REMAP_VALUE DISABLE
  148. #define TX_GPIO_PORT NUTGPIO_PORTA
  149. #define TX_GPIO_PIN 2
  150. #define RX_GPIO_PORT NUTGPIO_PORTA
  151. #define RX_GPIO_PIN 3
  152. #ifdef USART2_HARDWARE_HANDSHAKE
  153. #define RTS_GPIO_PORT NUTGPIO_PORTA
  154. #define RTS_GPIO_PIN 1
  155. #define CTS_GPIO_PORT NUTGPIO_PORTA
  156. #define CTS_GPIO_PIN 0
  157. #endif /* USART2_HARDWARE_HANDSHAKE */
  158. #endif /* USART2_REMAP_USART */
  159. #else /*L1/F2/F3/F4*/
  160. #if defined(MCU_STM32F3)
  161. #define STM_USART_REMAP GPIO_AF_7
  162. #elif defined(MCU_STM32F1)
  163. #define STM_USART_REMAP GPIO_AF_1
  164. #else
  165. #define STM_USART_REMAP GPIO_AF_USART2
  166. #endif
  167. #if !defined(USART2_TX_PIN)
  168. #if defined(USART2_REMAP_USART)
  169. #define TX_GPIO_PORT NUTGPIO_PORTD
  170. #define TX_GPIO_PIN 5
  171. #else
  172. #define TX_GPIO_PORT NUTGPIO_PORTA
  173. #define TX_GPIO_PIN 2
  174. #endif
  175. #elif USART2_TX_PIN == -1
  176. #elif USART2_TX_PIN == 5
  177. #define TX_GPIO_PORT NUTGPIO_PORTD
  178. #define TX_GPIO_PIN 5
  179. #elif USART2_TX_PIN == 2
  180. #define TX_GPIO_PORT NUTGPIO_PORTA
  181. #define TX_GPIO_PIN 2
  182. #elif (defined(STM32F0) || defined(MCU_STM32F3)) && USART2_TX_PIN == 14
  183. #define TX_GPIO_PORT NUTGPIO_PORTA
  184. #define TX_GPIO_PIN 14
  185. #elif (defined(STM32F0) || defined(MCU_STM32F3)) && USART2_TX_PIN == 3
  186. #define TX_GPIO_PORT NUTGPIO_PORTB
  187. #define TX_GPIO_PIN 3
  188. #else
  189. #warning "Illegal USART2 TX pin assignement"
  190. #endif
  191. #if !defined(USART2_RX_PIN)
  192. #if defined(USART2_REMAP_USART)
  193. #define RX_GPIO_PORT NUTGPIO_PORTD
  194. #define RX_GPIO_PIN 6
  195. #else
  196. #define RX_GPIO_PORT NUTGPIO_PORTA
  197. #define RX_GPIO_PIN 3
  198. #endif
  199. #elif USART2_RX_PIN == -1
  200. #elif USART2_RX_PIN == 6
  201. #define RX_GPIO_PORT NUTGPIO_PORTD
  202. #define RX_GPIO_PIN 6
  203. #elif USART2_RX_PIN == 3
  204. #define RX_GPIO_PORT NUTGPIO_PORTA
  205. #define RX_GPIO_PIN 3
  206. #elif defined(MCU_STM32F3) && USART2_TX_PIN == 15
  207. #define TX_GPIO_PORT NUTGPIO_PORT1
  208. #define TX_GPIO_PIN 15
  209. #elif defined(MCU_STM32F3) && USART2_TX_PIN == 4
  210. #define TX_GPIO_PORT NUTGPIO_PORTB
  211. #define TX_GPIO_PIN 4
  212. #else
  213. #warning "Illegal USART2 RX pin assignement"
  214. #endif
  215. #ifdef USART2_HARDWARE_HANDSHAKE
  216. #if !defined(USART2_CTS_PIN)
  217. #if defined(USART2_REMAP_USART)
  218. #define CTS_GPIO_PORT NUTGPIO_PORTD
  219. #define CTS_GPIO_PIN 3
  220. #else
  221. #define CTS_GPIO_PORT NUTGPIO_PORTA
  222. #define CTS_GPIO_PIN 0
  223. #endif
  224. #elif USART2_CTS_PIN == 3
  225. #define CTS_GPIO_PORT NUTGPIO_PORTD
  226. #define CTS_GPIO_PIN 3
  227. #elif USART2_CTS_PIN == 0
  228. #define CTS_GPIO_PORT NUTGPIO_PORTA
  229. #define CTS_GPIO_PIN 0
  230. #else
  231. #warning "Illegal USART2 CTS pin assignement"
  232. #endif
  233. #if !defined(USART2_RTS_PIN)
  234. #if defined(USART2_REMAP_USART)
  235. #define RTS_GPIO_PORT NUTGPIO_PORTD
  236. #define RTS_GPIO_PIN 4
  237. #else
  238. #define RTS_GPIO_PORT NUTGPIO_PORTA
  239. #define RTS_GPIO_PIN 1
  240. #endif
  241. #elif USART2_CTS_PIN == 4
  242. #define RTS_GPIO_PORT NUTGPIO_PORTD
  243. #define RTS_GPIO_PIN 4
  244. #elif USART2_CTS_PIN == 1
  245. #define RTS_GPIO_PORT NUTGPIO_PORTA
  246. #define RTS_GPIO_PIN 1
  247. #else
  248. #warning "Illegal USART2 RTS pin assignement"
  249. #endif
  250. #endif
  251. #endif
  252. #ifdef USART2_RS485_CTRL
  253. #define USART_485_CTRL
  254. #ifdef USART2_485RE_INV
  255. #define USART_4485RE_INV
  256. #endif
  257. #ifdef USART2_485DE_INV
  258. #define USART_4485DE_INV
  259. #endif
  260. #if defined(USART2_485DE_PORT) && defined(USART2_485DE_PIN)
  261. #define DE_GPIO_BASE GPIO_ID2GPIO(USART2_485DE_PORT)
  262. #define DE_GPIO_PORT USART2_485DE_PORT
  263. #define DE_GPIO_PIN USART2_485DE_PIN
  264. #endif
  265. #if defined(USART2_485RE_PORT) && defined(USART2_485RE_PIN)
  266. #define NRE_GPIO_BASE GPIO_ID2GPIO(USART2_485RE_PORT)
  267. #define NRE_GPIO_PORT USART2_485RE_PORT
  268. #define NRE_GPIO_PIN USART2_485RE_PIN
  269. #endif
  270. #endif /* USART2_RS485_CTRL */
  271. /*!
  272. * \brief USART2 base configuration.
  273. */
  274. static void StmUsartClkEnable(int enable)
  275. {
  276. if (enable)
  277. RCC->APB1ENR |= RCC_APB1ENR_USART2EN;
  278. RCC->APB1RSTR |= RCC_APB1RSTR_USART2RST;
  279. RCC->APB1RSTR &= ~RCC_APB1RSTR_USART2RST;
  280. if (!enable)
  281. RCC->APB1ENR &= ~RCC_APB1ENR_USART2EN;
  282. }
  283. #ifdef USART2_INIT_BAUDRATE
  284. #define USART_INIT_BAUTRATE USART2_INIT_BAUDRATE
  285. #endif
  286. #ifdef USART2_HARDWARE_HANDSHAKE
  287. #define USART_HWFLOWCTRL USART_HardwareFlowControl_RTS_CTS
  288. #define US_MODE_HWHANDSHAKE
  289. #else
  290. #define USART_HWFLOWCTRL USART_HardwareFlowControl_None
  291. #undef US_MODE_HWHANDSHAKE
  292. #endif
  293. #ifdef USART2_XONXOFF_CONTROL
  294. #define USART_XONXOFF_CONTROL
  295. #else
  296. #undef USART_XONXOFF_CONTROL
  297. #endif
  298. #ifdef USART2_MODE_IRDA
  299. #define USART_MODE_IRDA
  300. #else
  301. #undef USART_MODE_IRDA
  302. #endif
  303. #ifdef USART2_HARDWARE_HDX
  304. #define USART_HARDWARE_HDX
  305. #else
  306. #undef USART_HARDWARE_HDX
  307. #endif
  308. #ifdef USART2_SUPPORT_DMA
  309. #if defined(MCU_STM32F1)||defined(MCU_STM32L1)
  310. #define UART_DMA_TXCHANNEL DMA1_C7
  311. #define UART_DMA_RXCHANNEL DMA1_C6
  312. #elif defined(MCU_STM32F2)||defined(MCU_STM32F4)
  313. #define UART_DMA_TXCHANNEL USART2_TX_DMA
  314. #define UART_DMA_RXCHANNEL USART2_RX_DMA
  315. #else
  316. #warning "STM32 family has no implemented DMA"
  317. #endif
  318. #else
  319. #undef UART_DMA_TXCHANNEL
  320. #undef UART_DMA_RXCHANNEL
  321. #endif
  322. #if defined(USART2_TX_PIN_INV)
  323. #define USART_TX_PIN_INV
  324. #else
  325. #undef USART_TX_PIN_INV
  326. #endif
  327. #if defined(USART2_RX_PIN_INV)
  328. #define USART_RX_PIN_INV
  329. #else
  330. #undef USART_RX_PIN_INV
  331. #endif
  332. #if defined(USART2_SWAP)
  333. #define USART_SWAP
  334. #else
  335. #undef USART_SWAP
  336. #endif
  337. #define USARTn USART2
  338. #define USARTnBase USART2_BASE
  339. #define USARTirqn USART2_IRQn
  340. #define USARTclk NUT_HWCLK_PCLK1
  341. #define UART_DR_PTR (uint32_t*)(USARTnBase+4)
  342. #define SigUSART sig_USART2
  343. #define DcbUSART dcb_usart2
  344. /*@}*/
  345. #include "stm32_usart.c"