stm32_usart1.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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_usart1.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 xgNutArchArmStm32Us
  57. */
  58. /*@{*/
  59. /*!
  60. * \brief USART1 device control block structure.
  61. */
  62. static USARTDCB dcb_usart1 = {
  63. 0, /* dcb_modeflags */
  64. 0, /* dcb_statusflags */
  65. 0, /* dcb_rtimeout */
  66. 0, /* dcb_wtimeout */
  67. { 0,0,0,0,0,0,0,0,0}, /* dcb_tx_rbf */
  68. { 0,0,0,0,0,0,0,0,0}, /* dcb_rx_rbf */
  69. 0, /* dbc_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 USART1 Device
  91. */
  92. /*@{*/
  93. /*!
  94. * \brief USART1 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 AT91's on-chip USART1.
  99. *
  100. * The device is named \b uart1.
  101. *
  102. * \showinitializer
  103. */
  104. NUTDEVICE devUsartStm32_1 = {
  105. 0, /* Pointer to next device, dev_next. */
  106. {'u', 's', 'a', 'r', 't', '1', 0, 0, 0}, /* Unique device name, dev_name. */
  107. IFTYP_CHAR, /* Type of device, dev_type. */
  108. USART1_BASE, /* Base address, dev_base. */
  109. USART1_IRQn, /* First interrupt number, dev_irq. */
  110. NULL, /* Interface control block, dev_icb. */
  111. &dcb_usart1, /* 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 USART1 GPIO configuartion and assignment.
  124. */
  125. /*
  126. * F1 NOREMAP REMAP F30 F30 F30 F0
  127. * CK PA8 PA8
  128. * TX PA9 PB6 PC4 PB6 PE0 PB6
  129. * RX PA10 PB7 PC5 PB7 PE1 PB7
  130. * CTS PA11 PA11
  131. * RTS PA12 PA12
  132. */
  133. #if defined(MCU_STM32F1)
  134. #define STM_USART_REMAP_MASK AFIO_MAPR_USART1_REMAP
  135. #if defined(USART1_REMAP_USART)
  136. #define STM_USART_REMAP_VALUE AFIO_MAPR_USART1_REMAP
  137. #define TX_GPIO_PORT NUTGPIO_PORTB
  138. #define TX_GPIO_PIN 6
  139. #define RX_GPIO_PORT NUTGPIO_PORTB
  140. #define RX_GPIO_PIN 7
  141. #else
  142. #define STM_USART_REMAP_VALUE DISABLE
  143. #define TX_GPIO_PORT NUTGPIO_PORTA
  144. #define TX_GPIO_PIN 9
  145. #define RX_GPIO_PORT NUTGPIO_PORTA
  146. #define RX_GPIO_PIN 10
  147. #endif
  148. #else /* L1/F2/F3/F4*/
  149. #if defined(MCU_STM32F3)
  150. #define STM_USART_REMAP GPIO_AF_7
  151. #elif defined(MCU_STM32F0)
  152. #define STM_USART_REMAP GPIO_AF_1
  153. /* Fixme: May also be GPIO_AF_0*/
  154. #else
  155. #define STM_USART_REMAP GPIO_AF_USART1
  156. #endif
  157. #if !defined(USART1_TX_PIN)
  158. #if defined(USART1_REMAP_USART)
  159. #define TX_GPIO_PORT NUTGPIO_PORTB
  160. #define TX_GPIO_PIN 6
  161. #else
  162. #define TX_GPIO_PORT NUTGPIO_PORTA
  163. #define TX_GPIO_PIN 9
  164. #endif
  165. #elif USART1_TX_PIN == -1
  166. /* Allow non-used TX PIN*/
  167. #elif USART1_TX_PIN == 9
  168. #define TX_GPIO_PORT NUTGPIO_PORTA
  169. #define TX_GPIO_PIN 9
  170. #elif USART1_TX_PIN == 6
  171. #define TX_GPIO_PORT NUTGPIO_PORTB
  172. #define TX_GPIO_PIN 6
  173. #elif defined(MCU_STM32F3) && USART1_TX_PIN == 4
  174. #define TX_GPIO_PORT NUTGPIO_PORTC
  175. #define TX_GPIO_PIN 4
  176. #elif defined(MCU_STM32F3) && USART1_TX_PIN == 0
  177. #define TX_GPIO_PORT NUTGPIO_PORTE
  178. #define TX_GPIO_PIN 0
  179. #else
  180. #warning "Illegal USART1 TX pin assignement"
  181. #endif
  182. #if !defined(USART1_RX_PIN)
  183. #if defined(USART1_REMAP_USART)
  184. #define TX_GPIO_PORT NUTGPIO_PORTB
  185. #define TX_GPIO_PIN 7
  186. #else
  187. #define RX_GPIO_PORT NUTGPIO_PORTA
  188. #define RX_GPIO_PIN 10
  189. #endif
  190. #elif USART1_RX_PIN == -1
  191. /* Allow non-used RX PIN*/
  192. #elif USART1_RX_PIN == 10
  193. #define RX_GPIO_PORT NUTGPIO_PORTA
  194. #define RX_GPIO_PIN 10
  195. #elif USART1_RX_PIN == 7
  196. #define RX_GPIO_PORT NUTGPIO_PORTB
  197. #define RX_GPIO_PIN 7
  198. #elif defined(MCU_STM32F3) && USART1_RX_PIN == 5
  199. #define RX_GPIO_PORT NUTGPIO_PORTC
  200. #define RX_GPIO_PIN 5
  201. #elif defined(MCU_STM32F3) && USART1_RX_PIN == 1
  202. #define RX_GPIO_PORT NUTGPIO_PORTE
  203. #define RX_GPIO_PIN 1
  204. #else
  205. #warning "Illegal USART1 RX pin assignement"
  206. #endif
  207. #endif
  208. #ifdef USART1_HARDWARE_HANDSHAKE
  209. #define RTS_GPIO_PORT NUTGPIO_PORTA
  210. #define RTS_GPIO_PIN 12
  211. #define CTS_GPIO_PORT NUTGPIO_PORTA
  212. #define CTS_GPIO_PIN 11
  213. #endif /* USART1_HARDWARE_HANDSHAKE */
  214. #ifdef USART1_RS485_CTRL
  215. #define USART_485_CTRL
  216. #ifdef USART1_485DE_INV
  217. #define USART_485DE_INV
  218. #endif
  219. #ifdef USART1_485RE_INV
  220. #define USART_485RE_INV
  221. #endif
  222. #if defined(USART1_485DE_PORT) && defined(USART1_485DE_PIN)
  223. #define DE_GPIO_BASE GPIO_ID2GPIO(USART1_485DE_PORT)
  224. #define DE_GPIO_PORT USART1_485DE_PORT
  225. #define DE_GPIO_PIN USART1_485DE_PIN
  226. #endif
  227. #if defined(USART1_485RE_PORT) && defined(USART1_485RE_PIN)
  228. #define NRE_GPIO_BASE GPIO_ID2GPIO(USART1_485RE_PORT)
  229. #define NRE_GPIO_PORT USART1_485RE_PORT
  230. #define NRE_GPIO_PIN USART1_485RE_PIN
  231. #endif
  232. #endif /* USART1_RS485_CTRL */
  233. /*!
  234. * \brief USART1 base configuration.
  235. */
  236. static void StmUsartClkEnable(int enable)
  237. {
  238. if (enable)
  239. RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
  240. RCC->APB2RSTR |= RCC_APB2RSTR_USART1RST;
  241. RCC->APB2RSTR &= ~RCC_APB2RSTR_USART1RST;
  242. if (!enable)
  243. RCC->APB2ENR &= ~RCC_APB2ENR_USART1EN;
  244. }
  245. #ifdef USART1_INIT_BAUDRATE
  246. #define USART_INIT_BAUTRATE USART1_INIT_BAUDRATE
  247. #endif
  248. #ifdef USART1_HARDWARE_HANDSHAKE
  249. #define USART_HWFLOWCTRL USART_HardwareFlowControl_RTS_CTS
  250. #define US_MODE_HWHANDSHAKE
  251. #else
  252. #define USART_HWFLOWCTRL USART_HardwareFlowControl_None
  253. #undef US_MODE_HWHANDSHAKE
  254. #endif
  255. #ifdef USART1_XONXOFF_CONTROL
  256. #define USART_XONXOFF_CONTROL
  257. #else
  258. #undef USART_XONXOFF_CONTROL
  259. #endif
  260. #ifdef USART1_MODE_IRDA
  261. #define USART_MODE_IRDA
  262. #else
  263. #undef USART_MODE_IRDA
  264. #endif
  265. #ifdef USART1_HARDWARE_HDX
  266. #define USART_HARDWARE_HDX
  267. #else
  268. #undef USART_HARDWARE_HDX
  269. #endif
  270. #ifdef USART1_SUPPORT_DMA
  271. #if defined(MCU_STM32F1)||defined(MCU_STM32L1)
  272. #define UART_DMA_TXCHANNEL DMA1_C4
  273. #define UART_DMA_RXCHANNEL DMA1_C5
  274. #elif defined(MCU_STM32F2)||defined(MCU_STM32F4)
  275. #define UART_DMA_TXCHANNEL USART1_TX_DMA
  276. #define UART_DMA_RXCHANNEL USART1_RX_DMA
  277. #else
  278. #warning "STM32 family has no implemented DMA"
  279. #endif
  280. #else
  281. #undef UART_DMA_TXCHANNEL
  282. #undef UART_DMA_RXCHANNEL
  283. #endif
  284. #if defined(USART1_TX_PIN_INV)
  285. #define USART_TX_PIN_INV
  286. #else
  287. #undef USART_TX_PIN_INV
  288. #endif
  289. #if defined(USART1_RX_PIN_INV)
  290. #define USART_RX_PIN_INV
  291. #else
  292. #undef USART_RX_PIN_INV
  293. #endif
  294. #if defined(USART1_SWAP)
  295. #define USART_SWAP
  296. #else
  297. #undef USART_SWAP
  298. #endif
  299. #define USARTn USART1
  300. #define USARTnBase USART1_BASE
  301. #define USARTirqn USART1_IRQn
  302. #if defined(MCU_STM32F0)
  303. #define USARTclk NUT_HWCLK_PCLK1
  304. #else
  305. #define USARTclk NUT_HWCLK_PCLK2
  306. #endif
  307. #define UART_DR_PTR (uint32_t*)(USARTnBase+4)
  308. #define SigUSART sig_USART1
  309. #define DcbUSART dcb_usart1
  310. /*@}*/
  311. #include "stm32_usart.c"