]>
Commit | Line | Data |
---|---|---|
d684c216 L |
1 | /* |
2 | */ | |
3 | ||
4 | ||
5 | #include "common.h" | |
6 | ||
7 | #include <util/delay.h> | |
d684c216 | 8 | #include <avr/interrupt.h> |
d684c216 L |
9 | #include <stdlib.h> |
10 | #include <stdio.h> | |
11 | ||
d684c216 L |
12 | #include "config.h" |
13 | #include "debug.h" | |
14 | #include "z80-if.h" | |
61b0cfe9 | 15 | #include "i2c.h" |
d684c216 L |
16 | #include "con-utils.h" |
17 | #include "serial.h" | |
18 | #include "timer.h" | |
19 | #include "cli.h" | |
20 | #include "env.h" | |
21 | ||
35e9ec0c L |
22 | #define udelay(n) _delay_us(n) |
23 | ||
24 | static uint8_t mcusr; | |
25 | ||
69988dc1 L |
26 | /*--------------------------------------------------------------------------*/ |
27 | #if DEBUG | |
35e9ec0c L |
28 | |
29 | __attribute__ ((naked)) __attribute__ ((section (".init3"))) | |
30 | void preset_ram (void) | |
69988dc1 L |
31 | { |
32 | for (uint8_t *p = RAMSTART; p <= (uint8_t *) RAMEND; p++) | |
33 | *p = 0xdd; | |
34 | ||
35 | } | |
d684c216 | 36 | |
35e9ec0c L |
37 | static const FLASH char * const FLASH rreasons[] = { |
38 | FSTR("Power on"), | |
39 | FSTR("External"), | |
40 | FSTR("Brown out"), | |
41 | FSTR("Watchdog"), | |
42 | FSTR("JTAG"), | |
43 | }; | |
44 | ||
45 | static | |
46 | void print_reset_reason(void) | |
47 | { | |
48 | uint8_t r = mcusr & 0x1f; | |
49 | const FLASH char * const FLASH *p = rreasons; | |
50 | ||
51 | printf_P(PSTR("### Reset reason(s): %s"), r ? "" : "none"); | |
52 | for ( ; r; p++, r >>= 1) { | |
53 | if (r & 1) { | |
54 | my_puts_P(*p); | |
55 | if (r & ~1) | |
56 | printf_P(PSTR(", ")); | |
57 | } | |
58 | } | |
59 | printf_P(PSTR(".\n")); | |
60 | } | |
61 | ||
62 | #endif | |
f338df2a L |
63 | |
64 | static | |
d684c216 L |
65 | void setup_avr(void) |
66 | { | |
f338df2a | 67 | /* save and clear reset reason(s) */ |
35e9ec0c | 68 | /* TODO: move to init section? */ |
f338df2a L |
69 | mcusr = MCUSR; |
70 | MCUSR = 0; | |
71 | ||
d684c216 L |
72 | /* WD */ |
73 | ||
74 | /* CPU */ | |
75 | ||
76 | /* Disable JTAG Interface regardless of the JTAGEN fuse setting. */ | |
77 | MCUCR = _BV(JTD); | |
78 | MCUCR = _BV(JTD); | |
79 | ||
80 | /* disable unused periphels */ | |
81 | PRR0 = _BV(PRTIM2) | _BV(PRTIM0) | _BV(PRADC); | |
82 | PRR1 = _BV(PRTIM5) | _BV(PRTIM4) | _BV(PRTIM3) | | |
83 | _BV(PRUSART3) | _BV(PRUSART2) | _BV(PRUSART1); | |
84 | ||
85 | /* disable analog comparator */ | |
86 | ACSR = _BV(ACD); | |
87 | /* Ports */ | |
88 | ||
89 | /* Clock */ | |
90 | CLKPR = _BV(CLKPCE); | |
91 | CLKPR = 0; | |
92 | ||
93 | /* Timer */ | |
94 | ||
95 | OCR1A = F_CPU / 8 / 1000 - 1; // Timer1: 1000Hz interval (OC1A) | |
96 | TCCR1B = 0b00001010; | |
97 | TIMSK1 = _BV(OCIE1A); // Enable TC1.oca interrupt | |
98 | } | |
99 | ||
f338df2a | 100 | static |
35e9ec0c | 101 | int reset_reason_is_power_on(void) |
f338df2a | 102 | { |
35e9ec0c | 103 | return (mcusr & _BV(PORF)) != 0; |
f338df2a | 104 | } |
d684c216 | 105 | |
35e9ec0c | 106 | /*--------------------------------------------------------------------------*/ |
d684c216 L |
107 | |
108 | /* Stored value of bootdelay, used by autoboot_command() */ | |
109 | static int stored_bootdelay; | |
110 | ||
111 | ||
112 | /*************************************************************************** | |
113 | * Watch for 'delay' seconds for autoboot stop. | |
114 | * returns: 0 - no key, allow autoboot | |
115 | * 1 - got key, abort | |
116 | */ | |
117 | ||
118 | static int abortboot(int bootdelay) | |
119 | { | |
120 | int abort = 0; | |
121 | uint32_t ts; | |
122 | ||
123 | if (bootdelay >= 0) | |
124 | printf_P(PSTR("Hit any key to stop autoboot: %2d "), bootdelay); | |
125 | ||
126 | #if defined CONFIG_ZERO_BOOTDELAY_CHECK | |
127 | /* | |
128 | * Check if key already pressed | |
129 | * Don't check if bootdelay < 0 | |
130 | */ | |
131 | if (bootdelay >= 0) { | |
132 | if (tstc()) { /* we got a key press */ | |
133 | (void) my_getchar(); /* consume input */ | |
134 | my_puts_P(PSTR("\b\b\b 0")); | |
135 | abort = 1; /* don't auto boot */ | |
136 | } | |
137 | } | |
138 | #endif | |
139 | ||
140 | while ((bootdelay > 0) && (!abort)) { | |
141 | --bootdelay; | |
142 | /* delay 1000 ms */ | |
143 | ts = get_timer(0); | |
144 | do { | |
145 | if (tstc()) { /* we got a key press */ | |
146 | abort = 1; /* don't auto boot */ | |
147 | bootdelay = 0; /* no more delay */ | |
148 | break; | |
149 | } | |
150 | udelay(10000); | |
151 | } while (!abort && get_timer(ts) < 1000); | |
152 | ||
153 | printf_P(PSTR("\b\b\b%2d "), bootdelay); | |
154 | } | |
155 | ||
156 | putchar('\n'); | |
157 | ||
158 | return abort; | |
159 | } | |
160 | ||
f338df2a | 161 | static |
d684c216 L |
162 | const char *bootdelay_process(void) |
163 | { | |
164 | char *s; | |
165 | int bootdelay; | |
166 | ||
35e9ec0c | 167 | bootdelay = (int) getenv_ulong("bootdelay", 10, CONFIG_BOOTDELAY); |
d684c216 L |
168 | |
169 | ||
170 | debug("### main_loop entered: bootdelay=%d\n\n", bootdelay); | |
171 | _delay_ms(20); | |
172 | ||
173 | s = getenv("bootcmd"); | |
174 | stored_bootdelay = bootdelay; | |
175 | return s; | |
176 | } | |
177 | ||
f338df2a | 178 | static |
d684c216 L |
179 | void autoboot_command(const char *s) |
180 | { | |
181 | debug("### main_loop: bootcmd=\"%s\"\n", s ? s : PSTR("<UNDEFINED>")); | |
182 | _delay_ms(20); | |
183 | ||
184 | if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) { | |
185 | run_command_list(s, -1); | |
186 | } | |
187 | } | |
188 | ||
189 | ||
f338df2a | 190 | static |
d684c216 L |
191 | void main_loop(void) |
192 | { | |
193 | const char *s; | |
194 | ||
195 | s = bootdelay_process(); | |
196 | autoboot_command(s); | |
197 | cli_loop(); | |
198 | } | |
199 | ||
200 | int main(void) | |
201 | { | |
69988dc1 | 202 | |
d684c216 L |
203 | setup_avr(); |
204 | z80_setup_bus(); | |
35e9ec0c L |
205 | |
206 | if (reset_reason_is_power_on()) | |
207 | _delay_ms(CONFIG_POWRON_DELAY); | |
d684c216 L |
208 | |
209 | serial_setup(); | |
210 | sei(); | |
323398b1 | 211 | |
f338df2a | 212 | #if DEBUG |
69988dc1 | 213 | debug("\n=========================< (RE)START DEBUG >=========================\n"); |
f338df2a L |
214 | print_reset_reason(); |
215 | #endif | |
323398b1 | 216 | |
d684c216 | 217 | env_init(); |
35e9ec0c L |
218 | |
219 | #if DEBUG | |
220 | unsigned long i_speed = getenv_ulong("i2c_clock", 10, CONFIG_SYS_I2C_CLOCK); | |
221 | debug("### Setting I2C clock Frequency to %lu Hz.\n", i_speed); | |
222 | i2c_init(i_speed); | |
223 | #else | |
224 | i2c_init(CONFIG_SYS_I2C_CLOCK); | |
225 | #endif | |
d684c216 L |
226 | |
227 | printf_P(PSTR("\n(ATMEGA1281+HD64180)_stamp Tester\n")); | |
228 | ||
229 | main_loop(); | |
230 | } |