board.c 5.4 KB

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