]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | */ | |
3 | ||
4 | ||
5 | #include "common.h" | |
6 | ||
7 | #include <avr/interrupt.h> | |
8 | #include <stdlib.h> | |
9 | #include <stdio.h> | |
10 | ||
11 | #include "config.h" | |
12 | #include "debug.h" | |
13 | #include "z80-if.h" | |
14 | #include "i2c.h" | |
15 | #include "con-utils.h" | |
16 | #include "serial.h" | |
17 | #include "timer.h" | |
18 | #include "cli.h" | |
19 | #include "env.h" | |
20 | #include "z180-serv.h" | |
21 | #include "spi.h" | |
22 | ||
23 | static uint8_t mcusr; | |
24 | ||
25 | /*--------------------------------------------------------------------------*/ | |
26 | #if DEBUG | |
27 | ||
28 | __attribute__ ((naked)) __attribute__ ((section (".init3"))) | |
29 | void preset_ram (void) | |
30 | { | |
31 | for (uint8_t *p = RAMSTART; p <= (uint8_t *) RAMEND; p++) | |
32 | *p = 0xdd; | |
33 | ||
34 | } | |
35 | ||
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 | |
62 | ||
63 | ISR(INT5_vect) | |
64 | { | |
65 | Stat |= S_MSG_PENDING; | |
66 | } | |
67 | ||
68 | ISR(INT6_vect) | |
69 | { | |
70 | Stat |= S_CON_PENDING; | |
71 | } | |
72 | ||
73 | static | |
74 | void setup_avr(void) | |
75 | { | |
76 | /* save and clear reset reason(s) */ | |
77 | /* TODO: move to init section? */ | |
78 | mcusr = MCUSR; | |
79 | MCUSR = 0; | |
80 | ||
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 | ||
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 | ||
94 | PRR1 = _BV(PRTIM5) | _BV(PRTIM4) | _BV(PRTIM3) | | |
95 | _BV(PRUSART3) | _BV(PRUSART2) | _BV(PRUSART1); | |
96 | ||
97 | ||
98 | /* disable analog comparator */ | |
99 | ACSR = _BV(ACD); | |
100 | /* Ports */ | |
101 | ||
102 | /* Clock */ | |
103 | CLKPR = _BV(CLKPCE); | |
104 | CLKPR = 0; | |
105 | ||
106 | /* Timer */ | |
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 */ | |
111 | ||
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 | ||
119 | /* INT5, INT6: falling edge */ | |
120 | EICRB = (EICRB & ~((0b11 << ISC50) | (0b11 << ISC60))) | | |
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); | |
126 | } | |
127 | ||
128 | static | |
129 | int reset_reason_is_power_on(void) | |
130 | { | |
131 | return (mcusr & _BV(PORF)) != 0; | |
132 | } | |
133 | ||
134 | /*--------------------------------------------------------------------------*/ | |
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. | |
142 | * returns: 0 - no key, allow autoboot | |
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 */ | |
161 | (void) my_getchar(1); /* consume input */ | |
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 | ||
189 | static | |
190 | const char *bootdelay_process(void) | |
191 | { | |
192 | char *s; | |
193 | int bootdelay; | |
194 | ||
195 | bootdelay = (int) getenv_ulong(PSTR(ENV_BOOTDELAY), 10, CONFIG_BOOTDELAY); | |
196 | ||
197 | ||
198 | debug("### main_loop entered: bootdelay=%d\n\n", bootdelay); | |
199 | _delay_ms(20); | |
200 | ||
201 | s = getenv(PSTR(ENV_BOOTCMD)); | |
202 | stored_bootdelay = bootdelay; | |
203 | return s; | |
204 | } | |
205 | ||
206 | static | |
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 | ||
218 | static | |
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(); | |
232 | ||
233 | env_init(); | |
234 | ||
235 | if (reset_reason_is_power_on()) | |
236 | _delay_ms(CONFIG_PWRON_DELAY); | |
237 | ||
238 | serial_setup(getenv_ulong(PSTR(ENV_BAUDRATE), 10, CONFIG_BAUDRATE)); | |
239 | sei(); | |
240 | ||
241 | #if DEBUG | |
242 | debug("\n=========================< (RE)START DEBUG >=========================\n"); | |
243 | print_reset_reason(); | |
244 | #endif | |
245 | ||
246 | #if DEBUG | |
247 | unsigned long i_speed = getenv_ulong(PSTR("i2c_clock"), 10, CONFIG_SYS_I2C_CLOCK); | |
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 | |
253 | ||
254 | printf_P(PSTR("\nATMEGA1281+Z8S180 Stamp Monitor\n\n")); | |
255 | ||
256 | setup_z180_serv(); | |
257 | ||
258 | main_loop(); | |
259 | } |