drv_uart.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * Copyright (c) 2021 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-09-19 HPMICRO First version
  9. *
  10. */
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include <rtdbg.h>
  14. #include "board.h"
  15. #include "drv_uart.h"
  16. #include "hpm_uart_drv.h"
  17. #include "hpm_sysctl_drv.h"
  18. #ifdef RT_USING_SERIAL
  19. #define UART_ROOT_CLK_FREQ BOARD_APP_UART_SRC_FREQ
  20. struct hpm_uart {
  21. UART_Type *uart_base;
  22. uint32_t irq_num;
  23. struct rt_serial_device *serial;
  24. char *device_name;
  25. };
  26. extern void init_uart_pins(UART_Type *ptr);
  27. static void hpm_uart_isr(struct rt_serial_device *serial);
  28. static rt_err_t hpm_uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg);
  29. static rt_err_t hpm_uart_control(struct rt_serial_device *serial, int cmd, void *arg);
  30. static int hpm_uart_putc(struct rt_serial_device *serial, char ch);
  31. static int hpm_uart_getc(struct rt_serial_device *serial);
  32. #if defined(BSP_USING_UART0)
  33. struct rt_serial_device serial0;
  34. void uart0_isr(void)
  35. {
  36. hpm_uart_isr(&serial0);
  37. }
  38. SDK_DECLARE_EXT_ISR_M(IRQn_UART0,uart0_isr)
  39. #endif
  40. #if defined(BSP_USING_UART1)
  41. struct rt_serial_device serial1;
  42. void uart1_isr(void)
  43. {
  44. hpm_uart_isr(&serial1);
  45. }
  46. SDK_DECLARE_EXT_ISR_M(IRQn_UART1,uart1_isr)
  47. #endif
  48. #if defined(BSP_USING_UART2)
  49. struct rt_serial_device serial2;
  50. void uart2_isr(void)
  51. {
  52. hpm_uart_isr(&serial2);
  53. }
  54. SDK_DECLARE_EXT_ISR_M(IRQn_UART2,uart2_isr)
  55. #endif
  56. #if defined(BSP_USING_UART3)
  57. struct rt_serial_device serial3;
  58. void uart3_isr(void)
  59. {
  60. hpm_uart_isr(&serial3);
  61. }
  62. SDK_DECLARE_EXT_ISR_M(IRQn_UART3,uart3_isr)
  63. #endif
  64. #if defined(BSP_USING_UART4)
  65. struct rt_serial_device serial4;
  66. void uart4_isr(void)
  67. {
  68. hpm_uart_isr(&serial4);
  69. }
  70. SDK_DECLARE_EXT_ISR_M(IRQn_UART4,uart4_isr)
  71. #endif
  72. #if defined(BSP_USING_UART5)
  73. struct rt_serial_device serial5;
  74. void uart5_isr(void)
  75. {
  76. hpm_uart_isr(&serial5);
  77. }
  78. SDK_DECLARE_EXT_ISR_M(IRQn_UART5,uart5_isr)
  79. #endif
  80. #if defined(BSP_USING_UART6)
  81. struct rt_serial_device serial6;
  82. void uart6_isr(void)
  83. {
  84. hpm_uart_isr(&serial6);
  85. }
  86. SDK_DECLARE_EXT_ISR_M(IRQn_UART6,uart6_isr)
  87. #endif
  88. #if defined(BSP_USING_UART7)
  89. struct rt_serial_device serial7;
  90. void uart7_isr(void)
  91. {
  92. hpm_uart_isr(&serial7);
  93. }
  94. SDK_DECLARE_EXT_ISR_M(IRQn_UART7,uart7_isr)
  95. #endif
  96. #if defined(BSP_USING_UART8)
  97. struct rt_serial_device serial8;
  98. void uart8_isr(void)
  99. {
  100. hpm_uart_isr(&serial8);
  101. }
  102. SDK_DECLARE_EXT_ISR_M(IRQn_UART8,uart8_isr)
  103. #endif
  104. #if defined(BSP_USING_UART9)
  105. struct rt_serial_device serial9;
  106. void uart9_isr(void)
  107. {
  108. hpm_uart_isr(&serial9);
  109. }
  110. SDK_DECLARE_EXT_ISR_M(IRQn_UART9,uart9_isr)
  111. #endif
  112. #if defined(BSP_USING_UART10)
  113. struct rt_serial_device serial10;
  114. void uart10_isr(void)
  115. {
  116. hpm_uart_isr(&serial10);
  117. }
  118. SDK_DECLARE_EXT_ISR_M(IRQn_UART10,uart10_isr)
  119. #endif
  120. #if defined(BSP_USING_UART11)
  121. struct rt_serial_device serial11;
  122. void uart11_isr(void)
  123. {
  124. hpm_uart_isr(&serial11);
  125. }
  126. SDK_DECLARE_EXT_ISR_M(IRQn_UART11,uart11_isr)
  127. #endif
  128. #if defined(BSP_USING_UART12)
  129. struct rt_serial_device serial12;
  130. void uart12_isr(void)
  131. {
  132. hpm_uart_isr(&serial12);
  133. }
  134. SDK_DECLARE_EXT_ISR_M(IRQn_UART12,uart12_isr)
  135. #endif
  136. #if defined(BSP_USING_UART13)
  137. struct rt_serial_device serial13;
  138. void uart13_isr(void)
  139. {
  140. hpm_uart_isr(&serial13);
  141. }
  142. SDK_DECLARE_EXT_ISR_M(IRQn_UART13,uart13_isr)
  143. #endif
  144. #if defined(BSP_USING_UART14)
  145. struct rt_serial_device serial14;
  146. void uart14_isr(void)
  147. {
  148. hpm_uart_isr(&serial14);
  149. }
  150. SDK_DECLARE_EXT_ISR_M(IRQn_UART14,uart14_isr)
  151. #endif
  152. #if defined(BSP_USING_UART15)
  153. struct rt_serial_device serial15;
  154. void uart15_isr(void)
  155. {
  156. hpm_uart_isr(&serial15);
  157. }
  158. SDK_DECLARE_EXT_ISR_M(IRQn_UART15,uart15_isr)
  159. #endif
  160. static const struct hpm_uart uarts[] = {
  161. #if defined(BSP_USING_UART0)
  162. {
  163. HPM_UART0,
  164. IRQn_UART0,
  165. &serial0,
  166. "uart0",
  167. },
  168. #endif
  169. #if defined(BSP_USING_UART1)
  170. {
  171. HPM_UART1,
  172. IRQn_UART1,
  173. &serial1,
  174. "uart1",
  175. },
  176. #endif
  177. #if defined(BSP_USING_UART2)
  178. {
  179. HPM_UART2,
  180. IRQn_UART2,
  181. &serial2,
  182. "uart2",
  183. },
  184. #endif
  185. #if defined(BSP_USING_UART3)
  186. {
  187. HPM_UART3,
  188. IRQn_UART3,
  189. &serial3,
  190. "uart3",
  191. },
  192. #endif
  193. #if defined(BSP_USING_UART4)
  194. {
  195. HPM_UART4,
  196. IRQn_UART4,
  197. &serial4,
  198. "uart4",
  199. },
  200. #endif
  201. #if defined(BSP_USING_UART5)
  202. {
  203. HPM_UART5,
  204. IRQn_UART5,
  205. &serial5,
  206. "uart5",
  207. },
  208. #endif
  209. #if defined(BSP_USING_UART6)
  210. {
  211. HPM_UART6,
  212. IRQn_UART6,
  213. &serial6,
  214. "uart6",
  215. },
  216. #endif
  217. #if defined(BSP_USING_UART7)
  218. {
  219. HPM_UART7,
  220. IRQn_UART7,
  221. &serial7,
  222. "uart7",
  223. },
  224. #endif
  225. #if defined(BSP_USING_UART8)
  226. {
  227. HPM_UART8,
  228. IRQn_UART8,
  229. &serial8,
  230. "uart8",
  231. },
  232. #endif
  233. #if defined(BSP_USING_UART9)
  234. {
  235. HPM_UART9,
  236. IRQn_UART9,
  237. &serial9,
  238. "uart9",
  239. },
  240. #endif
  241. #if defined(BSP_USING_UART10)
  242. {
  243. HPM_UART10,
  244. IRQn_UART10,
  245. &serial10,
  246. "uart10",
  247. },
  248. #endif
  249. #if defined(BSP_USING_UART11)
  250. {
  251. HPM_UART11,
  252. IRQn_UART11,
  253. &serial11,
  254. "uart11",
  255. },
  256. #endif
  257. #if defined(BSP_USING_UART12)
  258. {
  259. HPM_UART12,
  260. IRQn_UART12,
  261. &serial12,
  262. "uart12",
  263. },
  264. #endif
  265. #if defined(BSP_USING_UART13)
  266. {
  267. HPM_UART13,
  268. IRQn_UART13,
  269. &serial13,
  270. "uart13",
  271. },
  272. #endif
  273. #if defined(BSP_USING_UART14)
  274. {
  275. HPM_UART14,
  276. IRQn_UART14,
  277. &serial14,
  278. "uart14",
  279. },
  280. #endif
  281. #if defined(BSP_USING_UART15)
  282. {
  283. HPM_UART15,
  284. IRQn_UART15,
  285. &serial15,
  286. "uart15",
  287. },
  288. #endif
  289. };
  290. /**
  291. * @brief UART common interrupt process. This
  292. *
  293. * @param serial Serial device
  294. */
  295. static void hpm_uart_isr(struct rt_serial_device *serial)
  296. {
  297. struct hpm_uart *uart;
  298. RT_ASSERT(serial != RT_NULL);
  299. uart = (struct hpm_uart *)serial->parent.user_data;
  300. RT_ASSERT(uart != RT_NULL);
  301. /* UART in mode Receiver */
  302. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  303. }
  304. static rt_err_t hpm_uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  305. {
  306. RT_ASSERT(serial != RT_NULL);
  307. RT_ASSERT(cfg != RT_NULL);
  308. uart_config_t uart_config;
  309. struct hpm_uart *uart = (struct hpm_uart *)serial->parent.user_data;
  310. init_uart_pins(uart->uart_base);
  311. uart_default_config(uart->uart_base, &uart_config);
  312. uart_config.src_freq_in_hz = board_init_uart_clock(uart->uart_base);
  313. uart_config.baudrate = cfg->baud_rate;
  314. uart_config.num_of_stop_bits = cfg->stop_bits;
  315. uart_config.parity = cfg->parity;
  316. uart_config.word_length = cfg->data_bits - DATA_BITS_5;
  317. board_init_uart(uart->uart_base);
  318. uart_init(uart->uart_base, &uart_config);
  319. hpm_uart_control(serial, RT_DEVICE_CTRL_SET_INT, NULL);
  320. }
  321. static rt_err_t hpm_uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  322. {
  323. RT_ASSERT(serial != RT_NULL);
  324. struct hpm_uart *uart = (struct hpm_uart *)serial->parent.user_data;
  325. switch (cmd) {
  326. case RT_DEVICE_CTRL_CLR_INT:
  327. /* disable rx irq */
  328. uart_disable_irq(uart->uart_base, uart_intr_rx_data_avail_or_timeout);
  329. intc_m_disable_irq(uart->irq_num);
  330. break;
  331. case RT_DEVICE_CTRL_SET_INT:
  332. /* enable rx irq */
  333. uart_enable_irq(uart->uart_base, uart_intr_rx_data_avail_or_timeout);
  334. intc_m_enable_irq_with_priority(uart->irq_num, 1);
  335. break;
  336. }
  337. return RT_EOK;
  338. }
  339. static int hpm_uart_putc(struct rt_serial_device *serial, char ch)
  340. {
  341. struct hpm_uart *uart = (struct hpm_uart *)serial->parent.user_data;
  342. uart_write_byte(uart->uart_base, ch);
  343. while(!uart_check_status(uart->uart_base, uart_stat_transmitter_empty)) {
  344. }
  345. }
  346. static int hpm_uart_getc(struct rt_serial_device *serial)
  347. {
  348. int result = -1;
  349. struct hpm_uart *uart = (struct hpm_uart *)serial->parent.user_data;
  350. if (uart_check_status(uart->uart_base, uart_stat_data_ready)) {
  351. uart_receive_byte(uart->uart_base, (uint8_t*)&result);
  352. }
  353. return result;
  354. }
  355. static const struct rt_uart_ops hpm_uart_ops = {
  356. hpm_uart_configure,
  357. hpm_uart_control,
  358. hpm_uart_putc,
  359. hpm_uart_getc,
  360. };
  361. int rt_hw_uart_init(void)
  362. {
  363. /* Added bypass logic here since the rt_hw_uart_init function will be initialized twice, the 2nd initialization should be bypassed */
  364. static bool initialized;
  365. rt_err_t err = RT_EOK;
  366. if (initialized)
  367. {
  368. return err;
  369. }
  370. else
  371. {
  372. initialized = true;
  373. }
  374. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  375. for (uint32_t i = 0; i < sizeof(uarts) / sizeof(uarts[0]); i++) {
  376. uarts[i].serial->ops = &hpm_uart_ops;
  377. uarts[i].serial->config = config;
  378. /* register UART deivce */
  379. err = rt_hw_serial_register(uarts[i].serial,
  380. uarts[i].device_name,
  381. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  382. (void*)&uarts[i]);
  383. }
  384. return err;
  385. }
  386. INIT_BOARD_EXPORT(rt_hw_uart_init);
  387. #endif /* RT_USING_SERIAL */