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