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