board.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 MicroBlaze
  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 void rt_hw_board_led_init(void);
  46. /**
  47. * This function will init led on the board
  48. */
  49. static void rt_hw_board_led_init()
  50. {
  51. rt_uint32_t status;
  52. led_data = 0;
  53. status = XGpio_Initialize(&gpio_output, LEDS_DEVICE_ID);
  54. if (status != XST_SUCCESS)
  55. {
  56. return;
  57. }
  58. /*
  59. * Set the direction for all signals to be outputs
  60. */
  61. XGpio_SetDataDirection(&gpio_output, 1, 0x0);
  62. /*
  63. * Set the GPIO outputs to high
  64. */
  65. XGpio_DiscreteWrite(&gpio_output, 1, 3);
  66. }
  67. /**
  68. * This function will take the led on board on.
  69. *
  70. * @param n the number nth led
  71. */
  72. void rt_hw_board_led_on(rt_uint32_t led)
  73. {
  74. led_data |= led;
  75. XGpio_DiscreteWrite(&gpio_output, 1, led_data);
  76. }
  77. /**
  78. * This function will take the led on board off.
  79. *
  80. * @param n the number nth led
  81. */
  82. void rt_hw_board_led_off(rt_uint32_t led)
  83. {
  84. led_data &= ~led;
  85. XGpio_DiscreteWrite(&gpio_output, 1, led_data);
  86. }
  87. void rt_hw_led_flash(void)
  88. {
  89. rt_uint32_t i;
  90. rt_hw_board_led_off(1);
  91. for (i = 0; i < 20000; i++) ;
  92. rt_hw_board_led_on(1);
  93. for (i = 0; i < 20000; i++) ;
  94. }
  95. #ifdef RT_USING_CONSOLE
  96. /*
  97. * RT-Thread Console Interface, used by rt_kprintf
  98. */
  99. /**
  100. * This function is used to display a string on console, normally, it's
  101. * invoked by rt_kprintf
  102. *
  103. * @param str the displayed string
  104. */
  105. void rt_hw_console_output(const char* str)
  106. {
  107. while (*str)
  108. {
  109. /* Transmit Character */
  110. XUartLite_SendByte(STDOUT_BASEADDRESS, *str);
  111. if (*str == '\n')
  112. XUartLite_SendByte(STDOUT_BASEADDRESS, '\r');
  113. str++;
  114. }
  115. }
  116. static void rt_hw_console_init()
  117. {
  118. rt_uint32_t status;
  119. /*
  120. * Initialize the UartLite driver so that it is ready to use.
  121. */
  122. status = XUartLite_Initialize(&uart_lite, RS232_DEVICE_ID);
  123. if (status != XST_SUCCESS)
  124. {
  125. return;
  126. }
  127. }
  128. #endif
  129. void rt_hw_timer_handler(void)
  130. {
  131. rt_uint32_t csr;
  132. csr = XTmrCtr_ReadReg(timer.BaseAddress, TIMER_CNTR_0, XTC_TCSR_OFFSET);
  133. /*
  134. * Check if timer expired and interrupt occured
  135. */
  136. if (csr & XTC_CSR_INT_OCCURED_MASK)
  137. {
  138. rt_tick_increase();
  139. XTmrCtr_WriteReg(timer.BaseAddress, TIMER_CNTR_0, XTC_TCSR_OFFSET, csr | XTC_CSR_INT_OCCURED_MASK);
  140. }
  141. }
  142. /*
  143. *********************************************************************************************************
  144. * rt_intc_init()
  145. *
  146. * Description: This function intializes the interrupt controller by registering the appropriate handler
  147. * functions and enabling interrupts.
  148. *
  149. * Arguments : None
  150. *
  151. * Returns : None
  152. *********************************************************************************************************
  153. */
  154. void rt_intc_init(void)
  155. {
  156. XStatus status;
  157. XIntc_MasterDisable(XPAR_INTC_0_BASEADDR);
  158. status = XIntc_Initialize(&int_ctl, XPAR_INTC_0_DEVICE_ID);
  159. /* install interrupt handler */
  160. rt_hw_interrupt_install(XPAR_INTC_0_TMRCTR_0_VEC_ID, (rt_isr_handler_t) rt_hw_timer_handler, RT_NULL);
  161. rt_hw_interrupt_umask(XPAR_INTC_0_TMRCTR_0_VEC_ID);
  162. XIntc_Start(&int_ctl, XIN_REAL_MODE);
  163. }
  164. void rt_tmr_init(void)
  165. {
  166. rt_uint32_t ctl;
  167. XStatus status;
  168. status = XTmrCtr_Initialize(&timer, XPAR_AXI_TIMER_0_DEVICE_ID);
  169. XTmrCtr_WriteReg(timer.BaseAddress, TIMER_CNTR_0, XTC_TLR_OFFSET, PIV);
  170. ctl = XTC_CSR_ENABLE_TMR_MASK | XTC_CSR_ENABLE_INT_MASK | XTC_CSR_AUTO_RELOAD_MASK | XTC_CSR_DOWN_COUNT_MASK;
  171. XTmrCtr_WriteReg(timer.BaseAddress, TIMER_CNTR_0, XTC_TCSR_OFFSET, ctl);
  172. }
  173. /**
  174. * This function will initial SPARTAN 6 LX9 board.
  175. */
  176. void rt_hw_board_init()
  177. {
  178. /* init hardware console */
  179. rt_hw_console_init();
  180. /* init led */
  181. rt_hw_board_led_init();
  182. /* init intc */
  183. rt_intc_init();
  184. /* timer init */
  185. rt_tmr_init();
  186. }