board.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. * 2013-7-14 Peng Fan sep6200 implementation
  9. */
  10. /**
  11. * @addtogroup sep6200
  12. */
  13. /*@{*/
  14. #include <rthw.h>
  15. #include <rtthread.h>
  16. #include <serial.h>
  17. #include <sep6200.h>
  18. void rt_hw_serial_putc(const char c);
  19. #define UART0 ((struct uartport *)SEP6200_UART0_BASE)
  20. struct rt_device uart0_device;
  21. struct serial_int_rx uart0_int_rx;
  22. struct serial_device uart0 =
  23. {
  24. UART0,
  25. &uart0_int_rx,
  26. RT_NULL
  27. };
  28. /*
  29. * This function will handle rtos timer
  30. */
  31. void rt_timer_handler(int vector, void *param)
  32. {
  33. rt_uint32_t clear_int;
  34. /* clear timer interrupt */
  35. if (read_reg(SEP6200_TIMER_T2IMSR) & 0x1)
  36. clear_int = read_reg(SEP6200_TIMER_T2ISCR);
  37. rt_tick_increase();
  38. }
  39. /*
  40. * This function will handle serial interrupt
  41. */
  42. void rt_serial_handler(int vector, void *param)
  43. {
  44. rt_uint32_t num;
  45. switch (vector) {
  46. case INTSRC_UART0:
  47. /*No interrupt*/
  48. if ((*(RP)SEP6200_UART0_IIR & 0x1))
  49. return;
  50. /*Get the serial interrupt num*/
  51. num = (*(RP)SEP6200_UART0_IIR >> 1) & 0x7;
  52. /*Receive or timeout*/
  53. if ((num == 6) || (num == 2))
  54. rt_hw_serial_isr(&uart0_device);
  55. break;
  56. /*1,2,3 not implemented now, do in future*/
  57. case INTSRC_UART1:
  58. break;
  59. case INTSRC_UART2:
  60. break;
  61. case INTSRC_UART3:
  62. break;
  63. }
  64. }
  65. /*
  66. * This function will init timer2 for system ticks
  67. */
  68. #define BUS4_FREQ 320000000UL
  69. #define TIMER_CLK BUS4_FREQ
  70. #define HZ 100
  71. void rt_hw_timer_init(void)
  72. {
  73. *(RP)SEP6200_TIMER_T2LCR = (TIMER_CLK + HZ / 2) / HZ;
  74. *(RP)SEP6200_TIMER_T2CR = 0x6;
  75. rt_hw_interrupt_install(INTSRC_TIMER1, rt_timer_handler, RT_NULL, "timer");
  76. rt_hw_interrupt_umask(INTSRC_TIMER1);
  77. /* start the timer */
  78. *(RP)SEP6200_TIMER_T2CR |= 0x1;
  79. }
  80. /*
  81. * This function will init uart
  82. */
  83. #define UART_CLK 60000000UL
  84. void rt_hw_uart_init(void)
  85. {
  86. const rt_uint32_t uartclk = UART_CLK;
  87. *(RP)(SEP6200_UART0_LCR) = 0x83;
  88. *(RP)(SEP6200_UART0_DLBH) = (uartclk/16/115200) >> 8;
  89. *(RP)(SEP6200_UART0_DLBL) = (uartclk/16/115200) & 0xff;
  90. *(RP)(SEP6200_UART0_LCR) = 0x83 & (~(0x1 << 7));
  91. *(RP)(SEP6200_UART0_FCR) = 0x0;
  92. *(RP)(SEP6200_UART0_MCR) = 0x0;
  93. *(RP)(SEP6200_UART0_IER) = 0x0;
  94. /* Enable rx interrupt*/
  95. *(RP)(SEP6200_UART0_IER) |= 0x1;
  96. /* Disable tx interrupt*/
  97. *(RP)(SEP6200_UART0_IER) &= ~(0x1 << 1);
  98. rt_hw_interrupt_install(INTSRC_UART0, rt_serial_handler, RT_NULL, "uart0");
  99. rt_hw_interrupt_umask(INTSRC_UART0);
  100. rt_hw_serial_register(&uart0_device, "uart0",
  101. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  102. &uart0);
  103. }
  104. void rt_hw_board_init(void)
  105. {
  106. int i = 0;
  107. rt_hw_uart_init();
  108. rt_hw_timer_init();
  109. }
  110. /*
  111. * Write one char to serial, must not trigger interrupt
  112. */
  113. void rt_hw_serial_putc(const char c)
  114. {
  115. if (c == '\n')
  116. rt_hw_serial_putc('\r');
  117. while (!((*(RP)SEP6200_UART0_LSR) & 0x40));
  118. *(RP)(SEP6200_UART0_TXFIFO) = c;
  119. }
  120. /**
  121. * This function is used by rt_kprintf to display a string on console.
  122. *
  123. * @param str the displayed string^M
  124. */
  125. void rt_hw_console_output(const char *str)
  126. {
  127. while (*str) {
  128. rt_hw_serial_putc(*str++);
  129. }
  130. }
  131. /*@}*/