board_uart.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * File : board_uart.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2018, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2017-09-23 Urey first implementation
  23. */
  24. #include <rtthread.h>
  25. #ifdef RT_USING_CONSOLE
  26. #include <rthw.h>
  27. #include <rtdevice.h>
  28. #include <stdint.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <stdbool.h>
  32. #include "board.h"
  33. #include "soc.h"
  34. #include "dw_usart.h"
  35. #include "drv_usart.h"
  36. #include "pin_name.h"
  37. #include "pinmux.h"
  38. #if CONFIG_USART
  39. struct
  40. {
  41. uint32_t base;
  42. uint32_t irq;
  43. } const sg_usart_config[CONFIG_USART_NUM] =
  44. {
  45. { CSKY_UART0_BASE, UART0_IRQn },
  46. { CSKY_UART1_BASE, UART1_IRQn },
  47. { CSKY_UART2_BASE, UART2_IRQn },
  48. { CSKY_UART3_BASE, UART3_IRQn }
  49. };
  50. typedef struct
  51. {
  52. pin_t tx;
  53. pin_t rx;
  54. uint16_t cfg_idx; //idx of sg_usart_config[]
  55. uint16_t function;
  56. } usart_pin_map_t;
  57. const static usart_pin_map_t s_usart_pin_map[] =
  58. {
  59. {
  60. PA0_TXD0_PWM0_XX_SIROUT0,
  61. PA1_RXD0_PWM1_XX_SIRIN0,
  62. 0,
  63. 0
  64. },
  65. {
  66. PA10_TXD1_PWM1_XX_SIROUT1,
  67. PA11_RXD1_PWM2_XX_SIRIN1,
  68. 1,
  69. 0
  70. },
  71. {
  72. PA23_TXD2_PWM5_XX_SIROUT2,
  73. PA22_RXD2_PWM4_XX_SIRIN2,
  74. 2,
  75. 0
  76. },
  77. {
  78. PA26_TXD3_PWMFAULT_XX_SIROUT3,
  79. PA27_RXD3_PWM0_XX_SIRIN3,
  80. 3,
  81. 0
  82. }
  83. };
  84. int32_t target_usart_init(pin_t tx, pin_t rx, uint32_t *base, uint32_t *irq)
  85. {
  86. uint32_t idx;
  87. for (idx = 0; idx < sizeof(s_usart_pin_map) / sizeof(usart_pin_map_t); idx++)
  88. {
  89. if (s_usart_pin_map[idx].tx == tx && s_usart_pin_map[idx].rx == rx)
  90. {
  91. *base = sg_usart_config[s_usart_pin_map[idx].cfg_idx].base;
  92. *irq = sg_usart_config[s_usart_pin_map[idx].cfg_idx].irq;
  93. /*pinmux*/
  94. pin_mux(s_usart_pin_map[idx].tx, s_usart_pin_map[idx].function);
  95. pin_mux(s_usart_pin_map[idx].rx, s_usart_pin_map[idx].function);
  96. return s_usart_pin_map[idx].cfg_idx;
  97. }
  98. }
  99. return -1;
  100. }
  101. #endif
  102. #ifdef RT_USING_UART1
  103. #define UART_TXD1 PA10_TXD1_PWM1_XX_SIROUT1
  104. #define UART_RXD1 PA11_RXD1_PWM2_XX_SIRIN1
  105. static usart_handle_t uart1_handle;
  106. static struct rt_serial_device serial1;
  107. /*
  108. static void usart1_event_cb(uint32_t event, void *cb_arg)
  109. {
  110. switch (event)
  111. {
  112. case USART_EVENT_SEND_COMPLETE:
  113. rt_hw_serial_isr(&serial1,RT_SERIAL_EVENT_TX_DONE);
  114. break;
  115. case USART_EVENT_RECEIVED:
  116. rt_hw_serial_isr(&serial1,RT_SERIAL_EVENT_RX_IND);
  117. break;
  118. default:
  119. break;
  120. }
  121. }
  122. */
  123. __attribute__((isr)) void USART1_IRQHandler(void)
  124. {
  125. rt_hw_serial_isr(&serial1,RT_SERIAL_EVENT_RX_IND);
  126. }
  127. #endif
  128. /*
  129. * UART interface
  130. */
  131. static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  132. {
  133. int ret;
  134. usart_handle_t uart;
  135. uint32_t bauds;
  136. usart_mode_e mode;
  137. usart_parity_e parity;
  138. usart_stop_bits_e stopbits;
  139. usart_data_bits_e databits;
  140. RT_ASSERT(serial != RT_NULL);
  141. uart = (usart_handle_t)serial->parent.user_data;
  142. RT_ASSERT(uart != RT_NULL);
  143. /* set baudrate parity...*/
  144. bauds = cfg->baud_rate;
  145. mode = USART_MODE_ASYNCHRONOUS;
  146. if (cfg->parity == PARITY_EVEN)
  147. parity = USART_PARITY_EVEN;
  148. else if (cfg->parity == PARITY_ODD)
  149. parity = USART_PARITY_ODD;
  150. else
  151. parity = USART_PARITY_NONE;
  152. stopbits = USART_STOP_BITS_1 ;
  153. databits = USART_DATA_BITS_8;
  154. ret = csi_usart_config(uart, SYSTEM_CLOCK, bauds, USART_MODE_ASYNCHRONOUS, parity, stopbits, databits);
  155. if (ret < 0)
  156. {
  157. return -RT_ERROR;
  158. }
  159. return RT_EOK;
  160. }
  161. static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  162. {
  163. usart_handle_t uart;
  164. RT_ASSERT(serial != RT_NULL);
  165. uart = (usart_handle_t)serial->parent.user_data;
  166. RT_ASSERT(uart != RT_NULL);
  167. switch (cmd)
  168. {
  169. case RT_DEVICE_CTRL_CLR_INT:
  170. /* Disable the UART Interrupt */
  171. dw_usart_clr_int_flag(uart,IER_RDA_INT_ENABLE);
  172. break;
  173. case RT_DEVICE_CTRL_SET_INT:
  174. /* Enable the UART Interrupt */
  175. dw_usart_set_int_flag(uart,IER_RDA_INT_ENABLE);
  176. break;
  177. }
  178. return (RT_EOK);
  179. }
  180. static int uart_putc(struct rt_serial_device *serial, char c)
  181. {
  182. usart_handle_t uart;
  183. RT_ASSERT(serial != RT_NULL);
  184. uart = (usart_handle_t)serial->parent.user_data;
  185. RT_ASSERT(uart != RT_NULL);
  186. dw_usart_putchar(uart,c);
  187. return (1);
  188. }
  189. static int uart_getc(struct rt_serial_device *serial)
  190. {
  191. uint8_t ch;
  192. usart_handle_t uart;
  193. RT_ASSERT(serial != RT_NULL);
  194. uart = (usart_handle_t)serial->parent.user_data;
  195. RT_ASSERT(uart != RT_NULL);
  196. if (!dw_usart_getchar_no_poll(uart, &ch))
  197. {
  198. return (int)(ch);
  199. }
  200. else
  201. {
  202. return -1;
  203. }
  204. }
  205. const struct rt_uart_ops _uart_ops =
  206. {
  207. uart_configure,
  208. uart_control,
  209. uart_putc,
  210. uart_getc,
  211. };
  212. int rt_hw_usart_init(void)
  213. {
  214. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  215. #ifdef RT_USING_UART1
  216. serial1.ops = & _uart_ops;
  217. serial1.config = config;
  218. serial1.config.bufsz = 2048;
  219. serial1.config.baud_rate = 115200;
  220. uart1_handle = csi_usart_initialize(UART_TXD1, UART_RXD1, NULL/*usart1_event_cb*/,
  221. (void *) 0);
  222. rt_hw_serial_register(&serial1,
  223. "uart1",
  224. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  225. uart1_handle);
  226. #endif
  227. return 0;
  228. }
  229. INIT_BOARD_EXPORT(rt_hw_usart_init);
  230. #endif