drv_usart.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * Copyright (c) 2006-2019, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-11-09 xiangxistu first version
  9. * 2020-05-18 chenyaxing modify stm32_uart_config struct
  10. */
  11. #include "stdlib.h"
  12. #include "drv_common.h"
  13. #include "uart_config.h"
  14. #define DBG_TAG "drv.usart"
  15. #ifdef DRV_DEBUG
  16. #define DBG_LVL DBG_LOG
  17. #else
  18. #define DBG_LVL DBG_INFO
  19. #endif
  20. #include <rtdbg.h>
  21. #ifdef RT_USING_CONSOLE
  22. static UART_HandleTypeDef handle;
  23. /* stm32 config class */
  24. struct stm32_uart_config
  25. {
  26. const char *name;
  27. USART_TypeDef *Instance;
  28. IRQn_Type irq_type;
  29. const char *tx_pin_name;
  30. const char *rx_pin_name;
  31. };
  32. static struct stm32_uart_config *_uart_config = RT_NULL;
  33. struct stm32_uart_config uart_config[] =
  34. {
  35. #ifdef BSP_USING_UART1
  36. UART1_CONFIG,
  37. #endif
  38. #ifdef BSP_USING_UART2
  39. UART2_CONFIG,
  40. #endif
  41. #ifdef BSP_USING_UART3
  42. UART3_CONFIG,
  43. #endif
  44. #ifdef BSP_USING_UART4
  45. UART4_CONFIG,
  46. #endif
  47. #ifdef BSP_USING_UART5
  48. UART5_CONFIG,
  49. #endif
  50. #ifdef BSP_USING_UART6
  51. UART6_CONFIG,
  52. #endif
  53. #ifdef BSP_USING_UART7
  54. UART7_CONFIG,
  55. #endif
  56. #ifdef BSP_USING_UART8
  57. UART8_CONFIG,
  58. #endif
  59. #ifdef BSP_USING_LPUART1
  60. LPUART1_CONFIG,
  61. #endif
  62. };
  63. static rt_err_t stm32_uart_clk_enable(struct stm32_uart_config *config)
  64. {
  65. /* check the parameters */
  66. RT_ASSERT(IS_UART_INSTANCE(config->Instance));
  67. /* uart clock enable */
  68. switch ((uint32_t)config->Instance)
  69. {
  70. #ifdef BSP_USING_UART1
  71. case (uint32_t)USART1:
  72. __HAL_RCC_USART1_CLK_ENABLE();
  73. break;
  74. #endif /* BSP_USING_UART1 */
  75. #ifdef BSP_USING_UART2
  76. case (uint32_t)USART2:
  77. __HAL_RCC_USART2_CLK_ENABLE();
  78. break;
  79. #endif /* BSP_USING_UART2 */
  80. #ifdef BSP_USING_UART3
  81. case (uint32_t)USART3:
  82. __HAL_RCC_USART3_CLK_ENABLE();
  83. break;
  84. #endif /* BSP_USING_UART3 */
  85. #ifdef BSP_USING_UART4
  86. #if defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32L0) || \
  87. defined(SOC_SERIES_STM32G0)
  88. case (uint32_t)USART4:
  89. __HAL_RCC_USART4_CLK_ENABLE();
  90. #else
  91. case (uint32_t)UART4:
  92. __HAL_RCC_UART4_CLK_ENABLE();
  93. #endif
  94. break;
  95. #endif /* BSP_USING_UART4 */
  96. #ifdef BSP_USING_UART5
  97. #if defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32L0) || \
  98. defined(SOC_SERIES_STM32G0)
  99. case (uint32_t)USART5:
  100. __HAL_RCC_USART5_CLK_ENABLE();
  101. #else
  102. case (uint32_t)UART5:
  103. __HAL_RCC_UART5_CLK_ENABLE();
  104. #endif
  105. break;
  106. #endif /* BSP_USING_UART5 */
  107. #ifdef BSP_USING_UART6
  108. case (uint32_t)USART6:
  109. __HAL_RCC_USART6_CLK_ENABLE();
  110. break;
  111. #endif /* BSP_USING_UART6 */
  112. #ifdef BSP_USING_UART7
  113. #if defined(SOC_SERIES_STM32F0)
  114. case (uint32_t)USART7:
  115. __HAL_RCC_USART7_CLK_ENABLE();
  116. #else
  117. case (uint32_t)UART7:
  118. __HAL_RCC_UART7_CLK_ENABLE();
  119. #endif
  120. break;
  121. #endif /* BSP_USING_UART7 */
  122. #ifdef BSP_USING_UART8
  123. #if defined(SOC_SERIES_STM32F0)
  124. case (uint32_t)USART8:
  125. __HAL_RCC_USART8_CLK_ENABLE();
  126. #else
  127. case (uint32_t)UART8:
  128. __HAL_RCC_UART8_CLK_ENABLE();
  129. #endif
  130. break;
  131. #endif /* BSP_USING_UART8 */
  132. #ifdef BSP_USING_LPUART1
  133. case (uint32_t)LPUART1:
  134. __HAL_RCC_LPUART1_CLK_ENABLE();
  135. break;
  136. #endif /* BSP_USING_LPUART1 */
  137. default:
  138. return -RT_ERROR;
  139. }
  140. return RT_EOK;
  141. }
  142. static rt_err_t stm32_gpio_clk_enable(GPIO_TypeDef *gpiox)
  143. {
  144. /* check the parameters */
  145. RT_ASSERT(IS_GPIO_ALL_INSTANCE(gpiox));
  146. /* gpio ports clock enable */
  147. switch ((uint32_t)gpiox)
  148. {
  149. #if defined(__HAL_RCC_GPIOA_CLK_ENABLE)
  150. case (uint32_t)GPIOA:
  151. __HAL_RCC_GPIOA_CLK_ENABLE();
  152. break;
  153. #endif
  154. #if defined(__HAL_RCC_GPIOB_CLK_ENABLE)
  155. case (uint32_t)GPIOB:
  156. __HAL_RCC_GPIOB_CLK_ENABLE();
  157. break;
  158. #endif
  159. #if defined(__HAL_RCC_GPIOC_CLK_ENABLE)
  160. case (uint32_t)GPIOC:
  161. __HAL_RCC_GPIOC_CLK_ENABLE();
  162. break;
  163. #endif
  164. #if defined(__HAL_RCC_GPIOD_CLK_ENABLE)
  165. case (uint32_t)GPIOD:
  166. __HAL_RCC_GPIOD_CLK_ENABLE();
  167. break;
  168. #endif
  169. #if defined(__HAL_RCC_GPIOE_CLK_ENABLE)
  170. case (uint32_t)GPIOE:
  171. __HAL_RCC_GPIOE_CLK_ENABLE();
  172. break;
  173. #endif
  174. #if defined(__HAL_RCC_GPIOF_CLK_ENABLE)
  175. case (uint32_t)GPIOF:
  176. __HAL_RCC_GPIOF_CLK_ENABLE();
  177. break;
  178. #endif
  179. #if defined(__HAL_RCC_GPIOG_CLK_ENABLE)
  180. case (uint32_t)GPIOG:
  181. __HAL_RCC_GPIOG_CLK_ENABLE();
  182. break;
  183. #endif
  184. #if defined(__HAL_RCC_GPIOH_CLK_ENABLE)
  185. case (uint32_t)GPIOH:
  186. __HAL_RCC_GPIOH_CLK_ENABLE();
  187. break;
  188. #endif
  189. #if defined(__HAL_RCC_GPIOI_CLK_ENABLE)
  190. case (uint32_t)GPIOI:
  191. __HAL_RCC_GPIOI_CLK_ENABLE();
  192. break;
  193. #endif
  194. #if defined(__HAL_RCC_GPIOJ_CLK_ENABLE)
  195. case (uint32_t)GPIOJ:
  196. __HAL_RCC_GPIOJ_CLK_ENABLE();
  197. break;
  198. #endif
  199. #if defined(__HAL_RCC_GPIOK_CLK_ENABLE)
  200. case (uint32_t)GPIOK:
  201. __HAL_RCC_GPIOK_CLK_ENABLE();
  202. break;
  203. #endif
  204. default:
  205. return -RT_ERROR;
  206. }
  207. return RT_EOK;
  208. }
  209. static int up_char(char * c)
  210. {
  211. if ((*c >= 'a') && (*c <= 'z'))
  212. {
  213. *c = *c - 32;
  214. }
  215. return 0;
  216. }
  217. static void get_pin_by_name(const char* pin_name, GPIO_TypeDef **port, uint16_t *pin)
  218. {
  219. int pin_num = atoi((char*) &pin_name[2]);
  220. char port_name = pin_name[1];
  221. up_char(&port_name);
  222. up_char(&port_name);
  223. *port = ((GPIO_TypeDef *) ((uint32_t) GPIOA
  224. + (uint32_t) (port_name - 'A') * ((uint32_t) GPIOB - (uint32_t) GPIOA)));
  225. *pin = (GPIO_PIN_0 << pin_num);
  226. }
  227. static uint16_t stm32_get_pin(GPIO_TypeDef *pin_port, rt_uint32_t pin_num)
  228. {
  229. return (uint16_t)((16 * (((rt_base_t)pin_port - (rt_base_t)GPIOA_BASE)/(0x0400UL))) + (__rt_ffs(pin_num) - 1));
  230. }
  231. static rt_err_t stm32_gpio_configure(struct stm32_uart_config *config)
  232. {
  233. #define UART_IS_TX (1U<<7)
  234. #define UART_IS_RX (0U)
  235. rt_uint16_t tx_pin_num = 0;
  236. int index = 0, tx_af_num = 0, rx_af_num = 0;
  237. uint8_t uart_num = 0;
  238. GPIO_TypeDef *tx_port;
  239. GPIO_TypeDef *rx_port;
  240. uint16_t tx_pin;
  241. uint16_t rx_pin;
  242. get_pin_by_name(config->rx_pin_name, &rx_port, &rx_pin);
  243. get_pin_by_name(config->tx_pin_name, &tx_port, &tx_pin);
  244. struct gpio_uart_af {
  245. /* index get by GET_PIN */
  246. uint16_t pin_index;
  247. struct {
  248. /* when is TX the bit7 is 1, bit6-bit0 is uart number (1-8) */
  249. uint8_t uart_num;
  250. uint8_t af_num;
  251. } afs[1];
  252. };
  253. static const struct gpio_uart_af uart_afs[] =
  254. {
  255. { .pin_index = GET_PIN(A, 0), .afs[0] = {.uart_num = UART_IS_TX|4, .af_num = 4}},
  256. { .pin_index = GET_PIN(A, 1), .afs[0] = {.uart_num = UART_IS_RX|4, .af_num = 4}},
  257. { .pin_index = GET_PIN(A, 2), .afs[0] = {.uart_num = UART_IS_TX|2, .af_num = 1}},
  258. { .pin_index = GET_PIN(A, 3), .afs[0] = {.uart_num = UART_IS_RX|2, .af_num = 1}},
  259. { .pin_index = GET_PIN(A, 9), .afs[0] = {.uart_num = UART_IS_TX|1, .af_num = 1}},
  260. { .pin_index = GET_PIN(A, 10), .afs[0] = {.uart_num = UART_IS_RX|1, .af_num = 1}},
  261. { .pin_index = GET_PIN(A, 14), .afs[0] = {.uart_num = UART_IS_TX|2, .af_num = 1}},
  262. { .pin_index = GET_PIN(A, 15), .afs[0] = {.uart_num = UART_IS_RX|2, .af_num = 1}},
  263. { .pin_index = GET_PIN(B, 6), .afs[0] = {.uart_num = UART_IS_TX|1, .af_num = 0}},
  264. { .pin_index = GET_PIN(B, 7), .afs[0] = {.uart_num = UART_IS_RX|1, .af_num = 0}},
  265. { .pin_index = GET_PIN(B, 8), .afs[0] = {.uart_num = UART_IS_TX|3, .af_num = 4}},
  266. { .pin_index = GET_PIN(B, 9), .afs[0] = {.uart_num = UART_IS_RX|3, .af_num = 4}},
  267. { .pin_index = GET_PIN(B, 10), .afs[0] = {.uart_num = UART_IS_TX|3, .af_num = 4}},
  268. { .pin_index = GET_PIN(B, 11), .afs[0] = {.uart_num = UART_IS_RX|3, .af_num = 4}},
  269. { .pin_index = GET_PIN(C, 4), .afs[0] = {.uart_num = UART_IS_TX|3, .af_num = 0}},
  270. { .pin_index = GET_PIN(C, 5), .afs[0] = {.uart_num = UART_IS_RX|3, .af_num = 0}},
  271. { .pin_index = GET_PIN(C, 4), .afs[0] = {.uart_num = UART_IS_TX|1, .af_num = 1}},
  272. { .pin_index = GET_PIN(C, 5), .afs[0] = {.uart_num = UART_IS_RX|1, .af_num = 1}},
  273. { .pin_index = GET_PIN(C, 10), .afs[0] = {.uart_num = UART_IS_TX|3, .af_num = 0}},
  274. { .pin_index = GET_PIN(C, 11), .afs[0] = {.uart_num = UART_IS_RX|3, .af_num = 0}},
  275. { .pin_index = GET_PIN(C, 10), .afs[0] = {.uart_num = UART_IS_TX|4, .af_num = 1}},
  276. { .pin_index = GET_PIN(C, 11), .afs[0] = {.uart_num = UART_IS_RX|4, .af_num = 1}},
  277. { .pin_index = GET_PIN(D, 5), .afs[0] = {.uart_num = UART_IS_TX|2, .af_num = 0}},
  278. { .pin_index = GET_PIN(D, 6), .afs[0] = {.uart_num = UART_IS_RX|2, .af_num = 0}},
  279. { .pin_index = GET_PIN(D, 8), .afs[0] = {.uart_num = UART_IS_TX|3, .af_num = 0}},
  280. { .pin_index = GET_PIN(D, 9), .afs[0] = {.uart_num = UART_IS_RX|3, .af_num = 0}},
  281. };
  282. /* get tx/rx pin index */
  283. uart_num = config->name[4] - '0';
  284. tx_pin_num = stm32_get_pin(tx_port, tx_pin);
  285. for (index = 0; index < sizeof(uart_afs) / sizeof(struct gpio_uart_af); index = index + 2)
  286. {
  287. if (uart_afs[index].pin_index == tx_pin_num)
  288. {
  289. if(uart_afs[index].afs[0].uart_num == (uart_num|UART_IS_TX))
  290. {
  291. tx_af_num = uart_afs[index].afs[0].af_num;
  292. rx_af_num = uart_afs[index + 1].afs[0].af_num;
  293. }
  294. }
  295. }
  296. /* gpio Init */
  297. GPIO_InitTypeDef GPIO_InitStruct = {0};
  298. /* gpio ports clock enable */
  299. stm32_gpio_clk_enable(tx_port);
  300. if (tx_port != rx_port)
  301. {
  302. stm32_gpio_clk_enable(rx_port);
  303. }
  304. /* rx pin initialize */
  305. GPIO_InitStruct.Pin = tx_pin;
  306. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  307. GPIO_InitStruct.Pull = GPIO_PULLUP;
  308. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  309. #if defined(SOC_SERIES_STM32L0) || defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32H7)
  310. GPIO_InitStruct.Alternate = tx_af_num;
  311. #endif
  312. HAL_GPIO_Init(tx_port, &GPIO_InitStruct);
  313. /* rx pin initialize */
  314. GPIO_InitStruct.Pin = rx_pin;
  315. #if defined(SOC_SERIES_STM32L0) || defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32H7)
  316. GPIO_InitStruct.Alternate = rx_af_num;
  317. #endif
  318. HAL_GPIO_Init(rx_port, &GPIO_InitStruct);
  319. return RT_EOK;
  320. }
  321. void HAL_UART_MspInit(UART_HandleTypeDef *huart)
  322. {
  323. // if this uart is shell function
  324. if(huart == &handle)
  325. {
  326. stm32_gpio_configure(_uart_config);
  327. }
  328. }
  329. static rt_err_t stm32_configure(struct stm32_uart_config *config)
  330. {
  331. stm32_uart_clk_enable(config);
  332. handle.Instance = config->Instance;
  333. handle.Init.BaudRate = 115200;
  334. handle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  335. handle.Init.Mode = UART_MODE_TX_RX;
  336. handle.Init.OverSampling = UART_OVERSAMPLING_16;
  337. handle.Init.WordLength = UART_WORDLENGTH_8B;
  338. handle.Init.StopBits = UART_STOPBITS_1;
  339. handle.Init.Parity = UART_PARITY_NONE;
  340. if (HAL_UART_Init(&handle) != HAL_OK)
  341. {
  342. return -RT_ERROR;
  343. }
  344. return RT_EOK;
  345. }
  346. int rt_hw_usart_init(void)
  347. {
  348. _uart_config = &uart_config[0];
  349. stm32_configure(_uart_config);
  350. return RT_EOK;
  351. }
  352. INIT_BOARD_EXPORT(rt_hw_usart_init);
  353. void rt_hw_console_output(const char *str)
  354. {
  355. rt_size_t i = 0, size = 0;
  356. char a = '\r';
  357. size = rt_strlen(str);
  358. for (i = 0; i < size; i++)
  359. {
  360. if (*(str + i) == '\n')
  361. {
  362. HAL_UART_Transmit(&handle, (uint8_t *)&a, 1, 1);
  363. }
  364. HAL_UART_Transmit(&handle, (uint8_t *)(str + i), 1, 1);
  365. }
  366. }
  367. #ifdef RT_USING_FINSH
  368. char rt_hw_console_getchar(void)
  369. {
  370. int ch = -1;
  371. if (__HAL_UART_GET_FLAG(&handle, UART_FLAG_RXNE) != RESET)
  372. {
  373. #if defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32F0) \
  374. || defined(SOC_SERIES_STM32L0) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32H7) \
  375. || defined(SOC_SERIES_STM32G4)
  376. ch = handle.Instance->RDR & 0xff;
  377. #else
  378. ch = handle.Instance->DR & 0xff;
  379. #endif
  380. }
  381. else
  382. {
  383. if(__HAL_UART_GET_FLAG(&handle, UART_FLAG_ORE) != RESET)
  384. {
  385. __HAL_UART_CLEAR_OREFLAG(&handle);
  386. }
  387. rt_thread_mdelay(10);
  388. }
  389. return ch;
  390. }
  391. #endif /* RT_USING_FINSH */
  392. #endif /* RT_USING_CONSLONE */