board_uart.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. * 2020-08-20 zx.chen 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 "ck_usart.h"
  21. #include "drv_usart.h"
  22. #include "pin_name.h"
  23. __attribute__((isr)) void ck_usart0_irqhandler(void);
  24. struct
  25. {
  26. uint32_t base;
  27. uint32_t irq;
  28. void *handler;
  29. }
  30. const sg_usart_config[CONFIG_USART_NUM] =
  31. {
  32. {CSKY_UART_BASE, UART_IRQn, ck_usart0_irqhandler},
  33. };
  34. int32_t target_usart_init(int32_t idx, uint32_t *base, uint32_t *irq, void **handler)
  35. {
  36. if (idx >= CONFIG_USART_NUM)
  37. {
  38. return -1;
  39. }
  40. if (base != NULL)
  41. {
  42. *base = sg_usart_config[idx].base;
  43. }
  44. if (irq != NULL)
  45. {
  46. *irq = sg_usart_config[idx].irq;
  47. }
  48. if (handler != NULL)
  49. {
  50. *handler = sg_usart_config[idx].handler;
  51. }
  52. return idx;
  53. }
  54. #ifdef RT_USING_UART1
  55. #define UART_TXD1 PA10_TXD1_PWM1_XX_SIROUT1
  56. #define UART_RXD1 PA11_RXD1_PWM2_XX_SIRIN1
  57. static usart_handle_t uart1_handle;
  58. static struct rt_serial_device serial1;
  59. __attribute__((isr)) void ck_usart0_irqhandler(void)
  60. {
  61. rt_hw_serial_isr(&serial1,RT_SERIAL_EVENT_RX_IND);
  62. }
  63. #endif
  64. /*
  65. * UART interface
  66. */
  67. static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  68. {
  69. int ret;
  70. usart_handle_t uart;
  71. uint32_t bauds;
  72. usart_mode_e mode;
  73. usart_parity_e parity;
  74. usart_stop_bits_e stopbits;
  75. usart_data_bits_e databits;
  76. RT_ASSERT(serial != RT_NULL);
  77. uart = (usart_handle_t)serial->parent.user_data;
  78. RT_ASSERT(uart != RT_NULL);
  79. /* set baudrate parity...*/
  80. bauds = cfg->baud_rate;
  81. mode = USART_MODE_ASYNCHRONOUS;
  82. if (cfg->parity == PARITY_EVEN)
  83. parity = USART_PARITY_EVEN;
  84. else if (cfg->parity == PARITY_ODD)
  85. parity = USART_PARITY_ODD;
  86. else
  87. parity = USART_PARITY_NONE;
  88. stopbits = USART_STOP_BITS_1 ;
  89. databits = USART_DATA_BITS_8;
  90. ret = csi_usart_config(uart, bauds, USART_MODE_ASYNCHRONOUS, parity, stopbits, databits);
  91. if (ret < 0)
  92. {
  93. return -RT_ERROR;
  94. }
  95. return RT_EOK;
  96. }
  97. static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  98. {
  99. usart_handle_t uart;
  100. RT_ASSERT(serial != RT_NULL);
  101. uart = (usart_handle_t)serial->parent.user_data;
  102. RT_ASSERT(uart != RT_NULL);
  103. switch (cmd)
  104. {
  105. case RT_DEVICE_CTRL_CLR_INT:
  106. /* Disable the UART Interrupt */
  107. ck_usart_clr_int_flag(uart,IER_RDA_INT_ENABLE);
  108. break;
  109. case RT_DEVICE_CTRL_SET_INT:
  110. /* Enable the UART Interrupt */
  111. ck_usart_set_int_flag(uart,IER_RDA_INT_ENABLE);
  112. break;
  113. }
  114. return (RT_EOK);
  115. }
  116. static int uart_putc(struct rt_serial_device *serial, char c)
  117. {
  118. usart_handle_t uart;
  119. RT_ASSERT(serial != RT_NULL);
  120. uart = (usart_handle_t)serial->parent.user_data;
  121. RT_ASSERT(uart != RT_NULL);
  122. csi_usart_putchar(uart,c);
  123. return (1);
  124. }
  125. static int uart_getc(struct rt_serial_device *serial)
  126. {
  127. int ch;
  128. usart_handle_t uart;
  129. RT_ASSERT(serial != RT_NULL);
  130. uart = (usart_handle_t)serial->parent.user_data;
  131. RT_ASSERT(uart != RT_NULL);
  132. ch = csi_uart_getchar(uart);
  133. return ch;
  134. }
  135. const struct rt_uart_ops _uart_ops =
  136. {
  137. uart_configure,
  138. uart_control,
  139. uart_putc,
  140. uart_getc,
  141. };
  142. int rt_hw_usart_init(void)
  143. {
  144. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  145. #ifdef RT_USING_UART1
  146. serial1.ops = & _uart_ops;
  147. serial1.config = config;
  148. serial1.config.bufsz = 2048;
  149. serial1.config.baud_rate = 115200;
  150. uart1_handle = csi_usart_initialize(0, NULL);
  151. rt_hw_serial_register(&serial1,
  152. "uart1",
  153. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  154. uart1_handle);
  155. #endif
  156. return 0;
  157. }
  158. INIT_BOARD_EXPORT(rt_hw_usart_init);
  159. #endif