]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de> | |
3 | * | |
4 | * SPDX-License-Identifier: GPL-2.0 | |
5 | */ | |
6 | ||
7 | ||
8 | #include "common.h" | |
9 | #include <avr/interrupt.h> | |
10 | #include <avr/wdt.h> | |
11 | #include <stdlib.h> | |
12 | ||
13 | #include "config.h" | |
14 | #include "ff.h" | |
15 | #include "z80-if.h" | |
16 | #include "i2c.h" | |
17 | #include "con-utils.h" | |
18 | #include "serial.h" | |
19 | #include "timer.h" | |
20 | #include "cli.h" | |
21 | #include "env.h" | |
22 | #include "z180-serv.h" | |
23 | #include "gpio.h" | |
24 | #include "time.h" | |
25 | #include "rtc.h" | |
26 | #include "debug.h" | |
27 | #include "cmd_fat.h" | |
28 | ||
29 | ||
30 | uint8_t mcusr __attribute__ ((section (".noinit"))); | |
31 | ||
32 | #if DEBUG | |
33 | __attribute__ ((naked)) __attribute__ ((section (".init3"))) | |
34 | void preset_ram (void) | |
35 | { | |
36 | for (uint8_t *p = (uint8_t *) RAMSTART; p <= (uint8_t *) RAMEND; p++) | |
37 | *p = 0xdd; | |
38 | ||
39 | } | |
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 | |
55 | ||
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 | |
82 | ||
83 | ISR(INT5_vect) | |
84 | { | |
85 | Stat |= S_MSG_PENDING; | |
86 | } | |
87 | ||
88 | ISR(INT6_vect) | |
89 | { | |
90 | Stat |= S_CON_PENDING; | |
91 | } | |
92 | ||
93 | static | |
94 | void setup_avr(void) | |
95 | { | |
96 | /* CPU */ | |
97 | ||
98 | /* Disable JTAG Interface regardless of the JTAGEN fuse setting. */ | |
99 | MCUCR = _BV(JTD); | |
100 | MCUCR = _BV(JTD); | |
101 | ||
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 | ||
107 | PRR1 = _BV(PRTIM5) | _BV(PRTIM4) | _BV(PRTIM3) | | |
108 | _BV(PRUSART3) | _BV(PRUSART2) | _BV(PRUSART1); | |
109 | ||
110 | ||
111 | /* disable analog comparator */ | |
112 | ACSR = _BV(ACD); | |
113 | /* Ports */ | |
114 | ||
115 | /* Clock */ | |
116 | CLKPR = _BV(CLKPCE); | |
117 | CLKPR = 0; | |
118 | ||
119 | /* Timer */ | |
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 */ | |
124 | ||
125 | /* INT5, INT6: falling edge */ | |
126 | EICRB = (EICRB & ~((0b11 << ISC50) | (0b11 << ISC60))) | | |
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); | |
132 | } | |
133 | ||
134 | static | |
135 | int reset_reason_is_power_on(void) | |
136 | { | |
137 | return (mcusr & _BV(PORF)) != 0; | |
138 | } | |
139 | ||
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 | ||
150 | ||
151 | /*--------------------------------------------------------------------------*/ | |
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. | |
159 | * returns: 0 - no key, allow autoboot | |
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 */ | |
178 | (void) my_getchar(1); /* consume input */ | |
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 | ||
206 | static | |
207 | const char *bootdelay_process(void) | |
208 | { | |
209 | char *s; | |
210 | int bootdelay; | |
211 | ||
212 | bootdelay = (int) getenv_ulong(PSTR(ENV_BOOTDELAY), 10, CONFIG_BOOTDELAY); | |
213 | ||
214 | ||
215 | debug("### main_loop entered: bootdelay=%d\n\n", bootdelay); | |
216 | _delay_ms(20); | |
217 | ||
218 | s = getenv_str(PSTR(ENV_BOOTCMD)); | |
219 | stored_bootdelay = bootdelay; | |
220 | return s; | |
221 | } | |
222 | ||
223 | static | |
224 | void autoboot_command(const char *s) | |
225 | { | |
226 | debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>"); | |
227 | _delay_ms(20); | |
228 | ||
229 | if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) { | |
230 | run_command_list(s, -1); | |
231 | } | |
232 | } | |
233 | ||
234 | ||
235 | static | |
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 | { | |
247 | extern void setup_mmc(void); | |
248 | ||
249 | __malloc_margin = CONFIG_SYS_MALLOC_MARGIN; | |
250 | setup_avr(); | |
251 | for (int i = 0; i < GPIO_MAX; i++) | |
252 | gpio_config(i, INPUT_PULLUP); | |
253 | setup_mmc(); | |
254 | env_init(); | |
255 | z80_setup_bus(); | |
256 | ||
257 | if (reset_reason_is_power_on()) | |
258 | _delay_ms(CONFIG_PWRON_DELAY); | |
259 | ||
260 | serial_setup(getenv_ulong(PSTR(ENV_BAUDRATE), 10, CONFIG_BAUDRATE)); | |
261 | sei(); | |
262 | ||
263 | #if DEBUG | |
264 | debug("\n=========================< (RE)START DEBUG >=========================\n"); | |
265 | print_reset_reason(); | |
266 | #endif | |
267 | ||
268 | i2c_init(CONFIG_SYS_I2C_CLOCK); | |
269 | setup_system_time(); | |
270 | setup_fatfs(); | |
271 | ||
272 | printf_P(PSTR("\n" MCU_STRING "+Z8S180 Stamp Monitor - Version: " VERSION " \n\n")); | |
273 | ||
274 | setup_z180_serv(); | |
275 | ||
276 | main_loop(); | |
277 | } |