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 * 2010-03-09 ljt8015 Fix a bug in rt_hw_console_init()
14 */
15
16 #include <rtthread.h>
17 #include <rthw.h>
18
19 #include <AT91SAM7X256.h>
20 #include "board.h"
21
22 static void rt_hw_board_led_init(void);
23
24 /**
25 * @addtogroup sam7s
26 */
27 /*@{*/
28
29 /* Periodic Interval Value */
30 #define PIV (((MCK/16)/1000)*(1000/RT_TICK_PER_SECOND))
31
32 /**
33 * This is the timer interrupt service routine.
34 * @param vector the irq number for timer
35 */
rt_hw_timer_handler(int vector,void * param)36 void rt_hw_timer_handler(int vector, void* param)
37 {
38 if (AT91C_BASE_PITC->PITC_PISR & 0x01)
39 {
40 /* increase a tick */
41 rt_tick_increase();
42
43 /* ack interrupt */
44 AT91C_BASE_AIC->AIC_EOICR = AT91C_BASE_PITC->PITC_PIVR;
45 }
46 else
47 {
48 /* end of interrupt */
49 AT91C_BASE_AIC->AIC_EOICR = 0;
50 }
51 }
52 /* PIO Flash PA PB PIN */
53 #define LED1 (1 << 19) /* PA0 / PGMEN0 & PWM0 TIOA0 48 */
54 #define LED2 (1 << 20) /* PA1 / PGMEN1 & PWM1 TIOB0 47 */
55 #define LED3 (1 << 21) /* PA2 & PWM2 SCK0 44 */
56 #define LED4 (1 << 22) /* PA3 & TWD NPCS3 43 */
57 #define LED_MASK (LED1|LED2|LED3|LED4)
58
59 int leds[] = {LED1, LED2, LED3, LED4};
60
61 /**
62 * This function will init led on the board
63 */
rt_hw_board_led_init()64 static void rt_hw_board_led_init()
65 {
66 /* enable the clock of the PIO A, PIO B */
67 AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOA | 1 << AT91C_ID_PIOB;
68
69 /* configure PIO as output for led */
70 AT91C_BASE_PIOB->PIO_PER = LED_MASK;
71 AT91C_BASE_PIOB->PIO_OER = LED_MASK;
72 }
73
74 /**
75 * This function will take the led on board on.
76 *
77 * @param n the number nth led
78 */
rt_hw_board_led_on(int n)79 void rt_hw_board_led_on(int n)
80 {
81 if (n >= 0 && n < 4)
82 {
83 AT91C_BASE_PIOB->PIO_CODR = leds[n];
84 }
85 }
86
87 /**
88 * This function will take the led on board off.
89 *
90 * @param n the number nth led
91 */
rt_hw_board_led_off(int n)92 void rt_hw_board_led_off(int n)
93 {
94 if (n >= 0 && n < 4)
95 {
96 AT91C_BASE_PIOB->PIO_SODR = leds[n];
97 }
98 }
99
100 /*
101 * RT-Thread Console Interface, used by rt_kprintf
102 */
103 /**
104 * This function is used to display a string on console, normally, it's
105 * invoked by rt_kprintf
106 *
107 * @param str the displayed string
108 */
rt_hw_console_output(const char * str)109 void rt_hw_console_output(const char* str)
110 {
111 while (*str)
112 {
113 if (*str == '\n')
114 {
115 while (!(AT91C_BASE_US0->US_CSR & AT91C_US_TXRDY));
116 AT91C_BASE_US0->US_THR = '\r';
117 }
118
119 /* Wait for Empty Tx Buffer */
120 while (!(AT91C_BASE_US0->US_CSR & AT91C_US_TXRDY));
121 /* Transmit Character */
122 AT91C_BASE_US0->US_THR = *str;
123
124 str ++;
125 }
126 }
127
rt_hw_console_init()128 static void rt_hw_console_init()
129 {
130 /* Enable Clock for USART0 */
131 AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_US0;
132 /* Enable RxD0 and TxD0 Pin */
133 //AT91C_BASE_PIOA->PIO_PDR = (1 << 5) | (1 << 6);
134 AT91C_BASE_PIOA->PIO_PDR = 1 | (1 << 1);//fix bug 2010-3-9
135
136 AT91C_BASE_US0->US_CR = AT91C_US_RSTRX | /* Reset Receiver */
137 AT91C_US_RSTTX | /* Reset Transmitter */
138 AT91C_US_RXDIS | /* Receiver Disable */
139 AT91C_US_TXDIS; /* Transmitter Disable */
140
141 AT91C_BASE_US0->US_MR = AT91C_US_USMODE_NORMAL | /* Normal Mode */
142 AT91C_US_CLKS_CLOCK | /* Clock = MCK */
143 AT91C_US_CHRL_8_BITS | /* 8-bit Data */
144 AT91C_US_PAR_NONE | /* No Parity */
145 AT91C_US_NBSTOP_1_BIT; /* 1 Stop Bit */
146
147 /* set baud rate divisor */
148 AT91C_BASE_US0->US_BRGR = BRD;
149
150 AT91C_BASE_US0->US_CR = AT91C_US_RXEN | /* Receiver Enable */
151 AT91C_US_TXEN; /* Transmitter Enable */
152 }
153
154 /**
155 * This function will initial sam7x board.
156 */
rt_hw_board_init(void)157 void rt_hw_board_init(void)
158 {
159 extern void rt_serial_init(void);
160
161 /* init hardware console */
162 rt_hw_console_init();
163
164 /* init led */
165 rt_hw_board_led_init();
166
167 /* init PITC */
168 AT91C_BASE_PITC->PITC_PIMR = (1 << 25) | (1 << 24) | PIV;
169
170 /* install timer handler */
171 rt_hw_interrupt_install(AT91C_ID_SYS, rt_hw_timer_handler, RT_NULL, "tick");
172 AT91C_BASE_AIC->AIC_SMR[AT91C_ID_SYS] = 0;
173 rt_hw_interrupt_umask(AT91C_ID_SYS);
174 }
175 /*@}*/
176