board.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * File : board.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, 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. * 2006-08-23 Bernard first implementation
  13. *
  14. * 2011-12-17 nl1031 for MacroBlaze
  15. *
  16. */
  17. #include <rtthread.h>
  18. #include <rthw.h>
  19. #include "xbasic_types.h"
  20. #include "board.h"
  21. #include "xgpio.h"
  22. #include "xparameters.h"
  23. #include "xuartlite.h"
  24. #include "xtmrctr.h"
  25. #include "xintc.h"
  26. #include "xstatus.h"
  27. #include "xuartlite_l.h"
  28. #include "xintc_l.h"
  29. #ifdef RT_USING_UART1
  30. #include "serial.h"
  31. #endif
  32. #define TIMER_CNTR_0 0
  33. #define PIV (XPAR_PROC_BUS_0_FREQ_HZ / RT_TICK_PER_SECOND)
  34. #define LEDS_DEVICE_ID XPAR_LEDS_4BITS_DEVICE_ID
  35. #define RS232_DEVICE_ID XPAR_USB_UART_DEVICE_ID
  36. #ifdef RT_USING_UART1
  37. #define USB_UART_BASE ((struct uartport *)XPAR_USB_UART_BASEADDR)
  38. #endif
  39. /* Global Variables: */
  40. XTmrCtr timer; /* The instance of the timer */
  41. XGpio gpio_output; /* The driver instance for GPIO Device configured as O/P */
  42. XUartLite uart_lite; /* Instance of the UartLite device */
  43. XIntc int_ctl; /* The instance of the Interrupt Controller */
  44. static rt_uint32_t led_data;
  45. static int cnt;
  46. static void rt_hw_board_led_init(void);
  47. /**
  48. * This function will init led on the board
  49. */
  50. static void rt_hw_board_led_init()
  51. {
  52. rt_uint32_t status;
  53. led_data = 0;
  54. cnt = 0;
  55. status = XGpio_Initialize(&gpio_output, LEDS_DEVICE_ID);
  56. if (status != XST_SUCCESS)
  57. {
  58. return;
  59. }
  60. /*
  61. * Set the direction for all signals to be outputs
  62. */
  63. XGpio_SetDataDirection(&gpio_output, 1, 0x0);
  64. /*
  65. * Set the GPIO outputs to high
  66. */
  67. XGpio_DiscreteWrite(&gpio_output, 1, 3);
  68. }
  69. /**
  70. * This function will take the led on board on.
  71. *
  72. * @param n the number nth led
  73. */
  74. void rt_hw_board_led_on(rt_uint32_t led)
  75. {
  76. led_data |= led;
  77. XGpio_DiscreteWrite(&gpio_output, 1, led_data);
  78. }
  79. /**
  80. * This function will take the led on board off.
  81. *
  82. * @param n the number nth led
  83. */
  84. void rt_hw_board_led_off(rt_uint32_t led)
  85. {
  86. led_data &= ~led;
  87. XGpio_DiscreteWrite(&gpio_output, 1, led_data);
  88. }
  89. void rt_hw_led_flash(void)
  90. {
  91. volatile rt_uint32_t i;
  92. rt_hw_board_led_off(1);
  93. for (i = 0; i < 20000; i ++);
  94. rt_hw_board_led_on(1);
  95. for (i = 0; i < 20000; i ++);
  96. }
  97. #ifdef RT_USING_CONSOLE
  98. /*
  99. * RT-Thread Console Interface, used by rt_kprintf
  100. */
  101. /**
  102. * This function is used to display a string on console, normally, it's
  103. * invoked by rt_kprintf
  104. *
  105. * @param str the displayed string
  106. */
  107. void rt_hw_console_output(const char* str)
  108. {
  109. while (*str)
  110. {
  111. /* Transmit Character */
  112. XUartLite_SendByte(STDOUT_BASEADDRESS, *str);
  113. if (*str == '\n')
  114. XUartLite_SendByte(STDOUT_BASEADDRESS, '\r');
  115. str++;
  116. }
  117. }
  118. static void rt_hw_console_init()
  119. {
  120. rt_uint32_t status;
  121. /*
  122. * Initialize the UartLite driver so that it is ready to use.
  123. */
  124. status = XUartLite_Initialize(&uart_lite, RS232_DEVICE_ID);
  125. if (status != XST_SUCCESS)
  126. {
  127. return;
  128. }
  129. }
  130. #endif
  131. void rt_hw_timer_handler(void)
  132. {
  133. rt_uint32_t csr;
  134. csr = XTmrCtr_ReadReg(timer.BaseAddress, TIMER_CNTR_0, XTC_TCSR_OFFSET);
  135. /*
  136. * Check if timer expired and interrupt occured
  137. */
  138. if (csr & XTC_CSR_INT_OCCURED_MASK)
  139. {
  140. rt_tick_increase();
  141. XTmrCtr_WriteReg(timer.BaseAddress, TIMER_CNTR_0, XTC_TCSR_OFFSET, csr | XTC_CSR_INT_OCCURED_MASK);
  142. }
  143. }
  144. /*
  145. *********************************************************************************************************
  146. * rt_intc_init()
  147. *
  148. * Description: This function intializes the interrupt controller by registering the appropriate handler
  149. * functions and enabling interrupts.
  150. *
  151. * Arguments : None
  152. *
  153. * Returns : None
  154. *********************************************************************************************************
  155. */
  156. void rt_intc_init (void)
  157. {
  158. XStatus status;
  159. XIntc_MasterDisable(XPAR_INTC_0_BASEADDR);
  160. status = XIntc_Initialize(&int_ctl, XPAR_INTC_0_DEVICE_ID);
  161. /* install interrupt handler */
  162. rt_hw_interrupt_install(XPAR_INTC_0_TMRCTR_0_VEC_ID, (rt_isr_handler_t)rt_hw_timer_handler, RT_NULL);
  163. rt_hw_interrupt_umask(XPAR_INTC_0_TMRCTR_0_VEC_ID);
  164. XIntc_Start(&int_ctl, XIN_REAL_MODE);
  165. }
  166. void rt_tmr_init (void)
  167. {
  168. rt_uint32_t ctl;
  169. XStatus status;
  170. status = XTmrCtr_Initialize(&timer,XPAR_AXI_TIMER_0_DEVICE_ID);
  171. XTmrCtr_WriteReg(timer.BaseAddress, TIMER_CNTR_0, XTC_TLR_OFFSET, PIV);
  172. ctl = XTC_CSR_ENABLE_TMR_MASK | XTC_CSR_ENABLE_INT_MASK | XTC_CSR_AUTO_RELOAD_MASK | XTC_CSR_DOWN_COUNT_MASK;
  173. XTmrCtr_WriteReg(timer.BaseAddress, TIMER_CNTR_0, XTC_TCSR_OFFSET, ctl);
  174. }
  175. /**
  176. * This function will initial SPARTAN 6 LX9 board.
  177. */
  178. void rt_hw_board_init()
  179. {
  180. microblaze_disable_icache();
  181. microblaze_disable_dcache();
  182. /* init hardware console */
  183. rt_hw_console_init();
  184. /* init led */
  185. rt_hw_board_led_init();
  186. /* init intc */
  187. rt_intc_init();
  188. /* timer init */
  189. rt_tmr_init();
  190. }