board_uart.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. * 2017-09-23 Urey first implementation
  9. */
  10. #include <rtthread.h>
  11. #ifdef RT_USING_CONSOLE
  12. #include <rthw.h>
  13. #include <rtdevice.h>
  14. #include <stdint.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <stdbool.h>
  18. #include "board.h"
  19. #include "soc.h"
  20. #include "dw_usart.h"
  21. #include "drv_usart.h"
  22. #include "pin_name.h"
  23. #include "pinmux.h"
  24. #if CONFIG_USART
  25. struct
  26. {
  27. uint32_t base;
  28. uint32_t irq;
  29. } const sg_usart_config[CONFIG_USART_NUM] =
  30. {
  31. { CSKY_UART0_BASE, UART0_IRQn },
  32. { CSKY_UART1_BASE, UART1_IRQn },
  33. { CSKY_UART2_BASE, UART2_IRQn },
  34. { CSKY_UART3_BASE, UART3_IRQn }
  35. };
  36. typedef struct
  37. {
  38. pin_t tx;
  39. pin_t rx;
  40. uint16_t cfg_idx; //idx of sg_usart_config[]
  41. uint16_t function;
  42. } usart_pin_map_t;
  43. const static usart_pin_map_t s_usart_pin_map[] =
  44. {
  45. {
  46. PA0_TXD0_PWM0_XX_SIROUT0,
  47. PA1_RXD0_PWM1_XX_SIRIN0,
  48. 0,
  49. 0
  50. },
  51. {
  52. PA10_TXD1_PWM1_XX_SIROUT1,
  53. PA11_RXD1_PWM2_XX_SIRIN1,
  54. 1,
  55. 0
  56. },
  57. {
  58. PA23_TXD2_PWM5_XX_SIROUT2,
  59. PA22_RXD2_PWM4_XX_SIRIN2,
  60. 2,
  61. 0
  62. },
  63. {
  64. PA26_TXD3_PWMFAULT_XX_SIROUT3,
  65. PA27_RXD3_PWM0_XX_SIRIN3,
  66. 3,
  67. 0
  68. }
  69. };
  70. int32_t target_usart_init(pin_t tx, pin_t rx, uint32_t *base, uint32_t *irq)
  71. {
  72. uint32_t idx;
  73. for (idx = 0; idx < sizeof(s_usart_pin_map) / sizeof(usart_pin_map_t); idx++)
  74. {
  75. if (s_usart_pin_map[idx].tx == tx && s_usart_pin_map[idx].rx == rx)
  76. {
  77. *base = sg_usart_config[s_usart_pin_map[idx].cfg_idx].base;
  78. *irq = sg_usart_config[s_usart_pin_map[idx].cfg_idx].irq;
  79. /*pinmux*/
  80. pin_mux(s_usart_pin_map[idx].tx, s_usart_pin_map[idx].function);
  81. pin_mux(s_usart_pin_map[idx].rx, s_usart_pin_map[idx].function);
  82. return s_usart_pin_map[idx].cfg_idx;
  83. }
  84. }
  85. return -1;
  86. }
  87. #endif
  88. #ifdef RT_USING_UART1
  89. #define UART_TXD1 PA10_TXD1_PWM1_XX_SIROUT1
  90. #define UART_RXD1 PA11_RXD1_PWM2_XX_SIRIN1
  91. static usart_handle_t uart1_handle;
  92. static struct rt_serial_device serial1;
  93. /*
  94. static void usart1_event_cb(uint32_t event, void *cb_arg)
  95. {
  96. switch (event)
  97. {
  98. case USART_EVENT_SEND_COMPLETE:
  99. rt_hw_serial_isr(&serial1,RT_SERIAL_EVENT_TX_DONE);
  100. break;
  101. case USART_EVENT_RECEIVED:
  102. rt_hw_serial_isr(&serial1,RT_SERIAL_EVENT_RX_IND);
  103. break;
  104. default:
  105. break;
  106. }
  107. }
  108. */
  109. __attribute__((isr)) void USART1_IRQHandler(void)
  110. {
  111. rt_hw_serial_isr(&serial1,RT_SERIAL_EVENT_RX_IND);
  112. }
  113. #endif
  114. /*
  115. * UART interface
  116. */
  117. static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  118. {
  119. int ret;
  120. usart_handle_t uart;
  121. uint32_t bauds;
  122. usart_mode_e mode;
  123. usart_parity_e parity;
  124. usart_stop_bits_e stopbits;
  125. usart_data_bits_e databits;
  126. RT_ASSERT(serial != RT_NULL);
  127. uart = (usart_handle_t)serial->parent.user_data;
  128. RT_ASSERT(uart != RT_NULL);
  129. /* set baudrate parity...*/
  130. bauds = cfg->baud_rate;
  131. mode = USART_MODE_ASYNCHRONOUS;
  132. if (cfg->parity == PARITY_EVEN)
  133. parity = USART_PARITY_EVEN;
  134. else if (cfg->parity == PARITY_ODD)
  135. parity = USART_PARITY_ODD;
  136. else
  137. parity = USART_PARITY_NONE;
  138. stopbits = USART_STOP_BITS_1 ;
  139. databits = USART_DATA_BITS_8;
  140. ret = csi_usart_config(uart, SYSTEM_CLOCK, bauds, USART_MODE_ASYNCHRONOUS, parity, stopbits, databits);
  141. if (ret < 0)
  142. {
  143. return -RT_ERROR;
  144. }
  145. return RT_EOK;
  146. }
  147. static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  148. {
  149. usart_handle_t uart;
  150. RT_ASSERT(serial != RT_NULL);
  151. uart = (usart_handle_t)serial->parent.user_data;
  152. RT_ASSERT(uart != RT_NULL);
  153. switch (cmd)
  154. {
  155. case RT_DEVICE_CTRL_CLR_INT:
  156. /* Disable the UART Interrupt */
  157. dw_usart_clr_int_flag(uart,IER_RDA_INT_ENABLE);
  158. break;
  159. case RT_DEVICE_CTRL_SET_INT:
  160. /* Enable the UART Interrupt */
  161. dw_usart_set_int_flag(uart,IER_RDA_INT_ENABLE);
  162. break;
  163. }
  164. return (RT_EOK);
  165. }
  166. static int uart_putc(struct rt_serial_device *serial, char c)
  167. {
  168. usart_handle_t uart;
  169. RT_ASSERT(serial != RT_NULL);
  170. uart = (usart_handle_t)serial->parent.user_data;
  171. RT_ASSERT(uart != RT_NULL);
  172. dw_usart_putchar(uart,c);
  173. return (1);
  174. }
  175. static int uart_getc(struct rt_serial_device *serial)
  176. {
  177. uint8_t ch;
  178. usart_handle_t uart;
  179. RT_ASSERT(serial != RT_NULL);
  180. uart = (usart_handle_t)serial->parent.user_data;
  181. RT_ASSERT(uart != RT_NULL);
  182. if (!dw_usart_getchar_no_poll(uart, &ch))
  183. {
  184. return (int)(ch);
  185. }
  186. else
  187. {
  188. return -1;
  189. }
  190. }
  191. const struct rt_uart_ops _uart_ops =
  192. {
  193. uart_configure,
  194. uart_control,
  195. uart_putc,
  196. uart_getc,
  197. };
  198. int rt_hw_usart_init(void)
  199. {
  200. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  201. #ifdef RT_USING_UART1
  202. serial1.ops = & _uart_ops;
  203. serial1.config = config;
  204. serial1.config.bufsz = 2048;
  205. serial1.config.baud_rate = 115200;
  206. uart1_handle = csi_usart_initialize(UART_TXD1, UART_RXD1, NULL/*usart1_event_cb*/,
  207. (void *) 0);
  208. rt_hw_serial_register(&serial1,
  209. "uart1",
  210. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  211. uart1_handle);
  212. #endif
  213. return 0;
  214. }
  215. INIT_BOARD_EXPORT(rt_hw_usart_init);
  216. #endif