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