board.c 3.8 KB

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