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