stm32_usart6.c 8.8 KB

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