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