]>
Commit | Line | Data |
---|---|---|
d684c216 L |
1 | /* |
2 | */ | |
3 | ||
4 | ||
5 | #include "common.h" | |
6 | ||
7 | #include <util/delay.h> | |
8 | //#include <avr/power.h> | |
9 | //#include <avr/pgmspace.h> | |
10 | #include <avr/interrupt.h> | |
11 | //#include <util/atomic.h> | |
12 | //#include <avr/sleep.h> | |
13 | //#include <string.h> | |
14 | ||
15 | #include <util/delay.h> | |
16 | ||
17 | #include <stdlib.h> | |
18 | #include <stdio.h> | |
19 | ||
20 | ||
21 | #include "config.h" | |
22 | #include "debug.h" | |
23 | #include "z80-if.h" | |
24 | #include "con-utils.h" | |
25 | #include "serial.h" | |
26 | #include "timer.h" | |
27 | #include "cli.h" | |
28 | #include "env.h" | |
29 | ||
30 | /*--------------------------------------------------------------------------*/ | |
31 | ||
f338df2a L |
32 | static uint8_t mcusr; |
33 | ||
34 | static | |
d684c216 L |
35 | void setup_avr(void) |
36 | { | |
f338df2a L |
37 | /* save and clear reset reason(s) */ |
38 | mcusr = MCUSR; | |
39 | MCUSR = 0; | |
40 | ||
d684c216 L |
41 | /* WD */ |
42 | ||
43 | /* CPU */ | |
44 | ||
45 | /* Disable JTAG Interface regardless of the JTAGEN fuse setting. */ | |
46 | MCUCR = _BV(JTD); | |
47 | MCUCR = _BV(JTD); | |
48 | ||
49 | /* disable unused periphels */ | |
50 | PRR0 = _BV(PRTIM2) | _BV(PRTIM0) | _BV(PRADC); | |
51 | PRR1 = _BV(PRTIM5) | _BV(PRTIM4) | _BV(PRTIM3) | | |
52 | _BV(PRUSART3) | _BV(PRUSART2) | _BV(PRUSART1); | |
53 | ||
54 | /* disable analog comparator */ | |
55 | ACSR = _BV(ACD); | |
56 | /* Ports */ | |
57 | ||
58 | /* Clock */ | |
59 | CLKPR = _BV(CLKPCE); | |
60 | CLKPR = 0; | |
61 | ||
62 | /* Timer */ | |
63 | ||
64 | OCR1A = F_CPU / 8 / 1000 - 1; // Timer1: 1000Hz interval (OC1A) | |
65 | TCCR1B = 0b00001010; | |
66 | TIMSK1 = _BV(OCIE1A); // Enable TC1.oca interrupt | |
67 | } | |
68 | ||
f338df2a L |
69 | static const FLASH char * const FLASH rreasons[] = { |
70 | FSTR("Power on"), | |
71 | FSTR("External"), | |
72 | FSTR("Brown out"), | |
73 | FSTR("Watchdog"), | |
74 | FSTR("JTAG"), | |
75 | }; | |
76 | ||
77 | static | |
78 | void print_reset_reason(void) | |
79 | { | |
80 | uint8_t r = mcusr & 0x1f; | |
81 | const FLASH char * const FLASH *p = rreasons; | |
82 | ||
83 | printf_P(PSTR("Reset reason(s): ")); | |
84 | for ( ; r; p++, r >>= 1) { | |
85 | if (r & 1) { | |
86 | my_puts_P(*p); | |
87 | if (r & ~1) | |
88 | printf_P(PSTR(", ")); | |
89 | } | |
90 | } | |
91 | printf_P(PSTR(".\n")); | |
92 | } | |
d684c216 L |
93 | |
94 | ||
95 | /*******************************************************************************/ | |
96 | /*******************************************************************************/ | |
97 | ||
98 | #define udelay(n) _delay_us(n) | |
99 | ||
100 | ||
101 | /* Stored value of bootdelay, used by autoboot_command() */ | |
102 | static int stored_bootdelay; | |
103 | ||
104 | ||
105 | /*************************************************************************** | |
106 | * Watch for 'delay' seconds for autoboot stop. | |
107 | * returns: 0 - no key, allow autoboot | |
108 | * 1 - got key, abort | |
109 | */ | |
110 | ||
111 | static int abortboot(int bootdelay) | |
112 | { | |
113 | int abort = 0; | |
114 | uint32_t ts; | |
115 | ||
116 | if (bootdelay >= 0) | |
117 | printf_P(PSTR("Hit any key to stop autoboot: %2d "), bootdelay); | |
118 | ||
119 | #if defined CONFIG_ZERO_BOOTDELAY_CHECK | |
120 | /* | |
121 | * Check if key already pressed | |
122 | * Don't check if bootdelay < 0 | |
123 | */ | |
124 | if (bootdelay >= 0) { | |
125 | if (tstc()) { /* we got a key press */ | |
126 | (void) my_getchar(); /* consume input */ | |
127 | my_puts_P(PSTR("\b\b\b 0")); | |
128 | abort = 1; /* don't auto boot */ | |
129 | } | |
130 | } | |
131 | #endif | |
132 | ||
133 | while ((bootdelay > 0) && (!abort)) { | |
134 | --bootdelay; | |
135 | /* delay 1000 ms */ | |
136 | ts = get_timer(0); | |
137 | do { | |
138 | if (tstc()) { /* we got a key press */ | |
139 | abort = 1; /* don't auto boot */ | |
140 | bootdelay = 0; /* no more delay */ | |
141 | break; | |
142 | } | |
143 | udelay(10000); | |
144 | } while (!abort && get_timer(ts) < 1000); | |
145 | ||
146 | printf_P(PSTR("\b\b\b%2d "), bootdelay); | |
147 | } | |
148 | ||
149 | putchar('\n'); | |
150 | ||
151 | return abort; | |
152 | } | |
153 | ||
f338df2a | 154 | static |
d684c216 L |
155 | const char *bootdelay_process(void) |
156 | { | |
157 | char *s; | |
158 | int bootdelay; | |
159 | ||
160 | s = getenv("bootdelay"); | |
161 | bootdelay = s ? atoi(s) : CONFIG_BOOTDELAY; | |
162 | ||
163 | ||
164 | debug("### main_loop entered: bootdelay=%d\n\n", bootdelay); | |
165 | _delay_ms(20); | |
166 | ||
167 | s = getenv("bootcmd"); | |
168 | stored_bootdelay = bootdelay; | |
169 | return s; | |
170 | } | |
171 | ||
f338df2a | 172 | static |
d684c216 L |
173 | void autoboot_command(const char *s) |
174 | { | |
175 | debug("### main_loop: bootcmd=\"%s\"\n", s ? s : PSTR("<UNDEFINED>")); | |
176 | _delay_ms(20); | |
177 | ||
178 | if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) { | |
179 | run_command_list(s, -1); | |
180 | } | |
181 | } | |
182 | ||
183 | ||
f338df2a | 184 | static |
d684c216 L |
185 | void main_loop(void) |
186 | { | |
187 | const char *s; | |
188 | ||
189 | s = bootdelay_process(); | |
190 | autoboot_command(s); | |
191 | cli_loop(); | |
192 | } | |
193 | ||
194 | int main(void) | |
195 | { | |
196 | setup_avr(); | |
197 | z80_setup_bus(); | |
198 | ||
199 | serial_setup(); | |
200 | sei(); | |
323398b1 L |
201 | |
202 | debug("\n=========================< (RE)START DEBUG >=========================\n"); | |
f338df2a L |
203 | #if DEBUG |
204 | print_reset_reason(); | |
205 | #endif | |
323398b1 | 206 | |
d684c216 L |
207 | env_init(); |
208 | ||
209 | printf_P(PSTR("\n(ATMEGA1281+HD64180)_stamp Tester\n")); | |
210 | ||
211 | main_loop(); | |
212 | } |