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