]>
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 | ||
209025e8 L |
70 | my_puts_P(PSTR("### Reset reason(s): ")); |
71 | if (r == 0) | |
72 | my_puts_P(PSTR("none")); | |
35e9ec0c L |
73 | for ( ; r; p++, r >>= 1) { |
74 | if (r & 1) { | |
75 | my_puts_P(*p); | |
76 | if (r & ~1) | |
209025e8 | 77 | my_puts_P(PSTR(", ")); |
35e9ec0c L |
78 | } |
79 | } | |
209025e8 | 80 | my_puts_P(PSTR(".\n")); |
35e9ec0c L |
81 | } |
82 | ||
83 | #endif | |
f338df2a | 84 | |
bad2d92d L |
85 | ISR(INT5_vect) |
86 | { | |
87 | Stat |= S_MSG_PENDING; | |
88 | } | |
89 | ||
89adce76 L |
90 | ISR(INT6_vect) |
91 | { | |
92 | Stat |= S_CON_PENDING; | |
93 | } | |
94 | ||
f338df2a | 95 | static |
d684c216 L |
96 | void setup_avr(void) |
97 | { | |
d684c216 L |
98 | /* CPU */ |
99 | ||
100 | /* Disable JTAG Interface regardless of the JTAGEN fuse setting. */ | |
101 | MCUCR = _BV(JTD); | |
102 | MCUCR = _BV(JTD); | |
103 | ||
04a63b0d L |
104 | /* Disable peripherals. Enable individually in respective init function. */ |
105 | PRR0 = _BV(PRTWI) | | |
106 | _BV(PRTIM2) | _BV(PRTIM0) | _BV(PRTIM1) | | |
107 | _BV(PRSPI) | _BV(PRUSART0) | _BV(PRADC); | |
108 | ||
f76ca346 | 109 | PRR1 = _BV(PRTIM5) | _BV(PRTIM4) | _BV(PRTIM3) | |
d684c216 L |
110 | _BV(PRUSART3) | _BV(PRUSART2) | _BV(PRUSART1); |
111 | ||
04a63b0d | 112 | |
d684c216 L |
113 | /* disable analog comparator */ |
114 | ACSR = _BV(ACD); | |
115 | /* Ports */ | |
116 | ||
117 | /* Clock */ | |
118 | CLKPR = _BV(CLKPCE); | |
119 | CLKPR = 0; | |
120 | ||
121 | /* Timer */ | |
57307890 L |
122 | PRR1 &= ~_BV(PRTIM4); |
123 | OCR4A = F_CPU / 1000 - 1; /* Timer4: 1000Hz interval */ | |
124 | TCCR4B = (0b00<<WGM42)|(0b001<<CS40); /* Normal Mode, Prescaler 1 */ | |
125 | TIMSK4 = _BV(OCIE4A); /* Enable Output Compare A interrupt */ | |
bad2d92d | 126 | |
89adce76 | 127 | /* INT5, INT6: falling edge */ |
7f552300 | 128 | EICRB = (EICRB & ~((0b11 << ISC50) | (0b11 << ISC60))) | |
89adce76 L |
129 | (0b10 << ISC50) | (0b10 << ISC60); |
130 | /* Reset pending ints */ | |
131 | EIFR |= _BV(INTF5) | _BV(INTF6); | |
132 | /* Enable INT5, and INT6 */ | |
133 | EIMSK |= _BV(INT5) | _BV(INT6); | |
d684c216 L |
134 | } |
135 | ||
f338df2a | 136 | static |
35e9ec0c | 137 | int reset_reason_is_power_on(void) |
f338df2a | 138 | { |
35e9ec0c | 139 | return (mcusr & _BV(PORF)) != 0; |
f338df2a | 140 | } |
d684c216 | 141 | |
be5cfb4b L |
142 | static |
143 | void setup_system_time(void) | |
144 | { | |
145 | struct tm rtc_time; | |
146 | ||
147 | rtc_get(&rtc_time); | |
148 | rtc_time.tm_isdst = 0; | |
149 | set_system_time(mk_gmtime(&rtc_time) ); | |
150 | } | |
151 | ||
bbd45c46 | 152 | |
35e9ec0c | 153 | /*--------------------------------------------------------------------------*/ |
d684c216 L |
154 | |
155 | /* Stored value of bootdelay, used by autoboot_command() */ | |
156 | static int stored_bootdelay; | |
157 | ||
158 | ||
159 | /*************************************************************************** | |
160 | * Watch for 'delay' seconds for autoboot stop. | |
f76ca346 | 161 | * returns: 0 - no key, allow autoboot |
d684c216 L |
162 | * 1 - got key, abort |
163 | */ | |
164 | ||
165 | static int abortboot(int bootdelay) | |
166 | { | |
167 | int abort = 0; | |
168 | uint32_t ts; | |
169 | ||
170 | if (bootdelay >= 0) | |
171 | printf_P(PSTR("Hit any key to stop autoboot: %2d "), bootdelay); | |
172 | ||
173 | #if defined CONFIG_ZERO_BOOTDELAY_CHECK | |
174 | /* | |
175 | * Check if key already pressed | |
176 | * Don't check if bootdelay < 0 | |
177 | */ | |
178 | if (bootdelay >= 0) { | |
179 | if (tstc()) { /* we got a key press */ | |
424b184a | 180 | (void) my_getchar(1); /* consume input */ |
d684c216 L |
181 | my_puts_P(PSTR("\b\b\b 0")); |
182 | abort = 1; /* don't auto boot */ | |
183 | } | |
184 | } | |
185 | #endif | |
186 | ||
187 | while ((bootdelay > 0) && (!abort)) { | |
188 | --bootdelay; | |
189 | /* delay 1000 ms */ | |
190 | ts = get_timer(0); | |
191 | do { | |
192 | if (tstc()) { /* we got a key press */ | |
193 | abort = 1; /* don't auto boot */ | |
194 | bootdelay = 0; /* no more delay */ | |
195 | break; | |
196 | } | |
197 | udelay(10000); | |
198 | } while (!abort && get_timer(ts) < 1000); | |
199 | ||
200 | printf_P(PSTR("\b\b\b%2d "), bootdelay); | |
201 | } | |
202 | ||
203 | putchar('\n'); | |
204 | ||
205 | return abort; | |
206 | } | |
207 | ||
f338df2a | 208 | static |
d684c216 L |
209 | const char *bootdelay_process(void) |
210 | { | |
211 | char *s; | |
212 | int bootdelay; | |
213 | ||
70c99491 | 214 | bootdelay = (int) getenv_ulong(PSTR(ENV_BOOTDELAY), 10, CONFIG_BOOTDELAY); |
d684c216 L |
215 | |
216 | ||
217 | debug("### main_loop entered: bootdelay=%d\n\n", bootdelay); | |
218 | _delay_ms(20); | |
219 | ||
bddc7e77 | 220 | s = getenv_str(PSTR(ENV_BOOTCMD)); |
d684c216 L |
221 | stored_bootdelay = bootdelay; |
222 | return s; | |
223 | } | |
224 | ||
f338df2a | 225 | static |
d684c216 L |
226 | void autoboot_command(const char *s) |
227 | { | |
209025e8 | 228 | debug("### main_loop: bootcmd=\"%s\"\n", s ? s : ""); |
d684c216 L |
229 | _delay_ms(20); |
230 | ||
231 | if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) { | |
232 | run_command_list(s, -1); | |
233 | } | |
234 | } | |
235 | ||
236 | ||
f338df2a | 237 | static |
d684c216 L |
238 | void main_loop(void) |
239 | { | |
240 | const char *s; | |
241 | ||
242 | s = bootdelay_process(); | |
243 | autoboot_command(s); | |
244 | cli_loop(); | |
245 | } | |
246 | ||
247 | int main(void) | |
248 | { | |
15e476bc L |
249 | extern void setup_mmc(void); |
250 | ||
fb9b17a9 | 251 | __malloc_margin = CONFIG_SYS_MALLOC_MARGIN; |
a1595a8e | 252 | setup_avr(); |
c79c80b4 L |
253 | for (int i = 0; i < GPIO_MAX; i++) |
254 | gpio_config(i, INPUT_PULLUP); | |
15e476bc | 255 | setup_mmc(); |
f14850db | 256 | env_init(); |
a1595a8e | 257 | z80_setup_bus(); |
f14850db | 258 | |
35e9ec0c | 259 | if (reset_reason_is_power_on()) |
f14850db | 260 | _delay_ms(CONFIG_PWRON_DELAY); |
f76ca346 | 261 | |
70c99491 | 262 | serial_setup(getenv_ulong(PSTR(ENV_BAUDRATE), 10, CONFIG_BAUDRATE)); |
d684c216 | 263 | sei(); |
323398b1 | 264 | |
f338df2a | 265 | #if DEBUG |
69988dc1 | 266 | debug("\n=========================< (RE)START DEBUG >=========================\n"); |
f338df2a L |
267 | print_reset_reason(); |
268 | #endif | |
f76ca346 | 269 | |
35e9ec0c | 270 | i2c_init(CONFIG_SYS_I2C_CLOCK); |
be5cfb4b | 271 | setup_system_time(); |
bbd45c46 | 272 | setup_fatfs(); |
d684c216 | 273 | |
e4344855 | 274 | printf_P(PSTR("\n" MCU_STRING "+Z8S180 Stamp Monitor - Version: " VERSION " \n\n")); |
f76ca346 | 275 | |
89adce76 | 276 | setup_z180_serv(); |
f76ca346 | 277 | |
d684c216 L |
278 | main_loop(); |
279 | } |