usart.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. * 2011-01-13 weety first version
  9. * 2013-07-21 weety using serial component
  10. * 2015-05-02 ArdaFu Port from AT91SAM9260 BSP
  11. */
  12. #include <rtthread.h>
  13. #include <rthw.h>
  14. #include <rtdevice.h>
  15. #include "interrupt.h"
  16. #include <asm9260t.h>
  17. #include "gpio.h"
  18. #include "uart.h"
  19. typedef struct
  20. {
  21. HW_USART_TypeDef *port;
  22. int irq;
  23. } asm_uart_t;
  24. /**
  25. * This function will handle serial
  26. */
  27. void rt_asm_usart_handler(int vector, void *param)
  28. {
  29. rt_uint32_t status;
  30. asm_uart_t *uart;
  31. rt_device_t dev = (rt_device_t)param;
  32. uart = (asm_uart_t *)dev->user_data;
  33. status = uart->port->INTR[R_VAL];
  34. if(!(status & (ASM_UART_INTR_RXIS | ASM_UART_INTR_RTIS)))
  35. return;
  36. uart->port->INTR[R_CLR] = ASM_UART_INTR_RXIS|ASM_UART_INTR_RTIS;
  37. //rt_interrupt_enter();
  38. rt_hw_serial_isr((struct rt_serial_device *)dev, RT_SERIAL_EVENT_RX_IND);
  39. //rt_interrupt_leave();
  40. }
  41. /**
  42. * UART device in RT-Thread
  43. */
  44. static rt_err_t asm_usart_configure(struct rt_serial_device *serial,
  45. struct serial_configure *cfg)
  46. {
  47. asm_uart_t *uart;
  48. RT_ASSERT(serial != RT_NULL);
  49. RT_ASSERT(cfg != RT_NULL);
  50. uart = (asm_uart_t *)serial->parent.user_data;
  51. Hw_UartDisable(uart->port);
  52. Hw_UartReset(uart->port);
  53. Hw_UartConfig(uart->port, cfg->baud_rate, cfg->data_bits,
  54. cfg->stop_bits, cfg->parity);
  55. Hw_UartEnable(uart->port);
  56. return RT_EOK;
  57. }
  58. static rt_err_t asm_usart_control(struct rt_serial_device *serial,
  59. int cmd, void *arg)
  60. {
  61. asm_uart_t* uart;
  62. RT_ASSERT(serial != RT_NULL);
  63. uart = (asm_uart_t *)serial->parent.user_data;
  64. switch (cmd)
  65. {
  66. case RT_DEVICE_CTRL_CLR_INT:
  67. /* disable rx irq */
  68. rt_hw_interrupt_mask(uart->irq);
  69. break;
  70. case RT_DEVICE_CTRL_SET_INT:
  71. /* enable rx irq */
  72. rt_hw_interrupt_umask(uart->irq);
  73. break;
  74. }
  75. return RT_EOK;
  76. }
  77. static int asm_usart_putc(struct rt_serial_device *serial, char c)
  78. {
  79. //rt_uint32_t level;
  80. asm_uart_t *uart = serial->parent.user_data;
  81. while ((uart->port->STAT[R_VAL] & ASM_UART_STAT_TXFF));
  82. uart->port->DATA[R_VAL] = c;
  83. return 1;
  84. }
  85. static int asm_usart_getc(struct rt_serial_device *serial)
  86. {
  87. asm_uart_t *uart = serial->parent.user_data;
  88. if (uart->port->STAT[R_VAL] & ASM_UART_STAT_RXFE)
  89. return -1;
  90. return uart->port->DATA[R_VAL] & 0xff;
  91. }
  92. static const struct rt_uart_ops asm_usart_ops =
  93. {
  94. asm_usart_configure,
  95. asm_usart_control,
  96. asm_usart_putc,
  97. asm_usart_getc,
  98. };
  99. #if defined(RT_USING_UART0)
  100. static struct rt_serial_device serial0;
  101. asm_uart_t uart0 =
  102. {
  103. USART0,
  104. INT_UART0
  105. };
  106. #endif
  107. #if defined(RT_USING_UART3)
  108. static struct rt_serial_device serial3;
  109. asm_uart_t uart3 =
  110. {
  111. USART3,
  112. INT_UART3
  113. };
  114. #endif
  115. #if defined(RT_USING_UART4)
  116. static struct rt_serial_device serial4;
  117. asm_uart_t uart4 =
  118. {
  119. USART4,
  120. INT_UART4
  121. };
  122. #endif
  123. //USART0 PINS TX=GP14_0:5 , RX=GP14_1:5
  124. //USART3 PINS TX=GP8_6:2 , RX=GP8_7:2
  125. //USART4 PINS TX=GP3_0:2 , RX=GP3_1:2
  126. void asm_usart_gpio_init(void)
  127. {
  128. // enable IOCONFIG GPIO
  129. outl(((1UL<<25) | (1UL<<4)) ,REG_SET(HW_AHBCLKCTRL0));
  130. #ifdef RT_USING_UART0
  131. HW_SetPinMux(14, 0, 5);
  132. HW_SetPinMux(14, 1, 5);
  133. #endif
  134. #ifdef RT_USING_UART3
  135. HW_SetPinMux(8, 6, 2);
  136. HW_SetPinMux(8, 7, 2);
  137. #endif
  138. #ifdef RT_USING_UART4
  139. HW_SetPinMux(3, 0, 2);
  140. HW_SetPinMux(3, 1, 2);
  141. #endif
  142. }
  143. void asm_serial_config_set_default(struct rt_serial_device* serial)
  144. {
  145. serial->ops = &asm_usart_ops;
  146. serial->config.baud_rate = BAUD_RATE_115200;
  147. serial->config.bit_order = BIT_ORDER_LSB;
  148. serial->config.data_bits = DATA_BITS_8;
  149. serial->config.parity = PARITY_NONE;
  150. serial->config.stop_bits = STOP_BITS_1;
  151. serial->config.invert = NRZ_NORMAL;
  152. serial->config.bufsz = RT_SERIAL_RB_BUFSZ;
  153. }
  154. #define DRV_REG_OPS (RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM)
  155. /**
  156. * This function will handle init uart
  157. */
  158. int rt_hw_uart_init(void)
  159. {
  160. asm_usart_gpio_init();
  161. #if defined(RT_USING_UART0)
  162. Hw_UartInit(0);
  163. asm_serial_config_set_default(&serial0);
  164. /* register uart device */
  165. rt_hw_serial_register(&serial0, "uart0", DRV_REG_OPS, &uart0);
  166. rt_hw_interrupt_install(uart0.irq, rt_asm_usart_handler,
  167. (void *)&(serial0.parent), "UART0");
  168. rt_hw_interrupt_umask(uart0.irq);
  169. #endif
  170. #if defined(RT_USING_UART3)
  171. Hw_UartInit(3);
  172. asm_serial_config_set_default(&serial3);
  173. /* register uart device */
  174. rt_hw_serial_register(&serial3, "uart3", DRV_REG_OPS, &uart3);
  175. rt_hw_interrupt_install(uart3.irq, rt_asm_usart_handler,
  176. (void *)&(serial3.parent), "UART3");
  177. rt_hw_interrupt_umask(uart3.irq);
  178. #endif
  179. #if defined(RT_USING_UART4)
  180. Hw_UartInit(4);
  181. asm_serial_config_set_default(&serial4);
  182. /* register uart device */
  183. rt_hw_serial_register(&serial4, "uart4", DRV_REG_OPS, &uart4);
  184. rt_hw_interrupt_install(uart4.irq, rt_asm_usart_handler,
  185. (void *)&(serial4.parent), "UART4");
  186. rt_hw_interrupt_umask(uart4.irq);
  187. #endif
  188. return 0;
  189. }
  190. INIT_BOARD_EXPORT(rt_hw_uart_init);