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