]>
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 | 28 | |
f76ca346 | 29 | __attribute__ ((naked)) __attribute__ ((section (".init3"))) |
35e9ec0c | 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; | |
f76ca346 | 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 | ||
04a63b0d L |
80 | /* Disable peripherals. Enable individually in respective init function. */ |
81 | PRR0 = _BV(PRTWI) | | |
82 | _BV(PRTIM2) | _BV(PRTIM0) | _BV(PRTIM1) | | |
83 | _BV(PRSPI) | _BV(PRUSART0) | _BV(PRADC); | |
84 | ||
f76ca346 | 85 | PRR1 = _BV(PRTIM5) | _BV(PRTIM4) | _BV(PRTIM3) | |
d684c216 L |
86 | _BV(PRUSART3) | _BV(PRUSART2) | _BV(PRUSART1); |
87 | ||
04a63b0d | 88 | |
d684c216 L |
89 | /* disable analog comparator */ |
90 | ACSR = _BV(ACD); | |
91 | /* Ports */ | |
92 | ||
93 | /* Clock */ | |
94 | CLKPR = _BV(CLKPCE); | |
95 | CLKPR = 0; | |
96 | ||
97 | /* Timer */ | |
04a63b0d | 98 | PRR0 &= ~_BV(PRTIM2); |
68e463ad L |
99 | OCR2A = F_CPU / 256 / 1000 - 1; /* Timer2: 1000Hz interval (OC2A) */ |
100 | TCCR2A = (0b10 << WGM20); /* CTC Mode */ | |
101 | TCCR2B = (0b110 << CS20); /* Prescaler 256 */ | |
102 | TIMSK2 = _BV(OCIE2A); /* Enable TC2.oca interrupt */ | |
d684c216 L |
103 | } |
104 | ||
f338df2a | 105 | static |
35e9ec0c | 106 | int reset_reason_is_power_on(void) |
f338df2a | 107 | { |
35e9ec0c | 108 | return (mcusr & _BV(PORF)) != 0; |
f338df2a | 109 | } |
d684c216 | 110 | |
35e9ec0c | 111 | /*--------------------------------------------------------------------------*/ |
d684c216 L |
112 | |
113 | /* Stored value of bootdelay, used by autoboot_command() */ | |
114 | static int stored_bootdelay; | |
115 | ||
116 | ||
117 | /*************************************************************************** | |
118 | * Watch for 'delay' seconds for autoboot stop. | |
f76ca346 | 119 | * returns: 0 - no key, allow autoboot |
d684c216 L |
120 | * 1 - got key, abort |
121 | */ | |
122 | ||
123 | static int abortboot(int bootdelay) | |
124 | { | |
125 | int abort = 0; | |
126 | uint32_t ts; | |
127 | ||
128 | if (bootdelay >= 0) | |
129 | printf_P(PSTR("Hit any key to stop autoboot: %2d "), bootdelay); | |
130 | ||
131 | #if defined CONFIG_ZERO_BOOTDELAY_CHECK | |
132 | /* | |
133 | * Check if key already pressed | |
134 | * Don't check if bootdelay < 0 | |
135 | */ | |
136 | if (bootdelay >= 0) { | |
137 | if (tstc()) { /* we got a key press */ | |
138 | (void) my_getchar(); /* consume input */ | |
139 | my_puts_P(PSTR("\b\b\b 0")); | |
140 | abort = 1; /* don't auto boot */ | |
141 | } | |
142 | } | |
143 | #endif | |
144 | ||
145 | while ((bootdelay > 0) && (!abort)) { | |
146 | --bootdelay; | |
147 | /* delay 1000 ms */ | |
148 | ts = get_timer(0); | |
149 | do { | |
150 | if (tstc()) { /* we got a key press */ | |
151 | abort = 1; /* don't auto boot */ | |
152 | bootdelay = 0; /* no more delay */ | |
153 | break; | |
154 | } | |
155 | udelay(10000); | |
156 | } while (!abort && get_timer(ts) < 1000); | |
157 | ||
158 | printf_P(PSTR("\b\b\b%2d "), bootdelay); | |
159 | } | |
160 | ||
161 | putchar('\n'); | |
162 | ||
163 | return abort; | |
164 | } | |
165 | ||
f338df2a | 166 | static |
d684c216 L |
167 | const char *bootdelay_process(void) |
168 | { | |
169 | char *s; | |
170 | int bootdelay; | |
171 | ||
fc30b18e | 172 | bootdelay = (int) getenv_ulong(PSTR("bootdelay"), 10, CONFIG_BOOTDELAY); |
d684c216 L |
173 | |
174 | ||
175 | debug("### main_loop entered: bootdelay=%d\n\n", bootdelay); | |
176 | _delay_ms(20); | |
177 | ||
fc30b18e | 178 | s = getenv(PSTR("bootcmd")); |
d684c216 L |
179 | stored_bootdelay = bootdelay; |
180 | return s; | |
181 | } | |
182 | ||
f338df2a | 183 | static |
d684c216 L |
184 | void autoboot_command(const char *s) |
185 | { | |
186 | debug("### main_loop: bootcmd=\"%s\"\n", s ? s : PSTR("<UNDEFINED>")); | |
187 | _delay_ms(20); | |
188 | ||
189 | if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) { | |
190 | run_command_list(s, -1); | |
191 | } | |
192 | } | |
193 | ||
194 | ||
f338df2a | 195 | static |
d684c216 L |
196 | void main_loop(void) |
197 | { | |
198 | const char *s; | |
199 | ||
200 | s = bootdelay_process(); | |
201 | autoboot_command(s); | |
202 | cli_loop(); | |
203 | } | |
204 | ||
205 | int main(void) | |
206 | { | |
69988dc1 | 207 | |
d684c216 L |
208 | setup_avr(); |
209 | z80_setup_bus(); | |
35e9ec0c | 210 | |
f14850db L |
211 | env_init(); |
212 | ||
35e9ec0c | 213 | if (reset_reason_is_power_on()) |
f14850db | 214 | _delay_ms(CONFIG_PWRON_DELAY); |
f76ca346 | 215 | |
fc30b18e | 216 | serial_setup(getenv_ulong(PSTR("baudrate"), 10, CONFIG_BAUDRATE)); |
d684c216 | 217 | sei(); |
323398b1 | 218 | |
f338df2a | 219 | #if DEBUG |
69988dc1 | 220 | debug("\n=========================< (RE)START DEBUG >=========================\n"); |
f338df2a L |
221 | print_reset_reason(); |
222 | #endif | |
f76ca346 | 223 | |
35e9ec0c | 224 | #if DEBUG |
fc30b18e | 225 | unsigned long i_speed = getenv_ulong(PSTR("i2c_clock"), 10, CONFIG_SYS_I2C_CLOCK); |
35e9ec0c L |
226 | debug("### Setting I2C clock Frequency to %lu Hz.\n", i_speed); |
227 | i2c_init(i_speed); | |
228 | #else | |
229 | i2c_init(CONFIG_SYS_I2C_CLOCK); | |
230 | #endif | |
d684c216 L |
231 | |
232 | printf_P(PSTR("\n(ATMEGA1281+HD64180)_stamp Tester\n")); | |
f76ca346 L |
233 | |
234 | ||
d684c216 L |
235 | main_loop(); |
236 | } |