board.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * File : board.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2012, RT-Thread Development 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://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2012-02-13 mojingxian first version
  13. */
  14. #include "board.h"
  15. #include "rtconfig.h"
  16. #include "rtdef.h"
  17. #include "rthw.h"
  18. #include "serial.h"
  19. #include <signal.h>
  20. #include <sys/platform.h>
  21. #include <ccblkfn.h>
  22. #include <sysreg.h>
  23. #include <string.h>
  24. #include <sys\exception.h>
  25. #include <stdio.h>
  26. #define IVG_CLR(index) (index > 0 ? ((0xFFFFFFF0 << (index * 0x04)) | \
  27. (0xFFFFFFF0 >> ((0x08 - index) * 0x04))):0xFFFFFFF0)
  28. #define IVG_SET(index,ivg) ((((ivg) - 0x07) & 0x0F) << (index * 0x04))
  29. #define UART0 ((struct uartport *)pUART_THR)
  30. struct serial_int_rx uart0_int_rx;
  31. struct serial_device uart0 =
  32. {
  33. UART0,
  34. &uart0_int_rx,
  35. RT_NULL
  36. };
  37. struct rt_device uart0_device;
  38. /**
  39. * This function is to set the EBIU(EXTERNAL BUS INTERFACE UNIT).
  40. */
  41. static void rt_hw_ebiu_init(void)
  42. {
  43. *pEBIU_AMBCTL0 = 0xffc2ffc2;
  44. *pEBIU_AMBCTL1 = 0xffc2ffc3;
  45. *pEBIU_AMGCTL = 0x010f;
  46. }
  47. /**
  48. * This is the timer interrupt service routine.
  49. */
  50. EX_INTERRUPT_HANDLER(rt_hw_timer_handler)
  51. {
  52. /* enter interrupt */
  53. rt_interrupt_enter();
  54. rt_tick_increase();
  55. /* leave interrupt */
  56. rt_interrupt_leave();
  57. }
  58. /**
  59. * This function is called to initialize system tick source (typically a
  60. * timer generating interrupts every 1 to 100 mS).
  61. * We decided to use Core Timer as the tick interrupt source.
  62. */
  63. void rt_hw_core_timer_init(void)
  64. {
  65. *pTCNTL = 1; // Turn on timer, TMPWR
  66. *pTSCALE = 0x00;
  67. *pTCOUNT = CCLKSPEED / RT_TICK_PER_SECOND;
  68. *pTPERIOD = CCLKSPEED / RT_TICK_PER_SECOND;
  69. register_handler(ik_timer,rt_hw_timer_handler);
  70. *pTCNTL = 0x07; // Start Timer and set Auto-reload
  71. }
  72. void rt_hw_interrupt_init(void)
  73. {
  74. extern rt_uint32_t rt_interrupt_from_thread;
  75. extern rt_uint32_t rt_interrupt_to_thread;
  76. extern rt_uint32_t rt_thread_switch_interrupt_flag;
  77. extern rt_uint8_t rt_interrupt_nest;
  78. extern void interrupt_thread_switch(void);
  79. register_handler(ik_ivg14,interrupt_thread_switch); //context_vdsp.S
  80. /* init interrupt nest, and context in thread sp */
  81. rt_interrupt_nest = 0;
  82. rt_interrupt_from_thread = 0;
  83. rt_interrupt_to_thread = 0;
  84. rt_thread_switch_interrupt_flag = 0;
  85. }
  86. static void rt_hw_pll_init(void)
  87. {
  88. unsigned long imask;
  89. sysreg_write(reg_SYSCFG, 0x32);
  90. *pSIC_IWR = 0x01;
  91. *pPLL_CTL = SET_MSEL(SPEED_MULTIPLE);
  92. // PLL Re-programming Sequence.
  93. // Core is idle'ed to allow the PPL to re-lock.
  94. imask = cli();
  95. idle();
  96. sti(imask);
  97. *pVR_CTL = 0x00FB;
  98. // PLL Re-programming Sequence.
  99. // Core is idle'ed to allow the PPL to re-lock.
  100. imask = cli();
  101. idle();
  102. sti(imask);
  103. *pPLL_DIV = BUS_DIVISOR;
  104. }
  105. /**
  106. * This function is called to initialize external sdram.
  107. */
  108. static void rt_hw_exdram_init(void)
  109. {
  110. // Initalize EBIU control registers to enable all banks
  111. *pEBIU_AMBCTL1 = 0xFFFFFF02;
  112. ssync();
  113. *pEBIU_AMGCTL = 0x00FF;
  114. ssync();
  115. // Check if already enabled
  116. if (SDRS != ((*pEBIU_SDSTAT) & SDRS))
  117. {
  118. return;
  119. }
  120. //SDRAM Refresh Rate Control Register
  121. *pEBIU_SDRRC = 0x01A0;
  122. //SDRAM Memory Bank Control Register
  123. *pEBIU_SDBCTL = 0x0025; //1.7 64 MB
  124. //SDRAM Memory Global Control Register
  125. *pEBIU_SDGCTL = 0x0091998D;//0x998D0491
  126. ssync();
  127. }
  128. short uart_set_bitrate(unsigned long bit_rate)
  129. {
  130. unsigned short int divisor;
  131. switch (bit_rate)
  132. {
  133. case 1200:
  134. case 2400:
  135. case 4800:
  136. case 9600:
  137. case 19200:
  138. case 28800:
  139. case 38400:
  140. case 57600:
  141. case 115200:
  142. case 125000:
  143. divisor = (unsigned short int) ((float) SCLKSPEED / ((float) bit_rate * 16.0f) + 0.5f);
  144. *(pUART_LCR) |= DLAB; // Enable access to DLL and DLH registers
  145. *(pUART_DLL) = divisor & 0xFF;
  146. *(pUART_DLH) = divisor >> 8;
  147. *(pUART_LCR) &= ~DLAB; // clear DLAB bit
  148. break;
  149. default: // baud rate not supported
  150. break;
  151. }
  152. return 0;
  153. }
  154. void rt_hw_uart_init(void)
  155. {
  156. // Apply UART configuration 8 bit data, No parity, 1 stop bit
  157. *pUART_LCR = 0x0000; // Reset value
  158. *pUART_LCR = WLS(8);
  159. // Ensure that Loopback mode is disabled by clearing LOOP_ENA bit
  160. *pUART_MCR = 0x0000; //Reset value
  161. uart_set_bitrate(19200);// Set communication baudrate 115200
  162. *pUART_IER = ERBFI;
  163. // Enable UART clock
  164. *pUART_GCTL = UCEN;
  165. }
  166. int uart_put_char(const char c)
  167. {
  168. while (!(*pUART_LSR & THRE))
  169. {
  170. /* wait */
  171. }
  172. *pUART_THR = c;
  173. return c;
  174. }
  175. void rt_hw_console_output(const char *str)
  176. {
  177. while (*str != '\0')
  178. {
  179. if (*str == '\n')
  180. uart_put_char('\r');
  181. uart_put_char(*str++);
  182. }
  183. }
  184. EX_INTERRUPT_HANDLER(uart_rx_isr)
  185. {
  186. rt_interrupt_enter();
  187. rt_hw_serial_isr(&uart0_device);
  188. rt_interrupt_leave();
  189. }
  190. void rt_hw_isr_install(void)
  191. {
  192. *pSIC_IWR = 0xFFFFFFFF;
  193. *pSIC_IMASK = 0x00000000;
  194. *pSIC_IAR1 &= IVG_CLR(IAR1_DMA6_UARTRX_IVG);
  195. *pSIC_IAR1 |= IVG_SET(IAR1_DMA6_UARTRX_IVG,ik_ivg9);
  196. register_handler(ik_ivg9,uart_rx_isr);
  197. *pSIC_IMASK |= DMA6_UART_RX_INT_MASK;/* ¿ªÖÐ¶Ï */
  198. }
  199. void rt_hw_board_init(void)
  200. {
  201. rt_hw_pll_init();
  202. rt_hw_ebiu_init();
  203. rt_hw_exdram_init();
  204. rt_hw_uart_init();
  205. }