board.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * File : board.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012 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, void *param)
  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, void *param)
  44. {
  45. //rt_kprintf("in rt_serial_handler\n");
  46. rt_int32_t stat = *(RP)UART0_IIR ;
  47. RT_UNUSED char c;
  48. /*Received data*/
  49. if (((stat & 0x0E) >> 1) == 0x02)
  50. {
  51. rt_hw_serial_isr(&uart0_device);
  52. }
  53. else
  54. {
  55. /*clear the timeout interrupt*/
  56. while (uart0.uart_device->lsr & USTAT_RCV_READY)
  57. c = uart0.uart_device->dlbl_fifo.rxfifo;
  58. }
  59. }
  60. /**
  61. * This function will init led on the board
  62. */
  63. static void rt_hw_board_led_init(void)
  64. {
  65. /* PE3 PE4 PE5 for led */
  66. *(RP)GPIO_PORTE_SEL |=0x38; /* GPIO */
  67. *(RP)GPIO_PORTE_DIR &= ~0x38; /* output*/
  68. *(RP)GPIO_PORTE_DATA &= ~0x38; /* low */
  69. }
  70. /**
  71. * This function will init timer4 for system ticks
  72. */
  73. void rt_hw_timer_init(void)
  74. {
  75. /*Set timer1*/
  76. *(RP)TIMER_T1LCR = 880000;
  77. *(RP)TIMER_T1CR = 0x06;
  78. rt_hw_interrupt_install(INTSRC_TIMER1, rt_timer_handler, RT_NULL, "tick");
  79. rt_hw_interrupt_umask(INTSRC_TIMER1);
  80. /*Enable timer1*/
  81. *(RP)TIMER_T1CR |= 0x01;
  82. }
  83. /**
  84. * This function will handle init uart
  85. */
  86. void rt_hw_uart_init(void)
  87. {
  88. const rt_int32_t sysclk = 72000000;
  89. /*Set data bit:8*/
  90. *(RP)(UART0_LCR) = 0x83;
  91. /*Set baud rate high*/
  92. *(RP)(UART0_DLBH) = (sysclk/16/115200) >> 8;
  93. /*Set baud rate low*/
  94. *(RP)(UART0_DLBL) = (sysclk/16/115200) & 0xff;
  95. *(RP)(UART0_LCR) = 0x83&(~(0x1 << 7));
  96. /*Set trigger level*/
  97. *(RP)(UART0_FCR) = 0x0;
  98. *(RP)(UART0_IER) = 0x0;
  99. /*Enable rx interrupt*/
  100. *(RP)(UART0_IER) |= 0x01;
  101. /*Disable tx interrupt*/
  102. *(RP)(UART0_IER) &= ~(0x1<<1);
  103. rt_hw_interrupt_install(INTSRC_UART0, rt_serial_handler, RT_NULL, "UART0");
  104. rt_hw_interrupt_umask(INTSRC_UART0);
  105. /* register uart0 */
  106. rt_hw_serial_register(&uart0_device, "uart0",
  107. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  108. &uart0);
  109. }
  110. void rt_hw_board_init(void)
  111. {
  112. /* initialize uart */
  113. rt_hw_uart_init();
  114. // rt_hw_board_led_init();
  115. rt_hw_timer_init();
  116. }
  117. /* write one character to serial, must not trigger interrupt */
  118. void rt_hw_serial_putc(const char c)
  119. {
  120. /*
  121. to be polite with serial console add a line feed
  122. to the carriage return character
  123. */
  124. if (c=='\n')
  125. rt_hw_serial_putc('\r');
  126. while (!((*(RP)UART0_LSR) & 0x40));
  127. *(RP)(UART0_BASE) = c;
  128. }
  129. /**
  130. * This function is used by rt_kprintf to display a string on console.
  131. *
  132. * @param str the displayed string
  133. */
  134. void rt_hw_console_output(const char *str)
  135. {
  136. while (*str)
  137. {
  138. rt_hw_serial_putc(*str++);
  139. }
  140. }