board.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * File : board.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2009 RT-Thread Develop Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://openlab.rt-thread.com/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2009-05-16 Bernard first implementation
  13. * 2010-10-5 Wangmeng sep4020 implementation
  14. */
  15. #include <rthw.h>
  16. #include <rtthread.h>
  17. #include <sep4020.h>
  18. #include <serial.h>
  19. void rt_hw_serial_putc(const char c);
  20. #define UART0 ((struct uartport *)UART0_BASE)
  21. struct rt_device uart0_device;
  22. struct serial_int_rx uart0_int_rx;
  23. struct serial_device uart0 =
  24. {
  25. UART0,
  26. &uart0_int_rx,
  27. RT_NULL
  28. };
  29. /**
  30. * This function will handle rtos timer
  31. */
  32. void rt_timer_handler(int vector)
  33. {
  34. rt_uint32_t clear_int;
  35. rt_tick_increase();
  36. /*Clear timer interrupt*/
  37. clear_int = *(RP)TIMER_T1ISCR;
  38. *(RP)TIMER_T1ISCR=clear_int;
  39. }
  40. /**
  41. * This function will handle serial
  42. */
  43. void rt_serial_handler(int vector)
  44. {
  45. //rt_kprintf("in rt_serial_handler\n");
  46. rt_int32_t stat = *(RP)UART0_IIR ;
  47. UNUSED char c;
  48. /*Received data*/
  49. if(((stat & 0x0E) >> 1) == 0x02)
  50. {
  51. rt_hw_serial_isr(&uart0_device);
  52. /*while (((*(RP)UART0_LSR) & 0x40))
  53. {
  54. c = (char)(*(RP)UART0_BASE);
  55. if(c == '\r')
  56. {
  57. rt_hw_serial_putc('\r');
  58. rt_hw_serial_putc('\n');
  59. }
  60. else
  61. rt_hw_serial_putc(c);
  62. } */
  63. }
  64. else
  65. {
  66. /*clear the timeout interrupt*/
  67. while (uart0.uart_device->lsr & USTAT_RCV_READY)
  68. c = uart0.uart_device->dlbl_fifo.rxfifo;
  69. }
  70. }
  71. /**
  72. * This function will init timer4 for system ticks
  73. */
  74. void rt_hw_timer_init()
  75. {
  76. /*Set timer1*/
  77. *(RP)TIMER_T1LCR = 880000;
  78. *(RP)TIMER_T1CR = 0x06;
  79. rt_hw_interrupt_install(INTSRC_TIMER1, rt_timer_handler, RT_NULL);
  80. rt_hw_interrupt_umask(INTSRC_TIMER1);
  81. /*Enable timer1*/
  82. *(RP)TIMER_T1CR |= 0x01;
  83. }
  84. /**
  85. * This function will handle init uart
  86. */
  87. void rt_hw_uart_init(void)
  88. {
  89. const rt_int32_t sysclk = 72000000;
  90. /*Set data bit:8*/
  91. *(RP)(UART0_LCR) = 0x83;
  92. /*Set baud rate high*/
  93. *(RP)(UART0_DLBH) = (sysclk/16/115200) >> 8;
  94. /*Set baud rate low*/
  95. *(RP)(UART0_DLBL) = (sysclk/16/115200) & 0xff;
  96. *(RP)(UART0_LCR) = 0x83&(~(0x1 << 7));
  97. /*Set trigger level*/
  98. *(RP)(UART0_FCR) = 0x0;
  99. *(RP)(UART0_IER) = 0x0;
  100. /*Enable rx interrupt*/
  101. *(RP)(UART0_IER) |= 0x01;
  102. /*Disable tx interrupt*/
  103. *(RP)(UART0_IER) &= ~(0x1<<1);
  104. rt_hw_interrupt_install(INTSRC_UART0, rt_serial_handler, RT_NULL);
  105. rt_hw_interrupt_umask(INTSRC_UART0);
  106. }
  107. void rt_hw_board_init()
  108. {
  109. /* initialize uart */
  110. rt_hw_uart_init();
  111. rt_hw_timer_init();
  112. }
  113. /* write one character to serial, must not trigger interrupt */
  114. void rt_hw_serial_putc(const char c)
  115. {
  116. /*
  117. to be polite with serial console add a line feed
  118. to the carriage return character
  119. */
  120. if (c=='\n')rt_hw_serial_putc('\r');
  121. while (!((*(RP)UART0_LSR) & 0x40));
  122. *(RP)(UART0_BASE) = c;
  123. }
  124. /**
  125. * This function is used by rt_kprintf to display a string on console.
  126. *
  127. * @param str the displayed string
  128. */
  129. void rt_hw_console_output(const char* str)
  130. {
  131. while (*str)
  132. {
  133. rt_hw_serial_putc(*str++);
  134. }
  135. }