usart.c 6.0 KB

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