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