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