stm32_usart3.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * Copyright (C) 2010 by Ulrich Prinz (uprinz2@netscape.net)
  3. * Copyright (C) 2010 by Rittal GmbH & Co. KG. All rights reserved.
  4. * Copyright (C) 2012 by Uwe Bonnes (bon@elektron.ikp.physik.tu-darmstadt.de)
  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. /*!
  36. * \verbatim
  37. * $Id: stm32_usart3.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 USART3 device control block structure.
  61. */
  62. static USARTDCB dcb_usart3 = {
  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 USART3 Device
  91. */
  92. /*@{*/
  93. /*!
  94. * \brief USART3 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 USART3.
  99. *
  100. * The device is named \b uart3.
  101. *
  102. * \showinitializer
  103. */
  104. NUTDEVICE devUsartStm32_3 = {
  105. 0, /* Pointer to next device, dev_next. */
  106. {'u', 's', 'a', 'r', 't', '3', 0, 0, 0}, /* Unique device name, dev_name. */
  107. IFTYP_CHAR, /* Type of device, dev_type. */
  108. USART3_BASE, /* Base address, dev_base. */
  109. USART3_IRQn, /* First interrupt number, dev_irq. */
  110. NULL, /* Interface control block, dev_icb. */
  111. &dcb_usart3, /* 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 USART3 GPIO configuartion and assignment.
  124. *
  125. * Remap NO PART FULL
  126. * F1: TX PB10 PC10 PD8
  127. * RX PB11 PC11 PD9
  128. * CK PB12 PC12 PD10
  129. * CTS PB13 PB13 PD11
  130. * RTS PB14 PB14 PD12
  131. * L1/F2/F4: TX PB10 PC10 PD8
  132. * RX PB11 PC11 PD9
  133. * CK PB12 PC12 PD10
  134. * CTS PB13 PD11
  135. * RTS PB14 PD12
  136. * F3: TX PB10 PC10 PD8
  137. * RX PB11 PC11 PD9 PE15
  138. * CK PB12 PC12 PD10
  139. * CTS PB13 PD11 PA13
  140. * RTS PB14 PD12 PF6
  141. *
  142. */
  143. #if defined(MCU_STM32F1)
  144. #define STM_USART_REMAP_MASK AFIO_MAPR_USART3_REMAP
  145. #if defined(USART3_PARTREMAP_USART)
  146. #define STM_USART_REMAP_VALUE AFIO_MAPR_USART3_REMAP_PARTIALREMAP
  147. #define TX_GPIO_PORT NUTGPIO_PORTC
  148. #define TX_GPIO_PIN 10
  149. #define RX_GPIO_PORT NUTGPIO_PORTC
  150. #define RX_GPIO_PIN 11
  151. #if defined(USART3_HARDWARE_HANDSHAKE)
  152. #define CTS_GPIO_PORT NUTGPIO_PORTB
  153. #define CTS_GPIO_PIN 13
  154. #define RTS_GPIO_PORT NUTGPIO_PORTB
  155. #define RTS_GPIO_PIN 14
  156. #endif
  157. #elif defined(USART3_FULLREMAP_USART)
  158. #define STM_USART_REMAP_VALUE AFIO_MAPR_USART3_REMAP_FULLREMAP
  159. #define TX_GPIO_PORT NUTGPIO_PORTD
  160. #define TX_GPIO_PIN 8
  161. #define RX_GPIO_PORT NUTGPIO_PORTD
  162. #define RX_GPIO_PIN 9
  163. #if defined(USART3_HARDWARE_HANDSHAKE)
  164. #define RTS_GPIO_PORT NUTGPIO_PORTD
  165. #define RTS_GPIO_PIN 12
  166. #define CTS_GPIO_PORT NUTGPIO_PORTD
  167. #define CTS_GPIO_PIN 11
  168. #endif
  169. #else /*USART3_NOREMAP_USART*/
  170. #define STM_USART_REMAP_VALUE AFIO_MAPR_USART3_REMAP_NOREMAP
  171. #define TX_GPIO_PORT NUTGPIO_PORTB
  172. #define TX_GPIO_PIN 10
  173. #define RX_GPIO_PORT NUTGPIO_PORTB
  174. #define RX_GPIO_PIN 11
  175. #if defined(USART3_HARDWARE_HANDSHAKE)
  176. #define RTS_GPIO_PORT NUTGPIO_PORTB
  177. #define RTS_GPIO_PIN 14
  178. #define CTS_GPIO_PORT NUTGPIO_PORTB
  179. #define CTS_GPIO_PIN 13
  180. #endif
  181. #endif /* USART3_NOREMAP_USART */
  182. #else /* L1/F2/F3/F4*/
  183. #if defined(MCU_STM32F3)
  184. #define STM_USART_REMAP GPIO_AF_7
  185. #else
  186. #define STM_USART_REMAP GPIO_AF_USART3
  187. #endif
  188. #if !defined(USART3_TX_PIN)
  189. #if defined(USART3_PARTREMAP_USART)
  190. #define TX_GPIO_PORT NUTGPIO_PORTC
  191. #define TX_GPIO_PIN 10
  192. #elif defined(USART3_FULLREMAP_USART)
  193. #define TX_GPIO_PORT NUTGPIO_PORTD
  194. #define TX_GPIO_PIN 8
  195. #else
  196. #define TX_GPIO_PORT NUTGPIO_PORTB
  197. #define TX_GPIO_PIN 10
  198. #endif
  199. #elif USART3_TX_PIN == -1
  200. #elif USART3_TX_PIN == 10
  201. #define TX_GPIO_PORT NUTGPIO_PORTB
  202. #define TX_GPIO_PIN 10
  203. #elif USART3_TX_PIN == 310
  204. #define TX_GPIO_PORT NUTGPIO_PORTC
  205. #define TX_GPIO_PIN 10
  206. #elif USART3_TX_PIN == 8
  207. #define TX_GPIO_PORT NUTGPIO_PORTD
  208. #define TX_GPIO_PIN 8
  209. #else
  210. #warning "Illegal USART3 TX pin assignement"
  211. #endif
  212. #if !defined(USART3_RX_PIN)
  213. #if defined(USART3_PARTREMAP_USART)
  214. #define RX_GPIO_PORT NUTGPIO_PORTC
  215. #define RX_GPIO_PIN 11
  216. #elif defined(USART3_FULLREMAP_USART)
  217. #define RX_GPIO_PORT NUTGPIO_PORTD
  218. #define RX_GPIO_PIN 9
  219. #else
  220. #define RX_GPIO_PORT NUTGPIO_PORTB
  221. #define RX_GPIO_PIN 11
  222. #endif
  223. #elif USART3_RX_PIN == -1
  224. #elif USART3_RX_PIN == 11
  225. #define RX_GPIO_PORT NUTGPIO_PORTB
  226. #define RX_GPIO_PIN 11
  227. #elif USART3_RX_PIN == 311
  228. #define RX_GPIO_PORT NUTGPIO_PORTC
  229. #define RX_GPIO_PIN 11
  230. #elif USART3_RX_PIN == 9
  231. #define RX_GPIO_PORT NUTGPIO_PORTD
  232. #define RX_GPIO_PIN 9
  233. #elif defined(MCU_STM32F3) && USART3_RX_PIN == 15
  234. #define RX_GPIO_PORT NUTGPIO_PORTE
  235. #define RX_GPIO_PIN 15
  236. #else
  237. #warning "Illegal USART3 RX pin assignement"
  238. #endif
  239. #if !defined(USART3_CK_PIN)
  240. #if defined(USART3_PARTREMAP_USART)
  241. #define CK_GPIO_PORT NUTGPIO_PORTC
  242. #define CK_GPIO_PIN 12
  243. #elif defined(USART3_FULLREMAP_USART)
  244. #define CK_GPIO_PORT NUTGPIO_PORTD
  245. #define CK_GPIO_PIN 10
  246. #else
  247. #define CK_GPIO_PORT NUTGPIO_PORTB
  248. #define CK_GPIO_PIN 12
  249. #endif
  250. #elif USART3_CK_PIN == 12
  251. #define CK_GPIO_PORT NUTGPIO_PORTB
  252. #define CK_GPIO_PIN 12
  253. #elif USART3_CK_PIN == 312
  254. #define CK_GPIO_PORT NUTGPIO_PORTC
  255. #define CK_GPIO_PIN 12
  256. #elif USART3_CK_PIN == 10
  257. #define CK_GPIO_PORT NUTGPIO_PORTD
  258. #define CK_GPIO_PIN 10
  259. #else
  260. #warning "Illegal USART3 CK pin assignement"
  261. #endif
  262. #if defined(USART3_HARDWARE_HANDSHAKE)
  263. #if !defined(USART3_CTS_PIN)
  264. #if defined(USART3_FULLREMAP_USART)
  265. #define CTS_GPIO_PORT NUTGPIO_PORTD
  266. #define CTS_GPIO_PIN 11
  267. #else
  268. #define CTS_GPIO_PORT NUTGPIO_PORTB
  269. #define CTS_GPIO_PIN 13
  270. #endif
  271. #elif USART3_CTS_PIN == 13
  272. #define CTS_GPIO_PORT NUTGPIO_PORTB
  273. #define CTS_GPIO_PIN 13
  274. #elif USART3_CTS_PIN == 11
  275. #define CTS_GPIO_PORT NUTGPIO_PORTD
  276. #define CTS_GPIO_PIN 11
  277. #elif defined(MCU_STM32F3) && USART3_CTS_PIN == 013
  278. #define CTS_GPIO_PORT NUTGPIO_PORTA
  279. #define CTS_GPIO_PIN 13
  280. #else
  281. #warning "Illegal USART3 CTS pin assignement"
  282. #endif
  283. #if !defined(USART3_RTS_PIN)
  284. #if defined(USART3_FULLREMAP_USART)
  285. #define RTS_GPIO_PORT NUTGPIO_PORTD
  286. #define RTS_GPIO_PIN 12
  287. #else
  288. #define RTS_GPIO_PORT NUTGPIO_PORTB
  289. #define RTS_GPIO_PIN 14
  290. #endif
  291. #elif USART3_RTS_PIN == 14
  292. #define RTS_GPIO_PORT NUTGPIO_PORTB
  293. #define RTS_GPIO_PIN 14
  294. #elif USART3_RTS_PIN == 12
  295. #define RTS_GPIO_PORT NUTGPIO_PORTD
  296. #define RTS_GPIO_PIN 12
  297. #elif defined(MCU_STM32F3) && USART3_CTS_PIN == 6
  298. #define CTS_GPIO_PORT NUTGPIO_PORTF
  299. #define CTS_GPIO_PIN 6
  300. #else
  301. #warning "Illegal USART3 RTS pin assignement"
  302. #endif
  303. #endif
  304. #endif
  305. #ifdef USART3_RS485_CTRL
  306. #define USART_485_CTRL
  307. #ifdef USART3_485DE_INV
  308. #define USART_485DE_INV
  309. #endif
  310. #ifdef USART3_485RE_INV
  311. #define USART_485RE_INV
  312. #endif
  313. #if defined(USART3_485DE_PORT) && defined(USART3_485DE_PIN)
  314. #define DE_GPIO_BASE GPIO_ID2GPIO(USART3_485DE_PORT)
  315. #define DE_GPIO_PORT USART3_485DE_PORT
  316. #define DE_GPIO_PIN USART3_485DE_PIN
  317. #endif
  318. #if defined(USART3_485RE_PORT) && defined(USART3_485RE_PIN)
  319. #define NRE_GPIO_BASE GPIO_ID2GPIO(USART3_485RE_PORT)
  320. #define NRE_GPIO_PORT USART3_485RE_PORT
  321. #define NRE_GPIO_PIN USART3_485RE_PIN
  322. #endif
  323. #endif /* USART3_RS485_CTRL */
  324. /*!
  325. * \brief USART3 base configuration.
  326. */
  327. static void StmUsartClkEnable(int enable)
  328. {
  329. if (enable)
  330. RCC->APB1ENR |= RCC_APB1ENR_USART3EN;
  331. RCC->APB1RSTR |= RCC_APB1RSTR_USART3RST;
  332. RCC->APB1RSTR &= ~RCC_APB1RSTR_USART3RST;
  333. if (!enable)
  334. RCC->APB1ENR &= ~RCC_APB1ENR_USART3EN;
  335. }
  336. #ifdef USART3_INIT_BAUDRATE
  337. #define USART_INIT_BAUTRATE USART3_INIT_BAUDRATE
  338. #endif
  339. #ifdef USART3_HARDWARE_HANDSHAKE
  340. #define USART_HWFLOWCTRL USART_HardwareFlowControl_RTS_CTS
  341. #define US_MODE_HWHANDSHAKE
  342. #else
  343. #define USART_HWFLOWCTRL USART_HardwareFlowControl_None
  344. #undef US_MODE_HWHANDSHAKE
  345. #endif
  346. #ifdef USART3_XONXOFF_CONTROL
  347. #define USART_XONXOFF_CONTROL
  348. #else
  349. #undef USART_XONXOFF_CONTROL
  350. #endif
  351. #ifdef USART3_MODE_IRDA
  352. #define USART_MODE_IRDA
  353. #else
  354. #undef USART_MODE_IRDA
  355. #endif
  356. #ifdef USART3_HARDWARE_HDX
  357. #define USART_HARDWARE_HDX
  358. #else
  359. #undef USART_HARDWARE_HDX
  360. #endif
  361. #ifdef USART3_SUPPORT_DMA
  362. #if defined(MCU_STM32F1)||defined(MCU_STM32L1)
  363. #define UART_DMA_TXCHANNEL DMA1_C2
  364. #define UART_DMA_RXCHANNEL DMA1_C3
  365. #elif defined(MCU_STM32F2)||defined(MCU_STM32F4)
  366. #define UART_DMA_TXCHANNEL USART3_TX_DMA
  367. #define UART_DMA_RXCHANNEL USART3_TRX_DMA
  368. #else
  369. #warning "STM32 family has no implemented DMA"
  370. #endif
  371. #else
  372. #undef UART_DMA_TXCHANNEL
  373. #undef UART_DMA_RXCHANNEL
  374. #endif
  375. #if defined(USART3_TX_PIN_INV)
  376. #define USART_TX_PIN_INV
  377. #else
  378. #undef USART_TX_PIN_INV
  379. #endif
  380. #if defined(USART3_RX_PIN_INV)
  381. #define USART_RX_PIN_INV
  382. #else
  383. #undef USART_RX_PIN_INV
  384. #endif
  385. #if defined(USART3_SWAP)
  386. #define USART_SWAP
  387. #else
  388. #undef USART_SWAP
  389. #endif
  390. #define USARTn USART3
  391. #define USARTnBase USART3_BASE
  392. #define USARTirqn USART3_IRQn
  393. #define USARTclk NUT_HWCLK_PCLK1
  394. #define UART_DR_PTR (uint32_t*)(USARTnBase+4)
  395. #define SigUSART sig_USART3
  396. #define DcbUSART dcb_usart3
  397. /*@}*/
  398. #include "stm32_usart.c"