ls1c_uart.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // 串口相关源码
  2. #include <stdio.h>
  3. #include <stdarg.h>
  4. #include "ls1c_public.h"
  5. #include "ls1c_regs.h"
  6. #include "ls1c_pin.h"
  7. #include "ls1c_uart.h"
  8. #include "ls1c_clock.h"
  9. #include "ls1c.h"
  10. // 串口线路状态寄存器的位域
  11. #define LS1C_UART_LSR_TE (1 << 6)
  12. #define LS1C_UART_LSR_TFE (1 << 5)
  13. // 打印缓存的大小
  14. #define LS1C_UART_PRINT_BUF_SIZE (256)
  15. // 调试串口信息
  16. ls1c_uart_info_t debug_uart_info = {0};
  17. /*
  18. * 获取指定串口模块的基地址
  19. * @UARTx 串口编号
  20. * @ret 基地址
  21. */
  22. void *uart_get_base(ls1c_uart_t UARTx)
  23. {
  24. void *base = NULL;
  25. switch (UARTx)
  26. {
  27. case LS1C_UART00:
  28. base = (void *)LS1C_UART00_BASE;
  29. break;
  30. case LS1C_UART01:
  31. base = (void *)LS1C_UART01_BASE;
  32. break;
  33. case LS1C_UART1:
  34. base = (void *)LS1C_UART1_BASE;
  35. break;
  36. case LS1C_UART2:
  37. base = (void *)LS1C_UART2_BASE;
  38. break;
  39. case LS1C_UART3:
  40. base = (void *)LS1C_UART3_BASE;
  41. break;
  42. case LS1C_UART4:
  43. base = (void *)LS1C_UART4_BASE;
  44. break;
  45. case LS1C_UART5:
  46. base = (void *)LS1C_UART5_BASE;
  47. break;
  48. case LS1C_UART6:
  49. base = (void *)LS1C_UART6_BASE;
  50. break;
  51. case LS1C_UART7:
  52. base = (void *)LS1C_UART7_BASE;
  53. break;
  54. case LS1C_UART8:
  55. base = (void *)LS1C_UART8_BASE;
  56. break;
  57. case LS1C_UART9:
  58. base = (void *)LS1C_UART9_BASE;
  59. break;
  60. case LS1C_UART10:
  61. base = (void *)LS1C_UART10_BASE;
  62. break;
  63. case LS1C_UART11:
  64. base = (void *)LS1C_UART11_BASE;
  65. break;
  66. default:
  67. break;
  68. }
  69. return base;
  70. }
  71. /*
  72. * 初始化指定的串口模块
  73. * @uart_info_p 串口模块信息
  74. */
  75. void uart_init(ls1c_uart_info_t *uart_info_p)
  76. {
  77. void *uart_base = uart_get_base(uart_info_p->UARTx);
  78. unsigned long baudrate_div = 0;
  79. // 禁止所有中断
  80. reg_write_8(0, uart_base + LS1C_UART_IER_OFFSET);
  81. // 接收FIFO的中断申请Trigger为14字节,清空发送和接收FIFO,并复位
  82. reg_write_8(0xc3, uart_base + LS1C_UART_FCR_OFFSET);
  83. // 设置波特率
  84. reg_write_8(0x80, uart_base + LS1C_UART_LCR_OFFSET);
  85. baudrate_div = clk_get_cpu_rate() / 16 / uart_info_p->baudrate / 2;
  86. reg_write_8((baudrate_div >> 8) & 0xff, uart_base + LS1C_UART_MSB_OFFSET);
  87. reg_write_8(baudrate_div & 0xff, uart_base + LS1C_UART_LSB_OFFSET);
  88. // 8个数据位,1个停止位,无校验
  89. reg_write_8(0x03, uart_base + LS1C_UART_LCR_OFFSET);
  90. // 使能接收中断
  91. if (TRUE == uart_info_p->rx_enable)
  92. {
  93. reg_write_8(IER_IRxE|IER_ILE , uart_base + LS1C_UART_IER_OFFSET);
  94. }
  95. return ;
  96. }
  97. /*
  98. * 判断FIFO是否为空
  99. * @uartx 串口号
  100. * @ret TRUE or FALSE
  101. */
  102. BOOL uart_is_transmit_empty(ls1c_uart_t uartx)
  103. {
  104. void *uart_base = uart_get_base(uartx);
  105. unsigned char status = reg_read_8(uart_base + LS1C_UART_LSR_OFFSET);
  106. if (status & (LS1C_UART_LSR_TE | LS1C_UART_LSR_TFE))
  107. {
  108. return TRUE;
  109. }
  110. else
  111. {
  112. return FALSE;
  113. }
  114. }
  115. /*
  116. * 发送一个字节
  117. * @uartx 串口号
  118. * @ch 待发送的字符串
  119. */
  120. void uart_putc(ls1c_uart_t uartx, unsigned char ch)
  121. {
  122. void *uart_base = uart_get_base(uartx);
  123. // 等待
  124. while (FALSE == uart_is_transmit_empty(uartx))
  125. ;
  126. // 发送
  127. reg_write_8(ch, uart_base + LS1C_UART_DAT_OFFSET);
  128. return ;
  129. }
  130. /*
  131. * 打印一个字符串到指定串口
  132. * @uartx 串口号
  133. * @str 待打印的字符串
  134. */
  135. void uart_print(ls1c_uart_t uartx, const char *str)
  136. {
  137. while ('\0' != *str) // 判断是否为字符串结束符
  138. {
  139. uart_putc(uartx, *str); // 发送一个字符
  140. str++;
  141. }
  142. return ;
  143. }
  144. /*
  145. * 初始化串口2
  146. */
  147. void uart2_init(void)
  148. {
  149. unsigned int tx_gpio = 37;
  150. unsigned int rx_gpio = 36;
  151. // 设置复用
  152. pin_set_remap(tx_gpio, PIN_REMAP_SECOND);
  153. pin_set_remap(rx_gpio, PIN_REMAP_SECOND);
  154. // 初始化相关寄存器
  155. debug_uart_info.UARTx = LS1C_UART2;
  156. debug_uart_info.baudrate = 115200;
  157. debug_uart_info.rx_enable = FALSE; // 调试串口只需要打印(发送)功能,不需要接收功能
  158. uart_init(&debug_uart_info);
  159. return ;
  160. }
  161. /*
  162. * 在串口2上打印字符串
  163. * @str 待打印的字符串
  164. */
  165. void uart2_print(const char *str)
  166. {
  167. uart_print(LS1C_UART2, str);
  168. return ;
  169. }
  170. /*
  171. * 在调试串口打印字符串
  172. * @str 待打印的字符串
  173. */
  174. void uart_debug_print(const char *str)
  175. {
  176. uart_print(debug_uart_info.UARTx, str);
  177. return ;
  178. }
  179. /*
  180. * 在调试串口打印一个字符
  181. * @ch 待打印的字符
  182. */
  183. void uart_debug_putc(unsigned char ch)
  184. {
  185. uart_putc(debug_uart_info.UARTx, ch);
  186. return ;
  187. }
  188. /*
  189. * 把中断号转换为串口号
  190. * @IRQn 中断号
  191. * @ret 串口号
  192. */
  193. ls1c_uart_t uart_irqn_to_uartx(int IRQn)
  194. {
  195. ls1c_uart_t uartx = LS1C_UART2;
  196. switch (IRQn)
  197. {
  198. /* 串口UART00和UART01的中断号还待确定
  199. case LS1C_UART00_IRQ:
  200. uartx = LS1C_UART00;
  201. break;
  202. case LS1C_UART01_IRQ:
  203. uartx = LS1C_UART01;
  204. break;
  205. */
  206. case LS1C_UART1_IRQ:
  207. uartx = LS1C_UART1;
  208. break;
  209. case LS1C_UART2_IRQ:
  210. uartx = LS1C_UART2;
  211. break;
  212. case LS1C_UART3_IRQ:
  213. uartx = LS1C_UART3;
  214. break;
  215. case LS1C_UART4_IRQ:
  216. uartx = LS1C_UART4;
  217. break;
  218. case LS1C_UART5_IRQ:
  219. uartx = LS1C_UART5;
  220. break;
  221. case LS1C_UART6_IRQ:
  222. uartx = LS1C_UART6;
  223. break;
  224. case LS1C_UART7_IRQ:
  225. uartx = LS1C_UART7;
  226. break;
  227. case LS1C_UART8_IRQ:
  228. uartx = LS1C_UART8;
  229. break;
  230. case LS1C_UART9_IRQ:
  231. uartx = LS1C_UART9;
  232. break;
  233. case LS1C_UART10_IRQ:
  234. uartx = LS1C_UART10;
  235. break;
  236. case LS1C_UART11_IRQ:
  237. uartx = LS1C_UART11;
  238. break;
  239. default:
  240. uartx = LS1C_UART2;
  241. break;
  242. }
  243. return uartx;
  244. }