drv_uart.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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. #include "hpm_clock_drv.h"
  19. #ifdef RT_USING_SERIAL
  20. #define UART_ROOT_CLK_FREQ BOARD_APP_UART_SRC_FREQ
  21. struct hpm_uart {
  22. UART_Type *uart_base;
  23. rt_uint32_t irq_num;
  24. rt_uint8_t irq_priority;
  25. clock_name_t clk_name;
  26. struct rt_serial_device *serial;
  27. char *device_name;
  28. };
  29. extern void init_uart_pins(UART_Type *ptr);
  30. static void hpm_uart_isr(struct rt_serial_device *serial);
  31. static rt_err_t hpm_uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg);
  32. static rt_err_t hpm_uart_control(struct rt_serial_device *serial, int cmd, void *arg);
  33. static int hpm_uart_putc(struct rt_serial_device *serial, char ch);
  34. static int hpm_uart_getc(struct rt_serial_device *serial);
  35. #if defined(BSP_USING_UART0)
  36. struct rt_serial_device serial0;
  37. SDK_DECLARE_EXT_ISR_M(IRQn_UART0,uart0_isr)
  38. void uart0_isr(void)
  39. {
  40. hpm_uart_isr(&serial0);
  41. }
  42. #endif
  43. #if defined(BSP_USING_UART1)
  44. struct rt_serial_device serial1;
  45. SDK_DECLARE_EXT_ISR_M(IRQn_UART1,uart1_isr)
  46. void uart1_isr(void)
  47. {
  48. hpm_uart_isr(&serial1);
  49. }
  50. #endif
  51. #if defined(BSP_USING_UART2)
  52. struct rt_serial_device serial2;
  53. SDK_DECLARE_EXT_ISR_M(IRQn_UART2,uart2_isr)
  54. void uart2_isr(void)
  55. {
  56. hpm_uart_isr(&serial2);
  57. }
  58. #endif
  59. #if defined(BSP_USING_UART3)
  60. struct rt_serial_device serial3;
  61. SDK_DECLARE_EXT_ISR_M(IRQn_UART3,uart3_isr)
  62. void uart3_isr(void)
  63. {
  64. hpm_uart_isr(&serial3);
  65. }
  66. #endif
  67. #if defined(BSP_USING_UART4)
  68. struct rt_serial_device serial4;
  69. SDK_DECLARE_EXT_ISR_M(IRQn_UART4,uart4_isr)
  70. void uart4_isr(void)
  71. {
  72. hpm_uart_isr(&serial4);
  73. }
  74. #endif
  75. #if defined(BSP_USING_UART5)
  76. struct rt_serial_device serial5;
  77. SDK_DECLARE_EXT_ISR_M(IRQn_UART5,uart5_isr)
  78. void uart5_isr(void)
  79. {
  80. hpm_uart_isr(&serial5);
  81. }
  82. #endif
  83. #if defined(BSP_USING_UART6)
  84. struct rt_serial_device serial6;
  85. SDK_DECLARE_EXT_ISR_M(IRQn_UART6,uart6_isr)
  86. void uart6_isr(void)
  87. {
  88. hpm_uart_isr(&serial6);
  89. }
  90. #endif
  91. #if defined(BSP_USING_UART7)
  92. struct rt_serial_device serial7;
  93. SDK_DECLARE_EXT_ISR_M(IRQn_UART7,uart7_isr)
  94. void uart7_isr(void)
  95. {
  96. hpm_uart_isr(&serial7);
  97. }
  98. #endif
  99. #if defined(BSP_USING_UART8)
  100. struct rt_serial_device serial8;
  101. SDK_DECLARE_EXT_ISR_M(IRQn_UART8,uart8_isr)
  102. void uart8_isr(void)
  103. {
  104. hpm_uart_isr(&serial8);
  105. }
  106. #endif
  107. #if defined(BSP_USING_UART9)
  108. struct rt_serial_device serial9;
  109. SDK_DECLARE_EXT_ISR_M(IRQn_UART9,uart9_isr)
  110. void uart9_isr(void)
  111. {
  112. hpm_uart_isr(&serial9);
  113. }
  114. #endif
  115. #if defined(BSP_USING_UART10)
  116. struct rt_serial_device serial10;
  117. SDK_DECLARE_EXT_ISR_M(IRQn_UART10,uart10_isr)
  118. void uart10_isr(void)
  119. {
  120. hpm_uart_isr(&serial10);
  121. }
  122. #endif
  123. #if defined(BSP_USING_UART11)
  124. struct rt_serial_device serial11;
  125. SDK_DECLARE_EXT_ISR_M(IRQn_UART11,uart11_isr)
  126. void uart11_isr(void)
  127. {
  128. hpm_uart_isr(&serial11);
  129. }
  130. #endif
  131. #if defined(BSP_USING_UART12)
  132. struct rt_serial_device serial12;
  133. SDK_DECLARE_EXT_ISR_M(IRQn_UART12,uart12_isr)
  134. void uart12_isr(void)
  135. {
  136. hpm_uart_isr(&serial12);
  137. }
  138. #endif
  139. #if defined(BSP_USING_UART13)
  140. struct rt_serial_device serial13;
  141. SDK_DECLARE_EXT_ISR_M(IRQn_UART13,uart13_isr)
  142. void uart13_isr(void)
  143. {
  144. hpm_uart_isr(&serial13);
  145. }
  146. #endif
  147. #if defined(BSP_USING_UART14)
  148. struct rt_serial_device serial14;
  149. SDK_DECLARE_EXT_ISR_M(IRQn_UART14,uart14_isr)
  150. void uart14_isr(void)
  151. {
  152. hpm_uart_isr(&serial14);
  153. }
  154. #endif
  155. #if defined(BSP_USING_UART15)
  156. struct rt_serial_device serial15;
  157. SDK_DECLARE_EXT_ISR_M(IRQn_UART15,uart15_isr)
  158. void uart15_isr(void)
  159. {
  160. hpm_uart_isr(&serial15);
  161. }
  162. #endif
  163. static const struct hpm_uart uarts[] = {
  164. #if defined(BSP_USING_UART0)
  165. {
  166. HPM_UART0,
  167. IRQn_UART0,
  168. #if defined(BSP_UART0_IRQ_PRIORITY)
  169. .irq_priority = BSP_UART0_IRQ_PRIORITY,
  170. #else
  171. .irq_priority = 1,
  172. #endif
  173. clock_uart0,
  174. &serial0,
  175. "uart0",
  176. },
  177. #endif
  178. #if defined(BSP_USING_UART1)
  179. {
  180. HPM_UART1,
  181. IRQn_UART1,
  182. #if defined(BSP_UART1_IRQ_PRIORITY)
  183. .irq_priority = BSP_UART1_IRQ_PRIORITY,
  184. #else
  185. .irq_priority = 1,
  186. #endif
  187. clock_uart1,
  188. &serial1,
  189. "uart1",
  190. },
  191. #endif
  192. #if defined(BSP_USING_UART2)
  193. {
  194. HPM_UART2,
  195. IRQn_UART2,
  196. #if defined(BSP_UART2_IRQ_PRIORITY)
  197. .irq_priority = BSP_UART2_IRQ_PRIORITY,
  198. #else
  199. .irq_priority = 1,
  200. #endif
  201. clock_uart2,
  202. &serial2,
  203. "uart2",
  204. },
  205. #endif
  206. #if defined(BSP_USING_UART3)
  207. {
  208. HPM_UART3,
  209. IRQn_UART3,
  210. #if defined(BSP_UART3_IRQ_PRIORITY)
  211. .irq_priority = BSP_UART3_IRQ_PRIORITY,
  212. #else
  213. .irq_priority = 1,
  214. #endif
  215. clock_uart3,
  216. &serial3,
  217. "uart3",
  218. },
  219. #endif
  220. #if defined(BSP_USING_UART4)
  221. {
  222. HPM_UART4,
  223. IRQn_UART4,
  224. #if defined(BSP_UART4_IRQ_PRIORITY)
  225. .irq_priority = BSP_UART4_IRQ_PRIORITY,
  226. #else
  227. .irq_priority = 1,
  228. #endif
  229. clock_uart4,
  230. &serial4,
  231. "uart4",
  232. },
  233. #endif
  234. #if defined(BSP_USING_UART5)
  235. {
  236. HPM_UART5,
  237. IRQn_UART5,
  238. #if defined(BSP_UART5_IRQ_PRIORITY)
  239. .irq_priority = BSP_UART5_IRQ_PRIORITY,
  240. #else
  241. .irq_priority = 1,
  242. #endif
  243. clock_uart5,
  244. &serial5,
  245. "uart5",
  246. },
  247. #endif
  248. #if defined(BSP_USING_UART6)
  249. {
  250. HPM_UART6,
  251. IRQn_UART6,
  252. #if defined(BSP_UART6_IRQ_PRIORITY)
  253. .irq_priority = BSP_UART6_IRQ_PRIORITY,
  254. #else
  255. .irq_priority = 1,
  256. #endif
  257. clock_uart6,
  258. &serial6,
  259. "uart6",
  260. },
  261. #endif
  262. #if defined(BSP_USING_UART7)
  263. {
  264. HPM_UART7,
  265. IRQn_UART7,
  266. #if defined(BSP_UART7_IRQ_PRIORITY)
  267. .irq_priority = BSP_UART7_IRQ_PRIORITY,
  268. #else
  269. .irq_priority = 1,
  270. #endif
  271. clock_uart7,
  272. &serial7,
  273. "uart7",
  274. },
  275. #endif
  276. #if defined(BSP_USING_UART8)
  277. {
  278. HPM_UART8,
  279. IRQn_UART8,
  280. #if defined(BSP_UART8_IRQ_PRIORITY)
  281. .irq_priority = BSP_UART8_IRQ_PRIORITY,
  282. #else
  283. .irq_priority = 1,
  284. #endif
  285. clock_uart8,
  286. &serial8,
  287. "uart8",
  288. },
  289. #endif
  290. #if defined(BSP_USING_UART9)
  291. {
  292. HPM_UART9,
  293. IRQn_UART9,
  294. #if defined(BSP_UART9_IRQ_PRIORITY)
  295. .irq_priority = BSP_UART9_IRQ_PRIORITY,
  296. #else
  297. .irq_priority = 1,
  298. #endif
  299. clock_uart9,
  300. &serial9,
  301. "uart9",
  302. },
  303. #endif
  304. #if defined(BSP_USING_UART10)
  305. {
  306. HPM_UART10,
  307. IRQn_UART10,
  308. #if defined(BSP_UART10_IRQ_PRIORITY)
  309. .irq_priority = BSP_UART10_IRQ_PRIORITY,
  310. #else
  311. .irq_priority = 1,
  312. #endif
  313. clock_uart10,
  314. &serial10,
  315. "uart10",
  316. },
  317. #endif
  318. #if defined(BSP_USING_UART11)
  319. {
  320. HPM_UART11,
  321. IRQn_UART11,
  322. #if defined(BSP_UART11_IRQ_PRIORITY)
  323. .irq_priority = BSP_UART11_IRQ_PRIORITY,
  324. #else
  325. .irq_priority = 1,
  326. #endif
  327. clock_uart11,
  328. &serial11,
  329. "uart11",
  330. },
  331. #endif
  332. #if defined(BSP_USING_UART12)
  333. {
  334. HPM_UART12,
  335. IRQn_UART12,
  336. #if defined(BSP_UART12_IRQ_PRIORITY)
  337. .irq_priority = BSP_UART12_IRQ_PRIORITY,
  338. #else
  339. .irq_priority = 1,
  340. #endif
  341. clock_uart12,
  342. &serial12,
  343. "uart12",
  344. },
  345. #endif
  346. #if defined(BSP_USING_UART13)
  347. {
  348. HPM_UART13,
  349. IRQn_UART13,
  350. #if defined(BSP_UART13_IRQ_PRIORITY)
  351. .irq_priority = BSP_UART13_IRQ_PRIORITY,
  352. #else
  353. .irq_priority = 1,
  354. #endif
  355. clock_uart13,
  356. &serial13,
  357. "uart13",
  358. },
  359. #endif
  360. #if defined(BSP_USING_UART14)
  361. {
  362. HPM_UART14,
  363. IRQn_UART14,
  364. #if defined(BSP_UART14_IRQ_PRIORITY)
  365. .irq_priority = BSP_UART14_IRQ_PRIORITY,
  366. #else
  367. .irq_priority = 1,
  368. #endif
  369. clock_uart14,
  370. &serial14,
  371. "uart14",
  372. },
  373. #endif
  374. #if defined(BSP_USING_UART15)
  375. {
  376. HPM_UART15,
  377. IRQn_UART15,
  378. #if defined(BSP_UART15_IRQ_PRIORITY)
  379. .irq_priority = BSP_UART15_IRQ_PRIORITY,
  380. #else
  381. .irq_priority = 1,
  382. #endif
  383. clock_uart15,
  384. &serial15,
  385. "uart15",
  386. },
  387. #endif
  388. };
  389. /**
  390. * @brief UART common interrupt process. This
  391. *
  392. * @param serial Serial device
  393. */
  394. static void hpm_uart_isr(struct rt_serial_device *serial)
  395. {
  396. struct hpm_uart *uart;
  397. RT_ASSERT(serial != RT_NULL);
  398. uart = (struct hpm_uart *)serial->parent.user_data;
  399. RT_ASSERT(uart != RT_NULL);
  400. /* UART in mode Receiver */
  401. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  402. }
  403. static rt_err_t hpm_uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  404. {
  405. RT_ASSERT(serial != RT_NULL);
  406. RT_ASSERT(cfg != RT_NULL);
  407. uart_config_t uart_config;
  408. struct hpm_uart *uart = (struct hpm_uart *)serial->parent.user_data;
  409. init_uart_pins(uart->uart_base);
  410. uart_default_config(uart->uart_base, &uart_config);
  411. clock_add_to_group(uart->clk_name, BOARD_RUNNING_CORE & 0x1);
  412. uart_config.src_freq_in_hz = clock_get_frequency(uart->clk_name);
  413. uart_config.baudrate = cfg->baud_rate;
  414. uart_config.num_of_stop_bits = cfg->stop_bits;
  415. uart_config.parity = cfg->parity;
  416. uart_config.word_length = cfg->data_bits - DATA_BITS_5;
  417. uart_init(uart->uart_base, &uart_config);
  418. hpm_uart_control(serial, RT_DEVICE_CTRL_SET_INT, NULL);
  419. }
  420. static rt_err_t hpm_uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  421. {
  422. RT_ASSERT(serial != RT_NULL);
  423. struct hpm_uart *uart = (struct hpm_uart *)serial->parent.user_data;
  424. switch (cmd) {
  425. case RT_DEVICE_CTRL_CLR_INT:
  426. /* disable rx irq */
  427. uart_disable_irq(uart->uart_base, uart_intr_rx_data_avail_or_timeout);
  428. intc_m_disable_irq(uart->irq_num);
  429. break;
  430. case RT_DEVICE_CTRL_SET_INT:
  431. /* enable rx irq */
  432. uart_enable_irq(uart->uart_base, uart_intr_rx_data_avail_or_timeout);
  433. intc_m_enable_irq_with_priority(uart->irq_num, uart->irq_priority);
  434. break;
  435. }
  436. return RT_EOK;
  437. }
  438. static int hpm_uart_putc(struct rt_serial_device *serial, char ch)
  439. {
  440. struct hpm_uart *uart = (struct hpm_uart *)serial->parent.user_data;
  441. uart_write_byte(uart->uart_base, ch);
  442. while(!uart_check_status(uart->uart_base, uart_stat_transmitter_empty)) {
  443. }
  444. }
  445. static int hpm_uart_getc(struct rt_serial_device *serial)
  446. {
  447. int result = -1;
  448. struct hpm_uart *uart = (struct hpm_uart *)serial->parent.user_data;
  449. if (uart_check_status(uart->uart_base, uart_stat_data_ready)) {
  450. uart_receive_byte(uart->uart_base, (uint8_t*)&result);
  451. }
  452. return result;
  453. }
  454. static const struct rt_uart_ops hpm_uart_ops = {
  455. hpm_uart_configure,
  456. hpm_uart_control,
  457. hpm_uart_putc,
  458. hpm_uart_getc,
  459. };
  460. int rt_hw_uart_init(void)
  461. {
  462. /* Added bypass logic here since the rt_hw_uart_init function will be initialized twice, the 2nd initialization should be bypassed */
  463. static bool initialized;
  464. rt_err_t err = RT_EOK;
  465. if (initialized)
  466. {
  467. return err;
  468. }
  469. else
  470. {
  471. initialized = true;
  472. }
  473. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  474. for (uint32_t i = 0; i < sizeof(uarts) / sizeof(uarts[0]); i++) {
  475. uarts[i].serial->ops = &hpm_uart_ops;
  476. uarts[i].serial->config = config;
  477. /* register UART deivce */
  478. err = rt_hw_serial_register(uarts[i].serial,
  479. uarts[i].device_name,
  480. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  481. (void*)&uarts[i]);
  482. }
  483. return err;
  484. }
  485. INIT_BOARD_EXPORT(rt_hw_uart_init);
  486. #endif /* RT_USING_SERIAL */