usart.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * File : usart.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006-2013, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2009-01-05 Bernard the first version
  13. * 2010-03-29 Bernard remove interrupt Tx and DMA Rx mode
  14. * 2013-05-13 aozima update for kehong-lingtai.
  15. */
  16. #include "stm32f10x.h"
  17. #include "usart.h"
  18. #include "board.h"
  19. #include <rtdevice.h>
  20. /* USART1 */
  21. #define UART1_GPIO_TX GPIO_Pin_9
  22. #define UART1_GPIO_RX GPIO_Pin_10
  23. #define UART1_GPIO GPIOA
  24. /* USART2 */
  25. #define UART2_GPIO_TX GPIO_Pin_2
  26. #define UART2_GPIO_RX GPIO_Pin_3
  27. #define UART2_GPIO GPIOA
  28. /* USART3_REMAP[1:0] = 00 */
  29. #define UART3_GPIO_TX GPIO_Pin_10
  30. #define UART3_GPIO_RX GPIO_Pin_11
  31. #define UART3_GPIO GPIOB
  32. /* STM32 uart driver */
  33. struct stm32_uart
  34. {
  35. USART_TypeDef* uart_device;
  36. IRQn_Type irq;
  37. };
  38. static rt_err_t stm32_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  39. {
  40. struct stm32_uart* uart;
  41. USART_InitTypeDef USART_InitStructure;
  42. RT_ASSERT(serial != RT_NULL);
  43. RT_ASSERT(cfg != RT_NULL);
  44. uart = (struct stm32_uart *)serial->parent.user_data;
  45. USART_InitStructure.USART_BaudRate = cfg->baud_rate;
  46. if (cfg->data_bits == DATA_BITS_8){
  47. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  48. } else if (cfg->data_bits == DATA_BITS_9) {
  49. USART_InitStructure.USART_WordLength = USART_WordLength_9b;
  50. }
  51. if (cfg->stop_bits == STOP_BITS_1){
  52. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  53. } else if (cfg->stop_bits == STOP_BITS_2){
  54. USART_InitStructure.USART_StopBits = USART_StopBits_2;
  55. }
  56. if (cfg->parity == PARITY_NONE){
  57. USART_InitStructure.USART_Parity = USART_Parity_No;
  58. } else if (cfg->parity == PARITY_ODD) {
  59. USART_InitStructure.USART_Parity = USART_Parity_Odd;
  60. } else if (cfg->parity == PARITY_EVEN) {
  61. USART_InitStructure.USART_Parity = USART_Parity_Even;
  62. }
  63. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  64. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  65. USART_Init(uart->uart_device, &USART_InitStructure);
  66. /* Enable USART */
  67. USART_Cmd(uart->uart_device, ENABLE);
  68. return RT_EOK;
  69. }
  70. static rt_err_t stm32_control(struct rt_serial_device *serial, int cmd, void *arg)
  71. {
  72. struct stm32_uart* uart;
  73. RT_ASSERT(serial != RT_NULL);
  74. uart = (struct stm32_uart *)serial->parent.user_data;
  75. switch (cmd)
  76. {
  77. /* disable interrupt */
  78. case RT_DEVICE_CTRL_CLR_INT:
  79. /* disable rx irq */
  80. UART_DISABLE_IRQ(uart->irq);
  81. /* disable interrupt */
  82. USART_ITConfig(uart->uart_device, USART_IT_RXNE, DISABLE);
  83. break;
  84. /* enable interrupt */
  85. case RT_DEVICE_CTRL_SET_INT:
  86. /* enable rx irq */
  87. UART_ENABLE_IRQ(uart->irq);
  88. /* enable interrupt */
  89. USART_ITConfig(uart->uart_device, USART_IT_RXNE, ENABLE);
  90. break;
  91. }
  92. return RT_EOK;
  93. }
  94. static int stm32_putc(struct rt_serial_device *serial, char c)
  95. {
  96. struct stm32_uart* uart;
  97. RT_ASSERT(serial != RT_NULL);
  98. uart = (struct stm32_uart *)serial->parent.user_data;
  99. while (!(uart->uart_device->SR & USART_FLAG_TXE));
  100. uart->uart_device->DR = c;
  101. return 1;
  102. }
  103. static int stm32_getc(struct rt_serial_device *serial)
  104. {
  105. int ch;
  106. struct stm32_uart* uart;
  107. RT_ASSERT(serial != RT_NULL);
  108. uart = (struct stm32_uart *)serial->parent.user_data;
  109. ch = -1;
  110. if (uart->uart_device->SR & USART_FLAG_RXNE)
  111. {
  112. ch = uart->uart_device->DR & 0xff;
  113. }
  114. return ch;
  115. }
  116. static const struct rt_uart_ops stm32_uart_ops =
  117. {
  118. stm32_configure,
  119. stm32_control,
  120. stm32_putc,
  121. stm32_getc,
  122. };
  123. #if defined(RT_USING_UART1)
  124. /* UART1 device driver structure */
  125. struct stm32_uart uart1 =
  126. {
  127. USART1,
  128. USART1_IRQn,
  129. };
  130. struct rt_serial_device serial1;
  131. void USART1_IRQHandler(void)
  132. {
  133. struct stm32_uart* uart;
  134. uart = &uart1;
  135. /* enter interrupt */
  136. rt_interrupt_enter();
  137. if(USART_GetITStatus(uart->uart_device, USART_IT_RXNE) != RESET)
  138. {
  139. rt_hw_serial_isr(&serial1, RT_SERIAL_EVENT_RX_IND);
  140. /* clear interrupt */
  141. USART_ClearITPendingBit(uart->uart_device, USART_IT_RXNE);
  142. }
  143. if (USART_GetITStatus(uart->uart_device, USART_IT_TC) != RESET)
  144. {
  145. /* clear interrupt */
  146. USART_ClearITPendingBit(uart->uart_device, USART_IT_TC);
  147. }
  148. if (USART_GetFlagStatus(uart->uart_device, USART_FLAG_ORE) == SET)
  149. {
  150. stm32_getc(&serial1);
  151. }
  152. /* leave interrupt */
  153. rt_interrupt_leave();
  154. }
  155. #endif /* RT_USING_UART1 */
  156. #if defined(RT_USING_UART2)
  157. /* UART1 device driver structure */
  158. struct stm32_uart uart2 =
  159. {
  160. USART2,
  161. USART2_IRQn,
  162. };
  163. struct rt_serial_device serial2;
  164. void USART2_IRQHandler(void)
  165. {
  166. struct stm32_uart* uart;
  167. uart = &uart2;
  168. /* enter interrupt */
  169. rt_interrupt_enter();
  170. if(USART_GetITStatus(uart->uart_device, USART_IT_RXNE) != RESET)
  171. {
  172. rt_hw_serial_isr(&serial2, RT_SERIAL_EVENT_RX_IND);
  173. /* clear interrupt */
  174. USART_ClearITPendingBit(uart->uart_device, USART_IT_RXNE);
  175. }
  176. if (USART_GetITStatus(uart->uart_device, USART_IT_TC) != RESET)
  177. {
  178. /* clear interrupt */
  179. USART_ClearITPendingBit(uart->uart_device, USART_IT_TC);
  180. }
  181. if (USART_GetFlagStatus(uart->uart_device, USART_FLAG_ORE) == SET)
  182. {
  183. stm32_getc(&serial2);
  184. }
  185. /* leave interrupt */
  186. rt_interrupt_leave();
  187. }
  188. #endif /* RT_USING_UART2 */
  189. #if defined(RT_USING_UART3)
  190. /* UART3 device driver structure */
  191. struct stm32_uart uart3 =
  192. {
  193. USART3,
  194. USART3_IRQn,
  195. };
  196. struct rt_serial_device serial3;
  197. void USART3_IRQHandler(void)
  198. {
  199. struct stm32_uart* uart;
  200. uart = &uart3;
  201. /* enter interrupt */
  202. rt_interrupt_enter();
  203. if(USART_GetITStatus(uart->uart_device, USART_IT_RXNE) != RESET)
  204. {
  205. rt_hw_serial_isr(&serial3, RT_SERIAL_EVENT_RX_IND);
  206. /* clear interrupt */
  207. USART_ClearITPendingBit(uart->uart_device, USART_IT_RXNE);
  208. }
  209. if (USART_GetITStatus(uart->uart_device, USART_IT_TC) != RESET)
  210. {
  211. /* clear interrupt */
  212. USART_ClearITPendingBit(uart->uart_device, USART_IT_TC);
  213. }
  214. if (USART_GetFlagStatus(uart->uart_device, USART_FLAG_ORE) == SET)
  215. {
  216. stm32_getc(&serial3);
  217. }
  218. /* leave interrupt */
  219. rt_interrupt_leave();
  220. }
  221. #endif /* RT_USING_UART3 */
  222. static void RCC_Configuration(void)
  223. {
  224. #if defined(RT_USING_UART1)
  225. /* Enable UART GPIO clocks */
  226. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  227. /* Enable UART clock */
  228. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  229. #endif /* RT_USING_UART1 */
  230. #if defined(RT_USING_UART2)
  231. /* Enable UART GPIO clocks */
  232. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  233. /* Enable UART clock */
  234. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  235. #endif /* RT_USING_UART2 */
  236. #if defined(RT_USING_UART3)
  237. /* Enable UART GPIO clocks */
  238. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  239. /* Enable UART clock */
  240. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
  241. #endif /* RT_USING_UART3 */
  242. }
  243. static void GPIO_Configuration(void)
  244. {
  245. GPIO_InitTypeDef GPIO_InitStructure;
  246. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  247. #if defined(RT_USING_UART1)
  248. /* Configure USART Rx/tx PIN */
  249. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  250. GPIO_InitStructure.GPIO_Pin = UART1_GPIO_RX;
  251. GPIO_Init(UART1_GPIO, &GPIO_InitStructure);
  252. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  253. GPIO_InitStructure.GPIO_Pin = UART1_GPIO_TX;
  254. GPIO_Init(UART1_GPIO, &GPIO_InitStructure);
  255. #endif /* RT_USING_UART1 */
  256. #if defined(RT_USING_UART2)
  257. /* Configure USART Rx/tx PIN */
  258. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  259. GPIO_InitStructure.GPIO_Pin = UART2_GPIO_RX;
  260. GPIO_Init(UART2_GPIO, &GPIO_InitStructure);
  261. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  262. GPIO_InitStructure.GPIO_Pin = UART2_GPIO_TX;
  263. GPIO_Init(UART2_GPIO, &GPIO_InitStructure);
  264. #endif /* RT_USING_UART2 */
  265. #if defined(RT_USING_UART3)
  266. /* Configure USART Rx/tx PIN */
  267. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  268. GPIO_InitStructure.GPIO_Pin = UART3_GPIO_RX;
  269. GPIO_Init(UART3_GPIO, &GPIO_InitStructure);
  270. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  271. GPIO_InitStructure.GPIO_Pin = UART3_GPIO_TX;
  272. GPIO_Init(UART3_GPIO, &GPIO_InitStructure);
  273. #endif /* RT_USING_UART3 */
  274. }
  275. static void NVIC_Configuration(struct stm32_uart* uart)
  276. {
  277. NVIC_InitTypeDef NVIC_InitStructure;
  278. /* Enable the USART1 Interrupt */
  279. NVIC_InitStructure.NVIC_IRQChannel = uart->irq;
  280. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  281. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  282. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  283. NVIC_Init(&NVIC_InitStructure);
  284. }
  285. void rt_hw_usart_init(void)
  286. {
  287. struct stm32_uart* uart;
  288. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  289. RCC_Configuration();
  290. GPIO_Configuration();
  291. #if defined(RT_USING_UART1)
  292. uart = &uart1;
  293. config.baud_rate = BAUD_RATE_115200;
  294. serial1.ops = &stm32_uart_ops;
  295. serial1.config = config;
  296. NVIC_Configuration(&uart1);
  297. /* register UART1 device */
  298. rt_hw_serial_register(&serial1, "uart1",
  299. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX ,
  300. uart);
  301. #endif /* RT_USING_UART1 */
  302. #if defined(RT_USING_UART2)
  303. uart = &uart2;
  304. config.baud_rate = BAUD_RATE_115200;
  305. serial2.ops = &stm32_uart_ops;
  306. serial2.config = config;
  307. NVIC_Configuration(&uart2);
  308. /* register UART1 device */
  309. rt_hw_serial_register(&serial2, "uart2",
  310. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  311. uart);
  312. #endif /* RT_USING_UART2 */
  313. #if defined(RT_USING_UART3)
  314. uart = &uart3;
  315. config.baud_rate = BAUD_RATE_115200;
  316. serial3.ops = &stm32_uart_ops;
  317. serial3.config = config;
  318. NVIC_Configuration(&uart3);
  319. /* register UART1 device */
  320. rt_hw_serial_register(&serial3, "uart3",
  321. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  322. uart);
  323. #endif /* RT_USING_UART3 */
  324. }