]>
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" |
c79c80b4 | 21 | #include "gpio.h" |
be5cfb4b L |
22 | #include "time.h" |
23 | #include "rtc.h" | |
d684c216 | 24 | |
35e9ec0c L |
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 | 114 | /* INT5, INT6: falling edge */ |
7f552300 | 115 | EICRB = (EICRB & ~((0b11 << ISC50) | (0b11 << ISC60))) | |
89adce76 L |
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 | |
be5cfb4b L |
129 | static |
130 | void setup_system_time(void) | |
131 | { | |
132 | struct tm rtc_time; | |
133 | ||
134 | rtc_get(&rtc_time); | |
135 | rtc_time.tm_isdst = 0; | |
136 | set_system_time(mk_gmtime(&rtc_time) ); | |
137 | } | |
138 | ||
35e9ec0c | 139 | /*--------------------------------------------------------------------------*/ |
d684c216 L |
140 | |
141 | /* Stored value of bootdelay, used by autoboot_command() */ | |
142 | static int stored_bootdelay; | |
143 | ||
144 | ||
145 | /*************************************************************************** | |
146 | * Watch for 'delay' seconds for autoboot stop. | |
f76ca346 | 147 | * returns: 0 - no key, allow autoboot |
d684c216 L |
148 | * 1 - got key, abort |
149 | */ | |
150 | ||
151 | static int abortboot(int bootdelay) | |
152 | { | |
153 | int abort = 0; | |
154 | uint32_t ts; | |
155 | ||
156 | if (bootdelay >= 0) | |
157 | printf_P(PSTR("Hit any key to stop autoboot: %2d "), bootdelay); | |
158 | ||
159 | #if defined CONFIG_ZERO_BOOTDELAY_CHECK | |
160 | /* | |
161 | * Check if key already pressed | |
162 | * Don't check if bootdelay < 0 | |
163 | */ | |
164 | if (bootdelay >= 0) { | |
165 | if (tstc()) { /* we got a key press */ | |
424b184a | 166 | (void) my_getchar(1); /* consume input */ |
d684c216 L |
167 | my_puts_P(PSTR("\b\b\b 0")); |
168 | abort = 1; /* don't auto boot */ | |
169 | } | |
170 | } | |
171 | #endif | |
172 | ||
173 | while ((bootdelay > 0) && (!abort)) { | |
174 | --bootdelay; | |
175 | /* delay 1000 ms */ | |
176 | ts = get_timer(0); | |
177 | do { | |
178 | if (tstc()) { /* we got a key press */ | |
179 | abort = 1; /* don't auto boot */ | |
180 | bootdelay = 0; /* no more delay */ | |
181 | break; | |
182 | } | |
183 | udelay(10000); | |
184 | } while (!abort && get_timer(ts) < 1000); | |
185 | ||
186 | printf_P(PSTR("\b\b\b%2d "), bootdelay); | |
187 | } | |
188 | ||
189 | putchar('\n'); | |
190 | ||
191 | return abort; | |
192 | } | |
193 | ||
f338df2a | 194 | static |
d684c216 L |
195 | const char *bootdelay_process(void) |
196 | { | |
197 | char *s; | |
198 | int bootdelay; | |
199 | ||
70c99491 | 200 | bootdelay = (int) getenv_ulong(PSTR(ENV_BOOTDELAY), 10, CONFIG_BOOTDELAY); |
d684c216 L |
201 | |
202 | ||
203 | debug("### main_loop entered: bootdelay=%d\n\n", bootdelay); | |
204 | _delay_ms(20); | |
205 | ||
70c99491 | 206 | s = getenv(PSTR(ENV_BOOTCMD)); |
d684c216 L |
207 | stored_bootdelay = bootdelay; |
208 | return s; | |
209 | } | |
210 | ||
f338df2a | 211 | static |
d684c216 L |
212 | void autoboot_command(const char *s) |
213 | { | |
214 | debug("### main_loop: bootcmd=\"%s\"\n", s ? s : PSTR("<UNDEFINED>")); | |
215 | _delay_ms(20); | |
216 | ||
217 | if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) { | |
218 | run_command_list(s, -1); | |
219 | } | |
220 | } | |
221 | ||
222 | ||
f338df2a | 223 | static |
d684c216 L |
224 | void main_loop(void) |
225 | { | |
226 | const char *s; | |
227 | ||
228 | s = bootdelay_process(); | |
229 | autoboot_command(s); | |
230 | cli_loop(); | |
231 | } | |
232 | ||
233 | int main(void) | |
234 | { | |
15e476bc L |
235 | extern void setup_mmc(void); |
236 | ||
c79c80b4 L |
237 | for (int i = 0; i < GPIO_MAX; i++) |
238 | gpio_config(i, INPUT_PULLUP); | |
15e476bc L |
239 | setup_avr(); |
240 | setup_mmc(); | |
241 | z80_setup_bus(); | |
f14850db L |
242 | env_init(); |
243 | ||
35e9ec0c | 244 | if (reset_reason_is_power_on()) |
f14850db | 245 | _delay_ms(CONFIG_PWRON_DELAY); |
f76ca346 | 246 | |
70c99491 | 247 | serial_setup(getenv_ulong(PSTR(ENV_BAUDRATE), 10, CONFIG_BAUDRATE)); |
d684c216 | 248 | sei(); |
323398b1 | 249 | |
f338df2a | 250 | #if DEBUG |
69988dc1 | 251 | debug("\n=========================< (RE)START DEBUG >=========================\n"); |
f338df2a L |
252 | print_reset_reason(); |
253 | #endif | |
f76ca346 | 254 | |
35e9ec0c | 255 | #if DEBUG |
fc30b18e | 256 | unsigned long i_speed = getenv_ulong(PSTR("i2c_clock"), 10, CONFIG_SYS_I2C_CLOCK); |
35e9ec0c L |
257 | debug("### Setting I2C clock Frequency to %lu Hz.\n", i_speed); |
258 | i2c_init(i_speed); | |
259 | #else | |
260 | i2c_init(CONFIG_SYS_I2C_CLOCK); | |
261 | #endif | |
be5cfb4b | 262 | setup_system_time(); |
d684c216 | 263 | |
cd5ee544 | 264 | printf_P(PSTR("\nATMEGA1281+Z8S180 Stamp Monitor\n\n")); |
f76ca346 | 265 | |
89adce76 | 266 | setup_z180_serv(); |
f76ca346 | 267 | |
d684c216 L |
268 | main_loop(); |
269 | } |