1
0

ls1b_uart.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-02-02 michael5hzg@gmail.com adapt to ls1b
  9. */
  10. // 串口相关源码
  11. #include <stdio.h>
  12. #include <stdarg.h>
  13. #include "ls1b_public.h"
  14. #include "ls1b_regs.h"
  15. #include "ls1b_pin.h"
  16. #include "ls1b_uart.h"
  17. #include "ls1b_clock.h"
  18. #include "ls1b.h"
  19. // 串口线路状态寄存器的位域
  20. #define LS1B_UART_LSR_TE (1 << 6)
  21. #define LS1B_UART_LSR_TFE (1 << 5)
  22. // 打印缓存的大小
  23. #define LS1B_UART_PRINT_BUF_SIZE (256)
  24. // 调试串口信息
  25. ls1b_uart_info_t debug_uart_info = {0};
  26. /*
  27. * 获取指定串口模块的基地址
  28. * @UARTx 串口编号
  29. * @ret 基地址
  30. */
  31. void *uart_get_base(ls1b_uart_t UARTx)
  32. {
  33. void *base = NULL;
  34. switch (UARTx)
  35. {
  36. case LS1B_UART00:
  37. base = (void *)LS1B_UART00_BASE;
  38. break;
  39. case LS1B_UART01:
  40. base = (void *)LS1B_UART01_BASE;
  41. break;
  42. case LS1B_UART1:
  43. base = (void *)LS1B_UART1_BASE;
  44. break;
  45. case LS1B_UART2:
  46. base = (void *)LS1B_UART2_BASE;
  47. break;
  48. case LS1B_UART3:
  49. base = (void *)LS1B_UART3_BASE;
  50. break;
  51. case LS1B_UART4:
  52. base = (void *)LS1B_UART4_BASE;
  53. break;
  54. case LS1B_UART5:
  55. base = (void *)LS1B_UART5_BASE;
  56. break;
  57. case LS1B_UART6:
  58. base = (void *)LS1B_UART6_BASE;
  59. break;
  60. case LS1B_UART7:
  61. base = (void *)LS1B_UART7_BASE;
  62. break;
  63. case LS1B_UART8:
  64. base = (void *)LS1B_UART8_BASE;
  65. break;
  66. case LS1B_UART9:
  67. base = (void *)LS1B_UART9_BASE;
  68. break;
  69. case LS1B_UART10:
  70. base = (void *)LS1B_UART10_BASE;
  71. break;
  72. case LS1B_UART11:
  73. base = (void *)LS1B_UART11_BASE;
  74. break;
  75. default:
  76. break;
  77. }
  78. return base;
  79. }
  80. /*
  81. * 初始化指定的串口模块
  82. * @uart_info_p 串口模块信息
  83. */
  84. void uart_init(ls1b_uart_info_t *uart_info_p)
  85. {
  86. void *uart_base = uart_get_base(uart_info_p->UARTx);
  87. unsigned long baudrate_div = 0;
  88. // 禁止所有中断
  89. reg_write_8(0, uart_base + LS1B_UART_IER_OFFSET);
  90. // 接收FIFO的中断申请Trigger为14字节,清空发送和接收FIFO,并复位
  91. reg_write_8(0xc3, uart_base + LS1B_UART_FCR_OFFSET);
  92. // 设置波特率
  93. reg_write_8(0x80, uart_base + LS1B_UART_LCR_OFFSET);
  94. baudrate_div = clk_get_apb_rate() / 16 / uart_info_p->baudrate;
  95. reg_write_8((baudrate_div >> 8) & 0xff, uart_base + LS1B_UART_MSB_OFFSET);
  96. reg_write_8(baudrate_div & 0xff, uart_base + LS1B_UART_LSB_OFFSET);
  97. // 8个数据位,1个停止位,无校验
  98. reg_write_8(0x03, uart_base + LS1B_UART_LCR_OFFSET);
  99. // 使能接收中断
  100. if (TRUE == uart_info_p->rx_enable)
  101. {
  102. reg_write_8(IER_IRxE|IER_ILE , uart_base + LS1B_UART_IER_OFFSET);
  103. }
  104. return ;
  105. }
  106. /*
  107. * 判断FIFO是否为空
  108. * @uartx 串口号
  109. * @ret TRUE or FALSE
  110. */
  111. BOOL uart_is_transmit_empty(ls1b_uart_t uartx)
  112. {
  113. void *uart_base = uart_get_base(uartx);
  114. unsigned char status = reg_read_8(uart_base + LS1B_UART_LSR_OFFSET);
  115. if (status & (LS1B_UART_LSR_TE | LS1B_UART_LSR_TFE))
  116. {
  117. return TRUE;
  118. }
  119. else
  120. {
  121. return FALSE;
  122. }
  123. }
  124. /*
  125. * 发送一个字节
  126. * @uartx 串口号
  127. * @ch 待发送的字符串
  128. */
  129. void uart_putc(ls1b_uart_t uartx, unsigned char ch)
  130. {
  131. void *uart_base = uart_get_base(uartx);
  132. // 等待
  133. while (FALSE == uart_is_transmit_empty(uartx))
  134. ;
  135. // 发送
  136. reg_write_8(ch, uart_base + LS1B_UART_DAT_OFFSET);
  137. return ;
  138. }
  139. /*
  140. * 打印一个字符串到指定串口
  141. * @uartx 串口号
  142. * @str 待打印的字符串
  143. */
  144. void uart_print(ls1b_uart_t uartx, const char *str)
  145. {
  146. while ('\0' != *str) // 判断是否为字符串结束符
  147. {
  148. uart_putc(uartx, *str); // 发送一个字符
  149. str++;
  150. }
  151. return ;
  152. }
  153. /*
  154. * 初始化串口2
  155. */
  156. void uart2_init(void)
  157. {
  158. unsigned int tx_gpio = 37;
  159. unsigned int rx_gpio = 36;
  160. // 设置复用
  161. pin_set_remap(tx_gpio, PIN_REMAP_SECOND);
  162. pin_set_remap(rx_gpio, PIN_REMAP_SECOND);
  163. // 初始化相关寄存器
  164. debug_uart_info.UARTx = LS1B_UART2;
  165. debug_uart_info.baudrate = 115200;
  166. debug_uart_info.rx_enable = FALSE; // 调试串口只需要打印(发送)功能,不需要接收功能
  167. uart_init(&debug_uart_info);
  168. return ;
  169. }
  170. /*
  171. * 在串口2上打印字符串
  172. * @str 待打印的字符串
  173. */
  174. void uart2_print(const char *str)
  175. {
  176. uart_print(LS1B_UART2, str);
  177. return ;
  178. }
  179. /*
  180. * 在调试串口打印字符串
  181. * @str 待打印的字符串
  182. */
  183. void uart_debug_print(const char *str)
  184. {
  185. uart_print(debug_uart_info.UARTx, str);
  186. return ;
  187. }
  188. /*
  189. * 在调试串口打印一个字符
  190. * @ch 待打印的字符
  191. */
  192. void uart_debug_putc(unsigned char ch)
  193. {
  194. uart_putc(debug_uart_info.UARTx, ch);
  195. return ;
  196. }