1
0

uart.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * File : interrupt.h
  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. * 2015-04-14 ArdaFu first version
  23. */
  24. #include "asm9260t.h"
  25. #include "rtthread.h"
  26. #include "uart.h"
  27. void Hw_UartDisable(HW_USART_TypeDef* uartBase)
  28. {
  29. uartBase->INTR[R_CLR] = ASM_UART_INTR_RXIEN | ASM_UART_INTR_TXIEN | ASM_UART_INTR_RTIS;
  30. uartBase->CTRL2[R_CLR] = ASM_UART_CTRL2_TXE | ASM_UART_CTRL2_RXE;
  31. }
  32. void Hw_UartEnable(HW_USART_TypeDef* uartBase)
  33. {
  34. uartBase->CTRL2[R_CLR] = 0x0000C000UL; //clear CTSEN and RTSEN
  35. uartBase->CTRL2[R_SET] = ASM_UART_CTRL2_TXE | ASM_UART_CTRL2_RXE | ASM_UART_CTRL2_USARTEN;
  36. uartBase->INTR[R_SET] = ASM_UART_INTR_RXIEN | ASM_UART_INTR_RTIEN;
  37. }
  38. void Hw_UartReset(HW_USART_TypeDef* uartBase)
  39. {
  40. uartBase->CTRL0[R_CLR] = ASM_UART_CTRL0_SFTRST | ASM_UART_CTRL0_CLKGATE | ASM_UART_CTRL0_RXTO_ENABLE;
  41. uartBase->CTRL0[R_SET] = ASM_UART_CTRL0_SFTRST | ASM_UART_CTRL0_CLKGATE | ASM_UART_CTRL0_RXTO_ENABLE;
  42. }
  43. void Hw_UartConfig(HW_USART_TypeDef* uartBase,int baudRate, int dataBits, int stopBits,int parity)
  44. {
  45. rt_uint32_t mode = ASM_UART_LINECTRL_FEN;
  46. switch (dataBits)
  47. {
  48. case 8:
  49. mode |= ASM_UART_LINECTRL_WLEN8;
  50. break;
  51. case 7:
  52. mode |= ASM_UART_LINECTRL_WLEN7;
  53. break;
  54. case 6:
  55. mode |= ASM_UART_LINECTRL_WLEN6;
  56. break;
  57. case 5:
  58. mode |= ASM_UART_LINECTRL_WLEN5;
  59. break;
  60. default:
  61. mode |= ASM_UART_LINECTRL_WLEN8;
  62. break;
  63. }
  64. switch (stopBits)
  65. {
  66. case 2:
  67. mode |= ASM_UART_LINECTRL_STP2;
  68. break;
  69. case 1:
  70. default:
  71. break;
  72. }
  73. switch (parity)
  74. {
  75. case 1:
  76. mode |= ASM_UART_LINECTRL_PEN;
  77. break;
  78. case 2:
  79. mode |= ASM_UART_LINECTRL_PEN | ASM_UART_LINECTRL_EPS;
  80. break;
  81. case 0:
  82. default:
  83. break;
  84. }
  85. //16bit nBaudDivint
  86. mode |= (((12000000 <<2 ) / baudRate) & UART_BAUD_DIVINT_MASK) << 10;
  87. //6bit nNaudDivfrac
  88. mode |= (((12000000 <<2 ) / baudRate) & UART_BAUD_DIVFRAC_MASK) << 8;
  89. uartBase->LINECTRL[R_VAL] = mode;
  90. }
  91. void Hw_UartInit(int index)
  92. {
  93. // uart0 = bit11, uart9 = bit20
  94. int ctrl_bit = index + 11;
  95. outl(1UL<<ctrl_bit,REG_SET(HW_AHBCLKCTRL0)); //UART4 ENABLE bit15
  96. outl(0x1, HW_UART0CLKDIV + index*4); //UART4 div 2
  97. outl(1UL<<ctrl_bit,REG_CLR(HW_AHBCLKCTRL0)); //UART4 clk gate
  98. outl(1UL<<ctrl_bit,REG_SET(HW_AHBCLKCTRL0)); //UART4 clk gate
  99. outl(1UL<<ctrl_bit,REG_CLR(HW_PRESETCTRL0)); //UART4 reset
  100. outl(1UL<<ctrl_bit,REG_SET(HW_PRESETCTRL0)); //UART4 reset
  101. }