drv_uart.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. */
  9. #include <rthw.h>
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include "board.h"
  13. #include "drv_uart.h"
  14. #include <stdio.h>
  15. #include <sysctl.h>
  16. #include "uart.h"
  17. #include "uarths.h"
  18. #include "plic.h"
  19. #define UART_DEFAULT_BAUDRATE 115200
  20. static volatile uarths_t *const _uarths = (volatile uarths_t *)UARTHS_BASE_ADDR;
  21. struct device_uart
  22. {
  23. rt_uint32_t hw_base;
  24. rt_uint32_t irqno;
  25. };
  26. static rt_err_t rt_uarths_configure(struct rt_serial_device *serial, struct serial_configure *cfg);
  27. static rt_err_t uarths_control(struct rt_serial_device *serial, int cmd, void *arg);
  28. static int drv_uarths_putc(struct rt_serial_device *serial, char c);
  29. static int drv_uarths_getc(struct rt_serial_device *serial);
  30. static void uarths_irq_handler(int irqno, void *param);
  31. static rt_err_t rt_uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg);
  32. static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg);
  33. static int drv_uart_putc(struct rt_serial_device *serial, char c);
  34. static int drv_uart_getc(struct rt_serial_device *serial);
  35. static void uart_irq_handler(int irqno, void *param);
  36. const struct rt_uart_ops _uart_hs_ops =
  37. {
  38. rt_uarths_configure,
  39. uarths_control,
  40. drv_uarths_putc,
  41. drv_uarths_getc,
  42. RT_NULL
  43. };
  44. const struct rt_uart_ops _uart_ops =
  45. {
  46. rt_uart_configure,
  47. uart_control,
  48. drv_uart_putc,
  49. drv_uart_getc,
  50. //TODO: add DMA support
  51. RT_NULL
  52. };
  53. /* START ported from kendryte standalone sdk uart.c */
  54. #define __UART_BRATE_CONST 16
  55. volatile uart_t* const _uart[3] =
  56. {
  57. (volatile uart_t*)UART1_BASE_ADDR,
  58. (volatile uart_t*)UART2_BASE_ADDR,
  59. (volatile uart_t*)UART3_BASE_ADDR
  60. };
  61. void _uart_init(uart_device_number_t channel)
  62. {
  63. sysctl_clock_enable(SYSCTL_CLOCK_UART1 + channel);
  64. sysctl_reset(SYSCTL_RESET_UART1 + channel);
  65. }
  66. /* END ported from kendryte standalone sdk uart.c */
  67. static inline uart_device_number_t _get_uart_channel(rt_uint32_t addr)
  68. {
  69. switch (addr)
  70. {
  71. case UART1_BASE_ADDR:
  72. return UART_DEVICE_1;
  73. case UART2_BASE_ADDR:
  74. return UART_DEVICE_2;
  75. case UART3_BASE_ADDR:
  76. return UART_DEVICE_3;
  77. default:
  78. return UART_DEVICE_MAX;
  79. }
  80. }
  81. /*
  82. * UART Initiation
  83. */
  84. int rt_hw_uart_init(void)
  85. {
  86. struct rt_serial_device *serial;
  87. struct device_uart *uart;
  88. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  89. #ifdef BSP_USING_UART_HS
  90. {
  91. static struct rt_serial_device serial_hs;
  92. static struct device_uart uart_hs;
  93. serial = &serial_hs;
  94. uart = &uart_hs;
  95. serial->ops = &_uart_hs_ops;
  96. serial->config = config;
  97. serial->config.baud_rate = 115200;
  98. uart->hw_base = UARTHS_BASE_ADDR;
  99. uart->irqno = IRQN_UARTHS_INTERRUPT;
  100. rt_hw_serial_register(serial,
  101. "uarths",
  102. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  103. uart);
  104. }
  105. #endif
  106. #ifdef BSP_USING_UART1
  107. {
  108. static struct rt_serial_device serial1;
  109. static struct device_uart uart1;
  110. serial = &serial1;
  111. uart = &uart1;
  112. serial->ops = &_uart_ops;
  113. serial->config = config;
  114. serial->config.baud_rate = UART_DEFAULT_BAUDRATE;
  115. uart->hw_base = UART1_BASE_ADDR;
  116. uart->irqno = IRQN_UART1_INTERRUPT;
  117. _uart_init(UART_DEVICE_1);
  118. rt_hw_serial_register(serial,
  119. "uart1",
  120. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  121. uart);
  122. }
  123. #endif
  124. #ifdef BSP_USING_UART2
  125. {
  126. static struct rt_serial_device serial2;
  127. static struct device_uart uart2;
  128. serial = &serial2;
  129. uart = &uart2;
  130. serial->ops = &_uart_ops;
  131. serial->config = config;
  132. serial->config.baud_rate = UART_DEFAULT_BAUDRATE;
  133. uart->hw_base = UART2_BASE_ADDR;
  134. uart->irqno = IRQN_UART2_INTERRUPT;
  135. _uart_init(UART_DEVICE_2);
  136. rt_hw_serial_register(serial,
  137. "uart2",
  138. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  139. uart);
  140. }
  141. #endif
  142. #ifdef BSP_USING_UART3
  143. {
  144. static struct rt_serial_device serial3;
  145. static struct device_uart uart3;
  146. serial = &serial3;
  147. uart = &uart3;
  148. serial->ops = &_uart_ops;
  149. serial->config = config;
  150. serial->config.baud_rate = UART_DEFAULT_BAUDRATE;
  151. uart->hw_base = UART3_BASE_ADDR;
  152. uart->irqno = IRQN_UART3_INTERRUPT;
  153. _uart_init(UART_DEVICE_3);
  154. rt_hw_serial_register(serial,
  155. "uart3",
  156. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  157. uart);
  158. }
  159. #endif
  160. return 0;
  161. }
  162. /*
  163. * UARTHS interface
  164. */
  165. static rt_err_t rt_uarths_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  166. {
  167. struct device_uart *uart;
  168. uint32_t freq_hs = sysctl_clock_get_freq(SYSCTL_CLOCK_CPU);
  169. uint16_t div_hs = freq_hs / cfg->baud_rate - 1;
  170. RT_ASSERT(serial != RT_NULL);
  171. serial->config = *cfg;
  172. uart = serial->parent.user_data;
  173. RT_ASSERT(uart != RT_NULL);
  174. if (uart->hw_base == UARTHS_BASE_ADDR)
  175. {
  176. _uarths->div.div = div_hs;
  177. _uarths->txctrl.txen = 1;
  178. _uarths->rxctrl.rxen = 1;
  179. _uarths->txctrl.txcnt = 0;
  180. _uarths->rxctrl.rxcnt = 0;
  181. _uarths->ip.txwm = 1;
  182. _uarths->ip.rxwm = 1;
  183. _uarths->ie.txwm = 0;
  184. _uarths->ie.rxwm = 1;
  185. }
  186. else
  187. {
  188. return (-1);
  189. /* other uart */
  190. }
  191. return (RT_EOK);
  192. }
  193. static rt_err_t uarths_control(struct rt_serial_device *serial, int cmd, void *arg)
  194. {
  195. struct device_uart *uart;
  196. uart = serial->parent.user_data;
  197. RT_ASSERT(uart != RT_NULL);
  198. switch (cmd)
  199. {
  200. case RT_DEVICE_CTRL_CLR_INT:
  201. /* Disable the UART Interrupt */
  202. rt_hw_interrupt_mask(uart->irqno);
  203. break;
  204. case RT_DEVICE_CTRL_SET_INT:
  205. /* install interrupt */
  206. rt_hw_interrupt_install(uart->irqno, uarths_irq_handler,
  207. serial, serial->parent.parent.name);
  208. rt_hw_interrupt_umask(uart->irqno);
  209. break;
  210. }
  211. return (RT_EOK);
  212. }
  213. static int drv_uarths_putc(struct rt_serial_device *serial, char c)
  214. {
  215. struct device_uart *uart = serial->parent.user_data;
  216. RT_ASSERT(uart->hw_base == UARTHS_BASE_ADDR);
  217. while (_uarths->txdata.full);
  218. _uarths->txdata.data = (uint8_t)c;
  219. return (1);
  220. }
  221. static int drv_uarths_getc(struct rt_serial_device *serial)
  222. {
  223. struct device_uart *uart = serial->parent.user_data;
  224. RT_ASSERT(uart->hw_base == UARTHS_BASE_ADDR);
  225. uarths_rxdata_t recv = _uarths->rxdata;
  226. if (recv.empty)
  227. return EOF;
  228. else
  229. return (recv.data & 0xff);
  230. /* Receive Data Available */
  231. return (-1);
  232. }
  233. /* UARTHS ISR */
  234. static void uarths_irq_handler(int irqno, void *param)
  235. {
  236. struct rt_serial_device *serial = (struct rt_serial_device *)param;
  237. struct device_uart *uart = serial->parent.user_data;
  238. RT_ASSERT(uart->hw_base == UARTHS_BASE_ADDR);
  239. /* read interrupt status and clear it */
  240. if (_uarths->ip.rxwm)
  241. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  242. }
  243. /*
  244. * UART interface
  245. */
  246. static rt_err_t rt_uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  247. {
  248. struct device_uart *uart;
  249. uart_bitwidth_t data_width = (uart_bitwidth_t)cfg->data_bits ;
  250. uart_stopbit_t stopbit = (uart_stopbit_t)cfg->stop_bits;
  251. uart_parity_t parity = (uart_parity_t)cfg->parity;
  252. uint32_t freq = sysctl_clock_get_freq(SYSCTL_CLOCK_APB0);
  253. uint32_t divisor = freq / (uint32_t)cfg->baud_rate;
  254. uint8_t dlh = divisor >> 12;
  255. uint8_t dll = (divisor - (dlh << 12)) / __UART_BRATE_CONST;
  256. uint8_t dlf = divisor - (dlh << 12) - dll * __UART_BRATE_CONST;
  257. RT_ASSERT(serial != RT_NULL);
  258. serial->config = *cfg;
  259. uart = serial->parent.user_data;
  260. RT_ASSERT(uart != RT_NULL);
  261. uart_device_number_t channel = _get_uart_channel(uart->hw_base);
  262. RT_ASSERT(channel != UART_DEVICE_MAX);
  263. RT_ASSERT(data_width >= 5 && data_width <= 8);
  264. if (data_width == 5)
  265. {
  266. RT_ASSERT(stopbit != UART_STOP_2);
  267. }
  268. else
  269. {
  270. RT_ASSERT(stopbit != UART_STOP_1_5);
  271. }
  272. uint32_t stopbit_val = stopbit == UART_STOP_1 ? 0 : 1;
  273. uint32_t parity_val;
  274. switch (parity)
  275. {
  276. case UART_PARITY_NONE:
  277. parity_val = 0;
  278. break;
  279. case UART_PARITY_ODD:
  280. parity_val = 1;
  281. break;
  282. case UART_PARITY_EVEN:
  283. parity_val = 3;
  284. break;
  285. default:
  286. RT_ASSERT(!"Invalid parity");
  287. break;
  288. }
  289. _uart[channel]->LCR |= 1u << 7;
  290. _uart[channel]->DLH = dlh;
  291. _uart[channel]->DLL = dll;
  292. _uart[channel]->DLF = dlf;
  293. _uart[channel]->LCR = 0;
  294. _uart[channel]->LCR = (data_width - 5) |
  295. (stopbit_val << 2) |
  296. (parity_val << 3);
  297. _uart[channel]->LCR &= ~(1u << 7);
  298. _uart[channel]->IER |= 0x80; /* THRE */
  299. _uart[channel]->FCR = UART_RECEIVE_FIFO_1 << 6 |
  300. UART_SEND_FIFO_8 << 4 |
  301. 0x1 << 3 |
  302. 0x1;
  303. return (RT_EOK);
  304. }
  305. static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  306. {
  307. struct device_uart *uart;
  308. uart = serial->parent.user_data;
  309. uart_device_number_t channel = _get_uart_channel(uart->hw_base);
  310. RT_ASSERT(uart != RT_NULL);
  311. RT_ASSERT(channel != UART_DEVICE_MAX);
  312. switch (cmd)
  313. {
  314. case RT_DEVICE_CTRL_CLR_INT:
  315. /* Disable the UART Interrupt */
  316. rt_hw_interrupt_mask(uart->irqno);
  317. _uart[channel]->IER &= ~0x1;
  318. break;
  319. case RT_DEVICE_CTRL_SET_INT:
  320. /* install interrupt */
  321. rt_hw_interrupt_install(uart->irqno, uart_irq_handler,
  322. serial, serial->parent.parent.name);
  323. rt_hw_interrupt_umask(uart->irqno);
  324. _uart[channel]->IER |= 0x1;
  325. break;
  326. }
  327. return (RT_EOK);
  328. }
  329. static int drv_uart_putc(struct rt_serial_device *serial, char c)
  330. {
  331. struct device_uart *uart = serial->parent.user_data;
  332. uart_device_number_t channel = _get_uart_channel(uart->hw_base);
  333. RT_ASSERT(channel != UART_DEVICE_MAX);
  334. while (_uart[channel]->LSR & (1u << 5));
  335. _uart[channel]->THR = c;
  336. return (1);
  337. }
  338. static int drv_uart_getc(struct rt_serial_device *serial)
  339. {
  340. struct device_uart *uart = serial->parent.user_data;
  341. uart_device_number_t channel = _get_uart_channel(uart->hw_base);
  342. RT_ASSERT(channel != UART_DEVICE_MAX);
  343. if (_uart[channel]->LSR & 1)
  344. return (char)(_uart[channel]->RBR & 0xff);
  345. else
  346. return EOF;
  347. /* Receive Data Available */
  348. return (-1);
  349. }
  350. /* UART ISR */
  351. static void uart_irq_handler(int irqno, void *param)
  352. {
  353. struct rt_serial_device *serial = (struct rt_serial_device *)param;
  354. struct device_uart *uart = serial->parent.user_data;
  355. uart_device_number_t channel = _get_uart_channel(uart->hw_base);
  356. RT_ASSERT(channel != UART_DEVICE_MAX);
  357. /* read interrupt status and clear it */
  358. if (_uart[channel]->LSR)
  359. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  360. }
  361. /* WEAK for SDK 0.5.6 */
  362. RT_WEAK void uart_debug_init(uart_device_number_t uart_channel)
  363. {
  364. }