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