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