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